package net.william278.huskhomes.command; import net.william278.huskhomes.HuskHomes; 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 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 CommandBase implements TabCompletable, ConsoleExecutable { 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().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; } onlineUser.sendMessage(plugin.getCache().getPublicHomeList(onlineUser, plugin.getLocales(), publicHomes, plugin.getSettings().listItemsPerPage, 1)); }); 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().teleportToHomeByName(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) { if (args.length != 2) { plugin.getLoggingAdapter().log(Level.WARNING, "Invalid syntax. Usage: publichome <[owner_name].[home_name]>"); return; } CompletableFuture.runAsync(() -> { final OnlineUser playerToTeleport = plugin.findPlayer(args[0]).orElse(null); if (playerToTeleport == null) { plugin.getLoggingAdapter().log(Level.WARNING, "Player not found: " + args[0]); return; } final AtomicReference 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.uuid.equals(playerToTeleport.uuid) && home.meta.name.equalsIgnoreCase(args[1])) .findFirst() .orElse(null))); final Home home = matchedHome.get(); if (home == null) { plugin.getLoggingAdapter().log(Level.WARNING, "Could not find public home '" + args[1] + "'"); return; } plugin.getLoggingAdapter().log(Level.INFO, "Teleporting " + playerToTeleport.username + " to " + home.owner.username + "." + home.meta.name); plugin.getTeleportManager().teleport(playerToTeleport, home) .thenAccept(result -> plugin.getTeleportManager().finishTeleport(playerToTeleport, result)); }); } @Override public @NotNull List onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) { // 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 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()); } }