From ab67837e5cd2be722c06e30f7870b0883d80f13b Mon Sep 17 00:00:00 2001 From: Dmitry Luk Date: Sat, 29 Jun 2024 19:30:11 +0500 Subject: [PATCH] chores: add CraftCreativeActionProcessor expression --- .../component/player/EntityPlayerBaseComponent.java | 7 ++++++- .../processor/CraftCreativeActionProcessor.java | 4 ++++ .../processor/BlockPickRequestPacketProcessor.java | 12 ++++-------- .../processor/PlayerAuthInputPacketProcessor.java | 5 ++--- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Allay-API/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerBaseComponent.java b/Allay-API/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerBaseComponent.java index 65bb74766..f04df6c46 100644 --- a/Allay-API/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerBaseComponent.java +++ b/Allay-API/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerBaseComponent.java @@ -15,6 +15,7 @@ import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.UnmodifiableView; +import org.joml.Vector3ic; import java.util.Map; @@ -133,7 +134,11 @@ default void setAction(boolean value) { void showForm(Form form); - default boolean canInteract(float x, float y, float z) { + default boolean canReach(Vector3ic pos) { + return canReach(pos.x(), pos.y(), pos.z()); + } + + default boolean canReach(float x, float y, float z) { var maxDistance = getMaxInteractDistance(); var location = getLocation(); if (location.distanceSquared(x, y, z) > maxDistance * maxDistance) return false; diff --git a/Allay-Server/src/main/java/org/allaymc/server/container/processor/CraftCreativeActionProcessor.java b/Allay-Server/src/main/java/org/allaymc/server/container/processor/CraftCreativeActionProcessor.java index 777585234..a58569ef2 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/container/processor/CraftCreativeActionProcessor.java +++ b/Allay-Server/src/main/java/org/allaymc/server/container/processor/CraftCreativeActionProcessor.java @@ -4,6 +4,7 @@ import org.allaymc.api.container.FullContainerType; import org.allaymc.api.entity.interfaces.EntityPlayer; import org.allaymc.api.item.registry.CreativeItemRegistry; +import org.cloudburstmc.protocol.bedrock.data.GameType; import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.CraftCreativeAction; import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.ItemStackRequestAction; import org.cloudburstmc.protocol.bedrock.data.inventory.itemstack.request.action.ItemStackRequestActionType; @@ -19,6 +20,9 @@ public class CraftCreativeActionProcessor implements ContainerActionProcessor { @Override public ActionResponse handle(CraftCreativeAction action, EntityPlayer player, int currentActionIndex, ItemStackRequestAction[] actions, Map dataPool) { + if (player.getGameType() != GameType.CREATIVE || player.getGameType() != GameType.SPECTATOR) + return error(); + var item = CreativeItemRegistry.getRegistry().get(action.getCreativeItemNetworkId() - 1); if (item == null) { log.warn("Unknown creative item network id: {}", action.getCreativeItemNetworkId() - 1); diff --git a/Allay-Server/src/main/java/org/allaymc/server/network/processor/BlockPickRequestPacketProcessor.java b/Allay-Server/src/main/java/org/allaymc/server/network/processor/BlockPickRequestPacketProcessor.java index 3bdbc795a..37699ea5a 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/network/processor/BlockPickRequestPacketProcessor.java +++ b/Allay-Server/src/main/java/org/allaymc/server/network/processor/BlockPickRequestPacketProcessor.java @@ -5,6 +5,7 @@ import org.allaymc.api.container.FullContainerType; import org.allaymc.api.entity.interfaces.EntityPlayer; import org.allaymc.api.network.processor.PacketProcessor; +import org.allaymc.api.utils.MathUtils; import org.cloudburstmc.protocol.bedrock.data.GameType; import org.cloudburstmc.protocol.bedrock.packet.BedrockPacketType; import org.cloudburstmc.protocol.bedrock.packet.BlockPickRequestPacket; @@ -18,15 +19,10 @@ public class BlockPickRequestPacketProcessor extends PacketProcessor { @Override public void handleSync(EntityPlayer player, BlockPickRequestPacket packet) { - if (player.getGameType() != GameType.CREATIVE) { - log.warn("Player {} tried to pick block in non-creative mode!", player.getOriginName()); - return; - } + var blockPos = MathUtils.CBVecToJOMLVec(packet.getBlockPosition()); + if (!player.canReach(blockPos) || player.getGameType() != GameType.CREATIVE) return; - var pos = packet.getBlockPosition(); - // TODO: includeBlockEntityData - var includeBlockEntityData = packet.isAddUserData(); - var block = player.getLocation().dimension().getBlockState(pos.getX(), pos.getY(), pos.getZ()); + var block = player.getLocation().dimension().getBlockState(blockPos); if (block.getBlockType() == BlockTypes.AIR_TYPE) { log.warn("Player {} tried to pick air!", player.getOriginName()); return; diff --git a/Allay-Server/src/main/java/org/allaymc/server/network/processor/PlayerAuthInputPacketProcessor.java b/Allay-Server/src/main/java/org/allaymc/server/network/processor/PlayerAuthInputPacketProcessor.java index cd5f6a257..5f9dcb360 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/network/processor/PlayerAuthInputPacketProcessor.java +++ b/Allay-Server/src/main/java/org/allaymc/server/network/processor/PlayerAuthInputPacketProcessor.java @@ -62,13 +62,12 @@ protected void handleMovement(EntityPlayer player, Vector3f newPos, Vector3f new } protected void handleBlockAction(EntityPlayer player, List blockActions) { - if (blockActions.isEmpty()) return; for (var action : blockActions) { var pos = action.getBlockPosition(); // Check interact distance switch (action.getAction()) { case START_BREAK, BLOCK_CONTINUE_DESTROY -> { - if (!player.canInteract(pos.getX(), pos.getY(), pos.getZ())) { + if (!player.canReach(pos.getX(), pos.getY(), pos.getZ())) { log.warn("Player {} tried to break a block out of reach", player.getOriginName()); continue; } @@ -202,7 +201,7 @@ protected void sendBreakingPracticeAndTime(EntityPlayer player) { } protected void checkInteractDistance(EntityPlayer player) { - if (!player.canInteract(breakBlockX, breakBlockY, breakBlockZ)) { + if (!player.canReach(breakBlockX, breakBlockY, breakBlockZ)) { log.warn("Player {} tried to interact with a block out of reach", player.getOriginName()); stopBreak(player); }