feat: allow setting max and min height for /rtp (#753)

This commit is contained in:
alazeprt authored and GitHub committed 2025-01-22 17:19:17 +00:00
1 parent 2aaf674306
commit 89db79da64
3 files changed
+67 -6

No files matched your search

@@ -25,6 +25,7 @@ import net.william278.huskhomes.position.Location;
import org.bukkit.Chunk;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Material;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
@@ -50,7 +51,8 @@ public interface BukkitSavePositionProvider extends SavePositionProvider {
.thenApply(snapshot -> findSafeLocationNear(
location,
snapshot,
bukkitLocation.getWorld().getMinHeight()
getMinHeight(bukkitLocation.getWorld()),
getMaxHeight(bukkitLocation.getWorld())
));
}
@@ -60,10 +62,11 @@ public interface BukkitSavePositionProvider extends SavePositionProvider {
* @param location The location to search around
* @param chunk The chunk snapshot to search
* @param minY The minimum Y value of the world
* @param maxY The maximum Y value of the world
* @return An optional safe location, within 4 blocks of the given location
*/
private Optional<Location> findSafeLocationNear(@NotNull Location location, @NotNull ChunkSnapshot chunk,
int minY) {
int minY, int maxY) {
final int chunkX = ((int) location.getX()) & 0xF;
final int chunkZ = ((int) location.getZ()) & 0xF;
@@ -74,7 +77,7 @@ public interface BukkitSavePositionProvider extends SavePositionProvider {
if (x < 0 || x >= 16 || z < 0 || z >= 16) {
continue;
}
final int y = Math.max((minY + 1), chunk.getHighestBlockYAt(x, z)) + 1;
final int y = Math.max((minY + 1), Math.min(chunk.getHighestBlockYAt(x, z), maxY)) + 1;
final Material blockType = chunk.getBlockType(x, y - 1, z);
final Material bodyBlockType = chunk.getBlockType(x, y, z);
final Material headBlockType = chunk.getBlockType(x, y + 1, z);
@@ -105,4 +108,30 @@ public interface BukkitSavePositionProvider extends SavePositionProvider {
return Optional.empty();
}
private int getMinHeight(World world) {
int minHeight = world.getMinHeight();
for (String pair : getPlugin().getSettings().getRtp().getMinHeight()) {
String worldName = pair.split(":")[0];
int settingsHeight = Integer.parseInt(pair.split(":")[1]);
if (world.getName().equals(worldName) & settingsHeight >= minHeight) {
minHeight = settingsHeight;
}
}
return minHeight;
}
private int getMaxHeight(World world) {
int maxHeight = world.getMaxHeight();
for (String pair : getPlugin().getSettings().getRtp().getMaxHeight()) {
String worldName = pair.split(":")[0];
int settingsHeight = Integer.parseInt(pair.split(":")[1]);
if (world.getName().equals(worldName) & settingsHeight >= maxHeight) {
maxHeight = settingsHeight;
}
}
return maxHeight;
}
}
@@ -330,6 +330,12 @@ public final class Settings {
@Comment("Standard deviation of the normal distribution for distributing players randomly")
private float distributionStandardDeviation = 2.0f;
@Comment({"Set the minimum random teleportation height for each world", "List of world_name:height pairs"})
private List<String> minHeight = Lists.newArrayList();
@Comment({"Set the maximum random teleportation height for each world", "List of world_name:height pairs"})
private List<String> maxHeight = Lists.newArrayList();
@Comment("List of worlds in which /rtp is disabled. Please note that /rtp does not work well in the nether.")
private List<String> restrictedWorlds = List.of("world_nether", "world_the_end");
@@ -29,6 +29,7 @@ import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.william278.huskhomes.FabricHuskHomes;
import net.william278.huskhomes.position.Location;
import org.jetbrains.annotations.NotNull;
@@ -57,7 +58,7 @@ public interface FabricSavePositionProvider extends SavePositionProvider {
return CompletableFuture.completedFuture(Optional.empty());
}
return CompletableFuture.completedFuture(findSafeLocationNear(location, world));
return CompletableFuture.completedFuture(findSafeLocationNear(location, world, location.getWorld().getName()));
}
/**
@@ -66,12 +67,13 @@ public interface FabricSavePositionProvider extends SavePositionProvider {
* @param location The location to search around
* @return An optional safe location, within 4 blocks of the given location
*/
private Optional<Location> findSafeLocationNear(@NotNull Location location, @NotNull ServerWorld world) {
private Optional<Location> findSafeLocationNear(@NotNull Location location, @NotNull ServerWorld world, @NotNull String worldName) {
final BlockPos.Mutable blockPos = new BlockPos.Mutable(location.getX(), location.getY(), location.getZ());
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()) + 1;
final int highestY = Math.max(getMinHeight(world, worldName), Math.min(getHighestYAt(world, blockPos.getX(),
blockPos.getY(), blockPos.getZ()) + 1, getMaxHeight(world, worldName)));
final Block block = world.getBlockState(blockPos.withY(highestY - 1)).getBlock();
final Identifier id = Registries.BLOCK.getId(block);
@@ -126,4 +128,28 @@ public interface FabricSavePositionProvider extends SavePositionProvider {
return cursor.getY();
}
private int getMinHeight(ServerWorld world, String worldName) {
int minHeight = world.getDimension().minY();
for (String pair : getPlugin().getSettings().getRtp().getMinHeight()) {
String settingsWorldName = pair.split(":")[0];
int settingsHeight = Integer.parseInt(pair.split(":")[1]);
if (settingsWorldName.equals(worldName) & settingsHeight >= minHeight) {
minHeight = settingsHeight;
}
}
return minHeight;
}
private int getMaxHeight(ServerWorld world, String worldName) {
int maxHeight = world.getDimension().height() + world.getDimension().minY();
for (String pair : getPlugin().getSettings().getRtp().getMaxHeight()) {
String settingsWorldName = pair.split(":")[0];
int settingsHeight = Integer.parseInt(pair.split(":")[1]);
if (settingsWorldName.equals(worldName) & settingsHeight >= maxHeight) {
maxHeight = settingsHeight;
}
}
return maxHeight;
}
}