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
This commit is contained in:
Jacob McSwain authored and GitHub committed 2023-06-29 10:05:57 +01:00
1 parent 377e46ff92
commit 771c16ed1a
6 files changed
+118 -26

No files matched your search

@@ -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()
));
}
}
}
@@ -40,15 +40,25 @@ public interface SafetyResolver {
CompletableFuture<Optional<Location>> 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();
@@ -36,6 +36,16 @@ public class UnsafeBlocks {
@YamlKey("unsafe_blocks")
public List<String> unsafeBlocks;
@YamlKey("safe_occupation_blocks")
public List<String> 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() {
}
@@ -22,4 +22,12 @@ unsafe_blocks:
- 'minecraft:cave_air'
- 'minecraft:void_air'
- 'minecraft:pointed_dripstone'
- 'minecraft:cactus'
- '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'
@@ -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()
));
}
@@ -60,13 +60,31 @@ public interface SpongeSafetyResolver extends SafetyResolver {
private Optional<Location> 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
));
}