mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
Decouple block safety from main class
This commit is contained in:
6 files changed
+167
-126
No files matched your search
@@ -19,7 +19,6 @@
|
||||
|
||||
package net.william278.huskhomes;
|
||||
|
||||
import io.papermc.lib.PaperLib;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import net.william278.annotaml.Annotaml;
|
||||
import net.william278.desertwell.Version;
|
||||
@@ -52,15 +51,10 @@ import net.william278.huskhomes.user.BukkitUser;
|
||||
import net.william278.huskhomes.user.ConsoleUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.SavedUser;
|
||||
import net.william278.huskhomes.util.BukkitAdapter;
|
||||
import net.william278.huskhomes.util.BukkitTaskRunner;
|
||||
import net.william278.huskhomes.util.UnsafeBlocks;
|
||||
import net.william278.huskhomes.util.Validator;
|
||||
import net.william278.huskhomes.util.*;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bstats.charts.SimplePie;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
@@ -73,11 +67,10 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTaskRunner, BukkitEventDispatcher, PluginMessageListener {
|
||||
public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTaskRunner, BukkitEventDispatcher, PluginMessageListener, BukkitSafetyResolver {
|
||||
|
||||
/**
|
||||
* Metrics ID for <a href="https://bstats.org/plugin/bukkit/HuskHomes/8430">HuskHomes on Bukkit</a>.
|
||||
@@ -371,40 +364,6 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
this.hooks = hooks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location) {
|
||||
final org.bukkit.Location bukkitLocation = BukkitAdapter.adaptLocation(location).orElse(null);
|
||||
if (bukkitLocation == null || bukkitLocation.getWorld() == null) {
|
||||
return CompletableFuture.completedFuture(Optional.empty());
|
||||
}
|
||||
|
||||
return PaperLib.getChunkAtAsync(bukkitLocation).thenApply(Chunk::getChunkSnapshot).thenApply(snapshot -> {
|
||||
final int chunkX = bukkitLocation.getBlockX() & 0xF;
|
||||
final int chunkZ = bukkitLocation.getBlockZ() & 0xF;
|
||||
|
||||
for (int dX = -1; dX <= 2; dX++) {
|
||||
for (int dZ = -1; dZ <= 2; 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 (!isBlockUnsafe(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();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Version getVersion() {
|
||||
@@ -445,6 +404,12 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
this.unsafeBlocks = unsafeBlocks;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public UnsafeBlocks getUnsafeBlocks() {
|
||||
return unsafeBlocks;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<World> getWorlds() {
|
||||
@@ -454,11 +419,6 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBlockUnsafe(@NotNull String blockId) {
|
||||
return unsafeBlocks.isUnsafe(blockId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerMetrics(int metricsId) {
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package net.william278.huskhomes.util;
|
||||
|
||||
import io.papermc.lib.PaperLib;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Material;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface BukkitSafetyResolver extends SafetyResolver {
|
||||
|
||||
@Override
|
||||
default CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location) {
|
||||
final org.bukkit.Location bukkitLocation = BukkitAdapter.adaptLocation(location).orElse(null);
|
||||
if (bukkitLocation == null || bukkitLocation.getWorld() == null) {
|
||||
return CompletableFuture.completedFuture(Optional.empty());
|
||||
}
|
||||
|
||||
return PaperLib.getChunkAtAsync(bukkitLocation).thenApply(Chunk::getChunkSnapshot).thenApply(snapshot -> {
|
||||
final int chunkX = bukkitLocation.getBlockX() & 0xF;
|
||||
final int chunkZ = bukkitLocation.getBlockZ() & 0xF;
|
||||
|
||||
for (int dX = -1; dX <= 2; dX++) {
|
||||
for (int dZ = -1; dZ <= 2; 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();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,10 +43,7 @@ import net.william278.huskhomes.user.ConsoleUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.SavedUser;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import net.william278.huskhomes.util.TaskRunner;
|
||||
import net.william278.huskhomes.util.ThrowingConsumer;
|
||||
import net.william278.huskhomes.util.UnsafeBlocks;
|
||||
import net.william278.huskhomes.util.Validator;
|
||||
import net.william278.huskhomes.util.*;
|
||||
import org.intellij.lang.annotations.Subst;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -56,7 +53,6 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Stream;
|
||||
@@ -64,7 +60,7 @@ import java.util.stream.Stream;
|
||||
/**
|
||||
* Represents a cross-platform instance of the plugin
|
||||
*/
|
||||
public interface HuskHomes extends TaskRunner, EventDispatcher {
|
||||
public interface HuskHomes extends TaskRunner, EventDispatcher, SafetyResolver {
|
||||
|
||||
int SPIGOT_RESOURCE_ID = 83767;
|
||||
|
||||
@@ -181,6 +177,9 @@ public interface HuskHomes extends TaskRunner, EventDispatcher {
|
||||
|
||||
void setUnsafeBlocks(@NotNull UnsafeBlocks unsafeBlocks);
|
||||
|
||||
@NotNull
|
||||
UnsafeBlocks getUnsafeBlocks();
|
||||
|
||||
/**
|
||||
* The {@link Database} that stores persistent plugin data
|
||||
*
|
||||
@@ -304,15 +303,6 @@ public interface HuskHomes extends TaskRunner, EventDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a safe ground location for the specified {@link Location} if possible
|
||||
*
|
||||
* @param location the {@link Location} to find a safe ground location for
|
||||
* @return a {@link CompletableFuture} that will complete with an optional of the safe ground position, if it is
|
||||
* possible to find one
|
||||
*/
|
||||
CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location);
|
||||
|
||||
/**
|
||||
* Returns a resource read from the plugin resources folder
|
||||
*
|
||||
@@ -475,14 +465,6 @@ public interface HuskHomes extends TaskRunner, EventDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if the block, by provided identifier, is unsafe
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
boolean isBlockUnsafe(@NotNull String blockId);
|
||||
|
||||
/**
|
||||
* Registers the plugin with bStats metrics
|
||||
*
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package net.william278.huskhomes.util;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface SafetyResolver {
|
||||
|
||||
/**
|
||||
* Returns a safe ground location for the specified {@link Location} if possible
|
||||
*
|
||||
* @param location the {@link Location} to find a safe ground location for
|
||||
* @return a {@link CompletableFuture} that will complete with an optional of the safe ground position, if it is
|
||||
* possible to find one
|
||||
*/
|
||||
CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location);
|
||||
|
||||
/**
|
||||
* Returns if the block, by provided identifier, is unsafe
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
default boolean isBlockSafe(@NotNull String blockId) {
|
||||
return !getPlugin().getUnsafeBlocks().isUnsafe(blockId);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
HuskHomes getPlugin();
|
||||
|
||||
}
|
||||
@@ -27,18 +27,12 @@ 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;
|
||||
@@ -66,6 +60,7 @@ import net.william278.huskhomes.user.ConsoleUser;
|
||||
import net.william278.huskhomes.user.FabricUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.SavedUser;
|
||||
import net.william278.huskhomes.util.FabricSafetyResolver;
|
||||
import net.william278.huskhomes.util.FabricTaskRunner;
|
||||
import net.william278.huskhomes.util.UnsafeBlocks;
|
||||
import net.william278.huskhomes.util.Validator;
|
||||
@@ -81,12 +76,11 @@ import java.io.InputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes,
|
||||
FabricTaskRunner, FabricEventDispatcher, ServerPlayNetworking.PlayChannelHandler {
|
||||
FabricTaskRunner, FabricEventDispatcher, FabricSafetyResolver, ServerPlayNetworking.PlayChannelHandler {
|
||||
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger("HuskHomes");
|
||||
private static FabricHuskHomes instance;
|
||||
@@ -281,6 +275,12 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
|
||||
this.unsafeBlocks = unsafeBlocks;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public UnsafeBlocks getUnsafeBlocks() {
|
||||
return unsafeBlocks;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Database getDatabase() {
|
||||
@@ -346,48 +346,6 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
|
||||
this.hooks = hooks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location) {
|
||||
final Identifier worldId = Identifier.tryParse(location.getWorld().getName());
|
||||
final Optional<ServerWorld> 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
|
||||
@Nullable
|
||||
public InputStream getResource(@NotNull String name) {
|
||||
@@ -463,11 +421,6 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
|
||||
return currentlyOnWarmup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBlockUnsafe(@NotNull String blockId) {
|
||||
return unsafeBlocks.isUnsafe(blockId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerMetrics(int metricsId) {
|
||||
// No metrics for Fabric
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package net.william278.huskhomes.util;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.Material;
|
||||
import net.minecraft.registry.Registries;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
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.huskhomes.FabricHuskHomes;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface FabricSafetyResolver extends SafetyResolver {
|
||||
|
||||
@Override
|
||||
default CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location) {
|
||||
final MinecraftServer server = ((FabricHuskHomes) getPlugin()).getMinecraftServer();
|
||||
final Identifier worldId = Identifier.tryParse(location.getWorld().getName());
|
||||
final Optional<ServerWorld> locationWorld = server.getWorldRegistryKeys().stream()
|
||||
.filter(key -> key.getValue().equals(worldId)).findFirst()
|
||||
.map(server::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 && isBlockSafe(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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user