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

@@ -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