mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-04 06:28:49 +00:00
Private home list display, caching
This commit is contained in:
1 parent
83d5a921ee
commit
bafd3837dd
10 files changed
+237
-19
No files matched your search
@@ -236,10 +236,13 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes {
|
|||||||
return getServer().getName();
|
return getServer().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class BukkitAdapter {
|
public static final class BukkitAdapter {
|
||||||
|
|
||||||
public static Optional<Location> adaptLocation(net.william278.huskhomes.position.Location location) {
|
public static Optional<Location> adaptLocation(net.william278.huskhomes.position.Location location) {
|
||||||
final World world = Bukkit.getWorld(location.world.name);
|
World world = Bukkit.getWorld(location.world.name);
|
||||||
|
if (world == null) {
|
||||||
|
world = Bukkit.getWorld(location.world.uuid);
|
||||||
|
}
|
||||||
if (world == null) {
|
if (world == null) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
@@ -247,5 +250,14 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes {
|
|||||||
location.yaw, location.pitch));
|
location.yaw, location.pitch));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public static net.william278.huskhomes.position.Location adaptLocation(@NotNull Location location) {
|
||||||
|
if (location.getWorld() == null) return null;
|
||||||
|
return new net.william278.huskhomes.position.Location(location.getX(), location.getY(), location.getZ(),
|
||||||
|
location.getYaw(), location.getPitch(),
|
||||||
|
new net.william278.huskhomes.position.World(location.getWorld().getName(), location.getWorld().getUID()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -72,9 +72,7 @@ public class BukkitPlayer implements Player {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Location getLocation() {
|
public Location getLocation() {
|
||||||
final org.bukkit.Location location = bukkitPlayer.getLocation();
|
return HuskHomesBukkit.BukkitAdapter.adaptLocation(bukkitPlayer.getLocation());
|
||||||
return new Location(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch(),
|
|
||||||
new World(bukkitPlayer.getWorld().getName(), bukkitPlayer.getWorld().getUID()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package net.william278.huskhomes.cache;
|
|||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.config.Settings;
|
import net.william278.huskhomes.config.Settings;
|
||||||
import net.william278.huskhomes.data.Database;
|
import net.william278.huskhomes.data.Database;
|
||||||
import net.william278.huskhomes.messenger.NetworkMessenger;
|
import net.william278.huskhomes.list.PositionList;
|
||||||
import net.william278.huskhomes.player.Player;
|
import net.william278.huskhomes.player.Player;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@@ -12,7 +12,6 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A cache used to hold persistently used data
|
* A cache used to hold persistently used data
|
||||||
@@ -39,6 +38,11 @@ public class Cache {
|
|||||||
*/
|
*/
|
||||||
public final List<String> players;
|
public final List<String> players;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cached position lists for optimized list navigating
|
||||||
|
*/
|
||||||
|
public final HashMap<UUID, PositionList> positionLists;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new cache
|
* Create a new cache
|
||||||
*/
|
*/
|
||||||
@@ -47,6 +51,7 @@ public class Cache {
|
|||||||
this.publicHomes = new HashMap<>();
|
this.publicHomes = new HashMap<>();
|
||||||
this.warps = new ArrayList<>();
|
this.warps = new ArrayList<>();
|
||||||
this.players = new ArrayList<>();
|
this.players = new ArrayList<>();
|
||||||
|
this.positionLists = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package net.william278.huskhomes.command;
|
package net.william278.huskhomes.command;
|
||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
|
import net.william278.huskhomes.list.PrivateHomeList;
|
||||||
import net.william278.huskhomes.player.Player;
|
import net.william278.huskhomes.player.Player;
|
||||||
import net.william278.huskhomes.player.User;
|
import net.william278.huskhomes.player.User;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -8,7 +9,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class HomeCommand extends CommandBase implements TabCompletable {
|
public class HomeCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
|
||||||
|
|
||||||
private final static String PERMISSION = "huskhomes.command.home";
|
private final static String PERMISSION = "huskhomes.command.home";
|
||||||
|
|
||||||
@@ -18,20 +19,28 @@ public class HomeCommand extends CommandBase implements TabCompletable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onExecute(@NotNull Player player, @NotNull String[] args) {
|
public void onExecute(@NotNull Player player, @NotNull String[] args) {
|
||||||
if (args.length >= 1) {
|
switch (args.length) {
|
||||||
//todo check if its a public home, process. Support sending a player
|
case 0 -> plugin.getDatabase().getHomes(new User(player)).thenAccept(homes -> {
|
||||||
plugin.getTeleportManager().teleportToHome(player, new User(player), args[0]);
|
|
||||||
} else {
|
|
||||||
plugin.getDatabase().getHomes(new User(player)).thenAccept(homes -> {
|
|
||||||
switch (homes.size()) {
|
switch (homes.size()) {
|
||||||
case 0 -> plugin.getLocales().getLocale("error_no_homes_set").ifPresent(player::sendMessage);
|
case 0 -> plugin.getLocales().getLocale("error_no_homes_set").ifPresent(player::sendMessage);
|
||||||
case 1 -> plugin.getTeleportManager().executeTeleport(player, homes.get(0)).thenAccept(result ->
|
case 1 -> plugin.getTeleportManager().executeTeleport(player, homes.get(0)).thenAccept(result ->
|
||||||
plugin.getTeleportManager().finishTeleport(player, result));
|
plugin.getTeleportManager().finishTeleport(player, result));
|
||||||
default -> {
|
default -> {
|
||||||
//todo show home list
|
final User user = new User(player.getUuid(), player.getName());
|
||||||
|
plugin.getDatabase().getHomes(user).thenAccept(playerHomes -> {
|
||||||
|
final PrivateHomeList homeList = new PrivateHomeList(homes, user, plugin);
|
||||||
|
plugin.getCache().positionLists.put(user.uuid, homeList);
|
||||||
|
homeList.getDisplay(1).forEach(player::sendMessage);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
case 1 -> {
|
||||||
|
//todo check if its a public home, process. Support sending a player
|
||||||
|
plugin.getTeleportManager().teleportToHome(player, new User(player), args[0]);
|
||||||
|
}
|
||||||
|
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/home [name]")
|
||||||
|
.ifPresent(player::sendMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,4 +50,9 @@ public class HomeCommand extends CommandBase implements TabCompletable {
|
|||||||
.filter(s -> s.startsWith(args.length >= 1 ? args[0] : ""))
|
.filter(s -> s.startsWith(args.length >= 1 ? args[0] : ""))
|
||||||
.sorted().collect(Collectors.toList());
|
.sorted().collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onConsoleExecute(@NotNull String[] args) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package net.william278.huskhomes.list;
|
||||||
|
|
||||||
|
import de.themoep.minedown.MineDown;
|
||||||
|
import net.william278.huskhomes.position.SavedPosition;
|
||||||
|
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 PositionList {
|
||||||
|
protected final List<SavedPosition> positions;
|
||||||
|
protected final int itemsPerPage;
|
||||||
|
|
||||||
|
public PositionList(@NotNull List<SavedPosition> positions, int itemsPerPage) {
|
||||||
|
this.positions = positions;
|
||||||
|
this.itemsPerPage = itemsPerPage;
|
||||||
|
Collections.sort(this.positions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<MineDown> getDisplay(int pageNumber) {
|
||||||
|
final List<MineDown> display = new ArrayList<>();
|
||||||
|
final int pageCount = (positions.size() / itemsPerPage) + (positions.size() % itemsPerPage);
|
||||||
|
|
||||||
|
if (pageNumber <= 0 || pageNumber > pageCount) {
|
||||||
|
return Collections.singletonList(getInvalidPageNumberMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(getNoItemsMessage());
|
||||||
|
} else {
|
||||||
|
final StringJoiner itemJoiner = new StringJoiner(getItemSeparator());
|
||||||
|
for (int i = itemIndexStart; i <= itemIndexEnd; i++) {
|
||||||
|
itemJoiner.add(getFormattedItem(positions.get(i)));
|
||||||
|
}
|
||||||
|
display.add(new MineDown(itemJoiner.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the footer with navigation buttons
|
||||||
|
display.add(getFooter(determineFooterLayout(pageNumber, itemIndexEnd, lastItemIndex), pageNumber, pageCount));
|
||||||
|
return display;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FooterLayout determineFooterLayout(int pageNumber, int pageItemEnd, int totalItemCount) {
|
||||||
|
if (positions.size() == 0 || pageNumber == 1 && pageItemEnd <= totalItemCount) {
|
||||||
|
return FooterLayout.NO_BUTTONS;
|
||||||
|
}
|
||||||
|
if (pageNumber == 1) {
|
||||||
|
return FooterLayout.NEXT_BUTTON;
|
||||||
|
}
|
||||||
|
if (totalItemCount > pageItemEnd) {
|
||||||
|
return FooterLayout.BOTH_BUTTONS;
|
||||||
|
}
|
||||||
|
return FooterLayout.PREVIOUS_BUTTON;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract String getItemSeparator();
|
||||||
|
|
||||||
|
protected abstract MineDown getHeader(int pageItemStart, int pageItemEnd, int totalItemCount);
|
||||||
|
|
||||||
|
protected abstract MineDown getFooter(@NotNull FooterLayout layout, int pageNumber, int maxPages);
|
||||||
|
|
||||||
|
protected abstract String getFormattedItem(SavedPosition position);
|
||||||
|
|
||||||
|
protected abstract MineDown getNoItemsMessage();
|
||||||
|
|
||||||
|
protected abstract MineDown getInvalidPageNumberMessage();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
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 net.william278.huskhomes.position.SavedPosition;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class PrivateHomeList extends PositionList {
|
||||||
|
|
||||||
|
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 -> (SavedPosition) position).collect(Collectors.toList()),
|
||||||
|
10); //todo config settable
|
||||||
|
this.plugin = implementor;
|
||||||
|
this.homeOwner = homeOwner;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected MineDown getFooter(@NotNull FooterLayout layout, int pageNumber, int maxPages) {
|
||||||
|
return switch (layout) {
|
||||||
|
case NEXT_BUTTON -> plugin.getLocales().getLocale("page_options_min",
|
||||||
|
Integer.toString(pageNumber), Integer.toString(maxPages),
|
||||||
|
"/homelist " + (pageNumber + 1))
|
||||||
|
.orElse(new MineDown(""));
|
||||||
|
case PREVIOUS_BUTTON -> plugin.getLocales().getLocale("page_options_max",
|
||||||
|
"/homelist " + (pageNumber - 1), Integer.toString(pageNumber),
|
||||||
|
Integer.toString(maxPages))
|
||||||
|
.orElse(new MineDown(""));
|
||||||
|
case NO_BUTTONS -> plugin.getLocales().getLocale("page_options_min_max",
|
||||||
|
Integer.toString(pageNumber), Integer.toString(maxPages))
|
||||||
|
.orElse(new MineDown(""));
|
||||||
|
case BOTH_BUTTONS -> plugin.getLocales().getLocale("page_options",
|
||||||
|
"/homelist " + (pageNumber - 1), Integer.toString(pageNumber),
|
||||||
|
Integer.toString(maxPages), "/homelist " + (pageNumber + 1))
|
||||||
|
.orElse(new MineDown(""));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getFormattedItem(@NotNull SavedPosition position) {
|
||||||
|
return plugin.getLocales().getRawLocale("home_list_item",
|
||||||
|
position.meta.name, homeOwner.username).orElse(position.meta.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected MineDown getNoItemsMessage() {
|
||||||
|
return plugin.getLocales().getLocale("page_no_items").orElse(new MineDown(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected MineDown getInvalidPageNumberMessage() {
|
||||||
|
return plugin.getLocales().getLocale("error_invalid_page_number").orElse(new MineDown(""));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,7 +30,7 @@ public class Home extends SavedPosition {
|
|||||||
this.isPublic = isPublic;
|
this.isPublic = isPublic;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Home(Position position, PositionMeta meta, @NotNull User owner) {
|
public Home(@NotNull Position position, @NotNull PositionMeta meta, @NotNull User owner) {
|
||||||
super(position, meta);
|
super(position, meta);
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
this.isPublic = false;
|
this.isPublic = false;
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import java.io.IOException;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a position - a location somewhere on the proxy network or server
|
* Represents a position - a {@link Location} somewhere on the proxy network or server
|
||||||
*/
|
*/
|
||||||
public class Position extends Location implements MessageSerializable {
|
public class Position extends Location implements MessageSerializable {
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import java.util.UUID;
|
|||||||
/**
|
/**
|
||||||
* Represents a persistently-saved {@link Position}
|
* 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)
|
* Metadata about this position (name, description)
|
||||||
@@ -42,4 +42,15 @@ public abstract class SavedPosition extends Position {
|
|||||||
this.uuid = UUID.randomUUID();
|
this.uuid = UUID.randomUUID();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compare two {@link SavedPosition}s, returns alphabetical compareTo result
|
||||||
|
*
|
||||||
|
* @param other the object to be compared.
|
||||||
|
* @return the alphabetical comparison with another {@link SavedPosition}'s {@link PositionMeta#name}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public final int compareTo(@NotNull SavedPosition other) {
|
||||||
|
return meta.name.compareToIgnoreCase(other.meta.name);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -21,8 +21,7 @@ public class Warp extends SavedPosition {
|
|||||||
* @param position The {@link Position} to save as a warp
|
* @param position The {@link Position} to save as a warp
|
||||||
* @param meta {@link PositionMeta} information about this position
|
* @param meta {@link PositionMeta} information about this position
|
||||||
*/
|
*/
|
||||||
public Warp(Position position, PositionMeta meta) {
|
public Warp(@NotNull Position position, @NotNull PositionMeta meta) {
|
||||||
super(position, meta);
|
super(position, meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user