Skip to content

Commit

Permalink
Clean up CloseWindowState
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Jan 5, 2025
1 parent 9c40d23 commit 641ffd6
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ public void printTrace(final PrettyPrinter printer) {
return null;
}

public boolean isClientSide() {
return false;
}

protected boolean isRunaway(final PhaseContext<?> phaseContext) {
return phaseContext.getClass() == this.getClass();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ default EffectTransactor logOpenInventory(final Player player) {
return this.pushEffect(new ResultingTransactionBySideEffect(InventoryEffect.getInstance()));
}

default EffectTransactor logCloseInventory(final Player player, final boolean clientSource) {
final CloseMenuTransaction transaction = new CloseMenuTransaction(player, clientSource);
default EffectTransactor logCloseInventory(final PhaseContext<@NonNull ?> current, final Player player) {
final CloseMenuTransaction transaction = new CloseMenuTransaction(player, current.isClientSide());
this.logTransaction(transaction);
return this.pushEffect(new ResultingTransactionBySideEffect(InventoryEffect.getInstance()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ public BasicPacketContext(final PacketState<BasicPacketContext> state, final Pha
@SuppressWarnings("unchecked")
@Override
public boolean hasCaptures() {
if (this.state == PacketPhase.General.CLOSE_WINDOW) {
return true;
}

return super.hasCaptures();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
*/
package org.spongepowered.common.event.tracking.phase.packet;

import net.minecraft.server.level.ServerPlayer;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.common.bridge.server.TickTaskBridge;
import org.spongepowered.common.event.tracking.PhaseTracker;

public class BasicPacketState extends PacketState<BasicPacketContext> {
Expand All @@ -35,14 +32,4 @@ public class BasicPacketState extends PacketState<BasicPacketContext> {
public BasicPacketContext createNewContext(final PhaseTracker tracker) {
return new BasicPacketContext(this, tracker);
}

@Override
public void foldContextForThread(final BasicPacketContext context, final TickTaskBridge returnValue) {
final @Nullable ServerPlayer source = context.getPacketPlayer();
returnValue.bridge$contextShift((c, f) -> {
if (source != null) {
f.pushCause(source);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.spongepowered.common.event.tracking.phase.packet.drag.PrimaryDragInventoryStopState;
import org.spongepowered.common.event.tracking.phase.packet.drag.SecondaryDragInventoryStopState;
import org.spongepowered.common.event.tracking.phase.packet.inventory.BasicInventoryPacketState;
import org.spongepowered.common.event.tracking.phase.packet.inventory.CloseWindowContext;
import org.spongepowered.common.event.tracking.phase.packet.inventory.CloseWindowState;
import org.spongepowered.common.event.tracking.phase.packet.inventory.CreativeInventoryPacketState;
import org.spongepowered.common.event.tracking.phase.packet.inventory.DoubleClickInventoryState;
Expand Down Expand Up @@ -127,7 +128,7 @@ public static final class General {
static final IPhaseState<BasicPacketContext> STOP_SPRINTING = new BasicPacketState();
static final IPhaseState<BasicPacketContext> STOP_SLEEPING = new StopSleepingPacketState();
static final IPhaseState<BasicPacketContext> TAB_COMPLETE = new BasicPacketState();
public static final IPhaseState<BasicPacketContext> CLOSE_WINDOW = new CloseWindowState();
public static final IPhaseState<CloseWindowContext> CLOSE_WINDOW = new CloseWindowState();
public static final IPhaseState<BasicPacketContext> UPDATE_SIGN = new BasicPacketState();
static final IPhaseState<BasicPacketContext> STOP_RIDING_JUMP = new BasicPacketState();
static final IPhaseState<BasicPacketContext> HANDLED_EXTERNALLY = new UnknownPacketState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
import net.minecraft.network.protocol.Packet;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.chunk.LevelChunk;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.CauseStackManager;
import org.spongepowered.api.event.cause.entity.SpawnType;
import org.spongepowered.api.event.cause.entity.SpawnTypes;
import org.spongepowered.common.bridge.server.TickTaskBridge;
import org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge;
import org.spongepowered.common.entity.PlayerTracker;
import org.spongepowered.common.event.tracking.IPhaseState;
Expand Down Expand Up @@ -106,6 +109,16 @@ protected boolean alwaysUnwinds() {
return false;
}

@Override
public void foldContextForThread(final P context, final TickTaskBridge returnValue) {
final @Nullable ServerPlayer source = context.getPacketPlayer();
returnValue.bridge$contextShift((c, f) -> {
if (source != null) {
f.pushCause(source);
}
});
}

private final String desc = TrackingUtil.phaseStateToString("Packet", this);

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.phase.packet.inventory;

import org.spongepowered.common.event.tracking.PhaseTracker;
import org.spongepowered.common.event.tracking.phase.packet.PacketContext;
import org.spongepowered.common.event.tracking.phase.packet.PacketState;

public final class CloseWindowContext extends PacketContext<CloseWindowContext> {

private boolean clientSide = true;

public CloseWindowContext(final PacketState<CloseWindowContext> state, final PhaseTracker tracker) {
super(state, tracker);
}

public CloseWindowContext isClientSide(final boolean clientSide) {
this.clientSide = clientSide;
return this;
}

@Override
public boolean hasCaptures() {
return true;
}

@Override
public boolean isClientSide() {
return this.clientSide;
}

@Override
protected void reset() {
super.reset();
this.clientSide = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,28 @@
import org.spongepowered.api.event.cause.entity.SpawnTypes;
import org.spongepowered.api.event.entity.SpawnEntityEvent;
import org.spongepowered.api.util.Tuple;
import org.spongepowered.common.event.tracking.PhaseTracker;
import org.spongepowered.common.event.tracking.context.transaction.GameTransaction;
import org.spongepowered.common.event.tracking.context.transaction.world.SpawnEntityTransaction;
import org.spongepowered.common.event.tracking.phase.packet.BasicPacketContext;
import org.spongepowered.common.event.tracking.phase.packet.BasicPacketState;
import org.spongepowered.common.event.tracking.phase.packet.PacketState;

import java.util.function.Supplier;
import java.util.stream.Collectors;

public final class CloseWindowState extends BasicPacketState {
public final class CloseWindowState extends PacketState<CloseWindowContext> {

@Override
public Supplier<SpawnType> getSpawnTypeForTransaction(final BasicPacketContext context, final Entity entityToSpawn) {
protected CloseWindowContext createNewContext(final PhaseTracker tracker) {
return new CloseWindowContext(this, tracker);
}

@Override
public Supplier<SpawnType> getSpawnTypeForTransaction(final CloseWindowContext context, final Entity entityToSpawn) {
return SpawnTypes.DROPPED_ITEM;
}

@Override
public SpawnEntityEvent createSpawnEvent(final BasicPacketContext context, final @Nullable GameTransaction<@NonNull ?> parent,
public SpawnEntityEvent createSpawnEvent(final CloseWindowContext context, final @Nullable GameTransaction<@NonNull ?> parent,
final ImmutableList<Tuple<Entity, SpawnEntityTransaction.DummySnapshot>> collect, final Cause currentCause) {
return SpongeEventFactory.createDropItemEventClose(currentCause,
collect.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
import org.spongepowered.api.item.inventory.Container;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.world.inventory.container.ContainerBridge;
import org.spongepowered.common.event.inventory.InventoryEventFactory;
import org.spongepowered.common.event.tracking.PhaseContext;
import org.spongepowered.common.event.tracking.PhaseTracker;
import org.spongepowered.common.event.tracking.TrackingUtil;
import org.spongepowered.common.event.tracking.context.transaction.EffectTransactor;
import org.spongepowered.common.event.tracking.phase.packet.PacketPhase;
import org.spongepowered.common.mixin.inventory.api.world.entity.player.PlayerMixin_Inventory_API;

Expand All @@ -44,6 +44,8 @@
@Mixin(net.minecraft.server.level.ServerPlayer.class)
public abstract class ServerPlayerMixin_Inventory_API extends PlayerMixin_Inventory_API implements ServerPlayer {

@Shadow public abstract void shadow$doCloseContainer();

@Override
public Optional<Container> openInventory() {
return Optional.ofNullable((Container) this.containerMenu);
Expand Down Expand Up @@ -75,12 +77,10 @@ public boolean closeInventory() throws IllegalArgumentException {
try (final PhaseContext<@NonNull ?> ctx = PacketPhase.General.CLOSE_WINDOW.createPhaseContext(PhaseTracker.SERVER)
.source(this)
.packetPlayer(player)
.isClientSide(false)
) {
ctx.buildAndSwitch();
try (final EffectTransactor ignored = ctx.getTransactor().logCloseInventory(player, false)) {
this.containerMenu.removed(player); // Drop & capture cursor item
this.containerMenu.broadcastChanges();
}
this.shadow$doCloseContainer();

if (!TrackingUtil.processBlockCaptures(ctx)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.HorseInventoryMenu;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.Slot;
import org.spongepowered.api.item.inventory.equipment.EquipmentType;
Expand All @@ -58,7 +56,6 @@
import org.spongepowered.common.event.tracking.context.transaction.EffectTransactor;
import org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier;
import org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction;
import org.spongepowered.common.event.tracking.phase.packet.PacketPhase;
import org.spongepowered.common.event.tracking.phase.packet.inventory.SwapHandItemsState;
import org.spongepowered.common.inventory.adapter.InventoryAdapter;
import org.spongepowered.common.inventory.fabric.Fabric;
Expand Down Expand Up @@ -118,28 +115,6 @@ public abstract class ServerPlayerMixin_Inventory extends PlayerMixin_Inventory
}
}

@Override
protected void inventory$switchToCloseWindowState(final InventoryMenu container, final Player player) {
// Corner case where the server is shutting down on the client, the server player is also being killed off.
if (Sponge.isServerAvailable() && Sponge.isClientAvailable()) {
container.removed(player);
return;
}
final ServerPlayer serverPlayer = (ServerPlayer) player;

try (final PhaseContext<@NonNull ?> ctx = PacketPhase.General.CLOSE_WINDOW.createPhaseContext(PhaseTracker.SERVER)
.source(serverPlayer)
.packetPlayer(serverPlayer)
) {
ctx.buildAndSwitch();
try (final EffectTransactor ignored = ctx.getTransactor().logCloseInventory(player, true)) {
container.removed(player); // Drop & capture cursor item
container.broadcastChanges();
}
}
this.impl$onCloseMenu(); // Handle Viewers
}

@Override
protected void inventory$onTouch(final Entity entity, final Player player) {
if (entity instanceof ItemEntity) {
Expand Down Expand Up @@ -255,7 +230,7 @@ public abstract class ServerPlayerMixin_Inventory extends PlayerMixin_Inventory
private void impl$onHandleContainerClose(final AbstractContainerMenu instance, final Player player) {
final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
final TransactionalCaptureSupplier transactor = context.getTransactor();
try (final EffectTransactor ignored = transactor.logCloseInventory(player, true)) {
try (final EffectTransactor ignored = transactor.logCloseInventory(context, player)) {
instance.removed(player);
instance.broadcastChanges();
}
Expand Down

0 comments on commit 641ffd6

Please sign in to comment.