mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
More command refactoring work
This commit is contained in:
1 parent
4ad77a53c1
commit
9ca141a58f
10 files changed
+116
-395
No files matched your search
@@ -4,6 +4,7 @@ import de.themoep.minedown.adventure.MineDown;
|
||||
import net.william278.huskhomes.command.Command;
|
||||
import net.william278.huskhomes.config.Locales;
|
||||
import net.william278.huskhomes.database.Database;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
@@ -175,10 +176,10 @@ public class Cache {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public MineDown getCommandList(@NotNull OnlineUser onlineUser, @NotNull Locales locales,
|
||||
public MineDown getCommandList(@NotNull CommandUser user, @NotNull Locales locales,
|
||||
@NotNull List<Command> commands, final int itemsPerPage, final int page) {
|
||||
return PaginatedList.of(commands.stream()
|
||||
.filter(command -> onlineUser.hasPermission(command.getPermission()))
|
||||
.filter(command -> user.hasPermission(command.getPermission()))
|
||||
.map(command -> locales.getRawLocale("command_list_item",
|
||||
Locales.escapeText(command.getName()),
|
||||
Locales.escapeText(command.getDescription().length() > 50
|
||||
|
||||
@@ -1,134 +1,53 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.teleport.Teleport;
|
||||
import net.william278.huskhomes.teleport.TimedTeleport;
|
||||
import net.william278.huskhomes.teleport.Teleportable;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.Optional;
|
||||
|
||||
public class HomeCommand extends Command implements TabProvider, ConsoleExecutable {
|
||||
public abstract class HomeCommand extends SavedPositionCommand<Home> {
|
||||
|
||||
protected HomeCommand(@NotNull HuskHomes implementor) {
|
||||
super("home", Permission.COMMAND_HOME, implementor);
|
||||
protected HomeCommand(@NotNull String name, @NotNull List<String> aliases, @NotNull HuskHomes plugin) {
|
||||
super(name, aliases, Home.class, List.of(), plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
switch (args.length) {
|
||||
case 0 -> plugin.getDatabase().getHomes(onlineUser).thenAcceptAsync(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 -> Teleport.builder(plugin, onlineUser)
|
||||
.setTarget(homes.get(0))
|
||||
.toTimedTeleport().thenAccept(TimedTeleport::execute);
|
||||
default -> plugin.getCache().getHomeList(onlineUser, onlineUser,
|
||||
plugin.getLocales(), homes, plugin.getSettings().getListItemsPerPage(), 1)
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
}
|
||||
});
|
||||
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())
|
||||
.thenAccept(optionalUserData -> optionalUserData.ifPresentOrElse(
|
||||
userData -> teleportToNamedHome(onlineUser, userData.user(), homeIdentifier.homeName()),
|
||||
() -> plugin.getLocales().getLocale("error_home_invalid_other", homeIdentifier.ownerName(), homeIdentifier.homeName())
|
||||
.ifPresent(onlineUser::sendMessage))),
|
||||
() -> teleportToNamedHome(onlineUser, onlineUser, homeName));
|
||||
}
|
||||
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/home [name]")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
}
|
||||
}
|
||||
|
||||
private void teleportToNamedHome(@NotNull OnlineUser teleporter, @NotNull User owner, @NotNull String homeName) {
|
||||
final boolean otherHome = !owner.equals(teleporter);
|
||||
plugin.getDatabase()
|
||||
.getHome(owner, homeName)
|
||||
.thenAccept(homeResult -> homeResult.ifPresentOrElse(home -> {
|
||||
if (otherHome && !home.isPublic) {
|
||||
if (!teleporter.hasPermission(Permission.COMMAND_HOME_OTHER.node)) {
|
||||
plugin.getLocales().getLocale("error_no_permission")
|
||||
.ifPresent(teleporter::sendMessage);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Teleport.builder(plugin, teleporter)
|
||||
.setTarget(home)
|
||||
.toTimedTeleport()
|
||||
.thenAccept(TimedTeleport::execute);
|
||||
}, () -> {
|
||||
if (otherHome) {
|
||||
plugin.getLocales().getLocale("error_home_invalid_other", owner.getUsername(), homeName)
|
||||
.ifPresent(teleporter::sendMessage);
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_home_invalid", homeName)
|
||||
.ifPresent(teleporter::sendMessage);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConsoleExecute(@NotNull String[] args) {
|
||||
if (args.length != 2) {
|
||||
plugin.log(Level.WARNING, "Invalid syntax. Usage: home <player> <home>");
|
||||
public void execute(@NotNull CommandUser executor, @NotNull Home home, @NotNull String[] args) {
|
||||
final Optional<Teleportable> optionalTeleporter = parseStringArg(args, 0).map(Teleportable::username)
|
||||
.or(() -> executor instanceof Teleportable ? Optional.of((Teleportable) executor) : Optional.empty());
|
||||
if (optionalTeleporter.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||
.ifPresent(executor::sendMessage);
|
||||
return;
|
||||
}
|
||||
CompletableFuture.runAsync(() -> {
|
||||
final OnlineUser playerToTeleport = plugin.findOnlinePlayer(args[0]).orElse(null);
|
||||
if (playerToTeleport == null) {
|
||||
plugin.log(Level.WARNING, "Player not found: " + args[0]);
|
||||
return;
|
||||
}
|
||||
final AtomicReference<Home> matchedHome = new AtomicReference<>(null);
|
||||
RegexUtil.matchDisambiguatedHomeIdentifier(args[1]).ifPresentOrElse(
|
||||
identifier -> matchedHome.set(plugin.getDatabase().getUserDataByName(identifier.ownerName()).join()
|
||||
.flatMap(user -> plugin.getDatabase().getHome(user.user(), identifier.homeName()).join())
|
||||
.orElse(null)),
|
||||
() -> matchedHome.set(plugin.getDatabase().getUserDataByName(playerToTeleport.getUsername()).join()
|
||||
.flatMap(user -> plugin.getDatabase().getHome(user.user(), args[1]).join())
|
||||
.orElse(null)));
|
||||
|
||||
final Home home = matchedHome.get();
|
||||
if (home == null) {
|
||||
plugin.log(Level.WARNING, "Could not find home '" + args[1] + "'");
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.log(Level.INFO, "Teleporting " + playerToTeleport.getUsername() + " to "
|
||||
+ home.getOwner().getUsername() + "." + home.getMeta().getName());
|
||||
Teleport.builder(plugin, playerToTeleport)
|
||||
.setTarget(home)
|
||||
.toTeleport()
|
||||
.thenAccept(Teleport::execute);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public final List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
|
||||
if (user == null) {
|
||||
return Collections.emptyList();
|
||||
if (executor instanceof OnlineUser user && !user.hasPermission(getOtherPermission())
|
||||
&& (!home.getOwner().equals(user) && !home.isPublic())) {
|
||||
plugin.getLocales().getLocale("error_public_home_invalid",
|
||||
home.getOwner().getUsername(), home.getName())
|
||||
.ifPresent(executor::sendMessage);
|
||||
return;
|
||||
}
|
||||
return args.length > 1 ? Collections.emptyList() : plugin.getCache().getHomes()
|
||||
.getOrDefault(user.getUuid(), new ArrayList<>())
|
||||
.stream()
|
||||
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
final Teleportable teleporter = optionalTeleporter.get();
|
||||
if (!teleporter.equals(executor) && !executor.hasPermission(Permission.COMMAND_TP_OTHER.node)) {
|
||||
plugin.getLocales().getLocale("error_no_permission")
|
||||
.ifPresent(executor::sendMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
Teleport.builder(plugin)
|
||||
.teleporter(teleporter)
|
||||
.target(home)
|
||||
.toTimedTeleport()
|
||||
.execute();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,24 +3,18 @@ package net.william278.huskhomes.command;
|
||||
import de.themoep.minedown.adventure.MineDown;
|
||||
import net.william278.desertwell.AboutMenu;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class HuskHomesCommand extends Command implements ConsoleExecutable, TabProvider {
|
||||
public class HuskHomesCommand extends Command {
|
||||
|
||||
private final String[] SUB_COMMANDS = {"about", "help", "reload", "update"};
|
||||
private static final List<String> SUB_COMMANDS = List.of("about", "help", "reload", "update");
|
||||
private final AboutMenu aboutMenu;
|
||||
|
||||
protected HuskHomesCommand(@NotNull HuskHomes implementor) {
|
||||
super("huskhomes", Permission.COMMAND_HUSKHOMES, implementor);
|
||||
super("huskhomes", List.of(), "[" + String.join("|", SUB_COMMANDS) + "]", implementor);
|
||||
this.aboutMenu = AboutMenu.create("HuskHomes")
|
||||
.withDescription("A powerful, intuitive and flexible teleportation suite")
|
||||
.withVersion(implementor.getVersion())
|
||||
@@ -47,124 +41,46 @@ public class HuskHomesCommand extends Command implements ConsoleExecutable, TabP
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
if (args.length == 0) {
|
||||
sendAboutMenu(onlineUser);
|
||||
return;
|
||||
}
|
||||
if (args.length > 2) {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", "/huskhomes [about|help|reload|update]")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
switch (args[0].toLowerCase()) {
|
||||
case "about" -> sendAboutMenu(onlineUser);
|
||||
case "help" -> {
|
||||
if (!onlineUser.hasPermission(Permission.COMMAND_HUSKHOMES_HELP.node)) {
|
||||
plugin.getLocales().getLocale("error_no_permission")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
int page = 1;
|
||||
if (args.length == 2) {
|
||||
try {
|
||||
page = Integer.parseInt(args[1]);
|
||||
} catch (NumberFormatException ignored) {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", "/huskhomes help <page>")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
}
|
||||
onlineUser.sendMessage(plugin.getCache().getCommandList(onlineUser,
|
||||
plugin.getLocales(), plugin.getCommands(), plugin.getSettings().getListItemsPerPage(), page));
|
||||
}
|
||||
case "reload" -> {
|
||||
if (!onlineUser.hasPermission(Permission.COMMAND_HUSKHOMES_RELOAD.node)) {
|
||||
plugin.getLocales().getLocale("error_no_permission")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
if (!plugin.reload()) {
|
||||
onlineUser.sendMessage(new MineDown("[Error:](#ff3300) [Failed to reload the plugin. Check console for errors.](#ff7e5e)"));
|
||||
return;
|
||||
}
|
||||
onlineUser.sendMessage(new MineDown("[HuskHomes](#00fb9a bold) �fb9a&| Reloaded config & message files."));
|
||||
}
|
||||
case "update" -> {
|
||||
if (!onlineUser.hasPermission(Permission.COMMAND_HUSKHOMES_UPDATE.node)) {
|
||||
plugin.getLocales().getLocale("error_no_permission")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
plugin.getLatestVersionIfOutdated().thenAccept(newestVersion ->
|
||||
newestVersion.ifPresentOrElse(
|
||||
newVersion -> onlineUser.sendMessage(
|
||||
new MineDown("[HuskHomes](#00fb9a bold) [| A new version of HuskHomes is available!"
|
||||
+ " (v" + newVersion + " (Running: v" + plugin.getVersion() + ")](#00fb9a)")),
|
||||
() -> onlineUser.sendMessage(
|
||||
new MineDown("[HuskHomes](#00fb9a bold) [| HuskHomes is up-to-date."
|
||||
+ " (Running: v" + plugin.getVersion() + ")](#00fb9a)"))));
|
||||
}
|
||||
case "migrate" -> plugin.getLocales().getLocale("error_console_command_only")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
default -> plugin.getLocales().getLocale("error_invalid_syntax", "/huskhomes [about|help|reload|update]")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConsoleExecute(@NotNull String[] args) {
|
||||
if (args.length == 0) {
|
||||
Arrays.stream(aboutMenu.toString().split("\n")).forEach(message ->
|
||||
plugin.log(Level.INFO, message));
|
||||
return;
|
||||
}
|
||||
switch (args[0].toLowerCase()) {
|
||||
case "about" -> Arrays.stream(aboutMenu.toString().split("\n")).forEach(message ->
|
||||
plugin.log(Level.INFO, message));
|
||||
case "help" -> {
|
||||
plugin.log(Level.INFO, "List of enabled console-executable commands:");
|
||||
plugin.getCommands()
|
||||
.stream().filter(command -> command instanceof ConsoleExecutable)
|
||||
.forEach(command -> plugin.log(Level.INFO,
|
||||
command.command +
|
||||
(command.command.length() < 16 ? " ".repeat(16 - command.command.length()) : "")
|
||||
+ " - " + command.getDescription()));
|
||||
}
|
||||
case "reload" -> {
|
||||
if (!plugin.reload()) {
|
||||
plugin.log(Level.SEVERE, "Failed to reload the plugin.");
|
||||
return;
|
||||
}
|
||||
plugin.log(Level.INFO, "Reloaded config & message files.");
|
||||
}
|
||||
case "update" -> plugin.getLatestVersionIfOutdated().thenAccept(newestVersion ->
|
||||
newestVersion.ifPresentOrElse(newVersion -> plugin.log(Level.WARNING,
|
||||
"An update is available for HuskHomes, v" + newVersion
|
||||
+ " (Running v" + plugin.getVersion() + ")"),
|
||||
() -> plugin.log(Level.INFO,
|
||||
"HuskHomes is up to date" +
|
||||
" (Running v" + plugin.getVersion() + ")")));
|
||||
}
|
||||
}
|
||||
|
||||
private void sendAboutMenu(@NotNull OnlineUser onlineUser) {
|
||||
if (!onlineUser.hasPermission(Permission.COMMAND_HUSKHOMES_ABOUT.node)) {
|
||||
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
final String action = parseStringArg(args, 0).orElse("about");
|
||||
if (SUB_COMMANDS.contains(action) && !executor.hasPermission(getPermission(action))) {
|
||||
plugin.getLocales().getLocale("error_no_permission")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
.ifPresent(executor::sendMessage);
|
||||
return;
|
||||
}
|
||||
onlineUser.sendMessage(aboutMenu.toMineDown());
|
||||
|
||||
switch (action.toLowerCase()) {
|
||||
case "about" -> executor.sendMessage(aboutMenu.toMineDown());
|
||||
case "help" -> executor.sendMessage(plugin.getCache().getCommandList(executor, plugin.getLocales(),
|
||||
plugin.getCommands(), plugin.getSettings().getListItemsPerPage(),
|
||||
parseIntArg(args, 1).orElse(1)));
|
||||
case "reload" -> {
|
||||
if (!plugin.reload()) {
|
||||
executor.sendMessage(new MineDown("[Error:](#ff3300) [Failed to reload the plugin. Check console for errors.](#ff7e5e)"));
|
||||
return;
|
||||
}
|
||||
executor.sendMessage(new MineDown("[HuskHomes](#00fb9a bold) �fb9a&| Reloaded config & message files."));
|
||||
}
|
||||
case "update" -> plugin.getLatestVersionIfOutdated()
|
||||
.thenAccept(newestVersion -> newestVersion.ifPresentOrElse(
|
||||
newVersion -> executor.sendMessage(
|
||||
new MineDown("[HuskHomes](#00fb9a bold) [| A new version of HuskHomes is available!"
|
||||
+ " (v" + newVersion + " (Running: v" + plugin.getVersion() + ")](#00fb9a)")),
|
||||
() -> executor.sendMessage(
|
||||
new MineDown("[HuskHomes](#00fb9a bold) [| HuskHomes is up-to-date."
|
||||
+ " (Running: v" + plugin.getVersion() + ")](#00fb9a)"))));
|
||||
default -> plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||
.ifPresent(executor::sendMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public final List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
|
||||
if (args.length == 0 || args.length == 1) {
|
||||
return Arrays.stream(SUB_COMMANDS)
|
||||
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
|
||||
.sorted().collect(Collectors.toList());
|
||||
public List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
|
||||
if (args.length < 2) {
|
||||
return filter(SUB_COMMANDS, args);
|
||||
}
|
||||
return Collections.emptyList();
|
||||
return List.of();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,10 +43,12 @@ public abstract class Node implements Executable {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getPermission() {
|
||||
public String getPermission(@NotNull String... child) {
|
||||
return new StringJoiner(".")
|
||||
.add(PERMISSION_PREFIX)
|
||||
.add(getName()).toString();
|
||||
.add(getName())
|
||||
.add(String.join(".", child))
|
||||
.toString();
|
||||
}
|
||||
|
||||
public boolean canPerform(@NotNull CommandUser user) {
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PrivateHomeCommand extends HomeCommand {
|
||||
|
||||
protected PrivateHomeCommand(@NotNull HuskHomes plugin) {
|
||||
super("home", List.of(), plugin);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,149 +1,24 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.teleport.Teleport;
|
||||
import net.william278.huskhomes.teleport.TimedTeleport;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.logging.Level;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PublicHomeCommand extends Command implements TabProvider, ConsoleExecutable {
|
||||
public class PublicHomeCommand extends HomeCommand {
|
||||
|
||||
protected 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().thenAcceptAsync(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;
|
||||
}
|
||||
|
||||
plugin.getCache().getPublicHomeList(onlineUser, plugin.getLocales(),
|
||||
publicHomes, plugin.getSettings().getListItemsPerPage(), 1)
|
||||
.ifPresent(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 -> teleportToNamedHome(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) {
|
||||
Teleport.builder(plugin, onlineUser)
|
||||
.setTarget(homeMatches.get(0))
|
||||
.toTimedTeleport().thenAccept(TimedTeleport::execute);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
private void teleportToNamedHome(@NotNull OnlineUser teleporter, @NotNull User owner, @NotNull String homeName) {
|
||||
final boolean otherHome = !owner.equals(teleporter);
|
||||
plugin.getDatabase()
|
||||
.getHome(owner, homeName)
|
||||
.thenAccept(homeResult -> homeResult.ifPresentOrElse(home -> {
|
||||
if (otherHome && !home.isPublic) {
|
||||
if (!teleporter.hasPermission(Permission.COMMAND_HOME_OTHER.node)) {
|
||||
plugin.getLocales().getLocale("error_no_permission")
|
||||
.ifPresent(teleporter::sendMessage);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Teleport.builder(plugin, teleporter)
|
||||
.setTarget(home)
|
||||
.toTimedTeleport()
|
||||
.thenAccept(TimedTeleport::execute);
|
||||
}, () -> {
|
||||
if (otherHome) {
|
||||
plugin.getLocales().getLocale("error_home_invalid_other", owner.getUsername(), homeName)
|
||||
.ifPresent(teleporter::sendMessage);
|
||||
} else {
|
||||
plugin.getLocales().getLocale("error_home_invalid", homeName)
|
||||
.ifPresent(teleporter::sendMessage);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConsoleExecute(@NotNull String[] args) {
|
||||
if (args.length != 2) {
|
||||
plugin.log(Level.WARNING, "Invalid syntax. Usage: publichome <player> <[owner_name].[home_name]>");
|
||||
return;
|
||||
}
|
||||
CompletableFuture.runAsync(() -> {
|
||||
final OnlineUser playerToTeleport = plugin.findOnlinePlayer(args[0]).orElse(null);
|
||||
if (playerToTeleport == null) {
|
||||
plugin.log(Level.WARNING, "Player not found: " + args[0]);
|
||||
return;
|
||||
}
|
||||
final AtomicReference<Home> matchedHome = new AtomicReference<>(null);
|
||||
RegexUtil.matchDisambiguatedHomeIdentifier(args[1]).ifPresentOrElse(
|
||||
identifier -> matchedHome.set(plugin.getDatabase().getUserDataByName(identifier.ownerName()).join()
|
||||
.flatMap(user -> plugin.getDatabase().getHome(user.user(), identifier.homeName()).join())
|
||||
.orElse(null)),
|
||||
() -> matchedHome.set(plugin.getDatabase().getPublicHomes().join()
|
||||
.stream()
|
||||
.filter(home -> home.owner.equals(playerToTeleport) && home.meta.name.equalsIgnoreCase(args[1]))
|
||||
.findFirst()
|
||||
.orElse(null)));
|
||||
|
||||
final Home home = matchedHome.get();
|
||||
if (home == null) {
|
||||
plugin.log(Level.WARNING, "Could not find public home '" + args[1] + "'");
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.log(Level.INFO, "Teleporting " + playerToTeleport.getUsername() + " to "
|
||||
+ home.getOwner().getUsername() + "." + home.getMeta().getName());
|
||||
Teleport.builder(plugin, playerToTeleport)
|
||||
.setTarget(home)
|
||||
.toTeleport()
|
||||
.thenAccept(Teleport::execute);
|
||||
});
|
||||
protected PublicHomeCommand(@NotNull HuskHomes plugin) {
|
||||
super("phome", List.of("publichome"), plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public final List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
|
||||
// Return every public home name as username.home_name from the cache
|
||||
final List<String> publicHomes = new ArrayList<>();
|
||||
plugin.getCache().getPublicHomes().forEach((ownerName, homeNames) ->
|
||||
homeNames.forEach(homeName -> publicHomes.add(ownerName + "." + homeName)));
|
||||
return args.length > 1 ? Collections.emptyList() : publicHomes
|
||||
.stream()
|
||||
.filter(publicHomeIdentifier -> publicHomeIdentifier.split(Pattern.quote("."))[1].toLowerCase()
|
||||
.startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
public List<String> suggest(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
if (args.length <= 2) {
|
||||
return filter(reduceHomeList(plugin.getCache().getHomes()), args);
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,7 +27,7 @@ public abstract class SavedPositionCommand<T extends SavedPosition> extends Comm
|
||||
|
||||
@NotNull
|
||||
public String getOtherPermission() {
|
||||
return (positionType == Home.class ? super.getPermission() + ".other" : super.getPermission());
|
||||
return (positionType == Home.class ? super.getPermission("other") : super.getPermission());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -120,7 +120,7 @@ public abstract class SavedPositionCommand<T extends SavedPosition> extends Comm
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<String> reduceHomeList(@NotNull Map<String, List<String>> cachedMap) {
|
||||
protected static List<String> reduceHomeList(@NotNull Map<String, List<String>> cachedMap) {
|
||||
final List<String> homeNames = new ArrayList<>();
|
||||
cachedMap.forEach((key, value) -> value.forEach(home -> homeNames.add(key + "." + home)));
|
||||
return homeNames;
|
||||
|
||||
Reference in new issue
Block a user