From 8e2567bd0d95b12c71e82b5dcb2fc53bb34b72de Mon Sep 17 00:00:00 2001 From: MrHell228 Date: Tue, 16 Jan 2024 15:26:59 +0300 Subject: [PATCH] Fix anvil rename warnings --- .../context/transaction/TransactionSink.java | 7 +++ .../ExplicitInventoryOmittedTransaction.java | 52 +++++++++++++++++++ ...GamePacketListenerImplMixin_Inventory.java | 11 ++++ 3 files changed, 70 insertions(+) create mode 100644 src/main/java/org/spongepowered/common/event/tracking/context/transaction/inventory/ExplicitInventoryOmittedTransaction.java diff --git a/src/main/java/org/spongepowered/common/event/tracking/context/transaction/TransactionSink.java b/src/main/java/org/spongepowered/common/event/tracking/context/transaction/TransactionSink.java index c90d4d3d182..1a650a32a44 100644 --- a/src/main/java/org/spongepowered/common/event/tracking/context/transaction/TransactionSink.java +++ b/src/main/java/org/spongepowered/common/event/tracking/context/transaction/TransactionSink.java @@ -78,6 +78,7 @@ import org.spongepowered.common.event.tracking.context.transaction.inventory.CraftingPreviewTransaction; import org.spongepowered.common.event.tracking.context.transaction.inventory.CraftingTransaction; import org.spongepowered.common.event.tracking.context.transaction.inventory.DropFromPlayerInventoryTransaction; +import org.spongepowered.common.event.tracking.context.transaction.inventory.ExplicitInventoryOmittedTransaction; import org.spongepowered.common.event.tracking.context.transaction.inventory.OpenMenuTransaction; import org.spongepowered.common.event.tracking.context.transaction.inventory.PlaceRecipeTransaction; import org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction; @@ -406,4 +407,10 @@ default void logCrafting(final Player player, @Nullable final ItemStack craftedS final CraftingTransaction transaction = new CraftingTransaction(player, craftedStack, craftInv, lastRecipe); this.logTransaction(transaction); } + + default EffectTransactor logIgnoredInventory(AbstractContainerMenu containerMenu) { + final ExplicitInventoryOmittedTransaction transaction = new ExplicitInventoryOmittedTransaction(containerMenu); + this.logTransaction(transaction); + return this.pushEffect(new ResultingTransactionBySideEffect(InventoryEffect.getInstance())); + } } diff --git a/src/main/java/org/spongepowered/common/event/tracking/context/transaction/inventory/ExplicitInventoryOmittedTransaction.java b/src/main/java/org/spongepowered/common/event/tracking/context/transaction/inventory/ExplicitInventoryOmittedTransaction.java new file mode 100644 index 00000000000..d48586ec405 --- /dev/null +++ b/src/main/java/org/spongepowered/common/event/tracking/context/transaction/inventory/ExplicitInventoryOmittedTransaction.java @@ -0,0 +1,52 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.common.event.tracking.context.transaction.inventory; + +import com.google.common.collect.ImmutableList; +import net.minecraft.world.inventory.AbstractContainerMenu; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.spongepowered.api.event.Cause; +import org.spongepowered.api.event.item.inventory.container.ClickContainerEvent; +import org.spongepowered.common.event.tracking.PhaseContext; +import org.spongepowered.common.event.tracking.context.transaction.GameTransaction; + +import java.util.Optional; + +public class ExplicitInventoryOmittedTransaction extends ContainerBasedTransaction { + public ExplicitInventoryOmittedTransaction( + final AbstractContainerMenu menu + ) { + super(menu); + } + + @Override + public Optional generateEvent( + final PhaseContext<@NonNull ?> context, @Nullable final GameTransaction<@NonNull ?> parent, + final ImmutableList> gameTransactions, final Cause currentCause + ) { + return Optional.empty(); + } +} diff --git a/src/mixins/java/org/spongepowered/common/mixin/inventory/event/server/network/ServerGamePacketListenerImplMixin_Inventory.java b/src/mixins/java/org/spongepowered/common/mixin/inventory/event/server/network/ServerGamePacketListenerImplMixin_Inventory.java index f700282ad15..2e06718deba 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/inventory/event/server/network/ServerGamePacketListenerImplMixin_Inventory.java +++ b/src/mixins/java/org/spongepowered/common/mixin/inventory/event/server/network/ServerGamePacketListenerImplMixin_Inventory.java @@ -38,6 +38,7 @@ import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.inventory.AnvilMenu; import net.minecraft.world.inventory.InventoryMenu; import net.minecraft.world.inventory.MerchantMenu; import net.minecraft.world.item.ItemStack; @@ -152,6 +153,16 @@ public class ServerGamePacketListenerImplMixin_Inventory { } } + @Redirect(method = "handleRenameItem(Lnet/minecraft/network/protocol/game/ServerboundRenameItemPacket;)V", + at = @At(value = "INVOKE", target = "Lnet/minecraft/world/inventory/AnvilMenu;setItemName(Ljava/lang/String;)V")) + private void impl$onHandleRenameItem(final AnvilMenu menu, final String name) { + final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext(); + final TransactionalCaptureSupplier transactor = context.getTransactor(); + try (EffectTransactor ignored = transactor.logIgnoredInventory(this.player.containerMenu)) { + menu.setItemName(name); + } + } + @Inject(method = "handleSelectTrade", at = @At("RETURN")) private void impl$onHandleSelectTrade(final ServerboundSelectTradePacket param0, final CallbackInfo ci) { if (this.player.containerMenu instanceof MerchantMenu) {