mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 22:16:08 +00:00
New lists via PagineDown, update edithome locales
This commit is contained in:
1 parent
3651e1ea90
commit
423f18b2c3
20 files changed
+170
-413
No files matched your search
@@ -1,16 +1,20 @@
|
||||
package net.william278.huskhomes.cache;
|
||||
|
||||
import de.themoep.minedown.MineDown;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.config.Locales;
|
||||
import net.william278.huskhomes.database.Database;
|
||||
import net.william278.huskhomes.list.ChatList;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.player.User;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import net.william278.paginedown.ListOptions;
|
||||
import net.william278.paginedown.PaginatedList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* A cache used to hold frequently accessed data (i.e. TAB-completed homes and warps)
|
||||
@@ -38,9 +42,19 @@ public class Cache {
|
||||
public final List<String> players;
|
||||
|
||||
/**
|
||||
* Cached position lists for optimized list navigating
|
||||
* Cached lists of private homes for pagination
|
||||
*/
|
||||
public final HashMap<UUID, ChatList> positionLists;
|
||||
public final HashMap<UUID, PaginatedList> privateHomeLists;
|
||||
|
||||
/**
|
||||
* Cached lists of public homes for pagination
|
||||
*/
|
||||
public final HashMap<UUID, PaginatedList> publicHomeLists;
|
||||
|
||||
/**
|
||||
* Cached lists of warps for pagination
|
||||
*/
|
||||
public final HashMap<UUID, PaginatedList> warpLists;
|
||||
|
||||
/**
|
||||
* Create a new cache
|
||||
@@ -50,7 +64,9 @@ public class Cache {
|
||||
this.publicHomes = new HashMap<>();
|
||||
this.warps = new ArrayList<>();
|
||||
this.players = new ArrayList<>();
|
||||
this.positionLists = new HashMap<>();
|
||||
this.privateHomeLists = new HashMap<>();
|
||||
this.publicHomeLists = new HashMap<>();
|
||||
this.warpLists = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +90,7 @@ public class Cache {
|
||||
*
|
||||
* @param plugin the implementing plugin
|
||||
*/
|
||||
public void updatePlayerList(@NotNull HuskHomes plugin, @NotNull OnlineUser requester) {
|
||||
public void updateOnlinePlayerList(@NotNull HuskHomes plugin, @NotNull OnlineUser requester) {
|
||||
if (plugin.getSettings().crossServer) {
|
||||
assert plugin.getNetworkMessenger() != null;
|
||||
CompletableFuture.runAsync(() -> plugin.getNetworkMessenger().
|
||||
@@ -88,4 +104,68 @@ public class Cache {
|
||||
}
|
||||
}
|
||||
|
||||
private ListOptions.Builder getBaseList(@NotNull Locales locales, int itemsPerPage) {
|
||||
return new ListOptions.Builder().setFooterFormat(locales.getRawLocale("list_footer",
|
||||
"%previous_page_button%", "%current_page%",
|
||||
"%total_pages%", "%next_page_button%", "%page_jumpers%").orElse(""))
|
||||
.setNextButtonFormat(locales.getRawLocale("list_next_page_button",
|
||||
"%next_page_index%", "%command%").orElse(""))
|
||||
.setPreviousButtonFormat(locales.getRawLocale("list_previous_page_button",
|
||||
"%previous_page_index%", "%command%").orElse(""))
|
||||
.setPageJumpersFormat(locales.getRawLocale("list_page_jumpers",
|
||||
"%page_jump_buttons%").orElse(""))
|
||||
.setPageJumperPageFormat(locales.getRawLocale("list_page_jumper_button",
|
||||
"%target_page_index%", "%command%").orElse(""))
|
||||
.setPageJumperCurrentPageFormat(locales.getRawLocale("list_page_jumper_current_page",
|
||||
"%current_page%").orElse(""))
|
||||
.setPageJumperPageSeparator(locales.getRawLocale("list_page_jumper_separator").orElse(""))
|
||||
.setPageJumperGroupSeparator(locales.getRawLocale("list_page_jumper_group_separator").orElse(""))
|
||||
.setItemSeparator(locales.getRawLocale("list_item_divider").orElse(" "))
|
||||
.setItemsPerPage(itemsPerPage)
|
||||
.setEscapeItemsMineDown(false);
|
||||
}
|
||||
|
||||
public MineDown getHomeList(@NotNull OnlineUser onlineUser, @NotNull User listOwner, @NotNull Locales locales,
|
||||
@NotNull List<Home> homes, final int itemsPerPage, final int page) {
|
||||
final PaginatedList homeList = PaginatedList.of(homes.stream().map(home ->
|
||||
locales.getRawLocale("home_list_item",
|
||||
home.meta.name, home.owner.username + "." + home.meta.name)
|
||||
.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%",
|
||||
"%last_item_on_page_index%", "%total_items%").orElse(""))
|
||||
.setCommand("/huskhomes:homelist").build());
|
||||
this.privateHomeLists.put(onlineUser.uuid, homeList);
|
||||
return homeList.getNearestValidPage(page);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public MineDown getPublicHomeList(@NotNull OnlineUser onlineUser, @NotNull Locales locales,
|
||||
@NotNull List<Home> publicHomes, final int itemsPerPage, final int page) {
|
||||
final PaginatedList publicHomeList = PaginatedList.of(publicHomes.stream().map(home ->
|
||||
locales.getRawLocale("public_home_list_item",
|
||||
home.meta.name, home.owner.username + "." + home.meta.name)
|
||||
.orElse(home.meta.name)).sorted().collect(Collectors.toList()), getBaseList(locales, itemsPerPage)
|
||||
.setHeaderFormat(locales.getRawLocale("public_home_list_page_title",
|
||||
"%first_item_on_page_index%", "%last_item_on_page_index%",
|
||||
"%total_items%").orElse(""))
|
||||
.setCommand("/huskhomes:publichomelist").build());
|
||||
publicHomeLists.put(onlineUser.uuid, publicHomeList);
|
||||
return publicHomeList.getNearestValidPage(page);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public MineDown getWarpList(@NotNull OnlineUser onlineUser, @NotNull Locales locales,
|
||||
@NotNull List<Warp> warps, final int itemsPerPage, final int page) {
|
||||
final PaginatedList warpList = PaginatedList.of(warps.stream().map(warp ->
|
||||
locales.getRawLocale("warp_list_item", warp.meta.name)
|
||||
.orElse(warp.meta.name)).sorted().collect(Collectors.toList()), getBaseList(locales, itemsPerPage)
|
||||
.setHeaderFormat(locales.getRawLocale("warp_list_page_title",
|
||||
"%first_item_on_page_index%", "%last_item_on_page_index%",
|
||||
"%total_items%").orElse(""))
|
||||
.setCommand("/huskhomes:warplist").build());
|
||||
warpLists.put(onlineUser.uuid, warpList);
|
||||
return warpList.getNearestValidPage(page);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -270,7 +270,9 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons
|
||||
.ifPresent(this::add);
|
||||
|
||||
if (home.meta.description.length() > 0) {
|
||||
final String escapedDescription = MineDown.escape(home.meta.description);
|
||||
final String escapedDescription = MineDown.escape(home.meta.description
|
||||
.replaceAll(Pattern.quote("("), "\\(")
|
||||
.replaceAll(Pattern.quote(")"), "\\)"));
|
||||
plugin.getLocales().getLocale("edit_home_menu_description",
|
||||
escapedDescription.length() > 50
|
||||
? escapedDescription.substring(0, 49).trim() + "…" : escapedDescription,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.list.PrivateHomeList;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import net.william278.huskhomes.util.RegexUtil;
|
||||
@@ -21,20 +20,12 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE
|
||||
switch (args.length) {
|
||||
case 0 -> plugin.getDatabase().getHomes(onlineUser).thenAccept(homes -> {
|
||||
// Send the home list if they have homes set. If they have just one home set, teleport the player
|
||||
try {
|
||||
switch (homes.size()) {
|
||||
case 0 ->
|
||||
plugin.getLocales().getLocale("error_no_homes_set").ifPresent(onlineUser::sendMessage);
|
||||
case 1 -> plugin.getTeleportManager().teleport(onlineUser, homes.get(0)).thenAccept(result ->
|
||||
plugin.getTeleportManager().finishTeleport(onlineUser, result)).join();
|
||||
default -> {
|
||||
final PrivateHomeList homeList = new PrivateHomeList(homes, onlineUser, plugin);
|
||||
plugin.getCache().positionLists.put(onlineUser.uuid, homeList);
|
||||
homeList.getDisplay(1).forEach(onlineUser::sendMessage);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
switch (homes.size()) {
|
||||
case 0 -> plugin.getLocales().getLocale("error_no_homes_set").ifPresent(onlineUser::sendMessage);
|
||||
case 1 -> plugin.getTeleportManager().teleport(onlineUser, homes.get(0)).thenAccept(result ->
|
||||
plugin.getTeleportManager().finishTeleport(onlineUser, result)).join();
|
||||
default -> onlineUser.sendMessage(plugin.getCache().getHomeList(onlineUser, onlineUser,
|
||||
plugin.getLocales(), homes, plugin.getSettings().listItemsPerPage,1));
|
||||
}
|
||||
});
|
||||
case 1 -> {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.list.PrivateHomeList;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -41,16 +40,14 @@ public class HomeListCommand extends CommandBase implements ConsoleExecutable {
|
||||
/**
|
||||
* Show a (cached) list of a {@link OnlineUser}'s homes
|
||||
*
|
||||
* @param onlineUser the user to display the homes to
|
||||
* @param onlineUser the user to display the homes to
|
||||
* @param homeOwner the user whose homes should be displayed
|
||||
* @param pageNumber page number to display
|
||||
*/
|
||||
private void showHomeList(@NotNull OnlineUser onlineUser, @NotNull String homeOwner, int pageNumber) {
|
||||
if (plugin.getCache().positionLists.containsKey(onlineUser.uuid)) {
|
||||
if (plugin.getCache().positionLists.get(onlineUser.uuid) instanceof PrivateHomeList privateHomeList) {
|
||||
privateHomeList.getDisplay(pageNumber).forEach(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
if (plugin.getCache().privateHomeLists.containsKey(onlineUser.uuid)) {
|
||||
onlineUser.sendMessage(plugin.getCache().privateHomeLists.get(onlineUser.uuid).getNearestValidPage(pageNumber));
|
||||
return;
|
||||
}
|
||||
plugin.getDatabase().getUserDataByName(homeOwner).thenAccept(optionalUser -> optionalUser.ifPresentOrElse(userData -> {
|
||||
if (!userData.getUserUuid().equals(onlineUser.uuid)) {
|
||||
@@ -64,9 +61,8 @@ public class HomeListCommand extends CommandBase implements ConsoleExecutable {
|
||||
plugin.getLocales().getLocale("error_no_homes_set").ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
final PrivateHomeList homeList = new PrivateHomeList(homes, userData.user(), plugin);
|
||||
plugin.getCache().positionLists.put(onlineUser.uuid, homeList);
|
||||
homeList.getDisplay(pageNumber).forEach(onlineUser::sendMessage);
|
||||
onlineUser.sendMessage(plugin.getCache().getHomeList(onlineUser, userData.user(),
|
||||
plugin.getLocales(), homes, plugin.getSettings().listItemsPerPage, pageNumber));
|
||||
});
|
||||
}, () -> plugin.getLocales().getLocale("error_invalid_player").ifPresent(onlineUser::sendMessage)));
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.list.PublicHomeList;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
@@ -29,9 +28,8 @@ public class PublicHomeCommand extends CommandBase implements TabCompletable, Co
|
||||
return;
|
||||
}
|
||||
|
||||
final PublicHomeList homeList = new PublicHomeList(publicHomes, plugin);
|
||||
plugin.getCache().positionLists.put(onlineUser.uuid, homeList);
|
||||
homeList.getDisplay(1).forEach(onlineUser::sendMessage);
|
||||
onlineUser.sendMessage(plugin.getCache().getPublicHomeList(onlineUser, plugin.getLocales(), publicHomes,
|
||||
plugin.getSettings().listItemsPerPage,1));
|
||||
});
|
||||
case 1 -> {
|
||||
final String homeName = args[0];
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.list.PublicHomeList;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -37,21 +36,18 @@ public class PublicHomeListCommand extends CommandBase implements ConsoleExecuta
|
||||
* @param pageNumber page number to display
|
||||
*/
|
||||
private void showPublicHomeList(@NotNull OnlineUser onlineUser, int pageNumber) {
|
||||
if (plugin.getCache().positionLists.containsKey(onlineUser.uuid)) {
|
||||
if (plugin.getCache().positionLists.get(onlineUser.uuid) instanceof PublicHomeList publicHomeList) {
|
||||
publicHomeList.getDisplay(pageNumber).forEach(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
if (plugin.getCache().publicHomeLists.containsKey(onlineUser.uuid)) {
|
||||
onlineUser.sendMessage(plugin.getCache().publicHomeLists.get(onlineUser.uuid).getNearestValidPage(pageNumber));
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getDatabase().getPublicHomes().thenAccept(warps -> {
|
||||
if (warps.isEmpty()) {
|
||||
plugin.getDatabase().getPublicHomes().thenAccept(publicHomes -> {
|
||||
if (publicHomes.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_no_public_homes_set").ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
final PublicHomeList warpList = new PublicHomeList(warps, plugin);
|
||||
plugin.getCache().positionLists.put(onlineUser.uuid, warpList);
|
||||
warpList.getDisplay(pageNumber).forEach(onlineUser::sendMessage);
|
||||
onlineUser.sendMessage(plugin.getCache().getPublicHomeList(onlineUser, plugin.getLocales(), publicHomes,
|
||||
plugin.getSettings().listItemsPerPage, pageNumber));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.list.WarpList;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -23,9 +22,8 @@ public class WarpCommand extends CommandBase implements TabCompletable, ConsoleE
|
||||
plugin.getLocales().getLocale("error_no_warps_set").ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
final WarpList warpList = new WarpList(warps, plugin);
|
||||
plugin.getCache().positionLists.put(onlineUser.uuid, warpList);
|
||||
warpList.getDisplay(1).forEach(onlineUser::sendMessage);
|
||||
onlineUser.sendMessage(plugin.getCache().getWarpList(onlineUser, plugin.getLocales(), warps,
|
||||
plugin.getSettings().listItemsPerPage, 1));
|
||||
});
|
||||
case 1 -> {
|
||||
final String warpName = args[0];
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.list.WarpList;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -37,11 +36,9 @@ public class WarpListCommand extends CommandBase implements ConsoleExecutable {
|
||||
* @param pageNumber page number to display
|
||||
*/
|
||||
private void showWarpList(@NotNull OnlineUser onlineUser, int pageNumber) {
|
||||
if (plugin.getCache().positionLists.containsKey(onlineUser.uuid)) {
|
||||
if (plugin.getCache().positionLists.get(onlineUser.uuid) instanceof WarpList warpList) {
|
||||
warpList.getDisplay(pageNumber).forEach(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
if (plugin.getCache().warpLists.containsKey(onlineUser.uuid)) {
|
||||
onlineUser.sendMessage(plugin.getCache().warpLists.get(onlineUser.uuid).getNearestValidPage(pageNumber));
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getDatabase().getWarps().thenAccept(warps -> {
|
||||
@@ -49,9 +46,8 @@ public class WarpListCommand extends CommandBase implements ConsoleExecutable {
|
||||
plugin.getLocales().getLocale("error_no_warps_set").ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
final WarpList warpList = new WarpList(warps, plugin);
|
||||
plugin.getCache().positionLists.put(onlineUser.uuid, warpList);
|
||||
warpList.getDisplay(pageNumber).forEach(onlineUser::sendMessage);
|
||||
onlineUser.sendMessage(plugin.getCache().getWarpList(onlineUser, plugin.getLocales(), warps,
|
||||
plugin.getSettings().listItemsPerPage, pageNumber));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,152 +0,0 @@
|
||||
package net.william278.huskhomes.list;
|
||||
|
||||
import de.themoep.minedown.MineDown;
|
||||
import net.william278.huskhomes.config.Locales;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* A list used to display saved positions
|
||||
*/
|
||||
public abstract class ChatList {
|
||||
|
||||
/**
|
||||
* Instance of the plugin {@link Locales}
|
||||
*/
|
||||
private final Locales locales;
|
||||
|
||||
/**
|
||||
* List of {@link ListItem}s to display
|
||||
*/
|
||||
protected final List<ListItem> positions;
|
||||
/**
|
||||
* The number of items to be displayed on each page
|
||||
*/
|
||||
protected final int itemsPerPage;
|
||||
/**
|
||||
* The command used to switch players
|
||||
*/
|
||||
protected final String command;
|
||||
|
||||
|
||||
public ChatList(@NotNull List<ListItem> positions, int itemsPerPage, @NotNull String command, @NotNull Locales locales) {
|
||||
this.positions = positions;
|
||||
this.itemsPerPage = itemsPerPage;
|
||||
this.command = command;
|
||||
this.locales = locales;
|
||||
Collections.sort(this.positions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of formatted MineDown sequences to form a page
|
||||
*/
|
||||
public List<MineDown> getDisplay(int pageNumber) {
|
||||
final List<MineDown> display = new ArrayList<>();
|
||||
final int pageCount = (int) Math.ceil((double) positions.size() / (double) itemsPerPage);
|
||||
|
||||
if (pageNumber <= 0 || pageNumber > pageCount) {
|
||||
return Collections.singletonList(locales.getLocale("error_invalid_page_number").orElse(new MineDown("")));
|
||||
}
|
||||
|
||||
// Calculate display bounds
|
||||
final int lastItemIndex = positions.size() - 1;
|
||||
final int itemIndexStart = Math.min(itemsPerPage * (pageNumber - 1), lastItemIndex);
|
||||
final int itemIndexEnd = Math.min((itemsPerPage * pageNumber) - 1, lastItemIndex);
|
||||
|
||||
// Add the header with page information
|
||||
display.add(getHeader(itemIndexStart + 1, itemIndexEnd + 1, lastItemIndex + 1));
|
||||
|
||||
// Add the items
|
||||
if (itemIndexEnd - itemIndexStart < 0) {
|
||||
display.add(locales.getLocale("page_no_items").orElse(new MineDown("")));
|
||||
} else {
|
||||
final StringJoiner itemJoiner = new StringJoiner(getItemSeparator());
|
||||
for (int i = itemIndexStart; i <= itemIndexEnd; i++) {
|
||||
itemJoiner.add(getItemDisplayLocale(positions.get(i)));
|
||||
}
|
||||
display.add(new MineDown(itemJoiner.toString()));
|
||||
}
|
||||
|
||||
// Add the footer with navigation buttons
|
||||
display.add(getFooter(determineFooterLayout(pageNumber, pageCount), pageNumber, pageCount));
|
||||
return display;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines the {@link FooterLayout} to use for a given page
|
||||
*
|
||||
* @param pageNumber number of page to get layout for
|
||||
* @param pageCount total number of pages
|
||||
* @return the {@link FooterLayout} for the page
|
||||
*/
|
||||
private FooterLayout determineFooterLayout(int pageNumber, int pageCount) {
|
||||
final boolean isNextPage = pageNumber + 1 <= pageCount;
|
||||
final boolean isPreviousPage = pageNumber - 1 > 0;
|
||||
if (isNextPage && isPreviousPage) {
|
||||
return FooterLayout.BOTH_BUTTONS;
|
||||
}
|
||||
if (isNextPage) {
|
||||
return FooterLayout.NEXT_BUTTON;
|
||||
}
|
||||
if (isPreviousPage) {
|
||||
return FooterLayout.PREVIOUS_BUTTON;
|
||||
}
|
||||
return FooterLayout.NO_BUTTONS;
|
||||
}
|
||||
|
||||
protected abstract String getItemDisplayLocale(@NotNull ListItem item);
|
||||
|
||||
protected abstract String getItemSeparator();
|
||||
|
||||
protected abstract MineDown getHeader(int pageItemStart, int pageItemEnd, int totalItemCount);
|
||||
|
||||
private MineDown getFooter(@NotNull FooterLayout layout, int pageNumber, int maxPages) {
|
||||
return switch (layout) {
|
||||
case NEXT_BUTTON -> locales.getLocale("page_options_min",
|
||||
Integer.toString(pageNumber), Integer.toString(maxPages),
|
||||
"/" + command + " " + (pageNumber + 1))
|
||||
.orElse(new MineDown(""));
|
||||
case PREVIOUS_BUTTON -> locales.getLocale("page_options_max",
|
||||
"/" + command + " " + (pageNumber - 1), Integer.toString(pageNumber),
|
||||
Integer.toString(maxPages))
|
||||
.orElse(new MineDown(""));
|
||||
case NO_BUTTONS -> locales.getLocale("page_options_min_max",
|
||||
Integer.toString(pageNumber), Integer.toString(maxPages))
|
||||
.orElse(new MineDown(""));
|
||||
case BOTH_BUTTONS -> locales.getLocale("page_options",
|
||||
"/" + command + " " + (pageNumber - 1), Integer.toString(pageNumber),
|
||||
Integer.toString(maxPages), "/" + command + " " + (pageNumber + 1))
|
||||
.orElse(new MineDown(""));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for identifying the layout of a list footer
|
||||
*/
|
||||
protected enum FooterLayout {
|
||||
/**
|
||||
* List footer with a previous page button
|
||||
*/
|
||||
PREVIOUS_BUTTON,
|
||||
|
||||
/**
|
||||
* List footer with a next page button
|
||||
*/
|
||||
NEXT_BUTTON,
|
||||
|
||||
/**
|
||||
* List footer with both a previous page and a next page button.
|
||||
*/
|
||||
BOTH_BUTTONS,
|
||||
|
||||
/**
|
||||
* List footer with no buttons
|
||||
*/
|
||||
NO_BUTTONS
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package net.william278.huskhomes.list;
|
||||
|
||||
import net.william278.huskhomes.config.Locales;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* An item on a {@link ChatList}
|
||||
*/
|
||||
public record ListItem(@NotNull String itemDisplayName,
|
||||
@NotNull String... itemDisplayMeta) implements Comparable<ListItem> {
|
||||
|
||||
/**
|
||||
* Returns the chat list formatted {@link ListItem} string
|
||||
*
|
||||
* @param localeId id of the locale to use when formatting this item
|
||||
* @param locales Instance of the {@link Locales} set
|
||||
* @return the pre-formatted MineDown of this ListItem
|
||||
*/
|
||||
public String getFormattedItem(String localeId, Locales locales) {
|
||||
final String[] itemDisplayComponents = new String[itemDisplayMeta.length + 1];
|
||||
itemDisplayComponents[0] = itemDisplayName;
|
||||
if (itemDisplayMeta.length >= 1) {
|
||||
System.arraycopy(itemDisplayMeta, 0, itemDisplayComponents, 1, itemDisplayMeta.length);
|
||||
}
|
||||
return locales.getRawLocale(localeId, itemDisplayComponents).orElse(itemDisplayName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two {@link ListItem}s, returns alphabetical compareTo result
|
||||
*
|
||||
* @param other the other ListItem to be compared.
|
||||
* @return the alphabetical comparison with another {@link ListItem}'s item display name
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(@NotNull ListItem other) {
|
||||
return itemDisplayName.compareToIgnoreCase(other.itemDisplayName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package net.william278.huskhomes.list;
|
||||
|
||||
import de.themoep.minedown.MineDown;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.player.User;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PrivateHomeList extends ChatList {
|
||||
|
||||
private final HuskHomes plugin;
|
||||
private final User homeOwner;
|
||||
|
||||
public PrivateHomeList(@NotNull List<Home> homes, @NotNull User homeOwner, @NotNull HuskHomes implementor) {
|
||||
super(homes.stream().map(position -> new ListItem(position.meta.name, position.owner.username,
|
||||
position.meta.description)).collect(Collectors.toList()),
|
||||
10, "homelist", implementor.getLocales()); //todo config settable items per page
|
||||
this.plugin = implementor;
|
||||
this.homeOwner = homeOwner;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getItemDisplayLocale(@NotNull ListItem item) {
|
||||
return item.getFormattedItem("home_list_item", plugin.getLocales());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getItemSeparator() {
|
||||
return plugin.getLocales().getRawLocale("list_item_divider").orElse(" ");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MineDown getHeader(int pageItemStart, int pageItemEnd, int totalItemCount) {
|
||||
return plugin.getLocales().getLocale("private_home_list_page_top", homeOwner.username,
|
||||
Integer.toString(pageItemStart), Integer.toString(pageItemEnd), Integer.toString(totalItemCount))
|
||||
.orElseGet(() -> new MineDown(""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package net.william278.huskhomes.list;
|
||||
|
||||
import de.themoep.minedown.MineDown;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.player.User;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PublicHomeList extends ChatList {
|
||||
private final HuskHomes plugin;
|
||||
|
||||
public PublicHomeList(@NotNull List<Home> homes, @NotNull HuskHomes implementor) {
|
||||
super(homes.stream().map(position -> new ListItem(position.meta.name, position.owner.username,
|
||||
position.meta.description)).collect(Collectors.toList()),
|
||||
10, "phomelist", implementor.getLocales()); //todo config settable items per page
|
||||
this.plugin = implementor;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getItemDisplayLocale(@NotNull ListItem item) {
|
||||
return item.getFormattedItem("public_home_list_item", plugin.getLocales());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getItemSeparator() {
|
||||
return plugin.getLocales().getRawLocale("list_item_divider").orElse(" ");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MineDown getHeader(int pageItemStart, int pageItemEnd, int totalItemCount) {
|
||||
return plugin.getLocales().getLocale("public_home_list_page_top", Integer.toString(pageItemStart),
|
||||
Integer.toString(pageItemEnd), Integer.toString(totalItemCount)).orElseGet(() -> new MineDown(""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package net.william278.huskhomes.list;
|
||||
|
||||
import de.themoep.minedown.MineDown;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class WarpList extends ChatList {
|
||||
|
||||
private final HuskHomes plugin;
|
||||
|
||||
public WarpList(@NotNull List<Warp> warps, @NotNull HuskHomes implementor) {
|
||||
super(warps.stream().map(position -> new ListItem(position.meta.name, position.meta.description))
|
||||
.collect(Collectors.toList()), 10, "warplist", implementor.getLocales());
|
||||
//todo config settable items per page
|
||||
this.plugin = implementor;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getItemDisplayLocale(@NotNull ListItem item) {
|
||||
return item.getFormattedItem("warp_list_item", plugin.getLocales());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getItemSeparator() {
|
||||
return plugin.getLocales().getRawLocale("list_item_divider").orElse(" ");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MineDown getHeader(int pageItemStart, int pageItemEnd, int totalItemCount) {
|
||||
return plugin.getLocales().getLocale("warp_list_page_top",
|
||||
Integer.toString(pageItemStart), Integer.toString(pageItemEnd), Integer.toString(totalItemCount))
|
||||
.orElseGet(() -> new MineDown(""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public class EventListener {
|
||||
}).thenRun(() -> plugin.getDatabase().setCurrentTeleport(onlineUser, null))));
|
||||
// Update the player list
|
||||
assert plugin.getNetworkMessenger() != null;
|
||||
plugin.getCache().updatePlayerList(plugin, onlineUser);
|
||||
plugin.getCache().updateOnlinePlayerList(plugin, onlineUser);
|
||||
}
|
||||
|
||||
// Cache this user's homes
|
||||
@@ -91,7 +91,7 @@ public class EventListener {
|
||||
plugin.getOnlinePlayers().stream().filter(
|
||||
onlinePlayer -> !onlinePlayer.uuid.equals(onlineUser.uuid))
|
||||
.findAny().ifPresent(updater ->
|
||||
plugin.getCache().updatePlayerList(plugin, updater));
|
||||
plugin.getCache().updateOnlinePlayerList(plugin, updater));
|
||||
}
|
||||
|
||||
// Set offline position
|
||||
|
||||
@@ -1,16 +1,9 @@
|
||||
package net.william278.huskhomes.position;
|
||||
|
||||
import de.themoep.minedown.MineDown;
|
||||
import net.william278.huskhomes.config.Locales;
|
||||
import net.william278.huskhomes.player.User;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Represents a home set by a {@link User}
|
||||
|
||||
@@ -7,7 +7,7 @@ import java.util.UUID;
|
||||
/**
|
||||
* Represents a persistently-saved {@link Position}
|
||||
*/
|
||||
public abstract class SavedPosition extends Position {
|
||||
public abstract class SavedPosition extends Position implements Comparable<SavedPosition> {
|
||||
|
||||
/**
|
||||
* Metadata about this position (name, description)
|
||||
@@ -42,4 +42,9 @@ public abstract class SavedPosition extends Position {
|
||||
this.uuid = UUID.randomUUID();
|
||||
}
|
||||
|
||||
// Compare based on names for alphabetical sorting
|
||||
@Override
|
||||
public int compareTo(@NotNull SavedPosition o) {
|
||||
return this.meta.name.compareTo(o.meta.name);
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ public class SavedPositionManager {
|
||||
final Home home = new Home(position, homeMeta, homeOwner);
|
||||
cache.homes.putIfAbsent(home.owner.uuid, new ArrayList<>());
|
||||
cache.homes.get(home.owner.uuid).add(home.meta.name);
|
||||
cache.positionLists.clear();
|
||||
cache.privateHomeLists.remove(home.owner.uuid);
|
||||
return database.setHome(home).thenApply(value ->
|
||||
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join();
|
||||
}
|
||||
@@ -72,7 +72,8 @@ public class SavedPositionManager {
|
||||
homeNames.remove(home.meta.name);
|
||||
return homeNames;
|
||||
});
|
||||
cache.positionLists.clear();
|
||||
cache.privateHomeLists.remove(home.owner.uuid);
|
||||
cache.publicHomeLists.clear();
|
||||
return true;
|
||||
}).join();
|
||||
}
|
||||
@@ -118,7 +119,8 @@ public class SavedPositionManager {
|
||||
cache.homes.get(home.owner.uuid).remove(existingHomeName);
|
||||
cache.homes.get(home.owner.uuid).add(newHomeMeta.name);
|
||||
}
|
||||
cache.positionLists.clear();
|
||||
cache.privateHomeLists.remove(home.owner.uuid);
|
||||
cache.publicHomeLists.clear();
|
||||
return database.setHome(home).thenApply(value ->
|
||||
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join();
|
||||
});
|
||||
@@ -197,7 +199,8 @@ public class SavedPositionManager {
|
||||
} else if (cache.publicHomes.containsKey(home.owner.username)) {
|
||||
cache.publicHomes.get(home.owner.username).remove(home.meta.name);
|
||||
}
|
||||
cache.positionLists.clear();
|
||||
cache.privateHomeLists.remove(home.owner.uuid);
|
||||
cache.publicHomeLists.clear();
|
||||
return true;
|
||||
});
|
||||
}
|
||||
@@ -216,7 +219,7 @@ public class SavedPositionManager {
|
||||
if (optionalWarp.isEmpty()) {
|
||||
final Warp warp = new Warp(position, warpMeta);
|
||||
cache.warps.add(warp.meta.name);
|
||||
cache.positionLists.clear();
|
||||
cache.warpLists.clear();
|
||||
return database.setWarp(warp).thenApply(ignored ->
|
||||
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(warp))).join();
|
||||
}
|
||||
@@ -237,7 +240,7 @@ public class SavedPositionManager {
|
||||
final Warp warp = optionalWarp.get();
|
||||
return database.deleteWarp(warp.uuid).thenApply(ignored -> {
|
||||
cache.warps.remove(warp.meta.name);
|
||||
cache.positionLists.clear();
|
||||
cache.warpLists.clear();
|
||||
return true;
|
||||
}).join();
|
||||
}
|
||||
@@ -282,7 +285,7 @@ public class SavedPositionManager {
|
||||
cache.warps.remove(existingWarpName);
|
||||
cache.warps.add(newWarpMeta.name);
|
||||
}
|
||||
cache.positionLists.clear();
|
||||
cache.warpLists.clear();
|
||||
return database.setWarp(warp).thenApply(value ->
|
||||
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(warp))).join();
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user