Use internal yaml resource for unsafe blocks by key, remove caching, fixes

This commit is contained in:
William committed 2022-11-23 21:51:23 +00:00
1 parent c4b16db9a9
commit a3ea62a85c
11 files changed
+215 -269

No files matched your search

@@ -38,6 +38,8 @@ import net.william278.huskhomes.util.*;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Material;
import org.bukkit.command.PluginCommand;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.Plugin;
@@ -49,6 +51,7 @@ import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
@@ -73,10 +76,10 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
private EventListener eventListener;
private RandomTeleportEngine randomTeleportEngine;
private CachedSpawn serverSpawn;
private UnsafeBlocks unsafeBlocks;
private EventDispatcher eventDispatcher;
private Set<PluginHook> pluginHooks;
private List<CommandBase> registeredCommands;
private List<Migrator> migrators;
@Nullable
@@ -117,7 +120,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
// Load settings and locales
getLoggingAdapter().log(Level.INFO, "Loading plugin configuration settings & locales...");
initialized.set(reload().join());
initialized.set(reload());
if (initialized.get()) {
logger.showDebugLogs(settings.debugLogging);
if (upgradeData != null) {
@@ -139,8 +142,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
if (initialized.get()) {
getLoggingAdapter().log(Level.INFO, "Successfully established a connection to the database");
} else {
throw new HuskHomesInitializationException("Failed to establish a connection to the database. " +
"Please check the supplied database credentials in the config file");
throw new HuskHomesInitializationException("Failed to establish a connection to the database. " + "Please check the supplied database credentials in the config file");
}
// Initialize the network messenger if proxy mode is enabled
@@ -165,8 +167,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
cache.initialize(database);
// Prepare the home and warp position manager
this.savedPositionManager = new SavedPositionManager(database, cache, eventDispatcher,
settings.allowUnicodeNames, settings.allowUnicodeDescriptions, settings.overwriteExistingHomesWarps);
this.savedPositionManager = new SavedPositionManager(database, cache, eventDispatcher, settings.allowUnicodeNames, settings.allowUnicodeDescriptions, settings.overwriteExistingHomesWarps);
// Initialize the RTP engine with the default normal distribution engine
setRandomTeleportEngine(new NormalDistributionEngine(this));
@@ -202,9 +203,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
if (pluginHooks.size() > 0) {
pluginHooks.forEach(PluginHook::initialize);
getLoggingAdapter().log(Level.INFO, "Registered " + pluginHooks.size() + " plugin hooks: " +
pluginHooks.stream().map(PluginHook::getHookName)
.collect(Collectors.joining(", ")));
getLoggingAdapter().log(Level.INFO, "Registered " + pluginHooks.size() + " plugin hooks: " + pluginHooks.stream().map(PluginHook::getHookName).collect(Collectors.joining(", ")));
}
// Register events
@@ -214,13 +213,11 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
// Register permissions
getLoggingAdapter().log(Level.INFO, "Registering permissions & commands...");
Arrays.stream(Permission.values()).forEach(permission -> getServer().getPluginManager().addPermission(
new org.bukkit.permissions.Permission(permission.node,
switch (permission.defaultAccess) {
case EVERYONE -> PermissionDefault.TRUE;
case NOBODY -> PermissionDefault.FALSE;
case OPERATORS -> PermissionDefault.OP;
})));
Arrays.stream(Permission.values()).forEach(permission -> getServer().getPluginManager().addPermission(new org.bukkit.permissions.Permission(permission.node, switch (permission.defaultAccess) {
case EVERYONE -> PermissionDefault.TRUE;
case NOBODY -> PermissionDefault.FALSE;
case OPERATORS -> PermissionDefault.OP;
})));
// Register commands
this.registeredCommands = new ArrayList<>();
@@ -233,9 +230,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
// If the command is disabled, use the disabled CommandBase
if (settings.disabledCommands.stream().anyMatch(disabledCommand -> {
final String command = (disabledCommand.startsWith("/") ? disabledCommand.substring(1) : disabledCommand);
return command.equalsIgnoreCase(commandType.commandBase.command) ||
Arrays.stream(commandType.commandBase.aliases)
.anyMatch(alias -> alias.equalsIgnoreCase(command));
return command.equalsIgnoreCase(commandType.commandBase.command) || Arrays.stream(commandType.commandBase.aliases).anyMatch(alias -> alias.equalsIgnoreCase(command));
})) {
new BukkitCommand(new DisabledCommand(this), this).register(pluginCommand);
return;
@@ -259,10 +254,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
// Check for updates
if (settings.checkForUpdates) {
getLoggingAdapter().log(Level.INFO, "Checking for updates...");
getLatestVersionIfOutdated().thenAccept(newestVersion ->
newestVersion.ifPresent(newVersion -> getLoggingAdapter().log(Level.WARNING,
"An update is available for HuskHomes, v" + newVersion
+ " (Currently running v" + getPluginVersion() + ")")));
getLatestVersionIfOutdated().thenAccept(newestVersion -> newestVersion.ifPresent(newVersion -> getLoggingAdapter().log(Level.WARNING, "An update is available for HuskHomes, v" + newVersion + " (Currently running v" + getPluginVersion() + ")")));
}
// Perform automatic upgrade if detected
@@ -328,8 +320,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
@NotNull
@Override
public List<OnlineUser> getOnlinePlayers() {
return Bukkit.getOnlinePlayers().stream().map(
player -> (OnlineUser) BukkitPlayer.adapt(player)).toList();
return Bukkit.getOnlinePlayers().stream().map(player -> (OnlineUser) BukkitPlayer.adapt(player)).toList();
}
@NotNull
@@ -345,7 +336,8 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
}
@Override
public @NotNull Database getDatabase() {
@NotNull
public Database getDatabase() {
return database;
}
@@ -356,7 +348,8 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
}
@Override
public @NotNull RequestManager getRequestManager() {
@NotNull
public RequestManager getRequestManager() {
return requestManager;
}
@@ -387,7 +380,8 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
}
@Override
public @NotNull EventDispatcher getEventDispatcher() {
@NotNull
public EventDispatcher getEventDispatcher() {
return eventDispatcher;
}
@@ -419,41 +413,55 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
}
@Override
public @NotNull Set<PluginHook> getPluginHooks() {
@NotNull
public Set<PluginHook> getPluginHooks() {
return pluginHooks;
}
@Override
public CompletableFuture<Optional<Location>> resolveSafeGroundLocation(@NotNull Location location) {
final CompletableFuture<Optional<Location>> future = new CompletableFuture<>();
Bukkit.getScheduler().runTask(getInstance(), () -> BukkitAdapter.adaptLocation(location).ifPresentOrElse(
bukkitLocation -> {
// Validate the location is within the world's border
assert bukkitLocation.getWorld() != null;
if (!bukkitLocation.getWorld().getWorldBorder().isInside(bukkitLocation)) {
future.complete(Optional.empty());
return;
}
final org.bukkit.Location bukkitLocation = BukkitAdapter.adaptLocation(location).orElse(null);
if (bukkitLocation == null || bukkitLocation.getWorld() == null) {
return CompletableFuture.completedFuture(Optional.empty());
}
// Find a safe position and return
PaperLib.getChunkAtAsync(bukkitLocation).thenAcceptAsync(chunk ->
future.complete(BukkitSafetyUtil.findSafeLocation(location.world, bukkitLocation,
chunk.getChunkSnapshot()))).exceptionally(throwable -> {
throwable.printStackTrace();
return null;
});
},
() -> future.complete(Optional.empty())));
return future;
final CompletableFuture<Optional<Location>> locationFuture = new CompletableFuture<>();
Bukkit.getScheduler().runTask(this, () -> PaperLib.getChunkAtAsync(bukkitLocation).thenApply(chunk -> {
final ChunkSnapshot snapshot = chunk.getChunkSnapshot();
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())) {
Bukkit.broadcastMessage("✔ Safe block: " + blockType.getKey());
return new Location((location.x + dX) + 0.5d, y + 1.25d, (location.z + dZ) + 0.5d, location.world);
} else {
Bukkit.broadcastMessage("❌ Unsafe block: " + blockType.getKey());
}
}
}
return null;
}).thenAccept(resolved -> locationFuture.complete(Optional.ofNullable(resolved))));
return locationFuture;
}
@Override
public @NotNull Version getPluginVersion() {
@NotNull
public Version getPluginVersion() {
return Version.fromString(getDescription().getVersion(), "-");
}
@Override
public @NotNull List<CommandBase> getCommands() {
@NotNull
public List<CommandBase> getCommands() {
return registeredCommands;
}
@@ -471,72 +479,71 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
if (!getSettings().crossServer || this.server != null) {
return CompletableFuture.completedFuture(null);
}
return getNetworkMessenger()
.fetchServerName(requester)
.orTimeout(5, TimeUnit.SECONDS)
.exceptionally(throwable -> null)
.thenAccept(serverName -> {
if (serverName == null) {
throw new HuskHomesException("GetServer plugin message call operation timed out");
}
try {
this.server = new Server(serverName);
Annotaml.create(new File(getDataFolder(), "server.yml"), new CachedServer(serverName));
getLoggingAdapter().log(Level.INFO, "Successfully cached server name to disk (" + serverName + ")");
} catch (IOException e) {
throw new HuskHomesException("Failed to write cached server name to disk", e);
}
})
.exceptionally(throwable -> {
getLoggingAdapter().log(Level.SEVERE, "Failed to fetch and cache server name", throwable);
return null;
});
}
@Override
public @NotNull List<World> getWorlds() {
return getServer().getWorlds().stream().filter(world -> BukkitAdapter.adaptWorld(world).isPresent())
.map(world -> BukkitAdapter.adaptWorld(world).orElse(null)).collect(Collectors.toList());
}
@Override
public CompletableFuture<Boolean> reload() {
return CompletableFuture.supplyAsync(() -> {
return getNetworkMessenger().fetchServerName(requester).orTimeout(5, TimeUnit.SECONDS).exceptionally(throwable -> null).thenAccept(serverName -> {
if (serverName == null) {
throw new HuskHomesException("GetServer plugin message call operation timed out");
}
try {
// Load settings
this.settings = Annotaml.create(new File(getDataFolder(), "config.yml"), new Settings()).get();
// Load locales from language preset default
final Locales languagePresets = Annotaml.create(Locales.class,
Objects.requireNonNull(getResource("locales/" + settings.language + ".yml"))).get();
this.locales = Annotaml.create(new File(getDataFolder(), "messages_" + settings.language + ".yml"),
languagePresets).get();
// Load cached server from file
if (settings.crossServer) {
final File serverFile = new File(getDataFolder(), "server.yml");
if (serverFile.exists()) {
this.server = Annotaml.create(serverFile, CachedServer.class).get().getServer();
}
} else {
this.server = new Server("server");
}
// Load spawn location from file
final File spawnFile = new File(getDataFolder(), "spawn.yml");
if (spawnFile.exists()) {
this.serverSpawn = Annotaml.create(spawnFile, CachedSpawn.class).get();
}
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new HuskHomesException("Failed to load config data", e);
this.server = new Server(serverName);
Annotaml.create(new File(getDataFolder(), "server.yml"), new CachedServer(serverName));
getLoggingAdapter().log(Level.INFO, "Successfully cached server name to disk (" + serverName + ")");
} catch (IOException e) {
throw new HuskHomesException("Failed to write cached server name to disk", e);
}
return true;
}).exceptionally(throwable -> {
getLoggingAdapter().log(Level.SEVERE, "Failed to load data from the config", throwable);
return false;
getLoggingAdapter().log(Level.SEVERE, "Failed to fetch and cache server name", throwable);
return null;
});
}
@Override
@NotNull
public List<World> getWorlds() {
return getServer().getWorlds().stream().filter(world -> BukkitAdapter.adaptWorld(world).isPresent()).map(world -> BukkitAdapter.adaptWorld(world).orElse(null)).collect(Collectors.toList());
}
@Override
public boolean reload() {
try {
// Load settings
this.settings = Annotaml.create(new File(getDataFolder(), "config.yml"), new Settings()).get();
// Load locales from language preset default
final Locales languagePresets = Annotaml.create(Locales.class, Objects.requireNonNull(getResource("locales/" + settings.language + ".yml"))).get();
this.locales = Annotaml.create(new File(getDataFolder(), "messages_" + settings.language + ".yml"), languagePresets).get();
// Load cached server from file
if (settings.crossServer) {
final File serverFile = new File(getDataFolder(), "server.yml");
if (serverFile.exists()) {
this.server = Annotaml.create(serverFile, CachedServer.class).get().getServer();
}
} else {
this.server = new Server("server");
}
// Load spawn location from file
final File spawnFile = new File(getDataFolder(), "spawn.yml");
if (spawnFile.exists()) {
this.serverSpawn = Annotaml.create(spawnFile, CachedSpawn.class).get();
}
// Load unsafe blocks from resources
final InputStream blocksResource = getResource("safety/unsafe_blocks.yml");
this.unsafeBlocks = Annotaml.create(new UnsafeBlocks(), Objects.requireNonNull(blocksResource)).get();
return true;
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
getLoggingAdapter().log(Level.SEVERE, "Failed to reload HuskHomes config or messages file", e);
}
return false;
}
@Override
public boolean isBlockUnsafe(@NotNull String blockId) {
return unsafeBlocks.isUnsafe(blockId);
}
@Override
public void registerMetrics(int metricsId) {
try {
@@ -565,8 +572,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
// Super constructor for unit testing
@SuppressWarnings("unused")
protected BukkitHuskHomes(@NotNull JavaPluginLoader loader, @NotNull PluginDescriptionFile description,
@NotNull File dataFolder, @NotNull File file) {
protected BukkitHuskHomes(@NotNull JavaPluginLoader loader, @NotNull PluginDescriptionFile description, @NotNull File dataFolder, @NotNull File file) {
super(loader, description, dataFolder, file);
}
@@ -1,78 +0,0 @@
package net.william278.huskhomes.util;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.World;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Material;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
public class BukkitSafetyUtil {
/**
* Finds a safe location to teleport to about a point.
*
* @param world The world to search in
* @param bukkitLocation The location to search around
* @param chunkSnapshot The chunk snapshot to search in
* @return The safe location, if found
*/
public static Optional<Location> findSafeLocation(@NotNull World world, @NotNull org.bukkit.Location bukkitLocation,
@NotNull ChunkSnapshot chunkSnapshot) {
// Validate world
final org.bukkit.World bukkitWorld = bukkitLocation.getWorld();
if (bukkitWorld == null) {
return Optional.empty();
}
// Search nearby blocks for a safe, valid position
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int chunkX = Math.abs(bukkitLocation.getBlockX() % 16) + i;
int chunkZ = Math.abs(bukkitLocation.getBlockZ() % 16) + j;
if (chunkX < 0 || chunkX > 15 || chunkZ < 0 || chunkZ > 15) {
continue;
}
int chunkY = chunkSnapshot.getHighestBlockYAt(chunkX, chunkZ);
if (isSafePosition(chunkSnapshot, chunkX, chunkY, chunkZ, bukkitWorld)) {
return Optional.of(new Location(bukkitLocation.getBlockX() + i,
chunkY + 1.5d, bukkitLocation.getBlockZ() + j,
90, 0, world));
}
}
}
return Optional.empty();
}
/**
* Searches a chunk and returns if a block is safe to teleport to.
*
* @param chunkSnapshot The chunk snapshot to search in
* @param chunkX The chunk x coordinate to search at
* @param chunkY The chunk y coordinate to search at
* @param chunkZ The chunk z coordinate to search at
* @param world The world to search in
* @return True if the block is safe, false otherwise
*/
public static boolean isSafePosition(@NotNull ChunkSnapshot chunkSnapshot,
final int chunkX, final int chunkY, final int chunkZ,
@NotNull org.bukkit.World world) {
// Check validity of the y-coordinate
if (chunkY < world.getMinHeight() || chunkY > world.getMaxHeight()) {
return false;
}
// Check block safety
final Material blockType = chunkSnapshot.getBlockType(chunkX, chunkY, chunkZ);
return switch (blockType) {
// Special case handling for safe, un-solid blocks
case TALL_GRASS, TALL_SEAGRASS, FERN, LARGE_FERN, DANDELION, POPPY, BLUE_ORCHID, ALLIUM, AZURE_BLUET,
RED_TULIP, ORANGE_TULIP, WHITE_TULIP, PINK_TULIP, OXEYE_DAISY, CORNFLOWER, LILY_OF_THE_VALLEY,
ROSE_BUSH, WHEAT_SEEDS, BEETROOT_SEEDS, MELON_SEEDS, PUMPKIN_SEEDS, GRASS_PATH, FARMLAND,
OAK_LEAVES, BIRCH_LEAVES, DEAD_BUSH, SUGAR_CANE, SNOW, ICE, BLUE_ICE, FROSTED_ICE -> true;
default -> blockType.isSolid() || blockType.isOccluding();
};
}
}