|
| 1 | +/* |
| 2 | + * This file is part of Sponge, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.common.event.tracking.context.transaction.inventory; |
| 26 | + |
| 27 | +import net.minecraft.network.protocol.game.ServerboundPlaceRecipePacket; |
| 28 | +import net.minecraft.server.level.ServerPlayer; |
| 29 | +import net.minecraft.world.item.crafting.RecipeHolder; |
| 30 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 31 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 32 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 33 | +import org.spongepowered.api.ResourceKey; |
| 34 | +import org.spongepowered.api.data.Transaction; |
| 35 | +import org.spongepowered.api.entity.Entity; |
| 36 | +import org.spongepowered.api.event.Cause; |
| 37 | +import org.spongepowered.api.event.SpongeEventFactory; |
| 38 | +import org.spongepowered.api.event.block.entity.CookingEvent; |
| 39 | +import org.spongepowered.api.event.item.inventory.container.ClickContainerEvent; |
| 40 | +import org.spongepowered.api.item.inventory.Container; |
| 41 | +import org.spongepowered.api.item.inventory.ItemStackSnapshot; |
| 42 | +import org.spongepowered.api.item.inventory.transaction.SlotTransaction; |
| 43 | +import org.spongepowered.api.item.recipe.cooking.CookingRecipe; |
| 44 | +import org.spongepowered.common.event.tracking.PhaseContext; |
| 45 | +import org.spongepowered.common.event.tracking.phase.packet.inventory.InventoryPacketContext; |
| 46 | +import org.spongepowered.common.item.util.ItemStackUtil; |
| 47 | + |
| 48 | +import java.util.List; |
| 49 | +import java.util.Optional; |
| 50 | + |
| 51 | +public class PlaceCookingRecipeTransaction extends ContainerBasedTransaction { |
| 52 | + |
| 53 | + private final ServerPlayer player; |
| 54 | + private final ItemStackSnapshot originalCursor; |
| 55 | + private final BlockEntity blockEntity; |
| 56 | + private final boolean shift; |
| 57 | + private RecipeHolder<?> recipe; |
| 58 | + |
| 59 | + public PlaceCookingRecipeTransaction( |
| 60 | + final BlockEntity blockEntity, |
| 61 | + final ServerPlayer player, |
| 62 | + boolean shift, final RecipeHolder<?> recipe |
| 63 | + ) { |
| 64 | + super(player.containerMenu); |
| 65 | + this.blockEntity = blockEntity; |
| 66 | + this.player = player; |
| 67 | + this.originalCursor = ItemStackUtil.snapshotOf(player.containerMenu.getCarried()); |
| 68 | + this.recipe = recipe; |
| 69 | + this.shift = shift; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + Optional<ClickContainerEvent> createInventoryEvent( |
| 74 | + final List<SlotTransaction> slotTransactions, final List<Entity> entities, |
| 75 | + final PhaseContext<@NonNull ?> context, |
| 76 | + final Cause cause |
| 77 | + ) { |
| 78 | + final Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(this.originalCursor, ItemStackUtil.snapshotOf(this.player.containerMenu.getCarried())); |
| 79 | + if (this.shift) { |
| 80 | + return Optional.of( |
| 81 | + SpongeEventFactory.createCookingEventRecipeAll(cause, |
| 82 | + (org.spongepowered.api.block.entity.BlockEntity) this.blockEntity, |
| 83 | + (Container) this.menu, cursorTransaction, Optional.empty(), |
| 84 | + Optional.of(this.recipe).map(RecipeHolder::value).map(CookingRecipe.class::cast), |
| 85 | + Optional.of(this.recipe).map(RecipeHolder::id).map(ResourceKey.class::cast), |
| 86 | + Optional.empty(), slotTransactions) |
| 87 | + ); |
| 88 | + } |
| 89 | + final CookingEvent.Recipe event = SpongeEventFactory.createCookingEventRecipeSingle(cause, |
| 90 | + (org.spongepowered.api.block.entity.BlockEntity) this.blockEntity, |
| 91 | + (Container) this.menu, cursorTransaction, Optional.empty(), |
| 92 | + Optional.of(this.recipe).map(RecipeHolder::value).map(CookingRecipe.class::cast), |
| 93 | + Optional.of(this.recipe).map(RecipeHolder::id).map(ResourceKey.class::cast), |
| 94 | + Optional.empty(), slotTransactions); |
| 95 | + return Optional.of(event); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void restore(final PhaseContext<@NonNull ?> context, final ClickContainerEvent event) { |
| 100 | + this.handleEventResults(this.player, event); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void postProcessEvent(PhaseContext<@NonNull ?> context, ClickContainerEvent event) { |
| 105 | + this.handleEventResults(this.player, event); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + boolean isContainerEventAllowed(final PhaseContext<@Nullable ?> context) { |
| 110 | + if (!(context instanceof InventoryPacketContext)) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + final int containerId = ((InventoryPacketContext) context).<ServerboundPlaceRecipePacket>getPacket().getContainerId(); |
| 114 | + return containerId != this.player.containerMenu.containerId; |
| 115 | + } |
| 116 | +} |
0 commit comments