From 059baa174d3713096f96344454a538c7c5d54014 Mon Sep 17 00:00:00 2001 From: William278 Date: Mon, 5 Sep 2022 15:44:43 +0300 Subject: [PATCH] Cleanup SavedPositionManager, remove unused name-based methods, cleaner position updating --- .../huskhomes/position/Position.java | 15 +++ .../position/SavedPositionManager.java | 117 +----------------- 2 files changed, 18 insertions(+), 114 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/position/Position.java b/common/src/main/java/net/william278/huskhomes/position/Position.java index 6fdee487..096489cc 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Position.java +++ b/common/src/main/java/net/william278/huskhomes/position/Position.java @@ -30,6 +30,21 @@ public class Position extends Location { public Position() { } + /** + * Update the position to match that of another position + * + * @param newPosition The position to update to + */ + public void update(@NotNull Position newPosition) { + this.x = newPosition.x; + this.y = newPosition.y; + this.z = newPosition.z; + this.yaw = newPosition.yaw; + this.pitch = newPosition.pitch; + this.world = newPosition.world; + this.server = newPosition.server; + } + /** * Parses the position from a set of values * diff --git a/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java b/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java index b8737146..1fc8557f 100644 --- a/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java +++ b/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java @@ -131,24 +131,6 @@ public class SavedPositionManager { }); } - /** - * Tries to update an existing home with new metadata - * - * @param homeOwner the owner of the home - * @param existingHomeName the current name of the home to update - * @param newHomeMeta the new metadata for the home - * @return a future supplying the {@link SaveResult}; the result of updating the home - */ - public CompletableFuture updateHomeMeta(@NotNull User homeOwner, @NotNull String existingHomeName, - @NotNull PositionMeta newHomeMeta) { - return database.getHome(homeOwner, existingHomeName).thenApply(optionalHome -> { - if (optionalHome.isPresent()) { - return updateHomeMeta(optionalHome.get(), newHomeMeta).join(); - } - return new SaveResult(SaveResult.ResultType.FAILED_DOES_NOT_EXIST, Optional.empty()); - }); - } - /** * Tries to update an existing home with new metadata * @@ -186,24 +168,6 @@ public class SavedPositionManager { } - /** - * Tries to update the position of a {@link User}'s home - * - * @param homeOwner the owner of the home - * @param homeName the name of the home - * @param newPosition the new position for the home - * @return a future supplying the {@link SaveResult}; the result of updating the home - */ - public CompletableFuture updateHomePosition(@NotNull User homeOwner, @NotNull String homeName, - @NotNull Position newPosition) { - return database.getHome(homeOwner, homeName).thenApply(optionalHome -> { - if (optionalHome.isPresent()) { - return updateHomePosition(optionalHome.get(), newPosition).join(); - } - return false; - }); - } - /** * Tries to update the position of a {@link User}'s home * @@ -212,13 +176,7 @@ public class SavedPositionManager { * @return a future supplying the {@link SaveResult}; the result of updating the home */ public CompletableFuture updateHomePosition(@NotNull Home home, @NotNull Position newPosition) { - home.x = newPosition.x; - home.y = newPosition.y; - home.z = newPosition.z; - home.pitch = newPosition.pitch; - home.yaw = newPosition.yaw; - home.world = newPosition.world; - home.server = newPosition.server; + home.update(newPosition); return eventDispatcher.dispatchHomeSaveEvent(home).thenApply(event -> { if (event.isCancelled()) { return false; @@ -230,25 +188,6 @@ public class SavedPositionManager { }); } - /** - * Tries to change the privacy of a {@link User}'s home by given name - * - * @param homeOwner the owner of the home - * @param homeName the name of the home - * @param isHomePublic whether the home should be public or not - * @return a future supplying a boolean; {@code true} if the privacy was changed, otherwise {@code false} - * if the home name was invalid. - */ - public CompletableFuture updateHomePrivacy(@NotNull User homeOwner, @NotNull String homeName, - final boolean isHomePublic) { - return database.getHome(homeOwner, homeName).thenApply(optionalHome -> { - if (optionalHome.isPresent()) { - return updateHomePrivacy(optionalHome.get(), isHomePublic).join(); - } - return false; - }); - } - /** * Tries to change the privacy of a {@link User}'s home by given name * @@ -344,23 +283,6 @@ public class SavedPositionManager { }); } - /** - * Tries to update an existing home with new metadata - * - * @param existingWarpName the current name of the home to update - * @param newWarpMeta the new metadata for the home - * @return a future supplying the {@link SaveResult}; the result of updating the home - */ - public CompletableFuture updateWarpMeta(@NotNull String existingWarpName, - @NotNull PositionMeta newWarpMeta) { - return database.getWarp(existingWarpName).thenApply(optionalWarp -> { - if (optionalWarp.isPresent()) { - return updateWarpMeta(optionalWarp.get(), newWarpMeta).join(); - } - return new SaveResult(SaveResult.ResultType.FAILED_DOES_NOT_EXIST, Optional.empty()); - }); - } - /** * Tries to update an existing warp with new metadata * @@ -397,23 +319,6 @@ public class SavedPositionManager { } - /** - * Tries to update the position of a warp - * - * @param warpName the name of the home - * @param newPosition the new position for the home - * @return a future supplying the {@link SaveResult}; the result of updating the home - */ - public CompletableFuture updateWarpPosition(@NotNull String warpName, - @NotNull Position newPosition) { - return database.getWarp(warpName).thenApply(optionalWarp -> { - if (optionalWarp.isPresent()) { - return updateWarpPosition(optionalWarp.get(), newPosition).join(); - } - return false; - }); - } - /** * Tries to update the position of a warp * @@ -422,13 +327,7 @@ public class SavedPositionManager { * @return a future supplying the {@link SaveResult}; the result of updating the warp */ public CompletableFuture updateWarpPosition(@NotNull Warp warp, @NotNull Position newPosition) { - warp.x = newPosition.x; - warp.y = newPosition.y; - warp.z = newPosition.z; - warp.pitch = newPosition.pitch; - warp.yaw = newPosition.yaw; - warp.world = newPosition.world; - warp.server = newPosition.server; + warp.update(newPosition); return eventDispatcher.dispatchWarpSaveEvent(warp).thenApply(event -> { if (event.isCancelled()) { return false; @@ -487,11 +386,6 @@ public class SavedPositionManager { */ SUCCESS(true), - /** - * The home or warp was not set or updated because it does not exist - */ - FAILED_DOES_NOT_EXIST(false), - /** * The position was not set; one by this name has already been set (for the user in the case of homes) */ @@ -520,12 +414,7 @@ public class SavedPositionManager { /** * The position was not set or updated; the save event was cancelled by a plugin */ - FAILED_EVENT_CANCELLED(false), - - /** - * The position was not set or updated; an exception occurred - */ - FAILED_ERROR(false); + FAILED_EVENT_CANCELLED(false); public final boolean successful;