Skip to content

Commit

Permalink
feat: allow setting max and min height for /rtp (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
alazeprt authored Jan 22, 2025
1 parent 2aaf674 commit 89db79d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand All @@ -50,7 +51,8 @@ default CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Lo
.thenApply(snapshot -> findSafeLocationNear(
location,
snapshot,
bukkitLocation.getWorld().getMinHeight()
getMinHeight(bukkitLocation.getWorld()),
getMaxHeight(bukkitLocation.getWorld())
));
}

Expand All @@ -60,10 +62,11 @@ default CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Lo
* @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;

Expand All @@ -74,7 +77,7 @@ private Optional<Location> findSafeLocationNear(@NotNull Location location, @Not
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);
Expand Down Expand Up @@ -105,4 +108,30 @@ && isBlockSafeForOccupation(headBlockType.getKey().toString())) {
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;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ public static class RtpRadius {
@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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -57,7 +58,7 @@ default CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Lo
return CompletableFuture.completedFuture(Optional.empty());
}

return CompletableFuture.completedFuture(findSafeLocationNear(location, world));
return CompletableFuture.completedFuture(findSafeLocationNear(location, world, location.getWorld().getName()));
}

/**
Expand All @@ -66,12 +67,13 @@ default CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Lo
* @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);
Expand Down Expand Up @@ -126,4 +128,28 @@ private int getHighestYAt(@NotNull BlockView blockView, int x, int y, int z) {
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;
}

}

0 comments on commit 89db79d

Please sign in to comment.