From c56936d9d58c90e226d1cc8c58584bac62cc788f Mon Sep 17 00:00:00 2001 From: William Date: Mon, 19 Sep 2022 11:14:01 +0100 Subject: [PATCH] Tweak caching for private home list viewing, fix #222 --- .../src/main/java/net/william278/huskhomes/Cache.java | 10 +++++----- .../william278/huskhomes/command/HomeListCommand.java | 4 ++-- .../huskhomes/position/SavedPositionManager.java | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/Cache.java b/common/src/main/java/net/william278/huskhomes/Cache.java index 380ae838..593d8001 100644 --- a/common/src/main/java/net/william278/huskhomes/Cache.java +++ b/common/src/main/java/net/william278/huskhomes/Cache.java @@ -43,9 +43,9 @@ public class Cache { public final List players; /** - * Cached lists of private homes for pagination + * Cached lists of private homes for pagination, mapped to the username of the home owner */ - public final HashMap privateHomeLists; + public final HashMap privateHomeLists; /** * Cached lists of public homes for pagination @@ -140,7 +140,7 @@ public class Cache { } @NotNull - public Optional getHomeList(@NotNull OnlineUser onlineUser, @NotNull User listOwner, @NotNull Locales locales, + public Optional getHomeList(@NotNull OnlineUser onlineUser, @NotNull User homeOwner, @NotNull Locales locales, @NotNull List homes, final int itemsPerPage, final int page) { if (eventDispatcher.dispatchViewHomeListEvent(homes, onlineUser, false).join().isCancelled()) { return Optional.empty(); @@ -152,10 +152,10 @@ public class Cache { Locales.escapeMineDown(locales.formatDescription(home.meta.description))) .orElse(home.meta.name)).sorted().collect(Collectors.toList()), getBaseList(locales, itemsPerPage) .setHeaderFormat(locales.getRawLocale("home_list_page_title", - listOwner.username, "%first_item_on_page_index%", + homeOwner.username, "%first_item_on_page_index%", "%last_item_on_page_index%", "%total_items%").orElse("")) .setCommand("/huskhomes:homelist").build()); - this.privateHomeLists.put(onlineUser.uuid, homeList); + this.privateHomeLists.put(homeOwner.username, homeList); return Optional.of(homeList.getNearestValidPage(page)); } diff --git a/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java b/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java index 5c1589cb..2243fe9c 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java @@ -53,8 +53,8 @@ public class HomeListCommand extends CommandBase implements ConsoleExecutable { * @param pageNumber page number to display */ private void showHomeList(@NotNull OnlineUser onlineUser, @NotNull String homeOwner, int pageNumber) { - if (plugin.getCache().privateHomeLists.containsKey(onlineUser.uuid)) { - onlineUser.sendMessage(plugin.getCache().privateHomeLists.get(onlineUser.uuid).getNearestValidPage(pageNumber)); + if (plugin.getCache().privateHomeLists.containsKey(homeOwner)) { + onlineUser.sendMessage(plugin.getCache().privateHomeLists.get(homeOwner).getNearestValidPage(pageNumber)); return; } plugin.getDatabase().getUserDataByName(homeOwner).thenAccept(optionalUser -> optionalUser.ifPresentOrElse(userData -> { 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 9d9036e8..0a3f46b9 100644 --- a/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java +++ b/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java @@ -88,7 +88,7 @@ public class SavedPositionManager { } cache.homes.putIfAbsent(home.owner.uuid, new ArrayList<>()); cache.homes.get(home.owner.uuid).add(home.meta.name); - cache.privateHomeLists.remove(home.owner.uuid); + cache.privateHomeLists.remove(home.owner.username); return database.saveHome(home).thenApply(value -> new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join(); }).join(); @@ -118,7 +118,7 @@ public class SavedPositionManager { homeNames.remove(home.meta.name); return homeNames; }); - cache.privateHomeLists.remove(home.owner.uuid); + cache.privateHomeLists.remove(home.owner.username); cache.publicHomeLists.clear(); if (mapHook != null) { mapHook.removeHome(home); @@ -155,7 +155,7 @@ public class SavedPositionManager { cache.homes.get(home.owner.uuid).remove(existingHomeName); cache.homes.get(home.owner.uuid).add(newHomeMeta.name); } - cache.privateHomeLists.remove(home.owner.uuid); + cache.privateHomeLists.remove(home.owner.username); cache.publicHomeLists.clear(); if (home.isPublic && mapHook != null) { mapHook.updateHome(home); @@ -217,7 +217,7 @@ public class SavedPositionManager { mapHook.removeHome(home); } } - cache.privateHomeLists.remove(home.owner.uuid); + cache.privateHomeLists.remove(home.owner.username); cache.publicHomeLists.clear(); return true; }).join();