mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-04 14:38:53 +00:00
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:
1 parent
377e46ff92
commit
771c16ed1a
6 files changed
+117
-25
No files matched your search
@@ -72,15 +72,31 @@ public interface BukkitSafetyResolver extends SafetyResolver {
|
|||||||
if (x < 0 || x >= 16 || z < 0 || z >= 16) {
|
if (x < 0 || x >= 16 || z < 0 || z >= 16) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
final int y = Math.max((minY + 1), snapshot.getHighestBlockYAt(x, z));
|
final int y = Math.max((minY + 1), snapshot.getHighestBlockYAt(x, z)) + 1;
|
||||||
final Material blockType = snapshot.getBlockType(chunkX, y, chunkZ);
|
final Material blockType = snapshot.getBlockType(x, y - 1, z);
|
||||||
if (isBlockSafe(blockType.getKey().toString())) {
|
final Material bodyBlockType = snapshot.getBlockType(x, y, z);
|
||||||
return Optional.of(Location.at(
|
final Material headBlockType = snapshot.getBlockType(x, y + 1, z);
|
||||||
(location.getX() + dX) + 0.5d,
|
if (isBlockSafeForStanding(blockType.getKey().toString())
|
||||||
y + 1,
|
&& isBlockSafeForOccupation(bodyBlockType.getKey().toString())
|
||||||
(location.getZ() + dZ) + 0.5d,
|
&& isBlockSafeForOccupation(headBlockType.getKey().toString())) {
|
||||||
location.getWorld()
|
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);
|
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})
|
* @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
|
* @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);
|
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
|
@NotNull
|
||||||
HuskHomes getPlugin();
|
HuskHomes getPlugin();
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,16 @@ public class UnsafeBlocks {
|
|||||||
@YamlKey("unsafe_blocks")
|
@YamlKey("unsafe_blocks")
|
||||||
public List<String> unsafeBlocks;
|
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
|
* 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
|
* @return {@code true} if the block is on the unsafe blocks list, {@code false} otherwise
|
||||||
*/
|
*/
|
||||||
public boolean isUnsafe(@NotNull String blockId) {
|
public boolean isUnsafe(@NotNull String blockId) {
|
||||||
if (!blockId.startsWith(Key.MINECRAFT_NAMESPACE + ":")) {
|
blockId = this.formatBlockId(blockId);
|
||||||
blockId = Key.MINECRAFT_NAMESPACE + ":" + blockId;
|
|
||||||
}
|
|
||||||
return unsafeBlocks.contains(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")
|
@SuppressWarnings("unused")
|
||||||
public UnsafeBlocks() {
|
public UnsafeBlocks() {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,3 +23,11 @@ unsafe_blocks:
|
|||||||
- 'minecraft:void_air'
|
- 'minecraft:void_air'
|
||||||
- 'minecraft:pointed_dripstone'
|
- '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 x = -SEARCH_RADIUS; x <= SEARCH_RADIUS; x++) {
|
||||||
for (int z = -SEARCH_RADIUS; z <= SEARCH_RADIUS; z++) {
|
for (int z = -SEARCH_RADIUS; z <= SEARCH_RADIUS; z++) {
|
||||||
blockPos.set(location.getX() + x, location.getY(), location.getZ() + 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);
|
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(
|
return Optional.of(Location.at(
|
||||||
blockPos.getX() + 0.5,
|
locx,
|
||||||
highestY + 1,
|
highestY,
|
||||||
blockPos.getZ() + 0.5,
|
locz,
|
||||||
location.getWorld()
|
location.getWorld()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,13 +60,31 @@ public interface SpongeSafetyResolver extends SafetyResolver {
|
|||||||
private Optional<Location> findSafeLocationNear(@NotNull ServerLocation location, @NotNull World world) {
|
private Optional<Location> findSafeLocationNear(@NotNull ServerLocation location, @NotNull World world) {
|
||||||
for (int x = -SEARCH_RADIUS; x <= SEARCH_RADIUS; x++) {
|
for (int x = -SEARCH_RADIUS; x <= SEARCH_RADIUS; x++) {
|
||||||
for (int z = -SEARCH_RADIUS; z <= SEARCH_RADIUS; z++) {
|
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);
|
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(
|
return Optional.of(Location.at(
|
||||||
cursor.x() + 0.5,
|
locx,
|
||||||
cursor.y() + 1,
|
cursor.y(),
|
||||||
cursor.z() + 0.5,
|
locz,
|
||||||
world
|
world
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user