diff --git a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java index 6e71a6e3..01585ff5 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java +++ b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java @@ -12,6 +12,8 @@ public enum BukkitCommandType { SET_HOME_COMMAND(new SetHomeCommand(BukkitHuskHomes.getInstance())), HOME_LIST_COMMAND(new HomeListCommand(BukkitHuskHomes.getInstance())), DEL_HOME_COMMAND(new DelHomeCommand(BukkitHuskHomes.getInstance())), + PUBLIC_HOME_COMMAND(new PublicHomeCommand(BukkitHuskHomes.getInstance())), + PUBLIC_HOME_LIST_COMMAND(new PublicHomeListCommand(BukkitHuskHomes.getInstance())), WARP_COMMAND(new WarpCommand(BukkitHuskHomes.getInstance())), SET_WARP_COMMAND(new SetWarpCommand(BukkitHuskHomes.getInstance())), WARP_LIST_COMMAND(new WarpListCommand(BukkitHuskHomes.getInstance())), diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index c4665611..1200a165 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -19,6 +19,10 @@ commands: usage: /homelist [page] delhome: usage: /delhome + publichome: + usage: /publichome [.] + publichomelist: + usage: /publichomelist [page] warp: usage: /warp setwarp: diff --git a/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java index d04a339e..7ead51ee 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java @@ -21,6 +21,7 @@ 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 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 -> @@ -33,6 +34,7 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE } }); case 1 -> { + // Parse the home name input and teleport the player to the home final String homeName = args[0]; RegexUtil.matchDisambiguatedHomeIdentifier(homeName).ifPresentOrElse( homeIdentifier -> plugin.getDatabase().getUserDataByName(homeIdentifier.ownerName()) @@ -55,7 +57,7 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE @Override public List onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) { return plugin.getCache().homes.get(onlineUser.uuid).stream() - .filter(s -> s.startsWith(args.length >= 1 ? args[0] : "")) + .filter(s -> s.toLowerCase().startsWith(args.length >= 1 ? args[0].toLowerCase() : "")) .sorted().collect(Collectors.toList()); } } 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 65f1e2fc..65a4b739 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HomeListCommand.java @@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull; public class HomeListCommand extends CommandBase implements ConsoleExecutable { public HomeListCommand(@NotNull HuskHomes implementor) { - super("homelist", Permission.COMMAND_HOME, implementor); + super("homelist", Permission.COMMAND_HOME, implementor, "homes"); } @Override @@ -65,7 +65,7 @@ public class HomeListCommand extends CommandBase implements ConsoleExecutable { return; } final PrivateHomeList homeList = new PrivateHomeList(homes, userData.user(), plugin); - plugin.getCache().positionLists.put(userData.user().uuid, homeList); + plugin.getCache().positionLists.put(onlineUser.uuid, homeList); homeList.getDisplay(pageNumber).forEach(onlineUser::sendMessage); }); }, () -> plugin.getLocales().getLocale("error_invalid_player").ifPresent(onlineUser::sendMessage))); diff --git a/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java new file mode 100644 index 00000000..409df062 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java @@ -0,0 +1,78 @@ +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; +import net.william278.huskhomes.util.RegexUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +public class PublicHomeCommand extends CommandBase implements TabCompletable, ConsoleExecutable { + + public PublicHomeCommand(@NotNull HuskHomes implementor) { + super("publichome", Permission.COMMAND_PUBLIC_HOME, implementor, "phome"); + } + + @Override + public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { + switch (args.length) { + case 0 -> plugin.getDatabase().getPublicHomes().thenAccept(publicHomes -> { + // Display the public home list if there are any public home set + if (publicHomes.size() == 0) { + plugin.getLocales().getLocale("error_no_public_homes_set").ifPresent(onlineUser::sendMessage); + return; + } + + final PublicHomeList homeList = new PublicHomeList(publicHomes, plugin); + plugin.getCache().positionLists.put(onlineUser.uuid, homeList); + homeList.getDisplay(1).forEach(onlineUser::sendMessage); + }); + case 1 -> { + final String homeName = args[0]; + // Match the input to a home identifier and teleport + RegexUtil.matchDisambiguatedHomeIdentifier(homeName).ifPresentOrElse( + homeIdentifier -> plugin.getDatabase().getUserDataByName(homeIdentifier.ownerName()) + .thenAccept(optionalUserData -> optionalUserData.ifPresentOrElse( + userData -> plugin.getTeleportManager().teleportToHome(onlineUser, userData.user(), homeIdentifier.homeName()), + () -> plugin.getLocales().getLocale("error_home_invalid_other", homeIdentifier.ownerName(), homeIdentifier.homeName()) + .ifPresent(onlineUser::sendMessage))), + () -> plugin.getDatabase().getPublicHomes().thenAccept(publicHomes -> { + // If the identifier format was not used, attempt to teleport the player to the closest match + final List homeMatches = publicHomes.stream() + .filter(home -> home.meta.name.equalsIgnoreCase(homeName)).toList(); + if ((long) homeMatches.size() == 1) { + plugin.getTeleportManager().teleportToHome(onlineUser, homeMatches.get(0)); + } else { + plugin.getLocales().getLocale("error_invalid_syntax", "/publichome [.]") + .ifPresent(onlineUser::sendMessage); + } + })); + } + default -> plugin.getLocales().getLocale("error_invalid_syntax", "/publichome [.]") + .ifPresent(onlineUser::sendMessage); + } + } + + @Override + public void onConsoleExecute(@NotNull String[] args) { + //todo + } + + @Override + public List onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) { + // Return every public home name as username.home_name from the cache + final List publicHomes = new ArrayList<>(); + plugin.getCache().publicHomes.forEach((ownerName, homeNames) -> + homeNames.forEach(homeName -> publicHomes.add(ownerName + "." + homeName))); + return publicHomes.stream().filter(publicHomeIdentifier -> + publicHomeIdentifier.split(Pattern.quote("."))[1].toLowerCase() + .startsWith(args.length >= 1 ? args[0].toLowerCase() : "")) + .sorted().collect(Collectors.toList()); + } +} diff --git a/common/src/main/java/net/william278/huskhomes/command/PublicHomeListCommand.java b/common/src/main/java/net/william278/huskhomes/command/PublicHomeListCommand.java new file mode 100644 index 00000000..72967127 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/command/PublicHomeListCommand.java @@ -0,0 +1,63 @@ +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; + +public class PublicHomeListCommand extends CommandBase implements ConsoleExecutable { + + public PublicHomeListCommand(@NotNull HuskHomes implementor) { + super("publichomelist", Permission.COMMAND_HOME, implementor, "phomelist", "phomes"); + } + + @Override + public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { + switch (args.length) { + case 0 -> showPublicHomeList(onlineUser, 1); + case 1 -> { + try { + int pageNumber = Integer.parseInt(args[0]); + showPublicHomeList(onlineUser, pageNumber); + } catch (NumberFormatException e) { + plugin.getLocales().getLocale("error_invalid_syntax", "/publichomelist [page]") + .ifPresent(onlineUser::sendMessage); + } + } + default -> plugin.getLocales().getLocale("error_invalid_syntax", "/publichomelist [page]") + .ifPresent(onlineUser::sendMessage); + } + } + + /** + * Show a (cached) list of public homes + * + * @param onlineUser the user to display the homes to + * @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; + } + } + + plugin.getDatabase().getPublicHomes().thenAccept(warps -> { + if (warps.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); + }); + + } + + @Override + public void onConsoleExecute(@NotNull String[] args) { + //todo + } +} diff --git a/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java index caf80038..9dd6263f 100644 --- a/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java @@ -39,7 +39,7 @@ public class WarpCommand extends CommandBase implements TabCompletable, ConsoleE @Override public List onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) { return plugin.getCache().warps.stream() - .filter(s -> s.startsWith(args.length >= 1 ? args[0] : "")) + .filter(s -> s.toLowerCase().startsWith(args.length >= 1 ? args[0].toLowerCase() : "")) .sorted().collect(Collectors.toList()); } diff --git a/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java b/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java index 17c60d04..9651e862 100644 --- a/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/WarpListCommand.java @@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull; public class WarpListCommand extends CommandBase implements ConsoleExecutable { public WarpListCommand(@NotNull HuskHomes implementor) { - super("warplist", Permission.COMMAND_WARP, implementor); + super("warplist", Permission.COMMAND_WARP, implementor, "warps"); } @Override diff --git a/common/src/main/java/net/william278/huskhomes/config/Settings.java b/common/src/main/java/net/william278/huskhomes/config/Settings.java index b20efa42..29ffd925 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Settings.java +++ b/common/src/main/java/net/william278/huskhomes/config/Settings.java @@ -22,7 +22,7 @@ import java.util.Optional; versionField = "config_version", versionNumber = 11) public class Settings { - // Main settings + // Top-level settings public String language = "en-gb"; @@ -147,6 +147,7 @@ public class Settings { public Settings() { } + /** * Options for use in establishing pooled connections to a database */ diff --git a/common/src/main/java/net/william278/huskhomes/list/PublicHomeList.java b/common/src/main/java/net/william278/huskhomes/list/PublicHomeList.java index ac862fce..f37ee0d9 100644 --- a/common/src/main/java/net/william278/huskhomes/list/PublicHomeList.java +++ b/common/src/main/java/net/william278/huskhomes/list/PublicHomeList.java @@ -12,7 +12,7 @@ import java.util.stream.Collectors; public class PublicHomeList extends ChatList { private final HuskHomes plugin; - public PublicHomeList(@NotNull List homes, @NotNull User homeOwner, @NotNull HuskHomes implementor) { + public PublicHomeList(@NotNull List 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 diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java index 996796fd..5f81edac 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java @@ -31,7 +31,7 @@ public abstract class TeleportManager { } /** - * Teleport a {@link OnlineUser} to another {@link User}'s {@link Home} + * Attempt to teleport a {@link OnlineUser} to a {@link User}'s home by the home name * * @param onlineUser the {@link OnlineUser} to teleport * @param homeOwner the {@link User} who owns the home @@ -39,16 +39,7 @@ public abstract class TeleportManager { */ public void teleportToHome(@NotNull OnlineUser onlineUser, @NotNull User homeOwner, @NotNull String homeName) { plugin.getDatabase().getHome(homeOwner, homeName).thenAccept(optionalHome -> - optionalHome.ifPresentOrElse(home -> { - if (!homeOwner.uuid.equals(onlineUser.uuid)) { - if (!home.isPublic && !onlineUser.hasPermission(Permission.COMMAND_HOME_OTHER.node)) { - plugin.getLocales().getLocale("error_public_home_invalid", homeOwner.username, homeName) - .ifPresent(onlineUser::sendMessage); - return; - } - } - teleport(onlineUser, home).thenAccept(result -> finishTeleport(onlineUser, result)); - }, () -> { + optionalHome.ifPresentOrElse(home -> teleportToHome(onlineUser, home), () -> { if (homeOwner.uuid.equals(onlineUser.uuid)) { plugin.getLocales().getLocale("error_home_invalid", homeName).ifPresent(onlineUser::sendMessage); } else { @@ -58,7 +49,24 @@ public abstract class TeleportManager { } /** - * Teleport a {@link OnlineUser} to a server {@link Warp} + * Attempt to teleport a {@link OnlineUser} to a {@link Home} + * + * @param onlineUser the {@link OnlineUser} to teleport + * @param home the {@link Home} to teleport to + */ + public void teleportToHome(@NotNull OnlineUser onlineUser, @NotNull Home home) { + if (!home.owner.uuid.equals(onlineUser.uuid)) { + if (!home.isPublic && !onlineUser.hasPermission(Permission.COMMAND_HOME_OTHER.node)) { + plugin.getLocales().getLocale("error_public_home_invalid", home.owner.username, home.meta.name) + .ifPresent(onlineUser::sendMessage); + return; + } + } + teleport(onlineUser, home).thenAccept(result -> finishTeleport(onlineUser, result)); + } + + /** + * Attempt to teleport a {@link OnlineUser} to a server warp by its' given name * * @param onlineUser the {@link OnlineUser} to teleport * @param warpName the name of the warp @@ -66,11 +74,21 @@ public abstract class TeleportManager { public void teleportToWarp(@NotNull OnlineUser onlineUser, @NotNull String warpName) { plugin.getDatabase().getWarp(warpName).thenAccept(optionalWarp -> optionalWarp.ifPresentOrElse(warp -> //todo permission restricted warps - teleport(onlineUser, warp).thenAccept(result -> finishTeleport(onlineUser, result)), () -> + teleportToWarp(onlineUser, warp), () -> plugin.getLocales().getLocale("error_warp_invalid", warpName) .ifPresent(onlineUser::sendMessage))); } + /** + * Attempt to teleport a {@link OnlineUser} to a server {@link Warp} + * + * @param onlineUser the {@link OnlineUser} to teleport + * @param warp the {@link Warp} to teleport to + */ + public void teleportToWarp(@NotNull OnlineUser onlineUser, @NotNull Warp warp) { + teleport(onlineUser, warp).thenAccept(result -> finishTeleport(onlineUser, result)); + } + /** * Teleport a {@link OnlineUser} to another player by given input name *