From a9e643d199ef0b4d5c71605b3c5d5b58032cd550 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 15 Sep 2022 16:18:10 +0100 Subject: [PATCH] Create `#getLocalPublicHomes` and `#getLocalWarps`, use server for checks with world UID fallback, fix #215 --- .../huskhomes/database/Database.java | 43 +++++++++++++++++++ .../william278/huskhomes/hook/MapHook.java | 11 ++--- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/database/Database.java b/common/src/main/java/net/william278/huskhomes/database/Database.java index 03dd5d57..1561e80d 100644 --- a/common/src/main/java/net/william278/huskhomes/database/Database.java +++ b/common/src/main/java/net/william278/huskhomes/database/Database.java @@ -1,5 +1,7 @@ package net.william278.huskhomes.database; +import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.HuskHomesException; import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.player.User; import net.william278.huskhomes.player.UserData; @@ -20,6 +22,7 @@ import java.util.Map; import java.util.Optional; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; /** * An abstract representation of the plugin database, storing home, warp and player data. @@ -258,6 +261,26 @@ public abstract class Database { */ public abstract CompletableFuture> getWarps(); + /** + * Get a list of publicly-set {@link Warp}s on this {@link Server} + * + * @param plugin The plugin instance + * @return A future returning a list containing all {@link Warp}s set on this server + * @implNote If the {@link Server} has not been initialized, this method will check against local world UUIDs + */ + public final CompletableFuture> getLocalWarps(@NotNull HuskHomes plugin) { + try { + final Server server = plugin.getPluginServer(); + return getWarps().thenApplyAsync(warps -> warps.stream() + .filter(warp -> warp.server.equals(server)) + .collect(Collectors.toList())); + } catch (HuskHomesException e) { + return getWarps().thenApplyAsync(warps -> warps.stream() + .filter(warp -> plugin.getWorlds().stream().anyMatch(world -> world.uuid.equals(warp.world.uuid))) + .collect(Collectors.toList())); + } + } + /** * Get a list of all publicly-set {@link Home}s * @@ -265,6 +288,26 @@ public abstract class Database { */ public abstract CompletableFuture> getPublicHomes(); + /** + * Get a list of publicly-set {@link Home}s on this {@link Server} + * + * @param plugin The plugin instance + * @return A future returning a list containing all publicly-set {@link Home}s on this server + * @implNote If the {@link Server} has not been initialized, this method will check against local world UUIDs + */ + public final CompletableFuture> getLocalPublicHomes(@NotNull HuskHomes plugin) { + try { + final Server server = plugin.getPluginServer(); + return getPublicHomes().thenApplyAsync(homes -> homes.stream() + .filter(home -> home.server.equals(server)) + .collect(Collectors.toList())); + } catch (HuskHomesException e) { + return plugin.getDatabase().getPublicHomes().thenApplyAsync(homes -> homes.stream() + .filter(home -> plugin.getWorlds().stream().anyMatch(world -> world.uuid.equals(home.world.uuid))) + .collect(Collectors.toList())); + } + } + /** * Get a {@link Home} set by a {@link User} * diff --git a/common/src/main/java/net/william278/huskhomes/hook/MapHook.java b/common/src/main/java/net/william278/huskhomes/hook/MapHook.java index 528d36f3..cf08b301 100644 --- a/common/src/main/java/net/william278/huskhomes/hook/MapHook.java +++ b/common/src/main/java/net/william278/huskhomes/hook/MapHook.java @@ -25,15 +25,12 @@ public abstract class MapHook extends PluginHook { public final boolean initialize() { initializeMap().thenRun(() -> { if (plugin.getSettings().publicHomesOnMap) { - plugin.getDatabase().getPublicHomes().thenAccept(homes -> homes.stream() - .filter(home -> plugin.getWorlds().stream().anyMatch(world -> world.uuid.equals(home.world.uuid))) - .forEach(this::updateHome)); + plugin.getDatabase().getLocalPublicHomes(plugin) + .thenAcceptAsync(homes -> homes.forEach(this::updateHome)); } if (plugin.getSettings().warpsOnMap) { - plugin.getDatabase().getWarps().thenAccept(warps -> warps - .stream() - .filter(warp -> plugin.getWorlds().stream().anyMatch(world -> world.uuid.equals(warp.world.uuid))) - .forEach(this::updateWarp)); + plugin.getDatabase().getLocalWarps(plugin) + .thenAcceptAsync(warps -> warps.forEach(this::updateWarp)); } }); return true;