diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotList.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotList.java index f0581ef1..9844b32b 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotList.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotList.java @@ -89,7 +89,12 @@ public class BotList { } public ServerBot loadNewBot(String realName) { - return this.loadNewBot(realName, this.dataStorage); + try { + return this.loadNewBot(realName, this.dataStorage); + } catch (Exception e) { + LOGGER.error("Failed to load bot {}", realName, e); + return null; + } } public ServerBot loadNewBot(String realName, IPlayerDataStorage playerIO) { diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/Configs.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/Configs.java index da53cc96..056c1cbf 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/Configs.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/Configs.java @@ -41,7 +41,7 @@ public class Configs { } @SuppressWarnings("unchecked") - private static > @NotNull E register(AbstractBotConfig instance) { + private static > @NotNull E register(AbstractBotConfig instance) { configs.put(instance.getClass(), instance); return (E) instance; } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/AbstractBotConfig.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/AbstractBotConfig.java index 7ee528b8..16a6e5b7 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/AbstractBotConfig.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/AbstractBotConfig.java @@ -19,14 +19,14 @@ import static net.kyori.adventure.text.Component.text; import static net.kyori.adventure.text.event.HoverEvent.showText; import static net.kyori.adventure.text.format.NamedTextColor.AQUA; -public abstract class AbstractBotConfig> { +public abstract class AbstractBotConfig> { private final String name; - private final WrappedArgument argument; + private final WrappedArgument argument; private final Supplier creator; protected ServerBot bot; - public AbstractBotConfig(String name, ArgumentType type, Supplier creator) { + public AbstractBotConfig(String name, ArgumentType type, Supplier creator) { this.name = name; this.argument = new WrappedArgument<>(name, type); if (shouldApplySuggestions()) { @@ -39,7 +39,7 @@ public abstract class AbstractBotConfig setBot(ServerBot bot) { + public AbstractBotConfig setBot(ServerBot bot) { this.bot = bot; return this; } @@ -48,11 +48,11 @@ public abstract class AbstractBotConfig> getExtraData() { return List.of(); @@ -75,7 +75,7 @@ public abstract class AbstractBotConfig getArgument() { + public WrappedArgument getArgument() { return argument; } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/command/CommandUtils.java b/leaves-server/src/main/java/org/leavesmc/leaves/command/CommandUtils.java index 8077fb94..8c518f74 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/command/CommandUtils.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/command/CommandUtils.java @@ -58,7 +58,7 @@ public class CommandUtils { candidates.add(Candidate.of(item, damerauLevenshteinDistance(lastLower, itemLower))); } } - candidates.sort(Comparator.comparingInt(c -> c.score)); + candidates.sort(Comparator.comparingInt(Candidate::score)); List results = new ArrayList<>(candidates.size()); for (Candidate candidate : candidates) { diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/command/WrappedArgument.java b/leaves-server/src/main/java/org/leavesmc/leaves/command/WrappedArgument.java index db8e6747..e41eb5c8 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/command/WrappedArgument.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/command/WrappedArgument.java @@ -44,17 +44,14 @@ public class WrappedArgument { public RequiredArgumentBuilder compile() { RequiredArgumentBuilder builder = Commands.argument(name, type); if (asyncSuggestionProvider != null) { - builder.suggests( - (context, b) -> - asyncSuggestionProvider.getSuggestions(new CommandContext(context), b) + builder.suggests((context, b) -> + asyncSuggestionProvider.getSuggestions(new CommandContext(context), b) ); } else if (suggestionApplier != null) { - builder.suggests( - (context, b) -> { - suggestionApplier.applySuggestions(new CommandContext(context), b); - return CompletableFuture.completedFuture(b.build()); - } - ); + builder.suggests((context, b) -> { + suggestionApplier.applySuggestions(new CommandContext(context), b); + return CompletableFuture.completedFuture(b.build()); + }); } return builder; } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/BotArgument.java b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/BotArgument.java index 21bd2202..ea55e25a 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/BotArgument.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/BotArgument.java @@ -6,11 +6,8 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.network.chat.Component; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; +import org.leavesmc.leaves.bot.BotList; import org.leavesmc.leaves.bot.ServerBot; -import org.leavesmc.leaves.entity.bot.Bot; -import org.leavesmc.leaves.entity.bot.CraftBot; import org.leavesmc.leaves.command.CommandContext; import org.leavesmc.leaves.command.CustomArgumentType; @@ -25,25 +22,25 @@ public class BotArgument implements CustomArgumentType { @Override public ServerBot transform(String value) throws CommandSyntaxException { - CraftBot craftBot = (CraftBot) Bukkit.getBotManager().getBot(value); - if (craftBot == null) { + ServerBot bot = BotList.INSTANCE.getBotByName(value); + if (bot == null) { throw new CommandSyntaxException( CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument(), Component.literal("Bot with name '" + value + "' does not exist") ); } - return craftBot.getHandle(); + return bot; } @Override public CompletableFuture getSuggestions(CommandContext context, SuggestionsBuilder builder) throws CommandSyntaxException { - Collection bots = Bukkit.getBotManager().getBots(); + Collection bots = BotList.INSTANCE.bots; if (bots.isEmpty()) { return builder .suggest("", net.minecraft.network.chat.Component.literal("There are no bots in the server, create one first.")) .buildFuture(); } - bots.stream().map(Player::getName).forEach(builder::suggest); + bots.stream().map(ServerBot::getScoreboardName).forEach(builder::suggest); return builder.buildFuture(); } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/ActionCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/ActionCommand.java index ac3d142f..227fbf11 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/ActionCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/ActionCommand.java @@ -26,7 +26,7 @@ public class ActionCommand extends BotSubcommand { public static class BotArgument extends CustomArgumentNode { - protected BotArgument() { + private BotArgument() { super("bot", new org.leavesmc.leaves.command.bot.BotArgument()); children( StartCommand::new, diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/ConfigCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/ConfigCommand.java index 7247303b..b28f9cd4 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/ConfigCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/ConfigCommand.java @@ -40,7 +40,7 @@ public class ConfigCommand extends BotSubcommand { private static class BotArgument extends CustomArgumentNode { - protected BotArgument() { + private BotArgument() { super("bot", new org.leavesmc.leaves.command.bot.BotArgument()); Configs.getConfigs().stream() .map(this::configNodeCreator) @@ -77,10 +77,10 @@ public class ConfigCommand extends BotSubcommand { } } - private static class ConfigNode extends LiteralNode { - private final AbstractBotConfig config; + private static class ConfigNode extends LiteralNode { + private final AbstractBotConfig config; - private ConfigNode(@NotNull AbstractBotConfig config) { + private ConfigNode(@NotNull AbstractBotConfig config) { super(config.getName()); this.config = config; } @@ -100,7 +100,7 @@ public class ConfigCommand extends BotSubcommand { @Override protected boolean execute(@NotNull CommandContext context) throws CommandSyntaxException { ServerBot bot = BotArgument.getBot(context); - AbstractBotConfig botConfig = bot.getConfig(config); + AbstractBotConfig botConfig = bot.getConfig(config); context.getSender().sendMessage(join(spaces(), text("Bot", GRAY), asAdventure(bot.getDisplayName()).append(text("'s", GRAY)), @@ -114,7 +114,7 @@ public class ConfigCommand extends BotSubcommand { private boolean executeSet(CommandContext context) throws CommandSyntaxException { ServerBot bot = BotArgument.getBot(context); - AbstractBotConfig botConfig = bot.getConfig(config); + AbstractBotConfig botConfig = bot.getConfig(config); try { botConfig.setValue(botConfig.loadFromCommand(context)); } catch (ClassCastException e) { diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/CreateCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/CreateCommand.java index b4f51dda..e095738a 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/CreateCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/CreateCommand.java @@ -100,7 +100,7 @@ public class CreateCommand extends BotSubcommand { private static class NameArgument extends ArgumentNode { - public NameArgument() { + private NameArgument() { super("name", StringArgumentType.word()); children(SkinNameArgument::new); } @@ -113,7 +113,7 @@ public class CreateCommand extends BotSubcommand { private static class SkinNameArgument extends ArgumentNode { - public SkinNameArgument() { + private SkinNameArgument() { super("skin_name", StringArgumentType.word()); children(WorldArgument::new); } @@ -122,12 +122,11 @@ public class CreateCommand extends BotSubcommand { protected boolean execute(CommandContext context) throws CommandSyntaxException { return handleCreateCommand(context); } - } private static class WorldArgument extends ArgumentNode { - public WorldArgument() { + private WorldArgument() { super("world", DimensionArgument.dimension()); children(LocationArgument::new); } @@ -140,7 +139,7 @@ public class CreateCommand extends BotSubcommand { private static class LocationArgument extends ArgumentNode { - public LocationArgument() { + private LocationArgument() { super("location", Vec3Argument.vec3(true)); } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/ListCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/ListCommand.java index 6f444ebe..3033a84f 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/ListCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/ListCommand.java @@ -56,13 +56,13 @@ public class ListCommand extends BotSubcommand { } protected static @Nullable Component getBotListMessage(@NotNull World world) { - BotList botList = BotList.INSTANCE; - List botsInLevel = botList.bots.stream() + List botsInLevel = BotList.INSTANCE.bots.stream() .filter((bot) -> bot.getBukkitEntity().getWorld().equals(world)) .toList(); if (botsInLevel.isEmpty()) { return null; } + Component botsMsg = botsInLevel.stream() .map(Player::getDisplayName) .map(PaperAdventure::asAdventure) @@ -78,7 +78,7 @@ public class ListCommand extends BotSubcommand { private static class WorldArgument extends ArgumentNode { - protected WorldArgument() { + private WorldArgument() { super("world", DimensionArgument.dimension()); } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/LoadCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/LoadCommand.java index 00fc8d46..2ef59a4b 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/LoadCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/LoadCommand.java @@ -37,7 +37,7 @@ public class LoadCommand extends BotSubcommand { private static class BotNameArgument extends ArgumentNode { - public BotNameArgument() { + private BotNameArgument() { super("bot_name", StringArgumentType.word()); } @@ -60,7 +60,6 @@ public class LoadCommand extends BotSubcommand { text("Successfully loaded bot", NamedTextColor.GRAY), asAdventure(bot.getDisplayName()) )); - return true; } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/RemoveCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/RemoveCommand.java index a850599b..0b154961 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/RemoveCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/RemoveCommand.java @@ -43,7 +43,7 @@ public class RemoveCommand extends BotSubcommand { private static class BotArgument extends CustomArgumentNode { - public BotArgument() { + private BotArgument() { super("bot", new org.leavesmc.leaves.command.bot.BotArgument()); children(RemoveTimeArgument::new); } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/action/StartCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/action/StartCommand.java index f5db7acb..23159cba 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/action/StartCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/action/StartCommand.java @@ -63,7 +63,7 @@ public class StartCommand extends LiteralNode { private class ActionLiteralNode extends LiteralNode { private final AbstractBotAction action; - public ActionLiteralNode(@NotNull AbstractBotAction action) { + private ActionLiteralNode(@NotNull AbstractBotAction action) { super(action.getName()); this.action = action; } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/action/StopCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/action/StopCommand.java index 13aa77ea..d92493de 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/action/StopCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/command/bot/subcommands/action/StopCommand.java @@ -37,7 +37,7 @@ public class StopCommand extends LiteralNode { private static class StopIndexArgument extends ArgumentNode { - protected StopIndexArgument() { + private StopIndexArgument() { super("index", IntegerArgumentType.integer(0)); } @@ -90,7 +90,7 @@ public class StopCommand extends LiteralNode { private static class StopAll extends LiteralNode { - public StopAll() { + private StopAll() { super("all"); } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/command/leaves/subcommands/ConfigCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/command/leaves/subcommands/ConfigCommand.java index ea319e4a..8dddd120 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/command/leaves/subcommands/ConfigCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/command/leaves/subcommands/ConfigCommand.java @@ -29,9 +29,9 @@ public class ConfigCommand extends LeavesSubcommand { private static class PathArgument extends ArgumentNode { - public PathArgument() { + private PathArgument() { super("path", StringArgumentType.string()); - children(ValueArgument::new); + children(ConfigCommand.ValueArgument::new); } @Override @@ -76,63 +76,64 @@ public class ConfigCommand extends LeavesSubcommand { return verifiedConfig; } - private static class ValueArgument extends ArgumentNode { + } - public ValueArgument() { - super("value", StringArgumentType.greedyString()); + private static class ValueArgument extends ArgumentNode { + + private ValueArgument() { + super("value", StringArgumentType.greedyString()); + } + + @Override + protected CompletableFuture getSuggestions(@NotNull CommandContext context, @NotNull SuggestionsBuilder builder) { + String path = context.getArgument(PathArgument.class); + VerifiedConfig verifiedConfig = GlobalConfigManager.getVerifiedConfig(path); + if (verifiedConfig == null) { + return builder + .suggest("", net.minecraft.network.chat.Component.literal("This config path does not exist.")) + .buildFuture(); } + verifiedConfig.validator().valueSuggest().forEach(builder::suggest); + return builder.buildFuture(); + } - @Override - protected CompletableFuture getSuggestions(@NotNull CommandContext context, @NotNull SuggestionsBuilder builder) { - String path = context.getArgument(PathArgument.class); - VerifiedConfig verifiedConfig = GlobalConfigManager.getVerifiedConfig(path); - if (verifiedConfig == null) { - return builder - .suggest("", net.minecraft.network.chat.Component.literal("This config path does not exist.")) - .buildFuture(); - } - verifiedConfig.validator().valueSuggest().forEach(builder::suggest); - return builder.buildFuture(); + @Override + protected boolean execute(@NotNull CommandContext context) { + VerifiedConfig verifiedConfig = PathArgument.getVerifiedConfig(context); + String path = context.getArgument(PathArgument.class); + String value = context.getArgument(ValueArgument.class); + if (verifiedConfig == null) { + return false; } - - @Override - protected boolean execute(@NotNull CommandContext context) { - VerifiedConfig verifiedConfig = getVerifiedConfig(context); - String path = context.getArgument(PathArgument.class); - String value = context.getArgument(ValueArgument.class); - if (verifiedConfig == null) { - return false; - } - try { - verifiedConfig.set(value); - context.getSender().sendMessage(join(spaces(), - text("Config", GRAY), - text(path, AQUA), - text("changed to", GRAY), - text(verifiedConfig.getString(), AQUA) - )); - Bukkit.getOnlinePlayers() - .stream() - .filter(player -> player.hasPermission("leaves.command.config.notify") && player != context.getSender()) - .forEach( - player -> player.sendMessage(join(spaces(), - text(context.getSender().getName() + ":", GRAY), - text("Config", GRAY), - text(path, AQUA), - text("changed to", GRAY), - text(verifiedConfig.getString(), AQUA) - )) - ); - return true; - } catch (IllegalArgumentException exception) { - context.getSender().sendMessage(join(spaces(), - text("Config", GRAY), - text(path, RED), - text("modify error by", GRAY), - text(exception.getMessage(), RED) - )); - return false; - } + try { + verifiedConfig.set(value); + context.getSender().sendMessage(join(spaces(), + text("Config", GRAY), + text(path, AQUA), + text("changed to", GRAY), + text(verifiedConfig.getString(), AQUA) + )); + Bukkit.getOnlinePlayers() + .stream() + .filter(player -> player.hasPermission("leaves.command.config.notify") && player != context.getSender()) + .forEach( + player -> player.sendMessage(join(spaces(), + text(context.getSender().getName() + ":", GRAY), + text("Config", GRAY), + text(path, AQUA), + text("changed to", GRAY), + text(verifiedConfig.getString(), AQUA) + )) + ); + return true; + } catch (IllegalArgumentException exception) { + context.getSender().sendMessage(join(spaces(), + text("Config", GRAY), + text(path, RED), + text("modify error by", GRAY), + text(exception.getMessage(), RED) + )); + return false; } } }