From 5c64c2d94209d94c8f95f335ac43e75515af70e1 Mon Sep 17 00:00:00 2001 From: Snownee Date: Sun, 31 Jan 2021 16:06:45 +0800 Subject: [PATCH] Add server-side check for extendo-grip and contraption interaction --- .../sync/ContraptionInteractionPacket.java | 37 ++++++++++-------- .../tools/ExtendoGripInteractionPacket.java | 39 +++++++++++-------- 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/sync/ContraptionInteractionPacket.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/sync/ContraptionInteractionPacket.java index 5960d43fc..83e37704b 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/sync/ContraptionInteractionPacket.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/sync/ContraptionInteractionPacket.java @@ -6,6 +6,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Abs import com.simibubi.create.foundation.networking.SimplePacketBase; import net.minecraft.entity.Entity; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.network.PacketBuffer; import net.minecraft.util.Direction; @@ -45,22 +46,26 @@ public class ContraptionInteractionPacket extends SimplePacketBase { @Override public void handle(Supplier context) { - context.get() - .enqueueWork(() -> { - ServerPlayerEntity sender = context.get() - .getSender(); - if (sender == null) - return; - Entity entityByID = sender.getServerWorld() - .getEntityByID(target); - if (!(entityByID instanceof AbstractContraptionEntity)) - return; - AbstractContraptionEntity contraptionEntity = (AbstractContraptionEntity) entityByID; - if (contraptionEntity.handlePlayerInteraction(sender, localPos, face, interactionHand)) - sender.swingHand(interactionHand, true); - }); - context.get() - .setPacketHandled(true); + context.get().enqueueWork(() -> { + ServerPlayerEntity sender = context.get().getSender(); + if (sender == null) + return; + Entity entityByID = sender.getServerWorld().getEntityByID(target); + if (!(entityByID instanceof AbstractContraptionEntity)) + return; + AbstractContraptionEntity contraptionEntity = (AbstractContraptionEntity) entityByID; + double d = sender.getAttribute(PlayerEntity.REACH_DISTANCE).getValue(); + if (!sender.canEntityBeSeen(entityByID)) + d -= 3; + d *= d; + if (sender.getDistanceSq(entityByID) > d) { + // TODO log? + return; + } + if (contraptionEntity.handlePlayerInteraction(sender, localPos, face, interactionHand)) + sender.swingHand(interactionHand, true); + }); + context.get().setPacketHandled(true); } } diff --git a/src/main/java/com/simibubi/create/content/curiosities/tools/ExtendoGripInteractionPacket.java b/src/main/java/com/simibubi/create/content/curiosities/tools/ExtendoGripInteractionPacket.java index 63739b632..36364c95f 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/tools/ExtendoGripInteractionPacket.java +++ b/src/main/java/com/simibubi/create/content/curiosities/tools/ExtendoGripInteractionPacket.java @@ -5,6 +5,7 @@ import java.util.function.Supplier; import com.simibubi.create.foundation.networking.SimplePacketBase; import net.minecraft.entity.Entity; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.network.PacketBuffer; import net.minecraft.util.Hand; @@ -53,25 +54,29 @@ public class ExtendoGripInteractionPacket extends SimplePacketBase { @Override public void handle(Supplier context) { - context.get() - .enqueueWork(() -> { - ServerPlayerEntity sender = context.get() - .getSender(); - if (sender == null) + context.get().enqueueWork(() -> { + ServerPlayerEntity sender = context.get().getSender(); + if (sender == null) + return; + Entity entityByID = sender.getServerWorld().getEntityByID(target); + if (entityByID != null && ExtendoGripItem.isHoldingExtendoGrip(sender)) { + double d = sender.getAttribute(PlayerEntity.REACH_DISTANCE).getValue(); + if (!sender.canEntityBeSeen(entityByID)) + d -= 3; + d *= d; + if (sender.getDistanceSq(entityByID) > d) { + // TODO log? return; - Entity entityByID = sender.getServerWorld() - .getEntityByID(target); - if (entityByID != null && ExtendoGripItem.isHoldingExtendoGrip(sender)) { - if (interactionHand == null) - sender.attackTargetEntityWithCurrentItem(entityByID); - else if (specificPoint == null) - sender.interactOn(entityByID, interactionHand); - else - entityByID.applyPlayerInteraction(sender, specificPoint, interactionHand); } - }); - context.get() - .setPacketHandled(true); + if (interactionHand == null) + sender.attackTargetEntityWithCurrentItem(entityByID); + else if (specificPoint == null) + sender.interactOn(entityByID, interactionHand); + else + entityByID.applyPlayerInteraction(sender, specificPoint, interactionHand); + } + }); + context.get().setPacketHandled(true); } }