mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-29 11:59:17 +00:00
refactor: use paper command api
This commit is contained in:
@@ -84,7 +84,7 @@ import java.util.function.Predicate;
|
||||
public class ServerBot extends ServerPlayer {
|
||||
|
||||
private final List<AbstractBotAction<?>> actions;
|
||||
private final Map<String, AbstractBotConfig<?, ?, ?>> configs;
|
||||
private final Map<String, AbstractBotConfig<?, ?>> configs;
|
||||
|
||||
public boolean resume = false;
|
||||
public BotCreateState createState;
|
||||
@@ -106,8 +106,8 @@ public class ServerBot extends ServerPlayer {
|
||||
this.gameMode = new ServerBotGameMode(this);
|
||||
|
||||
this.actions = new ArrayList<>();
|
||||
ImmutableMap.Builder<String, AbstractBotConfig<?, ?, ?>> configBuilder = ImmutableMap.builder();
|
||||
for (AbstractBotConfig<?, ?, ?> config : Configs.getConfigs()) {
|
||||
ImmutableMap.Builder<String, AbstractBotConfig<?, ?>> configBuilder = ImmutableMap.builder();
|
||||
for (AbstractBotConfig<?, ?> config : Configs.getConfigs()) {
|
||||
configBuilder.put(config.getName(), config.create().setBot(this));
|
||||
}
|
||||
this.configs = configBuilder.build();
|
||||
@@ -395,7 +395,7 @@ public class ServerBot extends ServerPlayer {
|
||||
|
||||
if (!this.configs.isEmpty()) {
|
||||
ValueOutput.TypedOutputList<CompoundTag> configNbt = nbt.list("configs", CompoundTag.CODEC);
|
||||
for (AbstractBotConfig<?, ?, ?> config : this.configs.values()) {
|
||||
for (AbstractBotConfig<?, ?> config : this.configs.values()) {
|
||||
configNbt.add(config.save(new CompoundTag()));
|
||||
}
|
||||
}
|
||||
@@ -440,7 +440,7 @@ public class ServerBot extends ServerPlayer {
|
||||
if (nbt.list("configs", CompoundTag.CODEC).isPresent()) {
|
||||
ValueInput.TypedInputList<CompoundTag> configNbt = nbt.list("configs", CompoundTag.CODEC).orElseThrow();
|
||||
for (CompoundTag configTag : configNbt) {
|
||||
AbstractBotConfig<?, ?, ?> config = Configs.getConfig(configTag.getString("configName").orElseThrow());
|
||||
AbstractBotConfig<?, ?> config = Configs.getConfig(configTag.getString("configName").orElseThrow());
|
||||
if (config != null) {
|
||||
config.load(configTag);
|
||||
}
|
||||
@@ -669,15 +669,15 @@ public class ServerBot extends ServerPlayer {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <O, I, E extends AbstractBotConfig<O, I, E>> AbstractBotConfig<O, I, E> getConfig(@NotNull AbstractBotConfig<O, I, E> config) {
|
||||
return (AbstractBotConfig<O, I, E>) Objects.requireNonNull(this.configs.get(config.getName()));
|
||||
public <T, E extends AbstractBotConfig<T, E>> AbstractBotConfig<T, E> getConfig(@NotNull AbstractBotConfig<T, E> config) {
|
||||
return (AbstractBotConfig<T, E>) Objects.requireNonNull(this.configs.get(config.getName()));
|
||||
}
|
||||
|
||||
public Collection<AbstractBotConfig<?, ?, ?>> getAllConfigs() {
|
||||
public Collection<AbstractBotConfig<?, ?>> getAllConfigs() {
|
||||
return configs.values();
|
||||
}
|
||||
|
||||
public <O, I, E extends AbstractBotConfig<O, I, E>> O getConfigValue(@NotNull AbstractBotConfig<O, I, E> config) {
|
||||
public <T, E extends AbstractBotConfig<T, E>> T getConfigValue(@NotNull AbstractBotConfig<T, E> config) {
|
||||
return this.getConfig(config).getValue();
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import java.util.Map;
|
||||
|
||||
@SuppressWarnings({"unused"})
|
||||
public class Configs {
|
||||
private static final Map<Class<?>, AbstractBotConfig<?, ?, ?>> configs = new HashMap<>();
|
||||
private static final Map<Class<?>, AbstractBotConfig<?, ?>> configs = new HashMap<>();
|
||||
|
||||
public static final SkipSleepConfig SKIP_SLEEP = register(new SkipSleepConfig());
|
||||
public static final AlwaysSendDataConfig ALWAYS_SEND_DATA = register(new AlwaysSendDataConfig());
|
||||
@@ -27,7 +27,7 @@ public class Configs {
|
||||
public static final LocatorBarConfig ENABLE_LOCATOR_BAR = register(new LocatorBarConfig());
|
||||
|
||||
@Nullable
|
||||
public static AbstractBotConfig<?, ?, ?> getConfig(String name) {
|
||||
public static AbstractBotConfig<?, ?> getConfig(String name) {
|
||||
return configs.values().stream()
|
||||
.filter(config -> config.getName().equals(name))
|
||||
.findFirst()
|
||||
@@ -36,12 +36,12 @@ public class Configs {
|
||||
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
public static Collection<AbstractBotConfig<?, ?, ?>> getConfigs() {
|
||||
public static Collection<AbstractBotConfig<?, ?>> getConfigs() {
|
||||
return configs.values();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <Value, Type, E extends AbstractBotConfig<Value, Type, E>> @NotNull E register(AbstractBotConfig<Value, Type, E> instance) {
|
||||
private static <T, E extends AbstractBotConfig<T, E>> @NotNull E register(AbstractBotConfig<T, E> instance) {
|
||||
configs.put(instance.getClass(), instance);
|
||||
return (E) instance;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package org.leavesmc.leaves.bot.agent.actions;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.arguments.EntityArgument;
|
||||
import net.minecraft.commands.arguments.coordinates.Coordinates;
|
||||
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
|
||||
import net.minecraft.commands.arguments.selector.EntitySelector;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
|
||||
import io.papermc.paper.command.brigadier.argument.resolvers.FinePositionResolver;
|
||||
import io.papermc.paper.command.brigadier.argument.resolvers.selector.PlayerSelectorArgumentResolver;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.craftbukkit.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
@@ -25,25 +25,28 @@ public class ServerLookAction extends AbstractBotAction<ServerLookAction> {
|
||||
|
||||
public ServerLookAction() {
|
||||
super("look", ServerLookAction::new);
|
||||
this.addArgument("player", EntityArgument.player()).setOptional(true);
|
||||
this.addArgument("player", ArgumentTypes.player()).setOptional(true);
|
||||
this.fork(1);
|
||||
this.addArgument("location", Vec3Argument.vec3(false));
|
||||
this.addArgument("location", ArgumentTypes.finePosition());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadCommand(@NotNull CommandContext context) throws CommandSyntaxException {
|
||||
EntitySelector selector = context.getArgumentOrDefault("player", EntitySelector.class, null);
|
||||
Coordinates location = context.getArgumentOrDefault("location", Coordinates.class, null);
|
||||
PlayerSelectorArgumentResolver playerSelectorResolver = context.getArgumentOrDefault("player", PlayerSelectorArgumentResolver.class, null);
|
||||
FinePositionResolver positionResolver = context.getArgumentOrDefault("location", FinePositionResolver.class, null);
|
||||
CommandSourceStack source = context.getSource();
|
||||
if (selector == null && location == null) {
|
||||
Entity sender = source.getEntityOrException();
|
||||
this.setPos(new Vector(sender.getX(), sender.getY(), sender.getZ()));
|
||||
} else if (selector != null) {
|
||||
ServerPlayer player = selector.findSinglePlayer(source);
|
||||
this.setTarget(player);
|
||||
if (playerSelectorResolver == null && positionResolver == null) {
|
||||
CommandSender sender = context.getSender();
|
||||
if (sender instanceof Entity entity) {
|
||||
this.setPos(entity.getLocation().toVector());
|
||||
} else {
|
||||
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().create();
|
||||
}
|
||||
} else if (playerSelectorResolver != null) {
|
||||
CraftPlayer player = (CraftPlayer) playerSelectorResolver.resolve(source).getFirst();
|
||||
this.setTarget(player.getHandle());
|
||||
} else {
|
||||
Vec3 vector = location.getPosition(source);
|
||||
this.setPos(new Vector(vector.x, vector.y, vector.z));
|
||||
this.setPos(positionResolver.resolve(source).toVector());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,42 +1,25 @@
|
||||
package org.leavesmc.leaves.bot.agent.actions;
|
||||
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.bot.agent.ExtraData;
|
||||
import org.leavesmc.leaves.command.CommandContext;
|
||||
import org.leavesmc.leaves.command.arguments.EnumArgumentType;
|
||||
import org.leavesmc.leaves.entity.bot.action.MoveAction.MoveDirection;
|
||||
import org.leavesmc.leaves.entity.bot.actions.CraftMoveAction;
|
||||
import org.leavesmc.leaves.event.bot.BotActionStopEvent;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
import static org.leavesmc.leaves.command.ArgumentNode.ArgumentSuggestions.strings;
|
||||
|
||||
public class ServerMoveAction extends AbstractStateBotAction<ServerMoveAction> {
|
||||
private static final Map<String, MoveDirection> NAME_TO_DIRECTION = Arrays.stream(MoveDirection.values()).collect(toMap(
|
||||
it -> it.name,
|
||||
it -> it
|
||||
));
|
||||
private MoveDirection direction = MoveDirection.FORWARD;
|
||||
|
||||
public ServerMoveAction() {
|
||||
super("move", ServerMoveAction::new);
|
||||
this.addArgument("direction", StringArgumentType.word())
|
||||
.suggests(strings(Arrays.stream(MoveDirection.values()).map((it) -> it.name).toList()));
|
||||
this.addArgument("direction", EnumArgumentType.fromEnum(MoveDirection.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadCommand(@NotNull CommandContext context) throws CommandSyntaxException {
|
||||
String raw = context.getArgument("direction", String.class);
|
||||
MoveDirection direction = NAME_TO_DIRECTION.get(raw);
|
||||
if (direction == null) {
|
||||
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().create();
|
||||
}
|
||||
this.direction = direction;
|
||||
public void loadCommand(@NotNull CommandContext context) {
|
||||
this.direction = context.getArgument("direction", MoveDirection.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package org.leavesmc.leaves.bot.agent.actions;
|
||||
|
||||
import com.mojang.brigadier.arguments.FloatArgumentType;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.bot.agent.ExtraData;
|
||||
@@ -19,16 +20,26 @@ public class ServerRotationAction extends AbstractBotAction<ServerRotationAction
|
||||
public ServerRotationAction() {
|
||||
super("rotation", ServerRotationAction::new);
|
||||
this.addArgument("yaw", FloatArgumentType.floatArg(-180, 180))
|
||||
.suggests((context, builder) -> builder.suggest(
|
||||
DF.format(context.getSource().getEntityOrException().getYRot()),
|
||||
Component.literal("current player yaw")
|
||||
))
|
||||
.suggests((context, builder) -> {
|
||||
CommandSender sender = context.getSender();
|
||||
if (sender instanceof Entity entity) {
|
||||
builder.suggest(
|
||||
DF.format(entity.getYaw()),
|
||||
Component.literal("current player yaw")
|
||||
);
|
||||
}
|
||||
})
|
||||
.setOptional(true);
|
||||
this.addArgument("pitch", FloatArgumentType.floatArg(-90, 90))
|
||||
.suggests((context, builder) -> builder.suggest(
|
||||
DF.format(context.getSource().getEntityOrException().getXRot()),
|
||||
Component.literal("current player pitch")
|
||||
))
|
||||
.suggests((context, builder) -> {
|
||||
CommandSender sender = context.getSender();
|
||||
if (sender instanceof Entity entity) {
|
||||
builder.suggest(
|
||||
DF.format(entity.getPitch()),
|
||||
Component.literal("current player pitch")
|
||||
);
|
||||
}
|
||||
})
|
||||
.setOptional(true);
|
||||
}
|
||||
|
||||
@@ -36,9 +47,14 @@ public class ServerRotationAction extends AbstractBotAction<ServerRotationAction
|
||||
private float pitch = 0.0f;
|
||||
|
||||
@Override
|
||||
public void loadCommand(@NotNull CommandContext context) throws CommandSyntaxException {
|
||||
this.yaw = context.getFloatOrDefault("yaw", context.getSource().getEntityOrException().getYRot());
|
||||
this.pitch = context.getFloatOrDefault("pitch", context.getSource().getEntityOrException().getXRot());
|
||||
public void loadCommand(@NotNull CommandContext context) {
|
||||
CommandSender sender = context.getSender();
|
||||
if (sender instanceof Entity entity) {
|
||||
this.yaw = entity.getYaw();
|
||||
this.pitch = entity.getPitch();
|
||||
}
|
||||
this.yaw = context.getFloatOrDefault("yaw", this.yaw);
|
||||
this.pitch = context.getFloatOrDefault("pitch", this.pitch);
|
||||
}
|
||||
|
||||
public void setYaw(float yaw) {
|
||||
|
||||
@@ -17,14 +17,14 @@ import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class AbstractBotConfig<Value, Type, E extends AbstractBotConfig<Value, Type, E>> {
|
||||
public abstract class AbstractBotConfig<T, E extends AbstractBotConfig<T, E>> {
|
||||
private final String name;
|
||||
private final WrappedArgument<Type> argument;
|
||||
private final WrappedArgument<T> argument;
|
||||
private final Supplier<E> creator;
|
||||
|
||||
protected ServerBot bot;
|
||||
|
||||
public AbstractBotConfig(String name, ArgumentType<Type> type, Supplier<E> creator) {
|
||||
public AbstractBotConfig(String name, ArgumentType<T> type, Supplier<E> creator) {
|
||||
this.name = name;
|
||||
this.argument = new WrappedArgument<>(name, type);
|
||||
if (shouldApplySuggestions()) {
|
||||
@@ -37,7 +37,7 @@ public abstract class AbstractBotConfig<Value, Type, E extends AbstractBotConfig
|
||||
public void applySuggestions(final CommandContext context, final SuggestionsBuilder builder) throws CommandSyntaxException {
|
||||
}
|
||||
|
||||
public AbstractBotConfig<Value, Type, E> setBot(ServerBot bot) {
|
||||
public AbstractBotConfig<T, E> setBot(ServerBot bot) {
|
||||
this.bot = bot;
|
||||
return this;
|
||||
}
|
||||
@@ -46,11 +46,11 @@ public abstract class AbstractBotConfig<Value, Type, E extends AbstractBotConfig
|
||||
return creator.get();
|
||||
}
|
||||
|
||||
public abstract Value getValue();
|
||||
public abstract T getValue();
|
||||
|
||||
public abstract void setValue(Value value) throws CommandSyntaxException;
|
||||
public abstract void setValue(T value) throws CommandSyntaxException;
|
||||
|
||||
public abstract Value loadFromCommand(@NotNull CommandContext context) throws CommandSyntaxException;
|
||||
public abstract T loadFromCommand(@NotNull CommandContext context) throws CommandSyntaxException;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
@@ -71,7 +71,7 @@ public abstract class AbstractBotConfig<Value, Type, E extends AbstractBotConfig
|
||||
.orElse("No data");
|
||||
}
|
||||
|
||||
public WrappedArgument<Type> getArgument() {
|
||||
public WrappedArgument<T> getArgument() {
|
||||
return argument;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.LeavesConfig;
|
||||
import org.leavesmc.leaves.command.CommandContext;
|
||||
|
||||
public class AlwaysSendDataConfig extends AbstractBotConfig<Boolean, Boolean, AlwaysSendDataConfig> {
|
||||
public class AlwaysSendDataConfig extends AbstractBotConfig<Boolean, AlwaysSendDataConfig> {
|
||||
private boolean value;
|
||||
|
||||
public AlwaysSendDataConfig() {
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.LeavesConfig;
|
||||
import org.leavesmc.leaves.command.CommandContext;
|
||||
|
||||
public class LocatorBarConfig extends AbstractBotConfig<Boolean, Boolean, LocatorBarConfig> {
|
||||
public class LocatorBarConfig extends AbstractBotConfig<Boolean, LocatorBarConfig> {
|
||||
private boolean value;
|
||||
|
||||
public LocatorBarConfig() {
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.leavesmc.leaves.command.CommandContext;
|
||||
|
||||
import static net.minecraft.network.chat.Component.literal;
|
||||
|
||||
public class SimulationDistanceConfig extends AbstractBotConfig<Integer, Integer, SimulationDistanceConfig> {
|
||||
public class SimulationDistanceConfig extends AbstractBotConfig<Integer, SimulationDistanceConfig> {
|
||||
|
||||
public SimulationDistanceConfig() {
|
||||
super("simulation_distance", IntegerArgumentType.integer(2, 32), SimulationDistanceConfig::new);
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.LeavesConfig;
|
||||
import org.leavesmc.leaves.command.CommandContext;
|
||||
|
||||
public class SkipSleepConfig extends AbstractBotConfig<Boolean, Boolean, SkipSleepConfig> {
|
||||
public class SkipSleepConfig extends AbstractBotConfig<Boolean, SkipSleepConfig> {
|
||||
|
||||
public SkipSleepConfig() {
|
||||
super("skip_sleep", BoolArgumentType.bool(), SkipSleepConfig::new);
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.leavesmc.leaves.LeavesConfig;
|
||||
import org.leavesmc.leaves.bot.agent.ExtraData;
|
||||
import org.leavesmc.leaves.command.CommandContext;
|
||||
|
||||
public class SpawnPhantomConfig extends AbstractBotConfig<Boolean, Boolean, SpawnPhantomConfig> {
|
||||
public class SpawnPhantomConfig extends AbstractBotConfig<Boolean, SpawnPhantomConfig> {
|
||||
private boolean value;
|
||||
|
||||
public SpawnPhantomConfig() {
|
||||
|
||||
@@ -1,36 +1,23 @@
|
||||
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.command.CommandContext;
|
||||
import org.leavesmc.leaves.command.arguments.EnumArgumentType;
|
||||
|
||||
public class TickTypeConfig extends AbstractBotConfig<ServerBot.TickType, String, TickTypeConfig> {
|
||||
public class TickTypeConfig extends AbstractBotConfig<ServerBot.TickType, TickTypeConfig> {
|
||||
private ServerBot.TickType value;
|
||||
|
||||
public TickTypeConfig() {
|
||||
super("tick_type", StringArgumentType.word(), TickTypeConfig::new);
|
||||
super("tick_type", EnumArgumentType.fromEnum(ServerBot.TickType.class), 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();
|
||||
};
|
||||
public ServerBot.TickType loadFromCommand(@NotNull CommandContext context) {
|
||||
return context.getArgument("tick_type", ServerBot.TickType.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -6,8 +6,8 @@ import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.Commands;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
@@ -4,9 +4,8 @@ import com.mojang.brigadier.Command;
|
||||
import com.mojang.brigadier.RedirectModifier;
|
||||
import com.mojang.brigadier.context.ParsedCommandNode;
|
||||
import com.mojang.brigadier.context.StringRange;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.tree.CommandNode;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -64,13 +63,6 @@ public class CommandContext {
|
||||
return (V) source.getArgument(name, Object.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V, T> @NotNull V getCustomArgument(final Class<? extends CustomArgumentNode<V, T>> nodeClass) throws CommandSyntaxException {
|
||||
String name = getNameForNode(nodeClass);
|
||||
T raw = (T) source.getArgument(name, Object.class);
|
||||
return CustomArgumentNode.transform(nodeClass, raw);
|
||||
}
|
||||
|
||||
public <V> V getArgumentOrDefault(final Class<? extends ArgumentNode<V>> nodeClass, final V defaultValue) {
|
||||
try {
|
||||
return getArgument(nodeClass);
|
||||
@@ -87,14 +79,6 @@ public class CommandContext {
|
||||
}
|
||||
}
|
||||
|
||||
public <V, T> V getCustomArgumentOrDefault(final Class<? extends CustomArgumentNode<V, T>> nodeClass, final V defaultValue) throws CommandSyntaxException {
|
||||
try {
|
||||
return getCustomArgument(nodeClass);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public String getStringOrDefault(final String name, final String defaultValue) {
|
||||
return getArgumentOrDefault(name, String.class, defaultValue);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.leavesmc.leaves.command;
|
||||
|
||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -40,7 +40,7 @@ public class CommandUtils {
|
||||
}
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public static @NotNull List<String> getListClosestMatchingLast(final String last, final Collection<?> collection) {
|
||||
public static List<String> getListClosestMatchingLast(final String last, final Collection<?> collection) {
|
||||
if (collection.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package org.leavesmc.leaves.command;
|
||||
|
||||
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.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CustomArgumentNode<T, B> extends ArgumentNode<B> {
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static final Map<Class<? extends CustomArgumentNode>, CustomArgumentType<?, ?>> TYPES = new HashMap<>();
|
||||
|
||||
protected CustomArgumentNode(String name, @NotNull CustomArgumentType<T, B> argumentType) {
|
||||
super(name, argumentType.getBaseArgumentType());
|
||||
TYPES.put(getClass(), argumentType);
|
||||
}
|
||||
|
||||
public static <T, B> T transform(Class<? extends CustomArgumentNode<T, B>> nodeClass, B base) throws CommandSyntaxException {
|
||||
@SuppressWarnings("unchecked")
|
||||
CustomArgumentType<T, B> type = (CustomArgumentType<T, B>) TYPES.get(nodeClass);
|
||||
if (type == null) {
|
||||
throw new IllegalArgumentException("No custom argument type registered for " + nodeClass.getName());
|
||||
}
|
||||
return type.transform(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected ArgumentBuilder<CommandSourceStack, ?> compileBase() {
|
||||
RequiredArgumentBuilder<CommandSourceStack, T> argumentBuilder = (RequiredArgumentBuilder<CommandSourceStack, T>) super.compileBase();
|
||||
|
||||
if (!overrideSuggestions()) {
|
||||
CustomArgumentType<T, B> customArgumentType = (CustomArgumentType<T, B>) TYPES.get(getClass());
|
||||
argumentBuilder.suggests(
|
||||
(context, builder) -> customArgumentType.getSuggestions(new CommandContext(context), builder)
|
||||
);
|
||||
}
|
||||
|
||||
return argumentBuilder;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package org.leavesmc.leaves.command;
|
||||
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface CustomArgumentType<T, B> {
|
||||
ArgumentType<B> getBaseArgumentType();
|
||||
|
||||
T transform(B value) throws CommandSyntaxException;
|
||||
|
||||
CompletableFuture<Suggestions> getSuggestions(CommandContext context, SuggestionsBuilder builder) throws CommandSyntaxException;
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package org.leavesmc.leaves.command;
|
||||
|
||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.Commands;
|
||||
|
||||
public class LiteralNode extends CommandNode {
|
||||
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package org.leavesmc.leaves.command;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.PaperCommands;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -36,18 +35,16 @@ public abstract class RootNode extends LiteralNode {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void register() {
|
||||
MinecraftServer.getServer()
|
||||
.getCommands()
|
||||
.getDispatcher()
|
||||
.register((LiteralArgumentBuilder<CommandSourceStack>) compile());
|
||||
PaperCommands.INSTANCE.setValid();
|
||||
PaperCommands.INSTANCE.getDispatcher().register((LiteralArgumentBuilder<CommandSourceStack>) compile());
|
||||
PaperCommands.INSTANCE.invalidate();
|
||||
Bukkit.getOnlinePlayers().forEach(org.bukkit.entity.Player::updateCommands);
|
||||
}
|
||||
|
||||
public void unregister() {
|
||||
CommandDispatcher<CommandSourceStack> dispatcher = MinecraftServer.getServer()
|
||||
.getCommands()
|
||||
.getDispatcher();
|
||||
dispatcher.getRoot().removeCommand(name);
|
||||
PaperCommands.INSTANCE.setValid();
|
||||
PaperCommands.INSTANCE.getDispatcher().getRoot().removeCommand(name);
|
||||
PaperCommands.INSTANCE.invalidate();
|
||||
Bukkit.getOnlinePlayers().forEach(org.bukkit.entity.Player::updateCommands);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.Commands;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
|
||||
@@ -1,39 +1,23 @@
|
||||
package org.leavesmc.leaves.command.bot;
|
||||
package org.leavesmc.leaves.command.arguments;
|
||||
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
import io.papermc.paper.command.brigadier.argument.CustomArgumentType;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.bot.BotList;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.command.CommandContext;
|
||||
import org.leavesmc.leaves.command.CustomArgumentType;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class BotArgument implements CustomArgumentType<ServerBot, String> {
|
||||
@Override
|
||||
public ArgumentType<String> getBaseArgumentType() {
|
||||
return StringArgumentType.word();
|
||||
}
|
||||
public class BotArgumentType implements CustomArgumentType.Converted<@NotNull ServerBot, @NotNull String> {
|
||||
|
||||
@Override
|
||||
public ServerBot transform(String value) throws CommandSyntaxException {
|
||||
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 bot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Suggestions> getSuggestions(CommandContext context, SuggestionsBuilder builder) throws CommandSyntaxException {
|
||||
public <S> @NotNull CompletableFuture<Suggestions> listSuggestions(com.mojang.brigadier.context.@NotNull CommandContext<S> context, @NotNull SuggestionsBuilder builder) {
|
||||
Collection<ServerBot> bots = BotList.INSTANCE.bots;
|
||||
if (bots.isEmpty()) {
|
||||
return builder
|
||||
@@ -43,4 +27,21 @@ public class BotArgument implements CustomArgumentType<ServerBot, String> {
|
||||
bots.stream().map(ServerBot::getScoreboardName).forEach(builder::suggest);
|
||||
return builder.buildFuture();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerBot convert(String nativeType) throws CommandSyntaxException {
|
||||
ServerBot bot = BotList.INSTANCE.getBotByName(nativeType);
|
||||
if (bot == null) {
|
||||
throw new CommandSyntaxException(
|
||||
CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument(),
|
||||
Component.literal("Bot with name '" + nativeType + "' does not exist")
|
||||
);
|
||||
}
|
||||
return bot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ArgumentType<@NotNull String> getNativeType() {
|
||||
return StringArgumentType.word();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package org.leavesmc.leaves.command.arguments;
|
||||
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
import io.papermc.paper.command.brigadier.argument.CustomArgumentType;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@SuppressWarnings("ClassCanBeRecord")
|
||||
public final class EnumArgumentType<T extends Enum<T>> implements CustomArgumentType.Converted<@NotNull T, @NotNull String> {
|
||||
private final Class<T> enumClass;
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
public static <T extends Enum<T>> @NotNull EnumArgumentType<T> fromEnum(Class<T> enumClass) {
|
||||
return new EnumArgumentType<>(enumClass);
|
||||
}
|
||||
|
||||
private EnumArgumentType(Class<T> enumClass) {
|
||||
this.enumClass = enumClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull T convert(@NotNull String nativeType) throws CommandSyntaxException {
|
||||
try {
|
||||
return Enum.valueOf(enumClass, nativeType.toUpperCase());
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().create();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> @NotNull CompletableFuture<Suggestions> listSuggestions(@NotNull CommandContext<S> context, @NotNull SuggestionsBuilder builder) {
|
||||
for (Enum<?> value : enumClass.getEnumConstants()) {
|
||||
String name = value.name().toLowerCase();
|
||||
if (name.startsWith(builder.getRemainingLowerCase())) {
|
||||
builder.suggest(name);
|
||||
}
|
||||
}
|
||||
return builder.buildFuture();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ArgumentType<@NotNull String> getNativeType() {
|
||||
return StringArgumentType.word();
|
||||
}
|
||||
|
||||
public Class<T> enumClass() {
|
||||
return enumClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null || obj.getClass() != this.getClass()) {
|
||||
return false;
|
||||
}
|
||||
EnumArgumentType<?> that = (EnumArgumentType<?>) obj;
|
||||
return Objects.equals(this.enumClass, that.enumClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(enumClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EnumArgumentType[" +
|
||||
"enumClass=" + enumClass + ']';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.leavesmc.leaves.command.bot;
|
||||
|
||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.command.RootNode;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.leavesmc.leaves.command.bot;
|
||||
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.command.LiteralNode;
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package org.leavesmc.leaves.command.bot.subcommands;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.LeavesConfig;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.command.ArgumentNode;
|
||||
import org.leavesmc.leaves.command.CommandContext;
|
||||
import org.leavesmc.leaves.command.CustomArgumentNode;
|
||||
import org.leavesmc.leaves.command.arguments.BotArgumentType;
|
||||
import org.leavesmc.leaves.command.bot.BotSubcommand;
|
||||
import org.leavesmc.leaves.command.bot.subcommands.action.ListCommand;
|
||||
import org.leavesmc.leaves.command.bot.subcommands.action.StartCommand;
|
||||
@@ -24,10 +24,10 @@ public class ActionCommand extends BotSubcommand {
|
||||
return LeavesConfig.modify.fakeplayer.canUseAction && super.requires(source);
|
||||
}
|
||||
|
||||
public static class BotArgument extends CustomArgumentNode<ServerBot, String> {
|
||||
public static class BotArgument extends ArgumentNode<ServerBot> {
|
||||
|
||||
private BotArgument() {
|
||||
super("bot", new org.leavesmc.leaves.command.bot.BotArgument());
|
||||
super("bot", new BotArgumentType());
|
||||
children(
|
||||
StartCommand::new,
|
||||
StopCommand::new,
|
||||
@@ -35,8 +35,8 @@ public class ActionCommand extends BotSubcommand {
|
||||
);
|
||||
}
|
||||
|
||||
public static @NotNull ServerBot getBot(@NotNull CommandContext context) throws CommandSyntaxException {
|
||||
return context.getCustomArgument(BotArgument.class);
|
||||
public static @NotNull ServerBot getBot(@NotNull CommandContext context) {
|
||||
return context.getArgument(BotArgument.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package org.leavesmc.leaves.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 io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -11,9 +11,10 @@ import org.leavesmc.leaves.LeavesConfig;
|
||||
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.command.ArgumentNode;
|
||||
import org.leavesmc.leaves.command.CommandContext;
|
||||
import org.leavesmc.leaves.command.CustomArgumentNode;
|
||||
import org.leavesmc.leaves.command.LiteralNode;
|
||||
import org.leavesmc.leaves.command.arguments.BotArgumentType;
|
||||
import org.leavesmc.leaves.command.bot.BotSubcommand;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -38,33 +39,33 @@ public class ConfigCommand extends BotSubcommand {
|
||||
return LeavesConfig.modify.fakeplayer.canModifyConfig && super.requires(source);
|
||||
}
|
||||
|
||||
private static class BotArgument extends CustomArgumentNode<ServerBot, String> {
|
||||
private static class BotArgument extends ArgumentNode<ServerBot> {
|
||||
|
||||
private BotArgument() {
|
||||
super("bot", new org.leavesmc.leaves.command.bot.BotArgument());
|
||||
super("bot", new BotArgumentType());
|
||||
Configs.getConfigs().stream().map(this::configNodeCreator).forEach(this::children);
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
private @NotNull Supplier<LiteralNode> configNodeCreator(AbstractBotConfig<?, ?, ?> config) {
|
||||
private @NotNull Supplier<LiteralNode> configNodeCreator(AbstractBotConfig<?, ?> config) {
|
||||
return () -> new ConfigNode<>(config);
|
||||
}
|
||||
|
||||
public static @NotNull ServerBot getBot(@NotNull CommandContext context) throws CommandSyntaxException {
|
||||
return context.getCustomArgument(BotArgument.class);
|
||||
public static @NotNull ServerBot getBot(@NotNull CommandContext context) {
|
||||
return context.getArgument(BotArgument.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean execute(CommandContext context) throws CommandSyntaxException {
|
||||
protected boolean execute(CommandContext context) {
|
||||
ServerBot bot = BotArgument.getBot(context);
|
||||
CommandSender sender = context.getSender();
|
||||
Collection<AbstractBotConfig<?, ?, ?>> botConfigs = bot.getAllConfigs();
|
||||
Collection<AbstractBotConfig<?, ?>> botConfigs = bot.getAllConfigs();
|
||||
sender.sendMessage(join(spaces(),
|
||||
text("Bot", GRAY),
|
||||
asAdventure(bot.getDisplayName()).append(text("'s", GRAY)),
|
||||
text("configs:", GRAY)
|
||||
));
|
||||
for (AbstractBotConfig<?, ?, ?> botConfig : botConfigs) {
|
||||
for (AbstractBotConfig<?, ?> botConfig : botConfigs) {
|
||||
sender.sendMessage(join(spaces(),
|
||||
botConfig.getNameComponent(),
|
||||
text("=", GRAY),
|
||||
@@ -75,10 +76,10 @@ public class ConfigCommand extends BotSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private static class ConfigNode<Value> extends LiteralNode {
|
||||
private final AbstractBotConfig<Value, ?, ?> config;
|
||||
private static class ConfigNode<T> extends LiteralNode {
|
||||
private final AbstractBotConfig<T, ?> config;
|
||||
|
||||
private ConfigNode(@NotNull AbstractBotConfig<Value, ?, ?> config) {
|
||||
private ConfigNode(@NotNull AbstractBotConfig<T, ?> config) {
|
||||
super(config.getName());
|
||||
this.config = config;
|
||||
}
|
||||
@@ -95,9 +96,9 @@ public class ConfigCommand extends BotSubcommand {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean execute(@NotNull CommandContext context) throws CommandSyntaxException {
|
||||
protected boolean execute(@NotNull CommandContext context) {
|
||||
ServerBot bot = BotArgument.getBot(context);
|
||||
AbstractBotConfig<Value, ?, ?> botConfig = bot.getConfig(config);
|
||||
AbstractBotConfig<T, ?> botConfig = bot.getConfig(config);
|
||||
context.getSender().sendMessage(join(spaces(),
|
||||
text("Bot", GRAY),
|
||||
asAdventure(bot.getDisplayName()).append(text("'s", GRAY)),
|
||||
@@ -111,7 +112,7 @@ public class ConfigCommand extends BotSubcommand {
|
||||
|
||||
private boolean executeSet(CommandContext context) throws CommandSyntaxException {
|
||||
ServerBot bot = BotArgument.getBot(context);
|
||||
AbstractBotConfig<Value, ?, ?> botConfig = bot.getConfig(config);
|
||||
AbstractBotConfig<T, ?> botConfig = bot.getConfig(config);
|
||||
try {
|
||||
botConfig.setValue(botConfig.loadFromCommand(context));
|
||||
} catch (ClassCastException e) {
|
||||
|
||||
@@ -2,19 +2,17 @@ package org.leavesmc.leaves.command.bot.subcommands;
|
||||
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
|
||||
import io.papermc.paper.command.brigadier.argument.resolvers.FinePositionResolver;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.arguments.DimensionArgument;
|
||||
import net.minecraft.commands.arguments.coordinates.Coordinates;
|
||||
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.LeavesConfig;
|
||||
import org.leavesmc.leaves.bot.BotCreateState;
|
||||
@@ -25,7 +23,6 @@ import org.leavesmc.leaves.command.bot.BotSubcommand;
|
||||
import org.leavesmc.leaves.event.bot.BotCreateEvent;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
import static net.minecraft.commands.arguments.DimensionArgument.getDimension;
|
||||
|
||||
public class CreateCommand extends BotSubcommand {
|
||||
|
||||
@@ -45,7 +42,7 @@ public class CreateCommand extends BotSubcommand {
|
||||
|
||||
World world;
|
||||
try {
|
||||
world = getDimension(context.getMojangContext(), "world").getWorld();
|
||||
world = context.getArgument(WorldArgument.class);
|
||||
} catch (IllegalArgumentException e) {
|
||||
if (!(sender instanceof Entity entity)) {
|
||||
sender.sendMessage(text("Must specify world and location when executed by console", NamedTextColor.RED));
|
||||
@@ -55,10 +52,10 @@ public class CreateCommand extends BotSubcommand {
|
||||
}
|
||||
|
||||
Location location = Bukkit.getWorlds().getFirst().getSpawnLocation();
|
||||
Coordinates coords = context.getArgumentOrDefault(LocationArgument.class, null);
|
||||
if (coords != null) {
|
||||
Vec3 vec3 = coords.getPosition(context.getSource());
|
||||
location = new Location(world, vec3.x, vec3.y, vec3.z);
|
||||
FinePositionResolver positionResolver = context.getArgumentOrDefault(LocationArgument.class, null);
|
||||
if (positionResolver != null) {
|
||||
Vector vec3 = positionResolver.resolve(context.getSource()).toVector();
|
||||
location = new Location(world, vec3.getX(), vec3.getY(), vec3.getZ());
|
||||
} else if (sender instanceof Entity entity) {
|
||||
location = entity.getLocation();
|
||||
}
|
||||
@@ -122,9 +119,9 @@ public class CreateCommand extends BotSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private static class WorldArgument extends ArgumentNode<ResourceLocation> {
|
||||
private static class WorldArgument extends ArgumentNode<World> {
|
||||
private WorldArgument() {
|
||||
super("world", DimensionArgument.dimension());
|
||||
super("world", ArgumentTypes.world());
|
||||
children(LocationArgument::new);
|
||||
}
|
||||
|
||||
@@ -134,9 +131,9 @@ public class CreateCommand extends BotSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private static class LocationArgument extends ArgumentNode<Coordinates> {
|
||||
private static class LocationArgument extends ArgumentNode<FinePositionResolver> {
|
||||
private LocationArgument() {
|
||||
super("location", Vec3Argument.vec3(true));
|
||||
super("location", ArgumentTypes.finePosition());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,10 +2,8 @@ package org.leavesmc.leaves.command.bot.subcommands;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import io.papermc.paper.adventure.PaperAdventure;
|
||||
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minecraft.commands.arguments.DimensionArgument;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
@@ -28,8 +26,6 @@ import static net.kyori.adventure.text.Component.text;
|
||||
import static net.kyori.adventure.text.JoinConfiguration.noSeparators;
|
||||
import static net.kyori.adventure.text.event.HoverEvent.showText;
|
||||
import static net.kyori.adventure.text.format.NamedTextColor.*;
|
||||
import static net.minecraft.commands.arguments.DimensionArgument.getDimension;
|
||||
|
||||
|
||||
public class ListCommand extends BotSubcommand {
|
||||
|
||||
@@ -76,16 +72,16 @@ public class ListCommand extends BotSubcommand {
|
||||
);
|
||||
}
|
||||
|
||||
private static class WorldArgument extends ArgumentNode<ResourceLocation> {
|
||||
private static class WorldArgument extends ArgumentNode<World> {
|
||||
|
||||
private WorldArgument() {
|
||||
super("world", DimensionArgument.dimension());
|
||||
super("world", ArgumentTypes.world());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean execute(@NotNull CommandContext context) throws CommandSyntaxException {
|
||||
ServerLevel dimension = getDimension(context.getMojangContext(), "world");
|
||||
Component botListMessage = getBotListMessage(dimension.getWorld());
|
||||
protected boolean execute(@NotNull CommandContext context) {
|
||||
World world = context.getArgument(WorldArgument.class);
|
||||
Component botListMessage = getBotListMessage(world);
|
||||
CommandSender sender = context.getSender();
|
||||
if (botListMessage == null) {
|
||||
sender.sendMessage(text("No bots in that world", RED));
|
||||
|
||||
@@ -4,8 +4,8 @@ import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.LeavesConfig;
|
||||
|
||||
@@ -11,7 +11,7 @@ import org.leavesmc.leaves.bot.BotList;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.command.ArgumentNode;
|
||||
import org.leavesmc.leaves.command.CommandContext;
|
||||
import org.leavesmc.leaves.command.CustomArgumentNode;
|
||||
import org.leavesmc.leaves.command.arguments.BotArgumentType;
|
||||
import org.leavesmc.leaves.command.bot.BotSubcommand;
|
||||
import org.leavesmc.leaves.event.bot.BotRemoveEvent;
|
||||
import org.leavesmc.leaves.plugin.MinecraftInternalPlugin;
|
||||
@@ -41,15 +41,15 @@ public class RemoveCommand extends BotSubcommand {
|
||||
return success;
|
||||
}
|
||||
|
||||
private static class BotArgument extends CustomArgumentNode<ServerBot, String> {
|
||||
private static class BotArgument extends ArgumentNode<ServerBot> {
|
||||
private BotArgument() {
|
||||
super("bot", new org.leavesmc.leaves.command.bot.BotArgument());
|
||||
super("bot", new BotArgumentType());
|
||||
children(RemoveTimeArgument::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean execute(@NotNull CommandContext context) throws CommandSyntaxException {
|
||||
ServerBot bot = context.getCustomArgument(BotArgument.class);
|
||||
protected boolean execute(@NotNull CommandContext context) {
|
||||
ServerBot bot = context.getArgument(BotArgument.class);
|
||||
return removeBot(bot, context.getSender());
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,7 @@ public class RemoveCommand extends BotSubcommand {
|
||||
protected boolean execute(@NotNull CommandContext context) throws CommandSyntaxException {
|
||||
String removeTimeStr = context.getArgument("remove_time", String.class);
|
||||
int removeTimeSeconds = parseRemoveTime(removeTimeStr);
|
||||
ServerBot bot = context.getCustomArgument(BotArgument.class);
|
||||
ServerBot bot = context.getArgument(BotArgument.class);
|
||||
CommandSender sender = context.getSender();
|
||||
|
||||
boolean isReschedule = bot.removeTaskId != -1;
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package org.leavesmc.leaves.command.bot.subcommands;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.LeavesConfig;
|
||||
import org.leavesmc.leaves.bot.BotList;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.command.ArgumentNode;
|
||||
import org.leavesmc.leaves.command.CommandContext;
|
||||
import org.leavesmc.leaves.command.CustomArgumentNode;
|
||||
import org.leavesmc.leaves.command.arguments.BotArgumentType;
|
||||
import org.leavesmc.leaves.command.bot.BotSubcommand;
|
||||
import org.leavesmc.leaves.event.bot.BotRemoveEvent;
|
||||
|
||||
@@ -30,15 +30,15 @@ public class SaveCommand extends BotSubcommand {
|
||||
return LeavesConfig.modify.fakeplayer.canManualSaveAndLoad && super.requires(source);
|
||||
}
|
||||
|
||||
private static class BotArgument extends CustomArgumentNode<ServerBot, String> {
|
||||
private static class BotArgument extends ArgumentNode<ServerBot> {
|
||||
|
||||
private BotArgument() {
|
||||
super("bot", new org.leavesmc.leaves.command.bot.BotArgument());
|
||||
super("bot", new BotArgumentType());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean execute(@NotNull CommandContext context) throws CommandSyntaxException {
|
||||
ServerBot bot = context.getCustomArgument(BotArgument.class);
|
||||
protected boolean execute(@NotNull CommandContext context) {
|
||||
ServerBot bot = context.getArgument(BotArgument.class);
|
||||
CommandSender sender = context.getSender();
|
||||
BotList botList = BotList.INSTANCE;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package org.leavesmc.leaves.command.bot.subcommands.action;
|
||||
import com.mojang.brigadier.Command;
|
||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.leavesmc.leaves.command.leaves;
|
||||
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.command.LiteralNode;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.leavesmc.leaves.command.leaves.subcommands;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -31,7 +31,7 @@ public class ConfigCommand extends LeavesSubcommand {
|
||||
|
||||
private PathArgument() {
|
||||
super("path", StringArgumentType.string());
|
||||
children(ConfigCommand.ValueArgument::new);
|
||||
children(ValueArgument::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package org.leavesmc.leaves.command.leaves.subcommands;
|
||||
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.world.item.DyeColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
Reference in New Issue
Block a user