From caa498875c6f024fb17ed5d5764afc9fe0df62b1 Mon Sep 17 00:00:00 2001 From: simibubi <31564874+simibubi@users.noreply.github.com> Date: Fri, 19 Aug 2022 14:35:33 +0200 Subject: [PATCH] Ask First - Fixed Arms and Ejectors sending their placement configuration too early --- .../logistics/block/depot/EjectorItem.java | 15 ++++++---- .../block/depot/EjectorPlacementPacket.java | 30 +++++++++++++++++++ .../block/mechanicalArm/ArmItem.java | 18 ++++++----- .../mechanicalArm/ArmPlacementPacket.java | 30 +++++++++++++++++++ .../block/mechanicalArm/ArmTileEntity.java | 4 +-- .../foundation/networking/AllPackets.java | 4 +++ 6 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/logistics/block/depot/EjectorItem.java b/src/main/java/com/simibubi/create/content/logistics/block/depot/EjectorItem.java index 18a2706a3..a11995884 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/depot/EjectorItem.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/depot/EjectorItem.java @@ -1,6 +1,9 @@ package com.simibubi.create.content.logistics.block.depot; +import com.simibubi.create.foundation.networking.AllPackets; + import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.BlockItem; @@ -10,9 +13,8 @@ import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; +import net.minecraftforge.network.PacketDistributor; @EventBusSubscriber public class EjectorItem extends BlockItem { @@ -36,11 +38,12 @@ public class EjectorItem extends BlockItem { } @Override - protected boolean updateCustomBlockEntityTag(BlockPos pos, Level world, Player p_195943_3_, ItemStack p_195943_4_, + protected boolean updateCustomBlockEntityTag(BlockPos pos, Level world, Player player, ItemStack p_195943_4_, BlockState p_195943_5_) { - if (world.isClientSide) - DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> EjectorTargetHandler.flushSettings(pos)); - return super.updateCustomBlockEntityTag(pos, world, p_195943_3_, p_195943_4_, p_195943_5_); + if (!world.isClientSide && player instanceof ServerPlayer sp) + AllPackets.channel.send(PacketDistributor.PLAYER.with(() -> sp), + new EjectorPlacementPacket.ClientBoundRequest(pos)); + return super.updateCustomBlockEntityTag(pos, world, player, p_195943_4_, p_195943_5_); } @Override diff --git a/src/main/java/com/simibubi/create/content/logistics/block/depot/EjectorPlacementPacket.java b/src/main/java/com/simibubi/create/content/logistics/block/depot/EjectorPlacementPacket.java index 3b56b438f..1ed70b699 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/depot/EjectorPlacementPacket.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/depot/EjectorPlacementPacket.java @@ -12,6 +12,8 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.network.NetworkEvent.Context; public class EjectorPlacementPacket extends SimplePacketBase { @@ -65,4 +67,32 @@ public class EjectorPlacementPacket extends SimplePacketBase { } + public static class ClientBoundRequest extends SimplePacketBase { + + BlockPos pos; + + public ClientBoundRequest(BlockPos pos) { + this.pos = pos; + } + + public ClientBoundRequest(FriendlyByteBuf buffer) { + this.pos = buffer.readBlockPos(); + } + + @Override + public void write(FriendlyByteBuf buffer) { + buffer.writeBlockPos(pos); + } + + @Override + public void handle(Supplier context) { + context.get() + .enqueueWork(() -> DistExecutor.unsafeRunWhenOn(Dist.CLIENT, + () -> () -> EjectorTargetHandler.flushSettings(pos))); + context.get() + .setPacketHandled(true); + } + + } + } diff --git a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmItem.java b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmItem.java index ee4389098..3ca57ea7d 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmItem.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmItem.java @@ -1,6 +1,9 @@ package com.simibubi.create.content.logistics.block.mechanicalArm; +import com.simibubi.create.foundation.networking.AllPackets; + import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.BlockItem; @@ -9,9 +12,8 @@ import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; +import net.minecraftforge.network.PacketDistributor; @EventBusSubscriber public class ArmItem extends BlockItem { @@ -30,16 +32,16 @@ public class ArmItem extends BlockItem { } @Override - protected boolean updateCustomBlockEntityTag(BlockPos pos, Level world, Player p_195943_3_, ItemStack p_195943_4_, + protected boolean updateCustomBlockEntityTag(BlockPos pos, Level world, Player player, ItemStack p_195943_4_, BlockState p_195943_5_) { - if (world.isClientSide) - DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ArmInteractionPointHandler.flushSettings(pos)); - return super.updateCustomBlockEntityTag(pos, world, p_195943_3_, p_195943_4_, p_195943_5_); + if (!world.isClientSide && player instanceof ServerPlayer sp) + AllPackets.channel.send(PacketDistributor.PLAYER.with(() -> sp), + new ArmPlacementPacket.ClientBoundRequest(pos)); + return super.updateCustomBlockEntityTag(pos, world, player, p_195943_4_, p_195943_5_); } @Override - public boolean canAttackBlock(BlockState state, Level world, BlockPos pos, - Player p_195938_4_) { + public boolean canAttackBlock(BlockState state, Level world, BlockPos pos, Player p_195938_4_) { return !ArmInteractionPoint.isInteractable(world, pos, state); } diff --git a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmPlacementPacket.java b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmPlacementPacket.java index afa3229be..f760e3aba 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmPlacementPacket.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmPlacementPacket.java @@ -13,6 +13,8 @@ import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.network.NetworkEvent.Context; public class ArmPlacementPacket extends SimplePacketBase { @@ -67,4 +69,32 @@ public class ArmPlacementPacket extends SimplePacketBase { } + public static class ClientBoundRequest extends SimplePacketBase { + + BlockPos pos; + + public ClientBoundRequest(BlockPos pos) { + this.pos = pos; + } + + public ClientBoundRequest(FriendlyByteBuf buffer) { + this.pos = buffer.readBlockPos(); + } + + @Override + public void write(FriendlyByteBuf buffer) { + buffer.writeBlockPos(pos); + } + + @Override + public void handle(Supplier context) { + context.get() + .enqueueWork(() -> DistExecutor.unsafeRunWhenOn(Dist.CLIENT, + () -> () -> ArmInteractionPointHandler.flushSettings(pos))); + context.get() + .setPacketHandled(true); + } + + } + } diff --git a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmTileEntity.java b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmTileEntity.java index 77eefd030..c4f74e047 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmTileEntity.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmTileEntity.java @@ -81,7 +81,7 @@ public class ArmTileEntity extends KineticTileEntity implements ITransformableTE super(typeIn, pos, state); inputs = new ArrayList<>(); outputs = new ArrayList<>(); - interactionPointTag = new ListTag(); + interactionPointTag = null; heldItem = ItemStack.EMPTY; phase = Phase.SEARCH_INPUTS; previousTarget = ArmAngleTarget.NO_TARGET; @@ -480,7 +480,7 @@ public class ArmTileEntity extends KineticTileEntity implements ITransformableTE } public void writeInteractionPoints(CompoundTag compound) { - if (updateInteractionPoints) { + if (updateInteractionPoints && interactionPointTag != null) { compound.put("InteractionPoints", interactionPointTag); } else { ListTag pointsNBT = new ListTag(); diff --git a/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java b/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java index 6dabd9875..2bbf93b93 100644 --- a/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java +++ b/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java @@ -184,6 +184,10 @@ public enum AllPackets { S_TRAIN_PROMPT(TrainPromptPacket.class, TrainPromptPacket::new, PLAY_TO_CLIENT), CONTRAPTION_RELOCATION(ContraptionRelocationPacket.class, ContraptionRelocationPacket::new, PLAY_TO_CLIENT), TRACK_GRAPH_ROLL_CALL(TrackGraphRollCallPacket.class, TrackGraphRollCallPacket::new, PLAY_TO_CLIENT), + S_PLACE_EJECTOR(ArmPlacementPacket.ClientBoundRequest.class, ArmPlacementPacket.ClientBoundRequest::new, + PLAY_TO_CLIENT), + S_PLACE_ARM(EjectorPlacementPacket.ClientBoundRequest.class, EjectorPlacementPacket.ClientBoundRequest::new, + PLAY_TO_CLIENT), ;