diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotCommand.java index 1a40ec37..32b5482d 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/BotCommand.java @@ -2,7 +2,6 @@ package org.leavesmc.leaves.bot; import net.minecraft.Util; import org.leavesmc.leaves.LeavesConfig; -import org.leavesmc.leaves.bot.subcommands.BotConfigCommand; import org.leavesmc.leaves.bot.subcommands.BotCreateCommand; import org.leavesmc.leaves.bot.subcommands.BotListCommand; import org.leavesmc.leaves.bot.subcommands.BotLoadCommand; @@ -24,7 +23,7 @@ public class BotCommand extends LeavesRootCommand { commands.put(Set.of("create"), new BotCreateCommand()); commands.put(Set.of("remove"), new BotRemoveCommand()); // commands.put(Set.of("action"), new BotActionCommand()); - commands.put(Set.of("config"), new BotConfigCommand()); +// commands.put(Set.of("config"), new BotConfigCommand()); commands.put(Set.of("save"), new BotSaveCommand()); commands.put(Set.of("load"), new BotLoadCommand()); commands.put(Set.of("list"), new BotListCommand()); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java index 159a0384..faa48bf7 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java @@ -59,10 +59,10 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.leavesmc.leaves.LeavesConfig; import org.leavesmc.leaves.LeavesLogger; -import org.leavesmc.leaves.bot.agent.AbstractBotConfig; import org.leavesmc.leaves.bot.agent.Actions; import org.leavesmc.leaves.bot.agent.Configs; -import org.leavesmc.leaves.bot.agent.actions.ServerBotAction; +import org.leavesmc.leaves.bot.agent.actions.AbstractBotAction; +import org.leavesmc.leaves.bot.agent.configs.AbstractBotConfig; import org.leavesmc.leaves.entity.bot.CraftBot; import org.leavesmc.leaves.event.bot.BotActionScheduleEvent; import org.leavesmc.leaves.event.bot.BotCreateEvent; @@ -83,8 +83,8 @@ import java.util.function.Predicate; public class ServerBot extends ServerPlayer { - private final List> actions; - private final Map, AbstractBotConfig> configs; + private final List> actions; + private final Map> configs; public boolean resume = false; public BotCreateState createState; @@ -106,9 +106,9 @@ public class ServerBot extends ServerPlayer { this.gameMode = new ServerBotGameMode(this); this.actions = new ArrayList<>(); - ImmutableMap.Builder, AbstractBotConfig> configBuilder = ImmutableMap.builder(); - for (Configs config : Configs.getConfigs()) { - configBuilder.put(config, config.createConfig(this)); + ImmutableMap.Builder> configBuilder = ImmutableMap.builder(); + for (AbstractBotConfig config : Configs.getConfigs()) { + configBuilder.put(config.getName(), config.create().setBot(this)); } this.configs = configBuilder.build(); @@ -388,14 +388,14 @@ public class ServerBot extends ServerPlayer { if (!this.actions.isEmpty()) { ValueOutput.TypedOutputList actionNbt = nbt.list("actions", CompoundTag.CODEC); - for (ServerBotAction action : this.actions) { + for (AbstractBotAction action : this.actions) { actionNbt.add(action.save(new CompoundTag())); } } if (!this.configs.isEmpty()) { ValueOutput.TypedOutputList configNbt = nbt.list("configs", CompoundTag.CODEC); - for (AbstractBotConfig config : this.configs.values()) { + for (AbstractBotConfig config : this.configs.values()) { configNbt.add(config.save(new CompoundTag())); } } @@ -428,9 +428,9 @@ public class ServerBot extends ServerPlayer { if (nbt.list("actions", CompoundTag.CODEC).isPresent()) { ValueInput.TypedInputList actionNbt = nbt.list("actions", CompoundTag.CODEC).orElseThrow(); actionNbt.forEach(actionTag -> { - ServerBotAction action = Actions.getForName(actionTag.getString("actionName").orElseThrow()); + AbstractBotAction action = Actions.getForName(actionTag.getString("actionName").orElseThrow()); if (action != null) { - ServerBotAction newAction = action.create(); + AbstractBotAction newAction = action.create(); newAction.load(actionTag); this.actions.add(newAction); } @@ -440,9 +440,9 @@ public class ServerBot extends ServerPlayer { if (nbt.list("configs", CompoundTag.CODEC).isPresent()) { ValueInput.TypedInputList configNbt = nbt.list("configs", CompoundTag.CODEC).orElseThrow(); for (CompoundTag configTag : configNbt) { - Configs configKey = Configs.getConfig(configTag.getString("configName").orElseThrow()); - if (configKey != null) { - this.configs.get(configKey).load(configTag); + AbstractBotConfig config = Configs.getConfig(configTag.getString("configName").orElseThrow()); + if (config != null) { + config.load(configTag); } } } @@ -640,11 +640,11 @@ public class ServerBot extends ServerPlayer { private void runAction() { if (LeavesConfig.modify.fakeplayer.canUseAction) { this.actions.forEach(action -> action.tryTick(this)); - this.actions.removeIf(ServerBotAction::isCancelled); + this.actions.removeIf(AbstractBotAction::isCancelled); } } - public boolean addBotAction(ServerBotAction action, CommandSender sender) { + public boolean addBotAction(AbstractBotAction action, CommandSender sender) { if (!LeavesConfig.modify.fakeplayer.canUseAction) { return false; } @@ -658,7 +658,7 @@ public class ServerBot extends ServerPlayer { return true; } - public List> getBotActions() { + public List> getBotActions() { return actions; } @@ -669,11 +669,11 @@ public class ServerBot extends ServerPlayer { } @SuppressWarnings("unchecked") - public AbstractBotConfig getConfig(Configs config) { - return (AbstractBotConfig) Objects.requireNonNull(this.configs.get(config)); + public > AbstractBotConfig getConfig(@NotNull AbstractBotConfig config) { + return (AbstractBotConfig) Objects.requireNonNull(this.configs.get(config.getName())); } - public E getConfigValue(Configs config) { + public > O getConfigValue(@NotNull AbstractBotConfig config) { return this.getConfig(config).getValue(); } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/AbstractBotConfig.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/AbstractBotConfig.java deleted file mode 100644 index 4d85f5be..00000000 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/AbstractBotConfig.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.leavesmc.leaves.bot.agent; - -import net.minecraft.nbt.CompoundTag; -import org.jetbrains.annotations.NotNull; -import org.leavesmc.leaves.bot.ServerBot; -import org.leavesmc.leaves.command.CommandArgument; -import org.leavesmc.leaves.command.CommandArgumentResult; - -import java.util.List; - -public abstract class AbstractBotConfig { - - private final String name; - private final CommandArgument argument; - - protected ServerBot bot; - - public AbstractBotConfig(String name, CommandArgument argument) { - this.name = name; - this.argument = argument; - } - - public AbstractBotConfig setBot(ServerBot bot) { - this.bot = bot; - return this; - } - - public abstract E getValue(); - - public abstract void setValue(E value) throws IllegalArgumentException; - - @SuppressWarnings("unchecked") - public void setFromCommand(@NotNull CommandArgumentResult result) throws IllegalArgumentException { - if (argument == CommandArgument.EMPTY) { - throw new IllegalArgumentException("No argument for " + this.getName()); - } - try { - this.setValue((E) result.read(argument.getArgumentTypes().getFirst().getType())); - } catch (ClassCastException e) { - throw new IllegalArgumentException("Invalid argument type for " + this.getName() + ": " + e.getMessage()); - } - } - - public List getMessage() { - return List.of(this.bot.getScoreboardName() + "'s " + this.getName() + ": " + this.getValue()); - } - - public List getChangeMessage() { - return List.of(this.bot.getScoreboardName() + "'s " + this.getName() + " changed: " + this.getValue()); - } - - public String getName() { - return name; - } - - public CommandArgument getArgument() { - return argument; - } - - @NotNull - public CompoundTag save(@NotNull CompoundTag nbt) { - nbt.putString("configName", this.name); - return nbt; - } - - public abstract void load(@NotNull CompoundTag nbt); -} diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/Actions.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/Actions.java index 6b9cabc7..9142fa14 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/Actions.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/Actions.java @@ -13,8 +13,8 @@ import java.util.Set; public class Actions { - private static final Map> actionsByName = new HashMap<>(); - private static final Map, ServerBotAction> actionsByClass = new HashMap<>(); + private static final Map> actionsByName = new HashMap<>(); + private static final Map, AbstractBotAction> actionsByClass = new HashMap<>(); public static void registerAll() { register(new ServerAttackAction(), AttackAction.class); @@ -38,7 +38,7 @@ public class Actions { register(new ServerSwapAction(), SwapAction.class); } - public static boolean register(@NotNull ServerBotAction action, Class> type) { + public static boolean register(@NotNull AbstractBotAction action, Class> type) { if (!actionsByName.containsKey(action.getName())) { actionsByName.put(action.getName(), action); actionsByClass.put(type, action); @@ -54,7 +54,7 @@ public class Actions { @NotNull @Contract(pure = true) - public static Collection> getAll() { + public static Collection> getAll() { return actionsByName.values(); } @@ -64,12 +64,12 @@ public class Actions { } @Nullable - public static ServerBotAction getForName(String name) { + public static AbstractBotAction getForName(String name) { return actionsByName.get(name); } @Nullable - public static ServerBotAction getForClass(@NotNull Class type) { + public static AbstractBotAction getForClass(@NotNull Class type) { return actionsByClass.get(type); } } 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 8608a262..da53cc96 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 @@ -3,7 +3,7 @@ package org.leavesmc.leaves.bot.agent; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.leavesmc.leaves.bot.ServerBot; +import org.leavesmc.leaves.bot.agent.configs.AbstractBotConfig; import org.leavesmc.leaves.bot.agent.configs.AlwaysSendDataConfig; import org.leavesmc.leaves.bot.agent.configs.LocatorBarConfig; import org.leavesmc.leaves.bot.agent.configs.SimulationDistanceConfig; @@ -14,57 +14,35 @@ import org.leavesmc.leaves.bot.agent.configs.TickTypeConfig; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.function.Supplier; -@SuppressWarnings("unused") -public class Configs { +@SuppressWarnings({"unused"}) +public class Configs { + private static final Map, AbstractBotConfig> configs = new HashMap<>(); - private static final Map> configs = new HashMap<>(); - - public static final Configs SKIP_SLEEP = register(SkipSleepConfig.class, SkipSleepConfig::new); - public static final Configs ALWAYS_SEND_DATA = register(AlwaysSendDataConfig.class, AlwaysSendDataConfig::new); - public static final Configs SPAWN_PHANTOM = register(SpawnPhantomConfig.class, SpawnPhantomConfig::new); - public static final Configs SIMULATION_DISTANCE = register(SimulationDistanceConfig.class, SimulationDistanceConfig::new); - public static final Configs TICK_TYPE = register(TickTypeConfig.class, TickTypeConfig::new); - public static final Configs ENABLE_LOCATOR_BAR = register(LocatorBarConfig.class, LocatorBarConfig::new); - - private final Class> configClass; - private final Supplier> configCreator; - - private Configs(Class> configClass, Supplier> configCreator) { - this.configClass = configClass; - this.configCreator = configCreator; - } - - public Class> getConfigClass() { - return configClass; - } - - public AbstractBotConfig createConfig(ServerBot bot) { - return configCreator.get().setBot(bot); - } + public static final SkipSleepConfig SKIP_SLEEP = register(new SkipSleepConfig()); + public static final AlwaysSendDataConfig ALWAYS_SEND_DATA = register(new AlwaysSendDataConfig()); + public static final SpawnPhantomConfig SPAWN_PHANTOM = register(new SpawnPhantomConfig()); + public static final SimulationDistanceConfig SIMULATION_DISTANCE = register(new SimulationDistanceConfig()); + public static final TickTypeConfig TICK_TYPE = register(new TickTypeConfig()); + public static final LocatorBarConfig ENABLE_LOCATOR_BAR = register(new LocatorBarConfig()); @Nullable - public static Configs getConfig(String name) { - return configs.get(name); + public static AbstractBotConfig getConfig(String name) { + return configs.values().stream() + .filter(config -> config.getName().equals(name)) + .findFirst() + .orElse(null); } @NotNull @Contract(pure = true) - public static Collection> getConfigs() { + public static Collection> getConfigs() { return configs.values(); } - @NotNull - @Contract(pure = true) - public static Collection getConfigNames() { - return configs.keySet(); - } - - @NotNull - private static Configs register(Class> configClass, Supplier> configCreator) { - Configs config = new Configs<>(configClass, configCreator); - configs.put(config.createConfig(null).getName(), config); - return config; + @SuppressWarnings("unchecked") + 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/actions/ServerBotAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractBotAction.java similarity index 98% rename from leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerBotAction.java rename to leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractBotAction.java index 73e77c56..86286b94 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerBotAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractBotAction.java @@ -23,7 +23,7 @@ import java.util.function.Consumer; import java.util.function.Supplier; @SuppressWarnings("unchecked") -public abstract class ServerBotAction> { +public abstract class AbstractBotAction> { private final String name; private final Map>>> arguments; @@ -43,7 +43,7 @@ public abstract class ServerBotAction> { private Consumer onSuccess; private Consumer onStop; - public ServerBotAction(String name, Supplier creator) { + public AbstractBotAction(String name, Supplier creator) { this.name = name; this.uuid = UUID.randomUUID(); this.creator = creator; diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractStateBotAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractStateBotAction.java new file mode 100644 index 00000000..6a6007fc --- /dev/null +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractStateBotAction.java @@ -0,0 +1,11 @@ +package org.leavesmc.leaves.bot.agent.actions; + +import java.util.function.Supplier; + +public abstract class AbstractStateBotAction> extends AbstractBotAction { + + public AbstractStateBotAction(String name, Supplier creator) { + super(name, creator); + this.setDoNumber(-1); + } +} diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerTimerBotAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractTimerBotAction.java similarity index 89% rename from leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerTimerBotAction.java rename to leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractTimerBotAction.java index 6cea5c42..9685e47f 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerTimerBotAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractTimerBotAction.java @@ -9,9 +9,9 @@ import java.util.function.Supplier; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; import static org.leavesmc.leaves.neo_command.ArgumentNode.ArgumentSuggestions.strings; -public abstract class ServerTimerBotAction> extends ServerBotAction { +public abstract class AbstractTimerBotAction> extends AbstractBotAction { - public ServerTimerBotAction(String name, Supplier creator) { + public AbstractTimerBotAction(String name, Supplier creator) { super(name, creator); this.addArgument("delay", integer(0)) .suggests(strings("0", "5", "10", "20")) diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseBotAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractUseBotAction.java similarity index 95% rename from leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseBotAction.java rename to leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractUseBotAction.java index 67657ec1..1a367cfb 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseBotAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractUseBotAction.java @@ -12,12 +12,12 @@ import java.util.function.Supplier; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; -public abstract class ServerUseBotAction> extends ServerTimerBotAction { +public abstract class AbstractUseBotAction> extends AbstractTimerBotAction { private int useTickTimeout = -1; private int alreadyUsedTick = 0; private int useItemRemainingTicks = 0; - public ServerUseBotAction(String name, Supplier supplier) { + public AbstractUseBotAction(String name, Supplier supplier) { super(name, supplier); this.addArgument("use_timeout", integer(-1)) .suggests((context, builder) -> { diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerAttackAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerAttackAction.java index c062b3e0..6a3add3b 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerAttackAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerAttackAction.java @@ -5,7 +5,7 @@ import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftAttackAction; -public class ServerAttackAction extends ServerTimerBotAction { +public class ServerAttackAction extends AbstractTimerBotAction { public ServerAttackAction() { super("attack", ServerAttackAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerBreakBlockAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerBreakBlockAction.java index 470c62fb..36a2e621 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerBreakBlockAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerBreakBlockAction.java @@ -13,7 +13,7 @@ import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftBreakBlockAction; -public class ServerBreakBlockAction extends ServerTimerBotAction { +public class ServerBreakBlockAction extends AbstractTimerBotAction { public ServerBreakBlockAction() { super("break", ServerBreakBlockAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerDropAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerDropAction.java index a2818e7e..68e7e817 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerDropAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerDropAction.java @@ -4,7 +4,7 @@ import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftDropAction; -public class ServerDropAction extends ServerTimerBotAction { +public class ServerDropAction extends AbstractTimerBotAction { public ServerDropAction() { super("drop", ServerDropAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerFishAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerFishAction.java index 015c8777..32cc7c14 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerFishAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerFishAction.java @@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftFishAction; -public class ServerFishAction extends ServerTimerBotAction { +public class ServerFishAction extends AbstractTimerBotAction { public ServerFishAction() { super("fish", ServerFishAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerJumpAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerJumpAction.java index 1930c1a7..f7571a79 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerJumpAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerJumpAction.java @@ -4,7 +4,7 @@ import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftJumpAction; -public class ServerJumpAction extends ServerTimerBotAction { +public class ServerJumpAction extends AbstractTimerBotAction { public ServerJumpAction() { super("jump", ServerJumpAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerLookAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerLookAction.java index 539a0dfb..c65d2199 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerLookAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerLookAction.java @@ -16,7 +16,7 @@ import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftLookAction; import org.leavesmc.leaves.neo_command.CommandContext; -public class ServerLookAction extends ServerBotAction { +public class ServerLookAction extends AbstractBotAction { private static final Vector ZERO_VECTOR = new Vector(0, 0, 0); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerMountAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerMountAction.java index 464bb761..39da096b 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerMountAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerMountAction.java @@ -11,7 +11,7 @@ import org.leavesmc.leaves.entity.bot.actions.CraftMountAction; import java.util.Comparator; import java.util.List; -public class ServerMountAction extends ServerBotAction { +public class ServerMountAction extends AbstractBotAction { public ServerMountAction() { super("mount", ServerMountAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerMoveAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerMoveAction.java index 3dc723bc..6ed01137 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerMoveAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerMoveAction.java @@ -15,7 +15,7 @@ import java.util.Map; import static java.util.stream.Collectors.toMap; import static org.leavesmc.leaves.neo_command.ArgumentNode.ArgumentSuggestions.strings; -public class ServerMoveAction extends ServerStateBotAction { +public class ServerMoveAction extends AbstractStateBotAction { private static final Map NAME_TO_DIRECTION = Arrays.stream(MoveDirection.values()).collect(toMap( it -> it.name, it -> it diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerRotationAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerRotationAction.java index 03c0e8d0..7218a740 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerRotationAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerRotationAction.java @@ -11,7 +11,7 @@ import org.leavesmc.leaves.neo_command.CommandContext; import java.text.DecimalFormat; -public class ServerRotationAction extends ServerBotAction { +public class ServerRotationAction extends AbstractBotAction { private static final DecimalFormat DF = new DecimalFormat("0.00"); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerSneakAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerSneakAction.java index 8476669a..7c6ac393 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerSneakAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerSneakAction.java @@ -5,7 +5,7 @@ import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftSneakAction; import org.leavesmc.leaves.event.bot.BotActionStopEvent; -public class ServerSneakAction extends ServerStateBotAction { +public class ServerSneakAction extends AbstractStateBotAction { public ServerSneakAction() { super("sneak", ServerSneakAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerStateBotAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerStateBotAction.java deleted file mode 100644 index c87f1ffc..00000000 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerStateBotAction.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.leavesmc.leaves.bot.agent.actions; - -import java.util.function.Supplier; - -public abstract class ServerStateBotAction> extends ServerBotAction { - - public ServerStateBotAction(String name, Supplier creator) { - super(name, creator); - this.setDoNumber(-1); - } -} diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerSwapAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerSwapAction.java index 9f6e7efc..12d737e3 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerSwapAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerSwapAction.java @@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftSwapAction; -public class ServerSwapAction extends ServerBotAction { +public class ServerSwapAction extends AbstractBotAction { public ServerSwapAction() { super("swap", ServerSwapAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerSwimAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerSwimAction.java index fce55dc5..df5b0bf5 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerSwimAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerSwimAction.java @@ -5,7 +5,7 @@ import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftSwimAction; -public class ServerSwimAction extends ServerStateBotAction { +public class ServerSwimAction extends AbstractStateBotAction { public ServerSwimAction() { super("swim", ServerSwimAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAction.java index 0172692f..ce5884b8 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAction.java @@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftUseItemAction; -public class ServerUseItemAction extends ServerUseBotAction { +public class ServerUseItemAction extends AbstractUseBotAction { public ServerUseItemAction() { super("use", ServerUseItemAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAutoAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAutoAction.java index ce979217..bfb5f537 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAutoAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAutoAction.java @@ -17,7 +17,7 @@ import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemAction.useItem; import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemOnAction.useItemOn; import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemToAction.useItemTo; -public class ServerUseItemAutoAction extends ServerUseBotAction { +public class ServerUseItemAutoAction extends AbstractUseBotAction { public ServerUseItemAutoAction() { super("use_auto", ServerUseItemAutoAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemOffhandAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemOffhandAction.java index 4983edb7..b05d8ab1 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemOffhandAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemOffhandAction.java @@ -7,7 +7,7 @@ import org.leavesmc.leaves.entity.bot.actions.CraftUseItemOffhandAction; import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemAction.useItem; -public class ServerUseItemOffhandAction extends ServerUseBotAction { +public class ServerUseItemOffhandAction extends AbstractUseBotAction { public ServerUseItemOffhandAction() { super("use_offhand", ServerUseItemOffhandAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemOnAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemOnAction.java index bec8358f..ee23b7a6 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemOnAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemOnAction.java @@ -8,7 +8,7 @@ import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftUseItemOnAction; -public class ServerUseItemOnAction extends ServerUseBotAction { +public class ServerUseItemOnAction extends AbstractUseBotAction { public ServerUseItemOnAction() { super("use_on", ServerUseItemOnAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemOnOffhandAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemOnOffhandAction.java index 09f69565..ac06e27c 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemOnOffhandAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemOnOffhandAction.java @@ -8,7 +8,7 @@ import org.leavesmc.leaves.entity.bot.actions.CraftUseItemOnOffhandAction; import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemOnAction.useItemOn; -public class ServerUseItemOnOffhandAction extends ServerUseBotAction { +public class ServerUseItemOnOffhandAction extends AbstractUseBotAction { public ServerUseItemOnOffhandAction() { super("use_on_offhand", ServerUseItemOnOffhandAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemToAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemToAction.java index 5f294055..9d985c0c 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemToAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemToAction.java @@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftUseItemToAction; -public class ServerUseItemToAction extends ServerUseBotAction { +public class ServerUseItemToAction extends AbstractUseBotAction { public ServerUseItemToAction() { super("use_to", ServerUseItemToAction::new); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemToOffhandAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemToOffhandAction.java index 733246df..6ce3aec6 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemToOffhandAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemToOffhandAction.java @@ -8,7 +8,7 @@ import org.leavesmc.leaves.entity.bot.actions.CraftUseItemToOffhandAction; import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemToAction.useItemTo; -public class ServerUseItemToOffhandAction extends ServerUseBotAction { +public class ServerUseItemToOffhandAction extends AbstractUseBotAction { public ServerUseItemToOffhandAction() { super("use_to_offhand", ServerUseItemToOffhandAction::new); 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 new file mode 100644 index 00000000..81d05050 --- /dev/null +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/AbstractBotConfig.java @@ -0,0 +1,102 @@ +package org.leavesmc.leaves.bot.agent.configs; + +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.kyori.adventure.text.Component; +import net.minecraft.nbt.CompoundTag; +import org.apache.commons.lang3.tuple.Pair; +import org.jetbrains.annotations.NotNull; +import org.leavesmc.leaves.bot.ServerBot; +import org.leavesmc.leaves.neo_command.CommandContext; +import org.leavesmc.leaves.neo_command.WrappedArgument; + +import java.lang.reflect.Method; +import java.util.List; +import java.util.function.Supplier; + +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> { + private final String name; + private final WrappedArgument argument; + private final Supplier creator; + + protected ServerBot bot; + + public AbstractBotConfig(String name, ArgumentType type, Supplier creator) { + this.name = name; + this.argument = new WrappedArgument<>(name, type); + if (shouldApplySuggestions()) { + this.argument.suggests(this::applySuggestions); + } + this.creator = creator; + } + + @SuppressWarnings("RedundantThrows") + public void applySuggestions(final CommandContext context, final SuggestionsBuilder builder) throws CommandSyntaxException { + } + + public AbstractBotConfig setBot(ServerBot bot) { + this.bot = bot; + return this; + } + + public E create() { + return creator.get(); + } + + public abstract O getValue(); + + public abstract void setValue(O value) throws CommandSyntaxException; + + public abstract O loadFromCommand(@NotNull CommandContext context) throws CommandSyntaxException; + + public List> getExtraData() { + return List.of(); + } + + public String getName() { + return name; + } + + public Component getNameComponent() { + Component result = text(getName(), AQUA); + if (!getExtraData().isEmpty()) { + result = result.hoverEvent(showText( + getExtraData().stream() + .map(pair -> text(pair.getKey() + "=" + pair.getValue())) + .reduce((a, b) -> a.append(text(", ")).append(b)) + .orElseGet(() -> text("")) + )); + } + return result; + } + + public WrappedArgument getArgument() { + return argument; + } + + public ServerBot getBot() { + return bot; + } + + @NotNull + public CompoundTag save(@NotNull CompoundTag nbt) { + nbt.putString("configName", this.name); + return nbt; + } + + public abstract void load(@NotNull CompoundTag nbt); + + private boolean shouldApplySuggestions() { + for (Method method : getClass().getDeclaredMethods()) { + if (method.getName().equals("applySuggestions")) { + return method.getDeclaringClass() != AbstractBotConfig.class; + } + } + return false; + } +} diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/AlwaysSendDataConfig.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/AlwaysSendDataConfig.java index 8ef871cc..b82fec5d 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/AlwaysSendDataConfig.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/AlwaysSendDataConfig.java @@ -1,22 +1,16 @@ package org.leavesmc.leaves.bot.agent.configs; +import com.mojang.brigadier.arguments.BoolArgumentType; import net.minecraft.nbt.CompoundTag; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.LeavesConfig; -import org.leavesmc.leaves.bot.agent.AbstractBotConfig; -import org.leavesmc.leaves.command.CommandArgument; -import org.leavesmc.leaves.command.CommandArgumentType; - -import java.util.List; - -public class AlwaysSendDataConfig extends AbstractBotConfig { - - public static final String NAME = "always_send_data"; +import org.leavesmc.leaves.neo_command.CommandContext; +public class AlwaysSendDataConfig extends AbstractBotConfig { private boolean value; public AlwaysSendDataConfig() { - super(NAME, CommandArgument.of(CommandArgumentType.BOOLEAN).setSuggestion(0, List.of("true", "false"))); + super("always_send_data", BoolArgumentType.bool(), AlwaysSendDataConfig::new); this.value = LeavesConfig.modify.fakeplayer.inGame.canSendDataAlways; } @@ -30,16 +24,21 @@ public class AlwaysSendDataConfig extends AbstractBotConfig { this.value = value; } + @Override + public Boolean loadFromCommand(@NotNull CommandContext context) { + return context.getBoolean(getName()); + } + @Override @NotNull public CompoundTag save(@NotNull CompoundTag nbt) { super.save(nbt); - nbt.putBoolean(NAME, this.getValue()); + nbt.putBoolean(getName(), this.getValue()); return nbt; } @Override public void load(@NotNull CompoundTag nbt) { - this.setValue(nbt.getBooleanOr(NAME, LeavesConfig.modify.fakeplayer.inGame.canSendDataAlways)); + this.setValue(nbt.getBooleanOr(getName(), LeavesConfig.modify.fakeplayer.inGame.canSendDataAlways)); } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/LocatorBarConfig.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/LocatorBarConfig.java index 1b0a02f0..ee3e119a 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/LocatorBarConfig.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/LocatorBarConfig.java @@ -1,23 +1,17 @@ package org.leavesmc.leaves.bot.agent.configs; +import com.mojang.brigadier.arguments.BoolArgumentType; import net.minecraft.nbt.CompoundTag; import net.minecraft.server.waypoints.ServerWaypointManager; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.LeavesConfig; -import org.leavesmc.leaves.bot.agent.AbstractBotConfig; -import org.leavesmc.leaves.command.CommandArgument; -import org.leavesmc.leaves.command.CommandArgumentType; - -import java.util.List; - -public class LocatorBarConfig extends AbstractBotConfig { - - public static final String NAME = "enable_locator_bar"; +import org.leavesmc.leaves.neo_command.CommandContext; +public class LocatorBarConfig extends AbstractBotConfig { private boolean value; public LocatorBarConfig() { - super(NAME, CommandArgument.of(CommandArgumentType.BOOLEAN).setSuggestion(0, List.of("true", "false"))); + super("enable_locator_bar", BoolArgumentType.bool(), LocatorBarConfig::new); this.value = LeavesConfig.modify.fakeplayer.inGame.enableLocatorBar; } @@ -27,7 +21,7 @@ public class LocatorBarConfig extends AbstractBotConfig { } @Override - public void setValue(Boolean value) throws IllegalArgumentException { + public void setValue(@NotNull Boolean value) throws IllegalArgumentException { this.value = value; ServerWaypointManager manager = this.bot.level().getWaypointManager(); if (value) { @@ -37,15 +31,20 @@ public class LocatorBarConfig extends AbstractBotConfig { } } + @Override + public Boolean loadFromCommand(@NotNull CommandContext context) { + return context.getBoolean(getName()); + } + @Override public @NotNull CompoundTag save(@NotNull CompoundTag nbt) { super.save(nbt); - nbt.putBoolean(NAME, this.getValue()); + nbt.putBoolean(getName(), this.getValue()); return nbt; } @Override public void load(@NotNull CompoundTag nbt) { - this.setValue(nbt.getBooleanOr(NAME, LeavesConfig.modify.fakeplayer.inGame.enableLocatorBar)); + this.setValue(nbt.getBooleanOr(getName(), LeavesConfig.modify.fakeplayer.inGame.enableLocatorBar)); } } \ No newline at end of file diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/SimulationDistanceConfig.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/SimulationDistanceConfig.java index 56751f72..1c6b832f 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/SimulationDistanceConfig.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/SimulationDistanceConfig.java @@ -1,21 +1,27 @@ package org.leavesmc.leaves.bot.agent.configs; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.nbt.CompoundTag; -import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.LeavesConfig; -import org.leavesmc.leaves.bot.agent.AbstractBotConfig; -import org.leavesmc.leaves.command.CommandArgument; -import org.leavesmc.leaves.command.CommandArgumentType; +import org.leavesmc.leaves.neo_command.CommandContext; -import java.util.List; +import static net.minecraft.network.chat.Component.literal; -public class SimulationDistanceConfig extends AbstractBotConfig { - - public static final String NAME = "simulation_distance"; +public class SimulationDistanceConfig extends AbstractBotConfig { public SimulationDistanceConfig() { - super(NAME, CommandArgument.of(CommandArgumentType.INTEGER).setSuggestion(0, Pair.of(List.of("2", "10"), ""))); + super("simulation_distance", IntegerArgumentType.integer(2, 32), SimulationDistanceConfig::new); + } + + @Override + public void applySuggestions(CommandContext context, @NotNull SuggestionsBuilder builder) { + builder.suggest("2", literal("Minimum simulation distance")); + builder.suggest("8"); + builder.suggest("12"); + builder.suggest("16"); + builder.suggest("32", literal("Maximum simulation distance")); } @Override @@ -25,22 +31,24 @@ public class SimulationDistanceConfig extends AbstractBotConfig { @Override public void setValue(Integer value) { - if (value < 2 || value > 32) { - throw new IllegalArgumentException("simulation_distance must be a number between 2 and 32, got: " + value); - } this.bot.getBukkitEntity().setSimulationDistance(value); } + @Override + public Integer loadFromCommand(@NotNull CommandContext context) { + return context.getInteger(getName()); + } + @Override @NotNull public CompoundTag save(@NotNull CompoundTag nbt) { super.save(nbt); - nbt.putInt(NAME, this.getValue()); + nbt.putInt(getName(), this.getValue()); return nbt; } @Override public void load(@NotNull CompoundTag nbt) { - this.setValue(nbt.getIntOr(NAME, LeavesConfig.modify.fakeplayer.inGame.getSimulationDistance(this.bot))); + this.setValue(nbt.getIntOr(getName(), LeavesConfig.modify.fakeplayer.inGame.getSimulationDistance(this.bot))); } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/SkipSleepConfig.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/SkipSleepConfig.java index cf4b0610..14e297fd 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/SkipSleepConfig.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/SkipSleepConfig.java @@ -1,20 +1,15 @@ package org.leavesmc.leaves.bot.agent.configs; +import com.mojang.brigadier.arguments.BoolArgumentType; import net.minecraft.nbt.CompoundTag; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.LeavesConfig; -import org.leavesmc.leaves.bot.agent.AbstractBotConfig; -import org.leavesmc.leaves.command.CommandArgument; -import org.leavesmc.leaves.command.CommandArgumentType; +import org.leavesmc.leaves.neo_command.CommandContext; -import java.util.List; - -public class SkipSleepConfig extends AbstractBotConfig { - - public static final String NAME = "skip_sleep"; +public class SkipSleepConfig extends AbstractBotConfig { public SkipSleepConfig() { - super(NAME, CommandArgument.of(CommandArgumentType.BOOLEAN).setSuggestion(0, List.of("true", "false"))); + super("skip_sleep", BoolArgumentType.bool(), SkipSleepConfig::new); } @Override @@ -27,15 +22,20 @@ public class SkipSleepConfig extends AbstractBotConfig { bot.fauxSleeping = value; } + @Override + public Boolean loadFromCommand(@NotNull CommandContext context) { + return context.getBoolean(getName()); + } + @Override public @NotNull CompoundTag save(@NotNull CompoundTag nbt) { super.save(nbt); - nbt.putBoolean(NAME, this.getValue()); + nbt.putBoolean(getName(), this.getValue()); return nbt; } @Override public void load(@NotNull CompoundTag nbt) { - this.setValue(nbt.getBooleanOr(NAME, LeavesConfig.modify.fakeplayer.inGame.canSkipSleep)); + this.setValue(nbt.getBooleanOr(getName(), LeavesConfig.modify.fakeplayer.inGame.canSkipSleep)); } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/SpawnPhantomConfig.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/SpawnPhantomConfig.java index 13be5fee..6b5dbdde 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/SpawnPhantomConfig.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/SpawnPhantomConfig.java @@ -1,22 +1,19 @@ package org.leavesmc.leaves.bot.agent.configs; +import com.mojang.brigadier.arguments.BoolArgumentType; import net.minecraft.nbt.CompoundTag; +import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.LeavesConfig; -import org.leavesmc.leaves.bot.agent.AbstractBotConfig; -import org.leavesmc.leaves.command.CommandArgument; -import org.leavesmc.leaves.command.CommandArgumentType; +import org.leavesmc.leaves.neo_command.CommandContext; import java.util.List; -public class SpawnPhantomConfig extends AbstractBotConfig { - - public static final String NAME = "spawn_phantom"; - +public class SpawnPhantomConfig extends AbstractBotConfig { private boolean value; public SpawnPhantomConfig() { - super(NAME, CommandArgument.of(CommandArgumentType.BOOLEAN).setSuggestion(0, List.of("true", "false"))); + super("spawn_phantom", BoolArgumentType.bool(), SpawnPhantomConfig::new); this.value = LeavesConfig.modify.fakeplayer.inGame.canSpawnPhantom; } @@ -31,22 +28,24 @@ public class SpawnPhantomConfig extends AbstractBotConfig { } @Override - public List getMessage() { - return List.of( - bot.getScoreboardName() + "'s spawn_phantom: " + this.getValue(), - bot.getScoreboardName() + "'s not_sleeping_ticks: " + bot.notSleepTicks - ); + public List> getExtraData() { + return List.of(Pair.of("not_sleeping_ticks", String.valueOf(bot.notSleepTicks))); + } + + @Override + public Boolean loadFromCommand(@NotNull CommandContext context) { + return context.getBoolean(getName()); } @Override public @NotNull CompoundTag save(@NotNull CompoundTag nbt) { super.save(nbt); - nbt.putBoolean(NAME, this.getValue()); + nbt.putBoolean(getName(), this.getValue()); return nbt; } @Override public void load(@NotNull CompoundTag nbt) { - this.setValue(nbt.getBooleanOr(NAME, LeavesConfig.modify.fakeplayer.inGame.canSpawnPhantom)); + this.setValue(nbt.getBooleanOr(getName(), LeavesConfig.modify.fakeplayer.inGame.canSpawnPhantom)); } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/TickTypeConfig.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/TickTypeConfig.java index 0615a439..961f6563 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/TickTypeConfig.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/configs/TickTypeConfig.java @@ -1,27 +1,38 @@ package org.leavesmc.leaves.bot.agent.configs; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.nbt.CompoundTag; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.LeavesConfig; import org.leavesmc.leaves.bot.ServerBot; -import org.leavesmc.leaves.bot.agent.AbstractBotConfig; -import org.leavesmc.leaves.command.CommandArgument; -import org.leavesmc.leaves.command.CommandArgumentType; - -import java.util.List; - -public class TickTypeConfig extends AbstractBotConfig { - - private static final String NAME = "tick_type"; - private static final CommandArgumentType TICK_TYPE_ARGUMENT = CommandArgumentType.ofEnum(ServerBot.TickType.class); +import org.leavesmc.leaves.neo_command.CommandContext; +public class TickTypeConfig extends AbstractBotConfig { private ServerBot.TickType value; public TickTypeConfig() { - super(NAME, CommandArgument.of(TICK_TYPE_ARGUMENT).setSuggestion(0, List.of("network", "entity_list"))); + super("tick_type", StringArgumentType.word(), TickTypeConfig::new); this.value = LeavesConfig.modify.fakeplayer.inGame.tickType; } + @Override + public void applySuggestions(CommandContext context, @NotNull SuggestionsBuilder builder) { + builder.suggest("network"); + builder.suggest("entity_list"); + } + + @Override + public ServerBot.TickType loadFromCommand(@NotNull CommandContext context) throws CommandSyntaxException { + String raw = context.getString(getName()); + return switch (raw) { + case "network" -> ServerBot.TickType.NETWORK; + case "entity_list" -> ServerBot.TickType.ENTITY_LIST; + default -> throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().create(); + }; + } + @Override public ServerBot.TickType getValue() { return value; @@ -36,12 +47,17 @@ public class TickTypeConfig extends AbstractBotConfig { @NotNull public CompoundTag save(@NotNull CompoundTag nbt) { super.save(nbt); - nbt.putString(NAME, this.getValue().toString()); + nbt.putString(getName(), this.getValue().toString()); return nbt; } @Override public void load(@NotNull CompoundTag nbt) { - this.setValue(TICK_TYPE_ARGUMENT.parse(nbt.getStringOr(NAME, LeavesConfig.modify.fakeplayer.inGame.tickType.name()))); + String raw = nbt.getStringOr(getName(), LeavesConfig.modify.fakeplayer.inGame.tickType.name()); + this.setValue(switch (raw) { + case "network" -> ServerBot.TickType.NETWORK; + case "entity_list" -> ServerBot.TickType.ENTITY_LIST; + default -> throw new IllegalStateException("Unexpected bot tick type value: " + raw); + }); } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/subcommands/BotConfigCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/subcommands/BotConfigCommand.java index 883a47bf..e066fe4a 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/subcommands/BotConfigCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/subcommands/BotConfigCommand.java @@ -1,110 +1,110 @@ -package org.leavesmc.leaves.bot.subcommands; - -import net.kyori.adventure.text.format.NamedTextColor; -import net.minecraft.network.chat.Component; -import org.apache.commons.lang3.tuple.Pair; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.command.CommandSender; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.leavesmc.leaves.LeavesConfig; -import org.leavesmc.leaves.bot.BotList; -import org.leavesmc.leaves.bot.ServerBot; -import org.leavesmc.leaves.bot.agent.AbstractBotConfig; -import org.leavesmc.leaves.bot.agent.Configs; -import org.leavesmc.leaves.command.CommandArgumentResult; -import org.leavesmc.leaves.command.LeavesSubcommand; -import org.leavesmc.leaves.command.LeavesSuggestionBuilder; -import org.leavesmc.leaves.event.bot.BotConfigModifyEvent; - -import java.util.Arrays; -import java.util.List; -import java.util.Objects; - -import static net.kyori.adventure.text.Component.text; - -public class BotConfigCommand implements LeavesSubcommand { - - @Override - public void execute(CommandSender sender, String subCommand, String[] args) { - if (args.length < 2) { - sender.sendMessage(text("Use /bot config to modify fakeplayer's config", NamedTextColor.RED)); - return; - } - - ServerBot bot = BotList.INSTANCE.getBotByName(args[0]); - if (bot == null) { - sender.sendMessage(text("This fakeplayer is not in server", NamedTextColor.RED)); - return; - } - - if (!Configs.getConfigNames().contains(args[1])) { - sender.sendMessage(text("This config is not accept", NamedTextColor.RED)); - return; - } - - AbstractBotConfig config = bot.getConfig(Objects.requireNonNull(Configs.getConfig(args[1]))); - if (args.length < 3) { - config.getMessage().forEach(sender::sendMessage); - } else { - String[] realArgs = Arrays.copyOfRange(args, 2, args.length); - - BotConfigModifyEvent event = new BotConfigModifyEvent(bot.getBukkitEntity(), config.getName(), realArgs, sender); - Bukkit.getPluginManager().callEvent(event); - - if (event.isCancelled()) { - return; - } - CommandArgumentResult result = config.getArgument().parse(0, realArgs); - - try { - config.setFromCommand(result); - config.getChangeMessage().forEach(sender::sendMessage); - } catch (IllegalArgumentException e) { - sender.sendMessage(text(e.getMessage(), NamedTextColor.RED)); - } - } - } - - - @Override - public void suggest(@NotNull CommandSender sender, @NotNull String alias, @NotNull String @NotNull [] args, @Nullable Location location, LeavesSuggestionBuilder builder) throws IllegalArgumentException { - BotList botList = BotList.INSTANCE; - ServerBot serverBot = null; - - if (args.length > 1 && (serverBot = botList.getBotByName(args[0])) == null) { - builder.suggest("<" + args[0] + " not found>"); - return; - } - - switch (args.length) { - case 0, 1 -> botList.bots.forEach(bot -> builder.suggest(bot.getName().getString())); - case 2 -> Configs.getConfigNames().forEach(builder::suggest); - case 3, 4 -> { - Configs config = Configs.getConfig(args[1]); - if (config == null) { - return; - } - AbstractBotConfig botConfig = serverBot.getConfig(config); - Pair, String> results = botConfig.getArgument().suggestion(args.length - 3, sender, args[args.length - 1]); - if (results == null || results.getLeft() == null) { - return; - } - - for (String s : results.getLeft()) { - if (results.getRight() != null) { - builder.suggest(s, Component.literal(results.getRight())); - } else { - builder.suggest(s); - } - } - } - } - } - - @Override - public boolean isEnabled() { - return LeavesConfig.modify.fakeplayer.canModifyConfig; - } -} +//package org.leavesmc.leaves.bot.subcommands; +// +//import net.kyori.adventure.text.format.NamedTextColor; +//import net.minecraft.network.chat.Component; +//import org.apache.commons.lang3.tuple.Pair; +//import org.bukkit.Bukkit; +//import org.bukkit.Location; +//import org.bukkit.command.CommandSender; +//import org.jetbrains.annotations.NotNull; +//import org.jetbrains.annotations.Nullable; +//import org.leavesmc.leaves.LeavesConfig; +//import org.leavesmc.leaves.bot.BotList; +//import org.leavesmc.leaves.bot.ServerBot; +//import org.leavesmc.leaves.bot.agent.configs.AbstractBotConfig; +//import org.leavesmc.leaves.bot.agent.Configs; +//import org.leavesmc.leaves.command.CommandArgumentResult; +//import org.leavesmc.leaves.command.LeavesSubcommand; +//import org.leavesmc.leaves.command.LeavesSuggestionBuilder; +//import org.leavesmc.leaves.event.bot.BotConfigModifyEvent; +// +//import java.util.Arrays; +//import java.util.List; +//import java.util.Objects; +// +//import static net.kyori.adventure.text.Component.text; +// +//public class BotConfigCommand implements LeavesSubcommand { +// +// @Override +// public void execute(CommandSender sender, String subCommand, String[] args) { +// if (args.length < 2) { +// sender.sendMessage(text("Use /bot config to modify fakeplayer's config", NamedTextColor.RED)); +// return; +// } +// +// ServerBot bot = BotList.INSTANCE.getBotByName(args[0]); +// if (bot == null) { +// sender.sendMessage(text("This fakeplayer is not in server", NamedTextColor.RED)); +// return; +// } +// +// if (!Configs.getConfigNames().contains(args[1])) { +// sender.sendMessage(text("This config is not accept", NamedTextColor.RED)); +// return; +// } +// +// AbstractBotConfig config = bot.getConfig(Objects.requireNonNull(Configs.getConfig(args[1]))); +// if (args.length < 3) { +// config.getMessage().forEach(sender::sendMessage); +// } else { +// String[] realArgs = Arrays.copyOfRange(args, 2, args.length); +// +// BotConfigModifyEvent event = new BotConfigModifyEvent(bot.getBukkitEntity(), config.getName(), realArgs, sender); +// Bukkit.getPluginManager().callEvent(event); +// +// if (event.isCancelled()) { +// return; +// } +// CommandArgumentResult result = config.getArgument().parse(0, realArgs); +// +// try { +// config.setFromCommand(result); +// config.getChangeMessage().forEach(sender::sendMessage); +// } catch (IllegalArgumentException e) { +// sender.sendMessage(text(e.getMessage(), NamedTextColor.RED)); +// } +// } +// } +// +// +// @Override +// public void suggest(@NotNull CommandSender sender, @NotNull String alias, @NotNull String @NotNull [] args, @Nullable Location location, LeavesSuggestionBuilder builder) throws IllegalArgumentException { +// BotList botList = BotList.INSTANCE; +// ServerBot serverBot = null; +// +// if (args.length > 1 && (serverBot = botList.getBotByName(args[0])) == null) { +// builder.suggest("<" + args[0] + " not found>"); +// return; +// } +// +// switch (args.length) { +// case 0, 1 -> botList.bots.forEach(bot -> builder.suggest(bot.getName().getString())); +// case 2 -> Configs.getConfigNames().forEach(builder::suggest); +// case 3, 4 -> { +// Configs config = Configs.getConfig(args[1]); +// if (config == null) { +// return; +// } +// AbstractBotConfig botConfig = serverBot.getConfig(config); +// Pair, String> results = botConfig.getArgument().suggestion(args.length - 3, sender, args[args.length - 1]); +// if (results == null || results.getLeft() == null) { +// return; +// } +// +// for (String s : results.getLeft()) { +// if (results.getRight() != null) { +// builder.suggest(s, Component.literal(results.getRight())); +// } else { +// builder.suggest(s); +// } +// } +// } +// } +// } +// +// @Override +// public boolean isEnabled() { +// return LeavesConfig.modify.fakeplayer.canModifyConfig; +// } +//} diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/CraftBot.java b/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/CraftBot.java index de83f04c..e8205d26 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/CraftBot.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/CraftBot.java @@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.leavesmc.leaves.bot.BotList; import org.leavesmc.leaves.bot.ServerBot; -import org.leavesmc.leaves.bot.agent.actions.ServerBotAction; +import org.leavesmc.leaves.bot.agent.actions.AbstractBotAction; import org.leavesmc.leaves.entity.bot.action.BotAction; import org.leavesmc.leaves.entity.bot.actions.CraftBotAction; import org.leavesmc.leaves.event.bot.BotActionStopEvent; @@ -63,7 +63,7 @@ public class CraftBot extends CraftPlayer implements Bot { @Override public void stopAllActions() { - for (ServerBotAction action : this.getHandle().getBotActions()) { + for (AbstractBotAction action : this.getHandle().getBotActions()) { action.stop(this.getHandle(), BotActionStopEvent.Reason.PLUGIN); } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/CraftBotManager.java b/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/CraftBotManager.java index dcbf2330..550985c4 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/CraftBotManager.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/CraftBotManager.java @@ -9,7 +9,7 @@ import org.leavesmc.leaves.bot.BotCreateState; import org.leavesmc.leaves.bot.BotList; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.bot.agent.Actions; -import org.leavesmc.leaves.bot.agent.actions.ServerBotAction; +import org.leavesmc.leaves.bot.agent.actions.AbstractBotAction; import org.leavesmc.leaves.entity.bot.action.BotAction; import org.leavesmc.leaves.event.bot.BotCreateEvent; @@ -55,7 +55,7 @@ public class CraftBotManager implements BotManager { @SuppressWarnings("unchecked") @Override public > T newAction(@NotNull Class type) { - ServerBotAction action = Actions.getForClass(type); + AbstractBotAction action = Actions.getForClass(type); if (action == null) { throw new IllegalArgumentException("No action registered for type: " + type.getName()); } else { diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/actions/CraftBotAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/actions/CraftBotAction.java index f3fbd60e..b0c0c20c 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/actions/CraftBotAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/actions/CraftBotAction.java @@ -2,14 +2,14 @@ package org.leavesmc.leaves.entity.bot.actions; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; -import org.leavesmc.leaves.bot.agent.actions.ServerBotAction; +import org.leavesmc.leaves.bot.agent.actions.AbstractBotAction; import org.leavesmc.leaves.entity.bot.action.BotAction; import java.util.UUID; import java.util.function.Consumer; import java.util.function.Function; -public abstract class CraftBotAction, S extends ServerBotAction> implements BotAction { +public abstract class CraftBotAction, S extends AbstractBotAction> implements BotAction { protected final S serverAction; protected final Function creator; @@ -23,7 +23,7 @@ public abstract class CraftBotAction, S extends ServerBot this.creator = creator; } - public ServerBotAction getHandle() { + public AbstractBotAction getHandle() { return serverAction; } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/actions/CraftTimerBotAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/actions/CraftTimerBotAction.java index 532c3e37..2ff20416 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/actions/CraftTimerBotAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/actions/CraftTimerBotAction.java @@ -1,11 +1,11 @@ package org.leavesmc.leaves.entity.bot.actions; -import org.leavesmc.leaves.bot.agent.actions.ServerTimerBotAction; +import org.leavesmc.leaves.bot.agent.actions.AbstractTimerBotAction; import org.leavesmc.leaves.entity.bot.action.TimerBotAction; import java.util.function.Function; -public class CraftTimerBotAction, S extends ServerTimerBotAction> extends CraftBotAction implements TimerBotAction { +public class CraftTimerBotAction, S extends AbstractTimerBotAction> extends CraftBotAction implements TimerBotAction { public CraftTimerBotAction(S serverAction, Function creator) { super(serverAction, creator); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/CommandContext.java b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/CommandContext.java index 1ff2c7f4..d075ccc7 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/CommandContext.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/CommandContext.java @@ -46,6 +46,18 @@ public class CommandContext { return source.getArgument(name, clazz); } + public int getInteger(final String name) { + return source.getArgument(name, Integer.class); + } + + public boolean getBoolean(final String name) { + return source.getArgument(name, Boolean.class); + } + + public String getString(final String name) { + return source.getArgument(name, String.class); + } + @SuppressWarnings("unchecked") public @NotNull V getArgument(final Class> nodeClass) { String name = getNameForNode(nodeClass); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/ActionCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/ActionCommand.java index 32bc4a94..268efef0 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/ActionCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/ActionCommand.java @@ -16,7 +16,7 @@ public class ActionCommand extends BotSubcommand { public ActionCommand() { super("action"); - children(ActionCommand.BotArgument::new); + children(BotArgument::new); } @Override diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/ConfigCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/ConfigCommand.java index 051bb3b1..477cd14c 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/ConfigCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/ConfigCommand.java @@ -1,10 +1,106 @@ package org.leavesmc.leaves.neo_command.bot.subcommands; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.minecraft.commands.CommandSourceStack; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.leavesmc.leaves.bot.ServerBot; +import org.leavesmc.leaves.bot.agent.Configs; +import org.leavesmc.leaves.bot.agent.configs.AbstractBotConfig; +import org.leavesmc.leaves.neo_command.CommandContext; +import org.leavesmc.leaves.neo_command.CustomArgumentNode; +import org.leavesmc.leaves.neo_command.LiteralNode; import org.leavesmc.leaves.neo_command.bot.BotSubcommand; +import java.util.function.Supplier; + +import static io.papermc.paper.adventure.PaperAdventure.asAdventure; +import static net.kyori.adventure.text.Component.join; +import static net.kyori.adventure.text.Component.text; +import static net.kyori.adventure.text.JoinConfiguration.spaces; +import static net.kyori.adventure.text.format.NamedTextColor.AQUA; +import static net.kyori.adventure.text.format.NamedTextColor.GRAY; + public class ConfigCommand extends BotSubcommand { public ConfigCommand() { super("config"); + children(BotArgument::new); + } + + private static class BotArgument extends CustomArgumentNode { + + protected BotArgument() { + super("bot", new org.leavesmc.leaves.neo_command.bot.BotArgument()); + Configs.getConfigs().stream() + .map(this::configNodeCreator) + .forEach(this::children); + } + + @Contract(pure = true) + private @NotNull Supplier configNodeCreator(AbstractBotConfig config) { + return () -> new ConfigNode<>(config); + } + + public static @NotNull ServerBot getBot(@NotNull CommandContext context) throws CommandSyntaxException { + return context.getCustomArgument(BotArgument.class); + } + } + + private static class ConfigNode extends LiteralNode { + private final AbstractBotConfig config; + + private ConfigNode(@NotNull AbstractBotConfig config) { + super(config.getName()); + this.config = config; + } + + @Override + protected ArgumentBuilder compileBase() { + RequiredArgumentBuilder argument = config.getArgument() + .compile() + .executes(mojangCtx -> { + CommandContext ctx = new CommandContext(mojangCtx); + return executeSet(ctx) ? 1 : 0; + }); + return super.compileBase() + .then(argument); + } + + @Override + protected boolean execute(@NotNull CommandContext context) throws CommandSyntaxException { + ServerBot bot = BotArgument.getBot(context); + AbstractBotConfig botConfig = bot.getConfig(config); + context.getSender().sendMessage(join(spaces(), + text("Bot", GRAY), + asAdventure(bot.getDisplayName()).append(text("'s", GRAY)), + text("config", GRAY), + botConfig.getNameComponent(), + text("is", GRAY), + text(String.valueOf(bot.getConfig(config).getValue()), AQUA) + )); + return true; + } + + private boolean executeSet(CommandContext context) throws CommandSyntaxException { + ServerBot bot = BotArgument.getBot(context); + AbstractBotConfig botConfig = bot.getConfig(config); + try { + botConfig.setValue(botConfig.loadFromCommand(context)); + } catch (ClassCastException e) { + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().create(); + } + context.getSender().sendMessage(join(spaces(), + text("Bot", GRAY), + asAdventure(bot.getDisplayName()).append(text("'s", GRAY)), + text("config", GRAY), + botConfig.getNameComponent(), + text("changed to", GRAY), + text(String.valueOf(botConfig.getValue()), AQUA) + )); + return true; + } } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/action/ListCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/action/ListCommand.java index 754309e0..54f90df9 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/action/ListCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/action/ListCommand.java @@ -4,7 +4,7 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; -import org.leavesmc.leaves.bot.agent.actions.ServerBotAction; +import org.leavesmc.leaves.bot.agent.actions.AbstractBotAction; import org.leavesmc.leaves.neo_command.CommandContext; import org.leavesmc.leaves.neo_command.LiteralNode; import org.leavesmc.leaves.neo_command.bot.subcommands.ActionCommand; @@ -30,7 +30,7 @@ public class ListCommand extends LiteralNode { ServerBot bot = ActionCommand.BotArgument.getBot(context); CommandSender sender = context.getSender(); - List> actions = bot.getBotActions(); + List> actions = bot.getBotActions(); if (actions.isEmpty()) { sender.sendMessage(text("This bot has no active actions", GRAY)); return true; @@ -41,7 +41,7 @@ public class ListCommand extends LiteralNode { .append(text("'s action list:", GRAY)) ); for (int i = 0; i < actions.size(); i++) { - ServerBotAction action = actions.get(i); + AbstractBotAction action = actions.get(i); sender.sendMessage(join(spaces(), text(i, GRAY), text(action.getName(), AQUA).hoverEvent(showText(text(action.getActionDataString()))) diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/action/StartCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/action/StartCommand.java index ea1a76ce..7611e284 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/action/StartCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/action/StartCommand.java @@ -10,7 +10,7 @@ import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.bot.agent.Actions; -import org.leavesmc.leaves.bot.agent.actions.ServerBotAction; +import org.leavesmc.leaves.bot.agent.actions.AbstractBotAction; import org.leavesmc.leaves.neo_command.CommandContext; import org.leavesmc.leaves.neo_command.LiteralNode; import org.leavesmc.leaves.neo_command.WrappedArgument; @@ -38,7 +38,7 @@ public class StartCommand extends LiteralNode { .forEach(this::children); } - private boolean handleStartCommand(CommandContext context, @NotNull ServerBotAction action) throws CommandSyntaxException { + private boolean handleStartCommand(CommandContext context, @NotNull AbstractBotAction action) throws CommandSyntaxException { ServerBot bot = getBot(context); CommandSender sender = context.getSender(); @@ -56,51 +56,60 @@ public class StartCommand extends LiteralNode { } @Contract(pure = true) - private @NotNull Supplier actionNodeCreator(ServerBotAction action) { - return () -> new LiteralNode(action.getName()) { - @Override - protected ArgumentBuilder compile() { - ArgumentBuilder builder = super.compile(); + private @NotNull Supplier actionNodeCreator(AbstractBotAction action) { + return () -> new ActionLiteralNode(action); + } - Map>>> arguments = action.getArguments(); - Command executor = context -> { - if (handleStartCommand(new CommandContext(context), action)) { - return Command.SINGLE_SUCCESS; + private class ActionLiteralNode extends LiteralNode { + private final AbstractBotAction action; + + public ActionLiteralNode(@NotNull AbstractBotAction action) { + super(action.getName()); + this.action = action; + } + + @Override + protected ArgumentBuilder compile() { + ArgumentBuilder builder = super.compile(); + + Map>>> arguments = action.getArguments(); + Command executor = context -> { + if (handleStartCommand(new CommandContext(context), action)) { + return Command.SINGLE_SUCCESS; + } else { + return 0; + } + }; + for (Map.Entry>>> entry : arguments.entrySet()) { + List>> value = entry.getValue(); + ArgumentBuilder branchArgumentBuilder = null; + + for (Pair> stringWrappedArgumentPair : value.reversed()) { + WrappedArgument argument = stringWrappedArgumentPair.getRight(); + if (branchArgumentBuilder == null) { + branchArgumentBuilder = argument.compile().executes(executor); } else { - return 0; - } - }; - for (Map.Entry>>> entry : arguments.entrySet()) { - List>> value = entry.getValue(); - ArgumentBuilder branchArgumentBuilder = null; - - for (Pair> stringWrappedArgumentPair : value.reversed()) { - WrappedArgument argument = stringWrappedArgumentPair.getRight(); - if (branchArgumentBuilder == null) { - branchArgumentBuilder = argument.compile().executes(executor); - } else { - branchArgumentBuilder = argument.compile().then(branchArgumentBuilder); - if (argument.isOptional()) { - branchArgumentBuilder = branchArgumentBuilder.executes(executor); - } + branchArgumentBuilder = argument.compile().then(branchArgumentBuilder); + if (argument.isOptional()) { + branchArgumentBuilder = branchArgumentBuilder.executes(executor); } } - - if (value.getFirst().getRight().isOptional() || value.isEmpty()) { - builder = builder.executes(executor); - } - - if (branchArgumentBuilder != null) { - builder = builder.then(branchArgumentBuilder); - } } - if (arguments.isEmpty()) { + if (value.getFirst().getRight().isOptional() || value.isEmpty()) { builder = builder.executes(executor); } - return builder; + if (branchArgumentBuilder != null) { + builder = builder.then(branchArgumentBuilder); + } } - }; + + if (arguments.isEmpty()) { + builder = builder.executes(executor); + } + + return builder; + } } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/action/StopCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/action/StopCommand.java index e23a077f..d7b7eda1 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/action/StopCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/bot/subcommands/action/StopCommand.java @@ -8,7 +8,7 @@ import net.minecraft.network.chat.Component; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; -import org.leavesmc.leaves.bot.agent.actions.ServerBotAction; +import org.leavesmc.leaves.bot.agent.actions.AbstractBotAction; import org.leavesmc.leaves.event.bot.BotActionStopEvent; import org.leavesmc.leaves.neo_command.ArgumentNode; import org.leavesmc.leaves.neo_command.CommandContext; @@ -46,7 +46,7 @@ public class StopCommand extends LiteralNode { ServerBot bot = ActionCommand.BotArgument.getBot(context); for (int i = 0; i < bot.getBotActions().size(); i++) { - ServerBotAction action = bot.getBotActions().get(i); + AbstractBotAction action = bot.getBotActions().get(i); builder.suggest(String.valueOf(i), Component.literal(action.getName())); } @@ -67,7 +67,7 @@ public class StopCommand extends LiteralNode { throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.integerTooHigh().create(index, maxIndex); } - ServerBotAction action = bot.getBotActions().get(index); + AbstractBotAction action = bot.getBotActions().get(index); BotActionStopEvent event = new BotActionStopEvent( bot.getBukkitEntity(), action.getName(), action.getUUID(), BotActionStopEvent.Reason.COMMAND, sender ); @@ -98,16 +98,16 @@ public class StopCommand extends LiteralNode { protected boolean execute(@NotNull CommandContext context) throws CommandSyntaxException { ServerBot bot = ActionCommand.BotArgument.getBot(context); - List> actions = bot.getBotActions(); + List> actions = bot.getBotActions(); CommandSender sender = context.getSender(); if (actions.isEmpty()) { sender.sendMessage(text("This bot has no active actions", GRAY)); return true; } - Set> canceled = new HashSet<>(); - Set> forRemoval = new HashSet<>(); - for (ServerBotAction action : actions) { + Set> canceled = new HashSet<>(); + Set> forRemoval = new HashSet<>(); + for (AbstractBotAction action : actions) { BotActionStopEvent event = new BotActionStopEvent( bot.getBukkitEntity(), action.getName(), action.getUUID(), BotActionStopEvent.Reason.COMMAND, sender ); @@ -132,7 +132,7 @@ public class StopCommand extends LiteralNode { asAdventure(bot.getDisplayName()).append(text("'s", GRAY)), text("'s action list, but following actions' stop was canceled by plugin:", GRAY) )); - for (ServerBotAction action : canceled) { + for (AbstractBotAction action : canceled) { context.getSender().sendMessage( text(action.getName(), AQUA).hoverEvent(showText(text(action.getActionDataString()))) ); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/leaves/subcommands/ConfigCommand.java b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/leaves/subcommands/ConfigCommand.java index ad547038..6e203d93 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/leaves/subcommands/ConfigCommand.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/neo_command/leaves/subcommands/ConfigCommand.java @@ -48,9 +48,9 @@ public class ConfigCommand extends LiteralNode { int dotIndex = path.lastIndexOf("."); builder = builder.createOffset(builder.getInput().lastIndexOf(' ') + dotIndex + 2); getListClosestMatchingLast( - path.substring(dotIndex + 1), - GlobalConfigManager.getVerifiedConfigSubPaths(path) - ).forEach(builder::suggest); + path.substring(dotIndex + 1), + GlobalConfigManager.getVerifiedConfigSubPaths(path) + ).forEach(builder::suggest); return builder.buildFuture(); }