diff --git a/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java b/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java index 956b8879..748be706 100644 --- a/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java +++ b/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java @@ -81,7 +81,8 @@ public final class NormalDistributionEngine extends RandomTeleportEngine { */ private CompletableFuture> generateSafeLocation(@NotNull World world) { return plugin.findSafeGroundLocation(generateLocation( - getCenterPoint(world), mean, standardDeviation, spawnRadius, radius)); + getCenterPoint(world), mean, standardDeviation, spawnRadius, radius + )); } /** diff --git a/fabric/build.gradle b/fabric/build.gradle index 78b3a7b9..e3d7f4e4 100644 --- a/fabric/build.gradle +++ b/fabric/build.gradle @@ -30,7 +30,7 @@ dependencies { compileOnly 'org.jetbrains:annotations:24.0.1' - shadow project(path: ":common") + implementation project(path: ":common") } shadowJar { diff --git a/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java b/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java index 7a6b42a6..6f5096bf 100644 --- a/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java +++ b/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java @@ -27,12 +27,18 @@ import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; import net.kyori.adventure.platform.fabric.FabricServerAudiences; +import net.minecraft.block.BlockState; +import net.minecraft.block.Material; import net.minecraft.network.PacketByteBuf; +import net.minecraft.registry.Registries; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; +import net.minecraft.world.BlockView; import net.william278.annotaml.Annotaml; import net.william278.desertwell.Version; import net.william278.huskhomes.command.Command; @@ -342,7 +348,44 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes @Override public CompletableFuture> findSafeGroundLocation(@NotNull Location location) { - return CompletableFuture.completedFuture(Optional.of(location)); //todo + final Identifier worldId = Identifier.tryParse(location.getWorld().getName()); + final Optional locationWorld = minecraftServer.getWorldRegistryKeys().stream() + .filter(key -> key.getValue().equals(worldId)).findFirst() + .map(minecraftServer::getWorld); + if (locationWorld.isEmpty()) { + return CompletableFuture.completedFuture(Optional.empty()); + } + final ServerWorld world = locationWorld.get(); + + final BlockPos.Mutable blockPos = new BlockPos.Mutable(location.getX(), location.getY(), location.getZ()); + for (int x = -1; x <= 1; x++) { + for (int z = -1; z <= 1; z++) { + blockPos.set(location.getX() + x, location.getY(), location.getZ() + z); + final int highestY = getHighestYAt(world, blockPos.getX(), blockPos.getY(), blockPos.getZ()); + final BlockState blockState = world.getBlockState(blockPos.withY(highestY)); + + final Material material = blockState.getMaterial(); + final Identifier blockId = Registries.BLOCK.getId(blockState.getBlock()); + if (!material.isLiquid() && material != Material.FIRE && !unsafeBlocks.isUnsafe(blockId.toString())) { + return CompletableFuture.completedFuture(Optional.of(Location.at( + blockPos.getX() + 0.5, + highestY + 1, + blockPos.getZ() + 0.5, + location.getWorld() + ))); + } + } + } + + return CompletableFuture.completedFuture(Optional.empty()); + } + + private int getHighestYAt(@NotNull BlockView blockView, int x, int y, int z) { + final BlockPos.Mutable cursor = new BlockPos.Mutable(x, y, z); + while (blockView.getBlockState(cursor).isAir() && cursor.getY() > blockView.getBottomY()) { + cursor.move(Direction.DOWN); + } + return cursor.getY(); } @Override