mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 22:16:34 +00:00
Ensure RTP positions are within border, cleanup safety logic (#395)
This commit is contained in:
1 parent
24190930a2
commit
8b94aae22a
8 files changed
+115
-40
No files matched your search
@@ -167,6 +167,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the random teleport engine
|
||||||
setRandomTeleportEngine(new NormalDistributionEngine(this));
|
setRandomTeleportEngine(new NormalDistributionEngine(this));
|
||||||
|
|
||||||
// Register plugin hooks (Economy, Maps, Plan)
|
// Register plugin hooks (Economy, Maps, Plan)
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ package net.william278.huskhomes.util;
|
|||||||
import io.papermc.lib.PaperLib;
|
import io.papermc.lib.PaperLib;
|
||||||
import net.william278.huskhomes.position.Location;
|
import net.william278.huskhomes.position.Location;
|
||||||
import org.bukkit.Chunk;
|
import org.bukkit.Chunk;
|
||||||
|
import org.bukkit.ChunkSnapshot;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@@ -37,31 +38,48 @@ public interface BukkitSafetyResolver extends SafetyResolver {
|
|||||||
return CompletableFuture.completedFuture(Optional.empty());
|
return CompletableFuture.completedFuture(Optional.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
return PaperLib.getChunkAtAsync(bukkitLocation).thenApply(Chunk::getChunkSnapshot).thenApply(snapshot -> {
|
// Ensure the location is within the world border
|
||||||
final int chunkX = bukkitLocation.getBlockX() & 0xF;
|
if (!bukkitLocation.getWorld().getWorldBorder().isInside(bukkitLocation)) {
|
||||||
final int chunkZ = bukkitLocation.getBlockZ() & 0xF;
|
return CompletableFuture.completedFuture(Optional.empty());
|
||||||
|
}
|
||||||
|
|
||||||
for (int dX = -1; dX <= 2; dX++) {
|
// Search nearby blocks for a safe location
|
||||||
for (int dZ = -1; dZ <= 2; dZ++) {
|
return PaperLib.getChunkAtAsync(bukkitLocation)
|
||||||
final int x = chunkX + dX;
|
.thenApply(Chunk::getChunkSnapshot)
|
||||||
final int z = chunkZ + dZ;
|
.thenApply(snapshot -> findSafeLocationNear(location, snapshot));
|
||||||
if (x < 0 || x >= 16 || z < 0 || z >= 16) {
|
}
|
||||||
continue;
|
|
||||||
}
|
/**
|
||||||
final int y = snapshot.getHighestBlockYAt(x, z);
|
* Search for a safe ground location near the given location
|
||||||
final Material blockType = snapshot.getBlockType(chunkX, y, chunkZ);
|
*
|
||||||
if (isBlockSafe(blockType.getKey().toString())) {
|
* @param location The location to search around
|
||||||
return Optional.of(Location.at(
|
* @param snapshot The chunk snapshot to search
|
||||||
(location.getX() + dX) + 0.5d,
|
* @return An optional safe location, within 4 blocks of the given location
|
||||||
y + 1.25d,
|
*/
|
||||||
(location.getZ() + dZ) + 0.5d,
|
private Optional<Location> findSafeLocationNear(@NotNull Location location, @NotNull ChunkSnapshot snapshot) {
|
||||||
location.getWorld()
|
final int chunkX = ((int) location.getX()) & 0xF;
|
||||||
));
|
final int chunkZ = ((int) location.getZ()) & 0xF;
|
||||||
}
|
|
||||||
|
for (int dX = -SEARCH_RADIUS; dX <= SEARCH_RADIUS; dX++) {
|
||||||
|
for (int dZ = -SEARCH_RADIUS; dZ <= SEARCH_RADIUS; dZ++) {
|
||||||
|
final int x = chunkX + dX;
|
||||||
|
final int z = chunkZ + dZ;
|
||||||
|
if (x < 0 || x >= 16 || z < 0 || z >= 16) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
final int y = 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.25d,
|
||||||
|
(location.getZ() + dZ) + 0.5d,
|
||||||
|
location.getWorld()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
}
|
||||||
});
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,10 @@ import net.william278.huskhomes.user.ConsoleUser;
|
|||||||
import net.william278.huskhomes.user.OnlineUser;
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
import net.william278.huskhomes.user.SavedUser;
|
import net.william278.huskhomes.user.SavedUser;
|
||||||
import net.william278.huskhomes.user.User;
|
import net.william278.huskhomes.user.User;
|
||||||
import net.william278.huskhomes.util.*;
|
import net.william278.huskhomes.util.SafetyResolver;
|
||||||
|
import net.william278.huskhomes.util.TaskRunner;
|
||||||
|
import net.william278.huskhomes.util.UnsafeBlocks;
|
||||||
|
import net.william278.huskhomes.util.Validator;
|
||||||
import org.intellij.lang.annotations.Subst;
|
import org.intellij.lang.annotations.Subst;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package net.william278.huskhomes.teleport;
|
package net.william278.huskhomes.teleport;
|
||||||
|
|
||||||
|
import net.william278.desertwell.util.ThrowingConsumer;
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.command.BackCommand;
|
import net.william278.huskhomes.command.BackCommand;
|
||||||
import net.william278.huskhomes.config.Settings;
|
import net.william278.huskhomes.config.Settings;
|
||||||
@@ -28,7 +29,6 @@ import net.william278.huskhomes.network.Message;
|
|||||||
import net.william278.huskhomes.network.Payload;
|
import net.william278.huskhomes.network.Payload;
|
||||||
import net.william278.huskhomes.position.Position;
|
import net.william278.huskhomes.position.Position;
|
||||||
import net.william278.huskhomes.user.OnlineUser;
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
import net.william278.desertwell.util.ThrowingConsumer;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|||||||
@@ -19,8 +19,8 @@
|
|||||||
|
|
||||||
package net.william278.huskhomes.teleport;
|
package net.william278.huskhomes.teleport;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import net.william278.huskhomes.user.OnlineUser;
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a teleporter; the person performing the teleport.
|
* Represents a teleporter; the person performing the teleport.
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
|
|
||||||
public interface SafetyResolver {
|
public interface SafetyResolver {
|
||||||
|
|
||||||
|
int SEARCH_RADIUS = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a safe ground location for the specified {@link Location} if possible
|
* Returns a safe ground location for the specified {@link Location} if possible
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -41,17 +41,34 @@ public interface FabricSafetyResolver extends SafetyResolver {
|
|||||||
default CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location) {
|
default CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location) {
|
||||||
final MinecraftServer server = ((FabricHuskHomes) getPlugin()).getMinecraftServer();
|
final MinecraftServer server = ((FabricHuskHomes) getPlugin()).getMinecraftServer();
|
||||||
final Identifier worldId = Identifier.tryParse(location.getWorld().getName());
|
final Identifier worldId = Identifier.tryParse(location.getWorld().getName());
|
||||||
|
|
||||||
|
// Ensure the location is on a valid world
|
||||||
final Optional<ServerWorld> locationWorld = server.getWorldRegistryKeys().stream()
|
final Optional<ServerWorld> locationWorld = server.getWorldRegistryKeys().stream()
|
||||||
.filter(key -> key.getValue().equals(worldId)).findFirst()
|
.filter(key -> key.getValue().equals(worldId)).findFirst()
|
||||||
.map(server::getWorld);
|
.map(server::getWorld);
|
||||||
if (locationWorld.isEmpty()) {
|
if (locationWorld.isEmpty()) {
|
||||||
return CompletableFuture.completedFuture(Optional.empty());
|
return CompletableFuture.completedFuture(Optional.empty());
|
||||||
}
|
}
|
||||||
final ServerWorld world = locationWorld.get();
|
|
||||||
|
|
||||||
|
// Ensure the location is within the world border
|
||||||
|
final ServerWorld world = locationWorld.get();
|
||||||
|
if (!world.getWorldBorder().contains(location.getX(), location.getZ())) {
|
||||||
|
return CompletableFuture.completedFuture(Optional.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
return CompletableFuture.completedFuture(findSafeLocationNear(location, world));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for a safe ground location near the given location
|
||||||
|
*
|
||||||
|
* @param location The location to search around
|
||||||
|
* @return An optional safe location, within 4 blocks of the given location
|
||||||
|
*/
|
||||||
|
private Optional<Location> findSafeLocationNear(Location location, ServerWorld world) {
|
||||||
final BlockPos.Mutable blockPos = new BlockPos.Mutable(location.getX(), location.getY(), location.getZ());
|
final BlockPos.Mutable blockPos = new BlockPos.Mutable(location.getX(), location.getY(), location.getZ());
|
||||||
for (int x = -1; x <= 1; x++) {
|
for (int x = -SEARCH_RADIUS; x <= SEARCH_RADIUS; x++) {
|
||||||
for (int z = -1; z <= 1; 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());
|
||||||
final BlockState blockState = world.getBlockState(blockPos.withY(highestY));
|
final BlockState blockState = world.getBlockState(blockPos.withY(highestY));
|
||||||
@@ -59,19 +76,27 @@ public interface FabricSafetyResolver extends SafetyResolver {
|
|||||||
final Material material = blockState.getMaterial();
|
final Material material = blockState.getMaterial();
|
||||||
final Identifier blockId = Registries.BLOCK.getId(blockState.getBlock());
|
final Identifier blockId = Registries.BLOCK.getId(blockState.getBlock());
|
||||||
if (!material.isLiquid() && material != Material.FIRE && isBlockSafe(blockId.toString())) {
|
if (!material.isLiquid() && material != Material.FIRE && isBlockSafe(blockId.toString())) {
|
||||||
return CompletableFuture.completedFuture(Optional.of(Location.at(
|
return Optional.of(Location.at(
|
||||||
blockPos.getX() + 0.5,
|
blockPos.getX() + 0.5,
|
||||||
highestY + 1,
|
highestY + 1,
|
||||||
blockPos.getZ() + 0.5,
|
blockPos.getZ() + 0.5,
|
||||||
location.getWorld()
|
location.getWorld()
|
||||||
)));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return Optional.empty();
|
||||||
return CompletableFuture.completedFuture(Optional.empty());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the highest Y value at the given X and Z coordinates
|
||||||
|
*
|
||||||
|
* @param blockView The block view to search
|
||||||
|
* @param x The X coordinate
|
||||||
|
* @param y The Y coordinate
|
||||||
|
* @param z The Z coordinate
|
||||||
|
* @return The highest Y value at the given X and Z coordinates
|
||||||
|
*/
|
||||||
private int getHighestYAt(@NotNull BlockView blockView, int x, int y, int z) {
|
private int getHighestYAt(@NotNull BlockView blockView, int x, int y, int z) {
|
||||||
final BlockPos.Mutable cursor = new BlockPos.Mutable(x, y, z);
|
final BlockPos.Mutable cursor = new BlockPos.Mutable(x, y, z);
|
||||||
while (blockView.getBlockState(cursor).isAir() && cursor.getY() > blockView.getBottomY()) {
|
while (blockView.getBlockState(cursor).isAir() && cursor.getY() > blockView.getBottomY()) {
|
||||||
|
|||||||
@@ -20,10 +20,13 @@
|
|||||||
package net.william278.huskhomes.util;
|
package net.william278.huskhomes.util;
|
||||||
|
|
||||||
import net.william278.huskhomes.position.Location;
|
import net.william278.huskhomes.position.Location;
|
||||||
|
import net.william278.huskhomes.position.World;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.spongepowered.api.block.BlockState;
|
import org.spongepowered.api.block.BlockState;
|
||||||
import org.spongepowered.api.registry.RegistryTypes;
|
import org.spongepowered.api.registry.RegistryTypes;
|
||||||
|
import org.spongepowered.api.world.border.WorldBorder;
|
||||||
import org.spongepowered.api.world.server.ServerLocation;
|
import org.spongepowered.api.world.server.ServerLocation;
|
||||||
|
import org.spongepowered.math.vector.Vector2d;
|
||||||
import org.spongepowered.math.vector.Vector3i;
|
import org.spongepowered.math.vector.Vector3i;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -38,23 +41,46 @@ public interface SpongeSafetyResolver extends SafetyResolver {
|
|||||||
return CompletableFuture.completedFuture(Optional.empty());
|
return CompletableFuture.completedFuture(Optional.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure the location is within the world border
|
||||||
final ServerLocation serverLocation = adaptedLocation.get();
|
final ServerLocation serverLocation = adaptedLocation.get();
|
||||||
for (int x = -1; x <= 1; x++) {
|
if (isInBorder(serverLocation.world().border(), serverLocation.blockPosition())) {
|
||||||
for (int z = -1; z <= 1; z++) {
|
return CompletableFuture.completedFuture(Optional.empty());
|
||||||
final Vector3i cursor = serverLocation.asHighestLocation().blockPosition().add(x, 0, z);
|
}
|
||||||
final BlockState blockState = serverLocation.world().block(cursor);
|
|
||||||
|
return CompletableFuture.completedFuture(findSafeLocationNear(serverLocation, location.getWorld()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for a safe ground location near the given location
|
||||||
|
*
|
||||||
|
* @param location The location to search around
|
||||||
|
* @param world The world to search in
|
||||||
|
* @return An optional safe location, within 4 blocks of the given location
|
||||||
|
*/
|
||||||
|
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 BlockState blockState = location.world().block(cursor);
|
||||||
if (isBlockSafe(blockState.type().key(RegistryTypes.BLOCK_TYPE).asString())) {
|
if (isBlockSafe(blockState.type().key(RegistryTypes.BLOCK_TYPE).asString())) {
|
||||||
return CompletableFuture.completedFuture(Optional.of(Location.at(
|
return Optional.of(Location.at(
|
||||||
cursor.x() + 0.5,
|
cursor.x() + 0.5,
|
||||||
cursor.y() + 1,
|
cursor.y() + 1,
|
||||||
cursor.z() + 0.5,
|
cursor.z() + 0.5,
|
||||||
location.getWorld()
|
world
|
||||||
)));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
return CompletableFuture.completedFuture(Optional.empty());
|
private boolean isInBorder(WorldBorder border, Vector3i position) {
|
||||||
|
final Vector2d center = border.center();
|
||||||
|
final double radius = border.diameter() / 2;
|
||||||
|
final double x = position.x() - center.x();
|
||||||
|
final double z = position.z() - center.y();
|
||||||
|
return x * x + z * z < radius * radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user