mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-29 11:59:17 +00:00
feat: switch to new bot command
This commit is contained in:
@@ -14,7 +14,6 @@ import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.permissions.PermissionDefault;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.bot.BotCommand;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.bot.agent.Actions;
|
||||
import org.leavesmc.leaves.command.LeavesCommand;
|
||||
@@ -31,6 +30,7 @@ import org.leavesmc.leaves.config.api.impl.ConfigValidatorImpl.IntConfigValidato
|
||||
import org.leavesmc.leaves.config.api.impl.ConfigValidatorImpl.ListConfigValidator;
|
||||
import org.leavesmc.leaves.config.api.impl.ConfigValidatorImpl.LongConfigValidator;
|
||||
import org.leavesmc.leaves.config.api.impl.ConfigValidatorImpl.StringConfigValidator;
|
||||
import org.leavesmc.leaves.neo_command.bot.BotCommand;
|
||||
import org.leavesmc.leaves.profile.LeavesMinecraftSessionService;
|
||||
import org.leavesmc.leaves.protocol.CarpetServerProtocol.CarpetRule;
|
||||
import org.leavesmc.leaves.protocol.CarpetServerProtocol.CarpetRules;
|
||||
@@ -58,6 +58,7 @@ import java.util.Locale;
|
||||
import java.util.Random;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@SuppressWarnings({"unused", "FieldMayBeFinal", "FieldCanBeLocal"})
|
||||
public final class LeavesConfig {
|
||||
|
||||
public static final String CONFIG_HEADER = "Configuration file for Leaves.";
|
||||
@@ -151,16 +152,15 @@ public final class LeavesConfig {
|
||||
@Override
|
||||
public void verify(Boolean old, Boolean value) throws IllegalArgumentException {
|
||||
if (value) {
|
||||
registerCommand("bot", new BotCommand());
|
||||
org.leavesmc.leaves.neo_command.bot.BotCommand.INSTANCE.register();
|
||||
BotCommand.INSTANCE.register();
|
||||
Actions.registerAll();
|
||||
} else {
|
||||
unregisterCommand("bot");
|
||||
org.leavesmc.leaves.neo_command.bot.BotCommand.INSTANCE.unregister();
|
||||
BotCommand.INSTANCE.unregister();
|
||||
}
|
||||
if (old != null && !old.equals(value)) {
|
||||
Bukkit.getOnlinePlayers().stream()
|
||||
.filter(org.leavesmc.leaves.neo_command.bot.BotCommand::hasPermission)
|
||||
.filter(BotCommand::hasPermission)
|
||||
.forEach(org.bukkit.entity.Player::updateCommands);
|
||||
}
|
||||
}
|
||||
@@ -206,10 +206,9 @@ public final class LeavesConfig {
|
||||
if (old != null && !old.equals(value)) {
|
||||
Bukkit.getOnlinePlayers().stream()
|
||||
.filter(sender ->
|
||||
org.leavesmc.leaves.neo_command.bot.BotCommand.hasPermission(sender)
|
||||
|| org.leavesmc.leaves.neo_command.bot.BotCommand.hasPermission(sender, "action")
|
||||
)
|
||||
.forEach(org.bukkit.entity.Player::updateCommands);
|
||||
BotCommand.hasPermission(sender)
|
||||
|| BotCommand.hasPermission(sender, "action")
|
||||
).forEach(org.bukkit.entity.Player::updateCommands);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -223,10 +222,9 @@ public final class LeavesConfig {
|
||||
if (old != null && !old.equals(value)) {
|
||||
Bukkit.getOnlinePlayers().stream()
|
||||
.filter(sender ->
|
||||
org.leavesmc.leaves.neo_command.bot.BotCommand.hasPermission(sender)
|
||||
|| org.leavesmc.leaves.neo_command.bot.BotCommand.hasPermission(sender, "config")
|
||||
)
|
||||
.forEach(org.bukkit.entity.Player::updateCommands);
|
||||
BotCommand.hasPermission(sender)
|
||||
|| BotCommand.hasPermission(sender, "config")
|
||||
).forEach(org.bukkit.entity.Player::updateCommands);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -240,11 +238,10 @@ public final class LeavesConfig {
|
||||
if (old != null && !old.equals(value)) {
|
||||
Bukkit.getOnlinePlayers().stream()
|
||||
.filter(sender ->
|
||||
org.leavesmc.leaves.neo_command.bot.BotCommand.hasPermission(sender)
|
||||
|| org.leavesmc.leaves.neo_command.bot.BotCommand.hasPermission(sender, "save")
|
||||
|| org.leavesmc.leaves.neo_command.bot.BotCommand.hasPermission(sender, "load")
|
||||
)
|
||||
.forEach(org.bukkit.entity.Player::updateCommands);
|
||||
BotCommand.hasPermission(sender)
|
||||
|| BotCommand.hasPermission(sender, "save")
|
||||
|| BotCommand.hasPermission(sender, "load")
|
||||
).forEach(org.bukkit.entity.Player::updateCommands);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package org.leavesmc.leaves.bot;
|
||||
|
||||
import net.minecraft.Util;
|
||||
import org.leavesmc.leaves.LeavesConfig;
|
||||
import org.leavesmc.leaves.bot.subcommands.BotCreateCommand;
|
||||
import org.leavesmc.leaves.bot.subcommands.BotListCommand;
|
||||
import org.leavesmc.leaves.bot.subcommands.BotLoadCommand;
|
||||
import org.leavesmc.leaves.bot.subcommands.BotRemoveCommand;
|
||||
import org.leavesmc.leaves.bot.subcommands.BotSaveCommand;
|
||||
import org.leavesmc.leaves.command.LeavesRootCommand;
|
||||
import org.leavesmc.leaves.command.LeavesSubcommand;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BotCommand extends LeavesRootCommand {
|
||||
|
||||
// subcommand label -> subcommand
|
||||
private static final Map<String, LeavesSubcommand> SUBCOMMANDS = Util.make(() -> {
|
||||
final Map<Set<String>, LeavesSubcommand> commands = new HashMap<>();
|
||||
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("save"), new BotSaveCommand());
|
||||
commands.put(Set.of("load"), new BotLoadCommand());
|
||||
commands.put(Set.of("list"), new BotListCommand());
|
||||
|
||||
return commands.entrySet().stream()
|
||||
.flatMap(entry -> entry.getKey().stream().map(s -> Map.entry(s, entry.getValue())))
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
});
|
||||
|
||||
public BotCommand() {
|
||||
super("bot", "FakePlayer Command", "bukkit.command.bot", SUBCOMMANDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return LeavesConfig.modify.fakeplayer.enable;
|
||||
}
|
||||
}
|
||||
@@ -1,181 +0,0 @@
|
||||
//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.Location;
|
||||
//import org.bukkit.command.CommandSender;
|
||||
//import org.bukkit.craftbukkit.entity.CraftPlayer;
|
||||
//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.bot.agent.Actions;
|
||||
//import org.leavesmc.leaves.bot.agent.actions.ServerBotAction;
|
||||
//import org.leavesmc.leaves.command.LeavesSubcommand;
|
||||
//import org.leavesmc.leaves.command.LeavesSuggestionBuilder;
|
||||
//import org.leavesmc.leaves.event.bot.BotActionStopEvent;
|
||||
//
|
||||
//import java.util.Arrays;
|
||||
//import java.util.HashSet;
|
||||
//import java.util.List;
|
||||
//import java.util.Set;
|
||||
//
|
||||
//import static net.kyori.adventure.text.Component.text;
|
||||
//
|
||||
//public class BotActionCommand implements LeavesSubcommand {
|
||||
//
|
||||
// @Override
|
||||
// public void execute(CommandSender sender, String subCommand, String[] args) {
|
||||
// if (args.length < 2) {
|
||||
// sender.sendMessage(text("Use /bot action <name> <action> to make fakeplayer do action", 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;
|
||||
// }
|
||||
//
|
||||
// switch (args[1].toLowerCase()) {
|
||||
// case "list" -> {
|
||||
// sender.sendMessage(bot.getScoreboardName() + "'s action list:");
|
||||
// for (int i = 0; i < bot.getBotActions().size(); i++) {
|
||||
// sender.sendMessage(i + " " + bot.getBotActions().get(i).getName());
|
||||
// }
|
||||
// }
|
||||
// case "start" -> executeStart(bot, sender, args);
|
||||
// case "stop" -> executeStop(bot, sender, args);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private void executeStart(ServerBot bot, CommandSender sender, String[] args) {
|
||||
// if (args.length < 3) {
|
||||
// sender.sendMessage(text("Invalid action", NamedTextColor.RED));
|
||||
// return;
|
||||
// }
|
||||
// ServerBotAction<?> action = Actions.getForName(args[2]);
|
||||
// if (action == null) {
|
||||
// sender.sendMessage(text("Invalid action", NamedTextColor.RED));
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// CraftPlayer player;
|
||||
// if (sender instanceof CraftPlayer) {
|
||||
// player = (CraftPlayer) sender;
|
||||
// } else {
|
||||
// player = bot.getBukkitEntity();
|
||||
// }
|
||||
//
|
||||
// String[] realArgs = Arrays.copyOfRange(args, 3, args.length);
|
||||
// ServerBotAction<?> newAction;
|
||||
// try {
|
||||
// newAction = action.create();
|
||||
// newAction.loadCommand(player.getHandle(), action.getArgument().parse(0, realArgs));
|
||||
// } catch (IllegalArgumentException e) {
|
||||
// sender.sendMessage(text("Action create error, please check your arguments, " + e.getMessage(), NamedTextColor.RED));
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// if (bot.addBotAction(newAction, sender)) {
|
||||
// sender.sendMessage("Action " + action.getName() + " has been issued to " + bot.getName().getString());
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private void executeStop(ServerBot bot, CommandSender sender, String[] args) {
|
||||
// if (args.length < 3) {
|
||||
// sender.sendMessage(text("Invalid index", NamedTextColor.RED));
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// String index = args[2];
|
||||
// if (index.equals("all")) {
|
||||
// Set<ServerBotAction<?>> forRemoval = new HashSet<>();
|
||||
// for (int i = 0; i < bot.getBotActions().size(); i++) {
|
||||
// ServerBotAction<?> action = bot.getBotActions().get(i);
|
||||
// BotActionStopEvent event = new BotActionStopEvent(
|
||||
// bot.getBukkitEntity(), action.getName(), action.getUUID(), BotActionStopEvent.Reason.COMMAND, sender
|
||||
// );
|
||||
// event.callEvent();
|
||||
// if (!event.isCancelled()) {
|
||||
// forRemoval.add(action);
|
||||
// action.stop(bot, BotActionStopEvent.Reason.COMMAND);
|
||||
// }
|
||||
// }
|
||||
// bot.getBotActions().removeAll(forRemoval);
|
||||
// sender.sendMessage(bot.getScoreboardName() + "'s action list cleared.");
|
||||
// return;
|
||||
// }
|
||||
// try {
|
||||
// int i = Integer.parseInt(index);
|
||||
// if (i < 0 || i >= bot.getBotActions().size()) {
|
||||
// sender.sendMessage(text("Invalid index", NamedTextColor.RED));
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// ServerBotAction<?> action = bot.getBotActions().get(i);
|
||||
// BotActionStopEvent event = new BotActionStopEvent(
|
||||
// bot.getBukkitEntity(), action.getName(), action.getUUID(), BotActionStopEvent.Reason.COMMAND, sender
|
||||
// );
|
||||
// event.callEvent();
|
||||
// if (!event.isCancelled()) {
|
||||
// action.stop(bot, BotActionStopEvent.Reason.COMMAND);
|
||||
// bot.getBotActions().remove(i);
|
||||
// sender.sendMessage(bot.getScoreboardName() + "'s " + action.getName() + " stopped.");
|
||||
//
|
||||
// }
|
||||
// } catch (NumberFormatException e) {
|
||||
// sender.sendMessage(text("Invalid index", NamedTextColor.RED));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void suggest(@NotNull CommandSender sender, @NotNull String subCommand, String @NotNull [] args, Location location, @NotNull LeavesSuggestionBuilder builder) {
|
||||
// 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 -> builder.suggest("start").suggest("stop").suggest("list");
|
||||
// case 3 -> {
|
||||
// switch (args[1].toLowerCase()) {
|
||||
// case "start" -> Actions.getNames().forEach(builder::suggest);
|
||||
// case "stop" -> {
|
||||
// builder.suggest("all");
|
||||
// int[] index = new int[]{0};
|
||||
// serverBot.getBotActions().forEach(a -> builder.suggest(String.valueOf(index[0]++)));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// case 4, 5, 6, 7 -> {
|
||||
// ServerBotAction<?> action = Actions.getForName(args[2]);
|
||||
// if (action == null) {
|
||||
// return;
|
||||
// }
|
||||
// Pair<List<String>, String> results = action.getArgument().suggestion(args.length - 4, sender, args[args.length - 2]);
|
||||
// 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.canUseAction;
|
||||
// }
|
||||
//}
|
||||
@@ -1,110 +0,0 @@
|
||||
//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 <name> <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<List<String>, 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;
|
||||
// }
|
||||
//}
|
||||
@@ -1,100 +0,0 @@
|
||||
package org.leavesmc.leaves.bot.subcommands;
|
||||
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
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.Player;
|
||||
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.BotCreateState;
|
||||
import org.leavesmc.leaves.bot.BotList;
|
||||
import org.leavesmc.leaves.bot.BotUtil;
|
||||
import org.leavesmc.leaves.command.LeavesSubcommand;
|
||||
import org.leavesmc.leaves.command.LeavesSuggestionBuilder;
|
||||
import org.leavesmc.leaves.event.bot.BotCreateEvent;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
public class BotCreateCommand implements LeavesSubcommand {
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String subCommand, String[] args) {
|
||||
if (args.length < 1) {
|
||||
sender.sendMessage(text("Use /bot create <name> [skin_name] to create a fakeplayer", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
|
||||
String botName = args[0];
|
||||
String fullName = BotUtil.getFullName(botName);
|
||||
if (this.canCreate(sender, fullName)) {
|
||||
BotCreateState.Builder builder = BotCreateState.builder(botName, Bukkit.getWorlds().getFirst().getSpawnLocation()).createReason(BotCreateEvent.CreateReason.COMMAND).creator(sender);
|
||||
|
||||
if (args.length >= 2) {
|
||||
builder.skinName(args[1]);
|
||||
}
|
||||
|
||||
if (sender instanceof Player player) {
|
||||
builder.location(player.getLocation());
|
||||
} else if (sender instanceof ConsoleCommandSender) {
|
||||
if (args.length >= 6) {
|
||||
try {
|
||||
World world = Bukkit.getWorld(args[2]);
|
||||
double x = Double.parseDouble(args[3]);
|
||||
double y = Double.parseDouble(args[4]);
|
||||
double z = Double.parseDouble(args[5]);
|
||||
if (world != null) {
|
||||
builder.location(new Location(world, x, y, z));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LeavesLogger.LOGGER.warning("Can't build location", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
builder.spawnWithSkin(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void suggest(@NotNull CommandSender sender, @NotNull String alias, @NotNull String @NotNull [] args, @Nullable Location location, LeavesSuggestionBuilder builder) throws IllegalArgumentException {
|
||||
if (args.length <= 1) {
|
||||
builder.suggest("<BotName>");
|
||||
}
|
||||
if (args.length == 2) {
|
||||
builder.suggest("[SkinName]");
|
||||
}
|
||||
if (sender instanceof ConsoleCommandSender && args.length == 3) {
|
||||
Bukkit.getWorlds().forEach(world -> builder.suggest(world.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean canCreate(CommandSender sender, @NotNull String name) {
|
||||
BotList botList = BotList.INSTANCE;
|
||||
if (!name.matches("^[a-zA-Z0-9_]{4,16}$")) {
|
||||
sender.sendMessage(text("This name is illegal", NamedTextColor.RED));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Bukkit.getPlayerExact(name) != null || botList.getBotByName(name) != null) {
|
||||
sender.sendMessage(text("This player is in server", NamedTextColor.RED));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (LeavesConfig.modify.fakeplayer.unableNames.contains(name)) {
|
||||
sender.sendMessage(text("This name is not allowed", NamedTextColor.RED));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (botList.bots.size() >= LeavesConfig.modify.fakeplayer.limit) {
|
||||
sender.sendMessage(text("Fakeplayer limit is full", NamedTextColor.RED));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
package org.leavesmc.leaves.bot.subcommands;
|
||||
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
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.command.LeavesSubcommand;
|
||||
import org.leavesmc.leaves.command.LeavesSuggestionBuilder;
|
||||
import org.leavesmc.leaves.entity.bot.Bot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
public class BotListCommand implements LeavesSubcommand {
|
||||
|
||||
@NotNull
|
||||
private static String formatPlayerNameList(@NotNull List<String> list) {
|
||||
if (list.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
String string = list.toString();
|
||||
return string.substring(1, string.length() - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String subCommand, String[] args) {
|
||||
BotList botList = BotList.INSTANCE;
|
||||
if (args.length < 2) {
|
||||
Map<World, List<String>> botMap = new HashMap<>();
|
||||
for (World world : Bukkit.getWorlds()) {
|
||||
botMap.put(world, new ArrayList<>());
|
||||
}
|
||||
|
||||
for (ServerBot bot : botList.bots) {
|
||||
Bot bukkitBot = bot.getBukkitEntity();
|
||||
botMap.get(bukkitBot.getWorld()).add(bukkitBot.getName());
|
||||
}
|
||||
|
||||
sender.sendMessage("Total number: (" + botList.bots.size() + "/" + LeavesConfig.modify.fakeplayer.limit + ")");
|
||||
for (World world : botMap.keySet()) {
|
||||
sender.sendMessage(world.getName() + "(" + botMap.get(world).size() + "): " + formatPlayerNameList(botMap.get(world)));
|
||||
}
|
||||
} else {
|
||||
World world = Bukkit.getWorld(args[1]);
|
||||
|
||||
if (world == null) {
|
||||
sender.sendMessage(text("Unknown world", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> snowBotList = new ArrayList<>();
|
||||
for (ServerBot bot : botList.bots) {
|
||||
Bot bukkitBot = bot.getBukkitEntity();
|
||||
if (bukkitBot.getWorld() == world) {
|
||||
snowBotList.add(bukkitBot.getName());
|
||||
}
|
||||
}
|
||||
|
||||
sender.sendMessage(world.getName() + "(" + botList.bots.size() + "): " + formatPlayerNameList(snowBotList));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void suggest(@NotNull CommandSender sender, @NotNull String alias, @NotNull String @NotNull [] args, @Nullable Location location, LeavesSuggestionBuilder builder) throws IllegalArgumentException {
|
||||
Bukkit.getWorlds().forEach(world -> builder.suggest(world.getName()));
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package org.leavesmc.leaves.bot.subcommands;
|
||||
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
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.command.LeavesSubcommand;
|
||||
import org.leavesmc.leaves.command.LeavesSuggestionBuilder;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
public class BotLoadCommand implements LeavesSubcommand {
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String subCommand, String[] args) {
|
||||
if (args.length < 1) {
|
||||
sender.sendMessage(text("Use /bot load <name> to save a fakeplayer", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
|
||||
String realName = args[0];
|
||||
BotList botList = BotList.INSTANCE;
|
||||
if (!botList.getSavedBotList().contains(realName)) {
|
||||
sender.sendMessage(text("This fakeplayer is not saved", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
|
||||
if (botList.loadNewBot(realName) == null) {
|
||||
sender.sendMessage(text("Can't load bot, please check", 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;
|
||||
if (args.length <= 1) {
|
||||
botList.getSavedBotList().keySet().forEach(builder::suggest);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return LeavesConfig.modify.fakeplayer.canManualSaveAndLoad;
|
||||
}
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
package org.leavesmc.leaves.bot.subcommands;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
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.bot.BotList;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.command.LeavesSubcommand;
|
||||
import org.leavesmc.leaves.command.LeavesSuggestionBuilder;
|
||||
import org.leavesmc.leaves.event.bot.BotRemoveEvent;
|
||||
import org.leavesmc.leaves.plugin.MinecraftInternalPlugin;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
public class BotRemoveCommand implements LeavesSubcommand {
|
||||
|
||||
private final Component errorMessage = text("Usage: /bot remove <name> [hour] [minute] [second]", NamedTextColor.RED);
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String subCommand, String[] args) {
|
||||
if (args.length < 1 || args.length > 4) {
|
||||
sender.sendMessage(errorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
BotList botList = BotList.INSTANCE;
|
||||
ServerBot bot = botList.getBotByName(args[0]);
|
||||
|
||||
if (bot == null) {
|
||||
sender.sendMessage(text("This fakeplayer is not in server", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.length == 2 && args[1].equals("cancel")) {
|
||||
if (bot.removeTaskId == -1) {
|
||||
sender.sendMessage(text("This fakeplayer is not scheduled to be removed", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().cancelTask(bot.removeTaskId);
|
||||
bot.removeTaskId = -1;
|
||||
sender.sendMessage(text("Remove cancel"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.length > 1) {
|
||||
long time = 0;
|
||||
int h; // Preventing out-of-range
|
||||
long s = 0;
|
||||
long m = 0;
|
||||
|
||||
try {
|
||||
h = Integer.parseInt(args[1]);
|
||||
if (h < 0) {
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
time += ((long) h) * 3600 * 20;
|
||||
if (args.length > 2) {
|
||||
m = Long.parseLong(args[2]);
|
||||
if (m > 59 || m < 0) {
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
time += m * 60 * 20;
|
||||
}
|
||||
if (args.length > 3) {
|
||||
s = Long.parseLong(args[3]);
|
||||
if (s > 59 || s < 0) {
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
time += s * 20;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage(errorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
boolean isReschedule = bot.removeTaskId != -1;
|
||||
|
||||
if (isReschedule) {
|
||||
Bukkit.getScheduler().cancelTask(bot.removeTaskId);
|
||||
}
|
||||
bot.removeTaskId = Bukkit.getScheduler().runTaskLater(MinecraftInternalPlugin.INSTANCE, () -> {
|
||||
bot.removeTaskId = -1;
|
||||
botList.removeBot(bot, BotRemoveEvent.RemoveReason.COMMAND, sender, false);
|
||||
}, time).getTaskId();
|
||||
|
||||
sender.sendMessage("This fakeplayer will be removed in " + h + "h " + m + "m " + s + "s" + (isReschedule ? " (rescheduled)" : ""));
|
||||
return;
|
||||
}
|
||||
|
||||
botList.removeBot(bot, BotRemoveEvent.RemoveReason.COMMAND, sender, false);
|
||||
}
|
||||
|
||||
@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;
|
||||
|
||||
if (args.length <= 1) {
|
||||
botList.bots.forEach(bot -> builder.suggest(bot.getName().getString()));
|
||||
}
|
||||
|
||||
if (args.length == 2) {
|
||||
builder.suggest("cancel");
|
||||
builder.suggest("[hour]");
|
||||
}
|
||||
|
||||
if (args.length > 2 && !args[1].equals("cancel")) {
|
||||
switch (args.length) {
|
||||
case 3 -> builder.suggest("[minute]");
|
||||
case 4 -> builder.suggest("[second]");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package org.leavesmc.leaves.bot.subcommands;
|
||||
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
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.command.LeavesSubcommand;
|
||||
import org.leavesmc.leaves.command.LeavesSuggestionBuilder;
|
||||
import org.leavesmc.leaves.event.bot.BotRemoveEvent;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
public class BotSaveCommand implements LeavesSubcommand {
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String subCommand, String[] args) {
|
||||
if (args.length < 1) {
|
||||
sender.sendMessage(text("Use /bot save <name> to save a fakeplayer", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
|
||||
BotList botList = BotList.INSTANCE;
|
||||
ServerBot bot = botList.getBotByName(args[0]);
|
||||
|
||||
if (bot == null) {
|
||||
sender.sendMessage(text("This fakeplayer is not in server", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
|
||||
if (botList.removeBot(bot, BotRemoveEvent.RemoveReason.COMMAND, sender, true)) {
|
||||
sender.sendMessage(bot.getScoreboardName() + " saved to " + bot.createState.realName());
|
||||
}
|
||||
}
|
||||
|
||||
@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;
|
||||
if (args.length <= 1) {
|
||||
botList.bots.forEach(bot -> builder.suggest(bot.getName().getString()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return LeavesConfig.modify.fakeplayer.canManualSaveAndLoad;
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ public class BotCommand extends LiteralNode {
|
||||
private static final String PERM_BASE = "bukkit.command.bot";
|
||||
|
||||
private BotCommand() {
|
||||
super("bot_neo");
|
||||
super("bot");
|
||||
this.children(
|
||||
ActionCommand::new,
|
||||
ListCommand::new,
|
||||
|
||||
@@ -6,6 +6,7 @@ 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.LeavesConfig;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.bot.agent.Configs;
|
||||
import org.leavesmc.leaves.bot.agent.configs.AbstractBotConfig;
|
||||
@@ -30,6 +31,11 @@ public class ConfigCommand extends BotSubcommand {
|
||||
children(BotArgument::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requires(@NotNull CommandSourceStack source) {
|
||||
return LeavesConfig.modify.fakeplayer.canModifyConfig && super.requires(source);
|
||||
}
|
||||
|
||||
private static class BotArgument extends CustomArgumentNode<ServerBot, String> {
|
||||
|
||||
protected BotArgument() {
|
||||
|
||||
Reference in New Issue
Block a user