From 771c16ed1a59486124607b6299ec87926477e6e6 Mon Sep 17 00:00:00 2001 From: Jacob McSwain Date: Thu, 29 Jun 2023 04:05:57 -0500 Subject: [PATCH] Prevent suffocation while using `/rtp` (#445) * rtp: check for suffocation before teleport * safety resolvers: teleport to middle of block * rtp: Fix negative coordinates being off by one * unsafe blocks: add campfires and magma * unsafe blocks: deduplicate block id formatting --- .../huskhomes/util/BukkitSafetyResolver.java | 34 ++++++++++++++----- .../huskhomes/util/SafetyResolver.java | 14 ++++++-- .../huskhomes/util/UnsafeBlocks.java | 25 ++++++++++++-- .../main/resources/safety/unsafe_blocks.yml | 10 +++++- .../huskhomes/util/FabricSafetyResolver.java | 33 ++++++++++++++---- .../huskhomes/util/SpongeSafetyResolver.java | 28 ++++++++++++--- 6 files changed, 118 insertions(+), 26 deletions(-) diff --git a/bukkit/src/main/java/net/william278/huskhomes/util/BukkitSafetyResolver.java b/bukkit/src/main/java/net/william278/huskhomes/util/BukkitSafetyResolver.java index 92be1b76..921c9767 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/util/BukkitSafetyResolver.java +++ b/bukkit/src/main/java/net/william278/huskhomes/util/BukkitSafetyResolver.java @@ -72,15 +72,31 @@ public interface BukkitSafetyResolver extends SafetyResolver { if (x < 0 || x >= 16 || z < 0 || z >= 16) { continue; } - final int y = Math.max((minY + 1), snapshot.getHighestBlockYAt(x, z)); - final Material blockType = snapshot.getBlockType(chunkX, y, chunkZ); - if (isBlockSafe(blockType.getKey().toString())) { - return Optional.of(Location.at( - (location.getX() + dX) + 0.5d, - y + 1, - (location.getZ() + dZ) + 0.5d, - location.getWorld() - )); + final int y = Math.max((minY + 1), snapshot.getHighestBlockYAt(x, z)) + 1; + final Material blockType = snapshot.getBlockType(x, y - 1, z); + final Material bodyBlockType = snapshot.getBlockType(x, y, z); + final Material headBlockType = snapshot.getBlockType(x, y + 1, z); + if (isBlockSafeForStanding(blockType.getKey().toString()) + && isBlockSafeForOccupation(bodyBlockType.getKey().toString()) + && isBlockSafeForOccupation(headBlockType.getKey().toString())) { + double locx = Math.floor(location.getX()) + dX; + if (locx < 0) { + locx += 1.5d; + } else { + locx = locx + 0.5d; + } + double locz = Math.floor(location.getZ()) + dZ; + if (locz < 0) { + locz += 1.5d; + } else { + locz = locz + 0.5d; + } + return Optional.of(Location.at( + locx, + y, + locz, + location.getWorld() + )); } } } diff --git a/common/src/main/java/net/william278/huskhomes/util/SafetyResolver.java b/common/src/main/java/net/william278/huskhomes/util/SafetyResolver.java index 2697891b..01b5cfec 100644 --- a/common/src/main/java/net/william278/huskhomes/util/SafetyResolver.java +++ b/common/src/main/java/net/william278/huskhomes/util/SafetyResolver.java @@ -40,15 +40,25 @@ public interface SafetyResolver { CompletableFuture> findSafeGroundLocation(@NotNull Location location); /** - * Returns if the block, by provided identifier, is unsafe + * Returns if the block, by provided identifier, is unsafe to stand on * * @param blockId The block identifier (e.g. {@code minecraft:stone}) * @return {@code true} if the block is on the unsafe blocks list, {@code false} otherwise */ - default boolean isBlockSafe(@NotNull String blockId) { + default boolean isBlockSafeForStanding(@NotNull String blockId) { return !getPlugin().getUnsafeBlocks().isUnsafe(blockId); } + /** + * Returns if the block, by provided identifier, is unsafe to stand in + * + * @param blockId The block identifier (e.g. {@code minecraft:stone}) + * @return {@code true} if the block is able to be occupied, {@code false} otherwise + */ + default boolean isBlockSafeForOccupation(@NotNull String blockId) { + return !getPlugin().getUnsafeBlocks().isUnsafeToOccupy(blockId); + } + @NotNull HuskHomes getPlugin(); diff --git a/common/src/main/java/net/william278/huskhomes/util/UnsafeBlocks.java b/common/src/main/java/net/william278/huskhomes/util/UnsafeBlocks.java index 3bad9182..2bbef5dd 100644 --- a/common/src/main/java/net/william278/huskhomes/util/UnsafeBlocks.java +++ b/common/src/main/java/net/william278/huskhomes/util/UnsafeBlocks.java @@ -36,6 +36,16 @@ public class UnsafeBlocks { @YamlKey("unsafe_blocks") public List unsafeBlocks; + @YamlKey("safe_occupation_blocks") + public List safeOccupationBlocks; + + private String formatBlockId(@NotNull String blockId) { + if (!blockId.startsWith(Key.MINECRAFT_NAMESPACE + ":")) { + blockId = Key.MINECRAFT_NAMESPACE + ":" + blockId; + } + return blockId; + } + /** * Returns if the block, by provided identifier, is unsafe * @@ -43,12 +53,21 @@ public class UnsafeBlocks { * @return {@code true} if the block is on the unsafe blocks list, {@code false} otherwise */ public boolean isUnsafe(@NotNull String blockId) { - if (!blockId.startsWith(Key.MINECRAFT_NAMESPACE + ":")) { - blockId = Key.MINECRAFT_NAMESPACE + ":" + blockId; - } + blockId = this.formatBlockId(blockId); return unsafeBlocks.contains(blockId); } + /** + * Returns if the block, by provided identifier, is unsafe to stand in + * + * @param blockId The block identifier (e.g. {@code minecraft:stone}) + * @return {@code true} if the block is on the unsafe blocks list, {@code false} otherwise + */ + public boolean isUnsafeToOccupy(@NotNull String blockId) { + blockId = this.formatBlockId(blockId); + return !safeOccupationBlocks.contains(blockId); + } + @SuppressWarnings("unused") public UnsafeBlocks() { } diff --git a/common/src/main/resources/safety/unsafe_blocks.yml b/common/src/main/resources/safety/unsafe_blocks.yml index e8ff7563..3746ffba 100644 --- a/common/src/main/resources/safety/unsafe_blocks.yml +++ b/common/src/main/resources/safety/unsafe_blocks.yml @@ -22,4 +22,12 @@ unsafe_blocks: - 'minecraft:cave_air' - 'minecraft:void_air' - 'minecraft:pointed_dripstone' - - 'minecraft:cactus' \ No newline at end of file + - 'minecraft:cactus' + - 'minecraft:magma_block' + - 'minecraft:campfire' + - 'minecraft:soul_campfire' + +# Blocks that are safe to teleport into +safe_occupation_blocks: + - 'minecraft:air' + - 'minecraft:cave_air' diff --git a/fabric/src/main/java/net/william278/huskhomes/util/FabricSafetyResolver.java b/fabric/src/main/java/net/william278/huskhomes/util/FabricSafetyResolver.java index ebbb0ba3..66848b87 100644 --- a/fabric/src/main/java/net/william278/huskhomes/util/FabricSafetyResolver.java +++ b/fabric/src/main/java/net/william278/huskhomes/util/FabricSafetyResolver.java @@ -71,15 +71,36 @@ public interface FabricSafetyResolver extends SafetyResolver { for (int x = -SEARCH_RADIUS; x <= SEARCH_RADIUS; x++) { for (int z = -SEARCH_RADIUS; z <= SEARCH_RADIUS; z++) { blockPos.set(location.getX() + x, location.getY(), location.getZ() + z); - final int highestY = getHighestYAt(world, blockPos.getX(), blockPos.getY(), blockPos.getZ()); + final int highestY = getHighestYAt(world, blockPos.getX(), blockPos.getY(), blockPos.getZ()) + 1; - final Block block = world.getBlockState(blockPos.withY(highestY)).getBlock(); + final Block block = world.getBlockState(blockPos.withY(highestY - 1)).getBlock(); final Identifier id = Registries.BLOCK.getId(block); - if (!(block instanceof FluidBlock) && !(block instanceof FireBlock) && isBlockSafe(id.toString())) { + + final Block bodyBlockType = world.getBlockState(blockPos.withY(highestY)).getBlock(); + final Identifier bodyBlockId = Registries.BLOCK.getId(bodyBlockType); + + final Block headBlockType = world.getBlockState(blockPos.withY(highestY + 1)).getBlock(); + final Identifier headBlockId = Registries.BLOCK.getId(headBlockType); + + if (!(block instanceof FluidBlock) && !(block instanceof FireBlock) && isBlockSafeForStanding(id.toString()) + && isBlockSafeForOccupation(bodyBlockId.toString()) + && isBlockSafeForOccupation(headBlockId.toString())) { + double locx = Math.floor(blockPos.getX()); + if (locx < 0) { + locx += 1.5d; + } else { + locx = locx + 0.5d; + } + double locz = Math.floor(blockPos.getZ()); + if (locz < 0) { + locz += 1.5d; + } else { + locz = locz + 0.5d; + } return Optional.of(Location.at( - blockPos.getX() + 0.5, - highestY + 1, - blockPos.getZ() + 0.5, + locx, + highestY, + locz, location.getWorld() )); } diff --git a/sponge/src/main/java/net/william278/huskhomes/util/SpongeSafetyResolver.java b/sponge/src/main/java/net/william278/huskhomes/util/SpongeSafetyResolver.java index b4da6a00..9c12cb9a 100644 --- a/sponge/src/main/java/net/william278/huskhomes/util/SpongeSafetyResolver.java +++ b/sponge/src/main/java/net/william278/huskhomes/util/SpongeSafetyResolver.java @@ -60,13 +60,31 @@ public interface SpongeSafetyResolver extends SafetyResolver { private Optional findSafeLocationNear(@NotNull ServerLocation location, @NotNull World world) { for (int x = -SEARCH_RADIUS; x <= SEARCH_RADIUS; x++) { for (int z = -SEARCH_RADIUS; z <= SEARCH_RADIUS; z++) { - final Vector3i cursor = location.asHighestLocation().blockPosition().add(x, 0, z); + final Vector3i cursor = location.asHighestLocation().blockPosition().add(x, -1, z); + final Vector3i cursorBody = location.asHighestLocation().blockPosition().add(x, 0, z); + final Vector3i cursorHead = location.asHighestLocation().blockPosition().add(x, 1, z); final BlockState blockState = location.world().block(cursor); - if (isBlockSafe(blockState.type().key(RegistryTypes.BLOCK_TYPE).asString())) { + final BlockState bodyBlockState = location.world().block(cursorBody); + final BlockState headBlockState = location.world().block(cursorHead); + if (isBlockSafeForStanding(blockState.type().key(RegistryTypes.BLOCK_TYPE).asString()) + && isBlockSafeForOccupation(bodyBlockState.type().key(RegistryTypes.BLOCK_TYPE).asString()) + && isBlockSafeForOccupation(headBlockState.type().key(RegistryTypes.BLOCK_TYPE).asString())) { + double locx = Math.floor(cursor.x()); + if (locx < 0) { + locx += 1.5d; + } else { + locx = locx + 0.5d; + } + double locz = Math.floor(cursor.z()); + if (locz < 0) { + locz += 1.5d; + } else { + locz = locz + 0.5d; + } return Optional.of(Location.at( - cursor.x() + 0.5, - cursor.y() + 1, - cursor.z() + 0.5, + locx, + cursor.y(), + locz, world )); }