Tweak caching for private home list viewing, fix #222

This commit is contained in:
William committed 2022-09-19 11:14:01 +01:00
1 parent 39931dba04
commit c56936d9d5
3 files changed
+11 -11

No files matched your search

@@ -43,9 +43,9 @@ public class Cache {
public final List<String> 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<UUID, PaginatedList> privateHomeLists;
public final HashMap<String, PaginatedList> privateHomeLists;
/**
* Cached lists of public homes for pagination
@@ -140,7 +140,7 @@ public class Cache {
}
@NotNull
public Optional<MineDown> getHomeList(@NotNull OnlineUser onlineUser, @NotNull User listOwner, @NotNull Locales locales,
public Optional<MineDown> getHomeList(@NotNull OnlineUser onlineUser, @NotNull User homeOwner, @NotNull Locales locales,
@NotNull List<Home> 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));
}
@@ -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 -> {
@@ -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();