Skip to content

Commit 5825843

Browse files
committed
Merge Anvil Transaction fix. Merges #3934
Fixes warnings being logged for item renames when using an Anvil inventory. Signed-off-by: Gabriel Harris-Rouquette <gabizou@me.com>
2 parents 80faee9 + dd96930 commit 5825843

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/main/java/org/spongepowered/common/event/tracking/context/transaction/TransactionSink.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import org.spongepowered.common.event.tracking.context.transaction.inventory.CraftingPreviewTransaction;
7979
import org.spongepowered.common.event.tracking.context.transaction.inventory.CraftingTransaction;
8080
import org.spongepowered.common.event.tracking.context.transaction.inventory.DropFromPlayerInventoryTransaction;
81+
import org.spongepowered.common.event.tracking.context.transaction.inventory.ExplicitInventoryOmittedTransaction;
8182
import org.spongepowered.common.event.tracking.context.transaction.inventory.OpenMenuTransaction;
8283
import org.spongepowered.common.event.tracking.context.transaction.inventory.PlaceRecipeTransaction;
8384
import org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction;
@@ -406,4 +407,10 @@ default void logCrafting(final Player player, @Nullable final ItemStack craftedS
406407
final CraftingTransaction transaction = new CraftingTransaction(player, craftedStack, craftInv, lastRecipe);
407408
this.logTransaction(transaction);
408409
}
410+
411+
default EffectTransactor logIgnoredInventory(AbstractContainerMenu containerMenu) {
412+
final ExplicitInventoryOmittedTransaction transaction = new ExplicitInventoryOmittedTransaction(containerMenu);
413+
this.logTransaction(transaction);
414+
return this.pushEffect(new ResultingTransactionBySideEffect(InventoryEffect.getInstance()));
415+
}
409416
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 com.google.common.collect.ImmutableList;
28+
import net.minecraft.world.inventory.AbstractContainerMenu;
29+
import org.checkerframework.checker.nullness.qual.NonNull;
30+
import org.checkerframework.checker.nullness.qual.Nullable;
31+
import org.spongepowered.api.event.Cause;
32+
import org.spongepowered.api.event.item.inventory.container.ClickContainerEvent;
33+
import org.spongepowered.common.event.tracking.PhaseContext;
34+
import org.spongepowered.common.event.tracking.context.transaction.GameTransaction;
35+
36+
import java.util.Optional;
37+
38+
public class ExplicitInventoryOmittedTransaction extends ContainerBasedTransaction {
39+
public ExplicitInventoryOmittedTransaction(
40+
final AbstractContainerMenu menu
41+
) {
42+
super(menu);
43+
}
44+
45+
@Override
46+
public Optional<ClickContainerEvent> generateEvent(
47+
final PhaseContext<@NonNull ?> context, @Nullable final GameTransaction<@NonNull ?> parent,
48+
final ImmutableList<GameTransaction<ClickContainerEvent>> gameTransactions, final Cause currentCause
49+
) {
50+
return Optional.empty();
51+
}
52+
}

src/mixins/java/org/spongepowered/common/mixin/inventory/event/server/network/ServerGamePacketListenerImplMixin_Inventory.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import net.minecraft.world.entity.item.ItemEntity;
3838
import net.minecraft.world.entity.player.Player;
3939
import net.minecraft.world.inventory.AbstractContainerMenu;
40+
import net.minecraft.world.inventory.AnvilMenu;
4041
import net.minecraft.world.inventory.InventoryMenu;
4142
import net.minecraft.world.inventory.MerchantMenu;
4243
import net.minecraft.world.item.ItemStack;
@@ -54,7 +55,10 @@
5455
import org.spongepowered.common.event.tracking.PhaseContext;
5556
import org.spongepowered.common.event.tracking.PhaseTracker;
5657
import org.spongepowered.common.event.tracking.context.transaction.EffectTransactor;
58+
import org.spongepowered.common.event.tracking.context.transaction.ResultingTransactionBySideEffect;
5759
import org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier;
60+
import org.spongepowered.common.event.tracking.context.transaction.effect.InventoryEffect;
61+
import org.spongepowered.common.event.tracking.context.transaction.inventory.ExplicitInventoryOmittedTransaction;
5862
import org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction;
5963
import org.spongepowered.common.inventory.custom.SpongeInventoryMenu;
6064
import org.spongepowered.common.item.util.ItemStackUtil;
@@ -152,6 +156,16 @@ public class ServerGamePacketListenerImplMixin_Inventory {
152156
}
153157
}
154158

159+
@Redirect(method = "handleRenameItem(Lnet/minecraft/network/protocol/game/ServerboundRenameItemPacket;)V",
160+
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/inventory/AnvilMenu;setItemName(Ljava/lang/String;)V"))
161+
private void impl$onHandleRenameItem(final AnvilMenu menu, final String name) {
162+
final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
163+
final TransactionalCaptureSupplier transactor = context.getTransactor();
164+
try (EffectTransactor ignored = transactor.logIgnoredInventory(this.player.containerMenu)) {
165+
menu.setItemName(name);
166+
}
167+
}
168+
155169
@Inject(method = "handleSelectTrade", at = @At("RETURN"))
156170
private void impl$onHandleSelectTrade(final ServerboundSelectTradePacket param0, final CallbackInfo ci) {
157171
if (this.player.containerMenu instanceof MerchantMenu) {

0 commit comments

Comments
 (0)