Add publichome and publichomelist commands

This commit is contained in:
William committed 2022-08-15 12:25:34 +01:00
1 parent c1df905b41
commit c31229c19e
11 files changed
+188 -20

No files matched your search

@@ -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())),
+4
View File
@@ -19,6 +19,10 @@ commands:
usage: /homelist [page]
delhome:
usage: /delhome <name>
publichome:
usage: /publichome [<owner_name>.<home_name>]
publichomelist:
usage: /publichomelist [page]
warp:
usage: /warp <name>
setwarp:
@@ -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<String> 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());
}
}
@@ -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)));
@@ -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<Home> 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 [<owner_name>.<home_name>]")
.ifPresent(onlineUser::sendMessage);
}
}));
}
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/publichome [<owner_name>.<home_name>]")
.ifPresent(onlineUser::sendMessage);
}
}
@Override
public void onConsoleExecute(@NotNull String[] args) {
//todo
}
@Override
public List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
// Return every public home name as username.home_name from the cache
final List<String> 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());
}
}
@@ -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
}
}
@@ -39,7 +39,7 @@ public class WarpCommand extends CommandBase implements TabCompletable, ConsoleE
@Override
public List<String> 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());
}
@@ -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
@@ -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
*/
@@ -12,7 +12,7 @@ import java.util.stream.Collectors;
public class PublicHomeList extends ChatList {
private final HuskHomes plugin;
public PublicHomeList(@NotNull List<Home> homes, @NotNull User homeOwner, @NotNull HuskHomes implementor) {
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
@@ -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
*