Add server-side check for extendo-grip and contraption interaction

This commit is contained in:
Snownee 2021-01-31 16:06:45 +08:00
parent 4a15fbcec3
commit 5c64c2d942
2 changed files with 43 additions and 33 deletions

View file

@ -6,6 +6,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Abs
import com.simibubi.create.foundation.networking.SimplePacketBase; import com.simibubi.create.foundation.networking.SimplePacketBase;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer; import net.minecraft.network.PacketBuffer;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
@ -45,22 +46,26 @@ public class ContraptionInteractionPacket extends SimplePacketBase {
@Override @Override
public void handle(Supplier<Context> context) { public void handle(Supplier<Context> context) {
context.get() context.get().enqueueWork(() -> {
.enqueueWork(() -> { ServerPlayerEntity sender = context.get().getSender();
ServerPlayerEntity sender = context.get() if (sender == null)
.getSender(); return;
if (sender == null) Entity entityByID = sender.getServerWorld().getEntityByID(target);
return; if (!(entityByID instanceof AbstractContraptionEntity))
Entity entityByID = sender.getServerWorld() return;
.getEntityByID(target); AbstractContraptionEntity contraptionEntity = (AbstractContraptionEntity) entityByID;
if (!(entityByID instanceof AbstractContraptionEntity)) double d = sender.getAttribute(PlayerEntity.REACH_DISTANCE).getValue();
return; if (!sender.canEntityBeSeen(entityByID))
AbstractContraptionEntity contraptionEntity = (AbstractContraptionEntity) entityByID; d -= 3;
if (contraptionEntity.handlePlayerInteraction(sender, localPos, face, interactionHand)) d *= d;
sender.swingHand(interactionHand, true); if (sender.getDistanceSq(entityByID) > d) {
}); // TODO log?
context.get() return;
.setPacketHandled(true); }
if (contraptionEntity.handlePlayerInteraction(sender, localPos, face, interactionHand))
sender.swingHand(interactionHand, true);
});
context.get().setPacketHandled(true);
} }
} }

View file

@ -5,6 +5,7 @@ import java.util.function.Supplier;
import com.simibubi.create.foundation.networking.SimplePacketBase; import com.simibubi.create.foundation.networking.SimplePacketBase;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer; import net.minecraft.network.PacketBuffer;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
@ -53,25 +54,29 @@ public class ExtendoGripInteractionPacket extends SimplePacketBase {
@Override @Override
public void handle(Supplier<Context> context) { public void handle(Supplier<Context> context) {
context.get() context.get().enqueueWork(() -> {
.enqueueWork(() -> { ServerPlayerEntity sender = context.get().getSender();
ServerPlayerEntity sender = context.get() if (sender == null)
.getSender(); return;
if (sender == null) 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; 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);
} }
}); if (interactionHand == null)
context.get() sender.attackTargetEntityWithCurrentItem(entityByID);
.setPacketHandled(true); else if (specificPoint == null)
sender.interactOn(entityByID, interactionHand);
else
entityByID.applyPlayerInteraction(sender, specificPoint, interactionHand);
}
});
context.get().setPacketHandled(true);
} }
} }