Skip to content

Commit a4138a2

Browse files
committed
Convert container owner capture to a stack.
1 parent 3651ebe commit a4138a2

File tree

5 files changed

+30
-23
lines changed

5 files changed

+30
-23
lines changed

arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/CraftHumanEntityMixin.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package io.izzel.arclight.common.mixin.bukkit;
22

33
import io.izzel.arclight.common.mod.util.ArclightCaptures;
4+
import io.izzel.arclight.mixin.Decorate;
5+
import io.izzel.arclight.mixin.DecorationOps;
46
import net.minecraft.world.entity.Entity;
57
import net.minecraft.world.entity.player.Player;
8+
import net.minecraft.world.inventory.AbstractContainerMenu;
69
import org.bukkit.craftbukkit.v.CraftServer;
710
import org.bukkit.craftbukkit.v.entity.CraftEntity;
811
import org.bukkit.craftbukkit.v.entity.CraftHumanEntity;
912
import org.bukkit.inventory.InventoryView;
1013
import org.spongepowered.asm.mixin.Mixin;
1114
import org.spongepowered.asm.mixin.Shadow;
1215
import org.spongepowered.asm.mixin.injection.At;
13-
import org.spongepowered.asm.mixin.injection.Inject;
14-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
1516

1617
@Mixin(value = CraftHumanEntity.class, remap = false)
1718
public abstract class CraftHumanEntityMixin extends CraftEntity {
@@ -25,14 +26,15 @@ public CraftHumanEntityMixin(CraftServer server, Entity entity) {
2526
super(server, entity);
2627
}
2728

28-
@Inject(method = "getOpenInventory", at = @At("HEAD"))
29-
private void arclight$capturePlayer(CallbackInfoReturnable<InventoryView> cir) {
30-
ArclightCaptures.captureContainerOwner(this.getHandle());
31-
}
32-
33-
@Inject(method = "getOpenInventory", at = @At("RETURN"))
34-
private void arclight$resetPlayer(CallbackInfoReturnable<InventoryView> cir) {
35-
ArclightCaptures.resetContainerOwner();
29+
@Decorate(method = "getOpenInventory", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/inventory/AbstractContainerMenu;getBukkitView()Lorg/bukkit/inventory/InventoryView;"))
30+
private InventoryView arclight$capturePlayer(AbstractContainerMenu instance) throws Throwable {
31+
Player handle = getHandle();
32+
try {
33+
ArclightCaptures.captureContainerOwner(handle);
34+
return (InventoryView) DecorationOps.callsite().invoke(instance);
35+
} finally {
36+
ArclightCaptures.popContainerOwner(handle);
37+
}
3638
}
3739

3840
@Override

arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/network/ServerGamePacketListenerImplMixin.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@
162162
import java.util.Set;
163163
import java.util.concurrent.CompletableFuture;
164164
import java.util.concurrent.ExecutionException;
165-
import java.util.function.Function;
166165
import java.util.logging.Level;
167166

168167
@Mixin(ServerGamePacketListenerImpl.class)
@@ -1252,9 +1251,14 @@ public void handleContainerClick(ServerboundContainerClickPacket packet) {
12521251
return;
12531252
}
12541253

1255-
ArclightCaptures.captureContainerOwner(this.player);
1256-
InventoryView inventory = ((ContainerBridge) this.player.containerMenu).bridge$getBukkitView();
1257-
ArclightCaptures.resetContainerOwner();
1254+
ServerPlayer owner = this.player;
1255+
InventoryView inventory;
1256+
try {
1257+
ArclightCaptures.captureContainerOwner(owner);
1258+
inventory = ((ContainerBridge) this.player.containerMenu).bridge$getBukkitView();
1259+
} finally {
1260+
ArclightCaptures.popContainerOwner(owner);
1261+
}
12581262
InventoryType.SlotType type = inventory.getSlotType(packet.getSlotNum());
12591263

12601264
InventoryClickEvent event;

arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/server/level/ServerPlayerMixin.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ private Either<Player.BedSleepingProblem, Unit> getBedResult(BlockPos blockposit
614614
boolean cancelled = false;
615615
ArclightCaptures.captureContainerOwner((ServerPlayer) (Object) this);
616616
container = CraftEventFactory.callInventoryOpenEvent((ServerPlayer) (Object) this, container, cancelled);
617-
ArclightCaptures.resetContainerOwner();
617+
ArclightCaptures.popContainerOwner((ServerPlayer) (Object) this);
618618
if (container == null && !cancelled) {
619619
if (iTileInventory instanceof Container) {
620620
((Container) iTileInventory).stopOpen((ServerPlayer) (Object) this);
@@ -648,10 +648,9 @@ private Either<Player.BedSleepingProblem, Unit> getBedResult(BlockPos blockposit
648648
@Inject(method = "doCloseContainer", at = @At("HEAD"))
649649
private void arclight$invClose(CallbackInfo ci) {
650650
if (this.containerMenu != this.inventoryMenu) {
651-
var old = ArclightCaptures.getContainerOwner();
652651
ArclightCaptures.captureContainerOwner((ServerPlayer) (Object) this);
653652
CraftEventFactory.handleInventoryCloseEvent((ServerPlayer) (Object) this);
654-
ArclightCaptures.captureContainerOwner(old);
653+
ArclightCaptures.popContainerOwner((ServerPlayer) (Object) this);
655654
}
656655
}
657656

arclight-common/src/main/java/io/izzel/arclight/common/mod/util/ArclightCaptures.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,20 @@ public static BlockPos getDamageEventBlock() {
225225
}
226226
}
227227

228-
private static transient Player containerOwner;
228+
private static transient Stack<Player> containerOwner = new Stack<>();
229229

230230
public static void captureContainerOwner(Player entity) {
231-
containerOwner = entity;
231+
containerOwner.push(entity);
232232
}
233233

234234
public static Player getContainerOwner() {
235-
return containerOwner;
235+
return containerOwner.peek();
236236
}
237237

238-
public static void resetContainerOwner() {
239-
containerOwner = null;
238+
public static void popContainerOwner(Player entity) {
239+
if (!containerOwner.empty() && entity == getContainerOwner()) {
240+
containerOwner.pop();
241+
}
240242
}
241243

242244
private static transient CraftPortalEvent craftPortalEvent;

arclight-forge/src/main/java/io/izzel/arclight/forge/mixin/forge/IForgeServerPlayerMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ default void openMenu(MenuProvider containerSupplier, Consumer<FriendlyByteBuf>
4949
((ContainerBridge) c).bridge$setTitle(containerSupplier.getDisplayName());
5050
ArclightCaptures.captureContainerOwner(player);
5151
c = CraftEventFactory.callInventoryOpenEvent(player, c);
52-
ArclightCaptures.resetContainerOwner();
52+
ArclightCaptures.popContainerOwner(player);
5353
if (c == null) {
5454
if (containerSupplier instanceof Container) {
5555
((Container) containerSupplier).stopOpen(player);

0 commit comments

Comments
 (0)