Skip to content

Commit

Permalink
Merge Anvil Transaction fix. Merges #3934
Browse files Browse the repository at this point in the history
Fixes warnings being logged for item renames when
using an Anvil inventory.

Signed-off-by: Gabriel Harris-Rouquette <gabizou@me.com>
  • Loading branch information
gabizou committed Jan 16, 2024
2 parents 80faee9 + dd96930 commit 1c4ba14
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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<ClickContainerEvent> generateEvent(
final PhaseContext<@NonNull ?> context, @Nullable final GameTransaction<@NonNull ?> parent,
final ImmutableList<GameTransaction<ClickContainerEvent>> gameTransactions, final Cause currentCause
) {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1c4ba14

Please sign in to comment.