Skip to content

Commit

Permalink
feat: check player interact distance
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 13, 2024
1 parent 0277b7c commit c87e8ad
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.enchantment.type.EnchantmentAquaAffinityType;
import org.allaymc.api.item.enchantment.type.EnchantmentEfficiencyType;
import org.allaymc.api.item.type.ItemType;
import org.allaymc.api.world.Dimension;

import static org.allaymc.api.item.ItemHelper.isSword;
Expand Down Expand Up @@ -45,6 +46,10 @@ default <DATATYPE> void updateBlockProperty(BlockPropertyType<DATATYPE> property
chunk.sendChunkPacket(Dimension.createBlockUpdatePacket(newBlockState, x, y, z, layer));
}

default boolean canDamageItem(ItemType<?> itemType) {
return true;
}

default double calculateBreakTime(BlockState blockState, ItemStack usedItem, Entity entity) {
checkBlockType(blockState);
if (usedItem.canInstantBreak(blockState)) return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.allaymc.api.entity.metadata.Metadata;
import org.allaymc.api.entity.type.EntityType;
import org.allaymc.api.math.location.Location3fc;
import org.allaymc.api.utils.MathUtils;
import org.allaymc.api.world.Dimension;
import org.allaymc.api.world.World;
import org.allaymc.api.world.chunk.Chunk;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import org.allaymc.api.client.skin.Skin;
import org.allaymc.api.client.storage.PlayerData;
import org.allaymc.api.entity.component.common.EntityBaseComponent;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.form.type.CustomForm;
import org.allaymc.api.form.type.Form;
import org.allaymc.api.math.location.Location3ic;
import org.allaymc.api.scoreboard.ScoreboardViewer;
import org.allaymc.api.utils.MathUtils;
import org.allaymc.api.world.chunk.ChunkLoader;
import org.cloudburstmc.protocol.bedrock.data.GameType;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
Expand Down Expand Up @@ -123,4 +125,22 @@ default void setAction(boolean value) {
CustomForm removeServerSettingForm(int id);

void showForm(Form form);

double BLOCK_INTERACT_MAX_DV_DIFF = 2.0;

default boolean canInteract(float x, float y, float z) {
var maxDistance = getMaxInteractDistance();
var location = getLocation();
if (location.distanceSquared(x, y, z) > maxDistance * maxDistance) return false;

var dv = MathUtils.JOMLVecToCBVec(MathUtils.getDirectionVector(location.yaw(), location.pitch()));
org.cloudburstmc.math.vector.Vector3f target = org.cloudburstmc.math.vector.Vector3f.from(x - location.x(), y - location.y(), z - location.z()).normalize();
var delta = dv.sub(target);
var diff = delta.dot(delta);
return diff < BLOCK_INTERACT_MAX_DV_DIFF;
}

default double getMaxInteractDistance() {
return getGameType() == GameType.CREATIVE ? 13 : 7;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.math.location.Location3f;
import org.allaymc.api.network.processor.PacketProcessor;
import org.allaymc.api.utils.MathUtils;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.GameType;
import org.cloudburstmc.protocol.bedrock.data.LevelEvent;
Expand Down Expand Up @@ -48,6 +49,15 @@ protected void handleBlockAction(EntityPlayer player, List<PlayerBlockActionData
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())) {
log.warn("Player {} tried to break a block out of reach", player.getOriginName());
continue;
}
}
}
switch (action.getAction()) {
case START_BREAK -> {
if (isInvalidGameType(player)) continue;
Expand Down Expand Up @@ -180,6 +190,13 @@ protected void sendBreakingPracticeAndTime(EntityPlayer player) {
player.getCurrentChunk().addChunkPacket(pk2);
}

protected void checkInteractDistance(EntityPlayer player) {
if (!player.canInteract(breakBlockX, breakBlockY, breakBlockZ)) {
log.warn("Player {} tried to interact with a block out of reach", player.getOriginName());
stopBreak(player);
}
}

protected void updateBreakingTime(EntityPlayer player) {
var newBreakingTime = breakBlock.getBehavior().calculateBreakTime(breakBlock, player.getContainer(FullContainerType.PLAYER_INVENTORY).getItemInHand(), player);
if (needBreakingTime == newBreakingTime) return;
Expand Down Expand Up @@ -218,7 +235,10 @@ public PacketSignal handleAsync(EntityPlayer player, PlayerAuthInputPacket packe
// The pos which client sends to the server is higher than the actual coordinates (one base offset)
handleMovement(player, packet.getPosition().sub(0, player.getBaseOffset(), 0), packet.getRotation());
handleBlockAction(player, packet.getPlayerActions());
if (isBreakingBlock()) sendBreakingPracticeAndTime(player);
if (isBreakingBlock()) {
sendBreakingPracticeAndTime(player);
checkInteractDistance(player);
}
return PacketSignal.UNHANDLED;
}

Expand Down

0 comments on commit c87e8ad

Please sign in to comment.