did a bit of refactoring
This commit is contained in:
139
src/main/java/net/nuggetmc/tplus/command/CommandHandler.java
Normal file
139
src/main/java/net/nuggetmc/tplus/command/CommandHandler.java
Normal file
@@ -0,0 +1,139 @@
|
||||
package net.nuggetmc.tplus.command;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import net.nuggetmc.tplus.TerminatorPlus;
|
||||
import net.nuggetmc.tplus.command.annotation.Command;
|
||||
import net.nuggetmc.tplus.command.annotation.Require;
|
||||
import net.nuggetmc.tplus.command.commands.AICommand;
|
||||
import net.nuggetmc.tplus.command.commands.BotCommand;
|
||||
import net.nuggetmc.tplus.command.commands.MainCommand;
|
||||
import net.nuggetmc.tplus.utils.ChatUtils;
|
||||
import net.nuggetmc.tplus.utils.Debugger;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.craftbukkit.v1_16_R3.CraftServer;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandHandler {
|
||||
|
||||
private final TerminatorPlus plugin;
|
||||
|
||||
private final Map<String, List<String>> help;
|
||||
private final Map<String, CommandInstance> commandMap;
|
||||
|
||||
public CommandHandler(TerminatorPlus plugin) {
|
||||
this.plugin = plugin;
|
||||
this.help = new HashMap<>();
|
||||
this.commandMap = new HashMap<>();
|
||||
this.registerCommands();
|
||||
}
|
||||
|
||||
public Map<String, CommandInstance> getCommands() {
|
||||
return commandMap;
|
||||
}
|
||||
|
||||
private void registerCommands() {
|
||||
registerCommands(
|
||||
new MainCommand(this, "terminatorplus", "The TerminatorPlus main command.", "tplus"),
|
||||
new BotCommand(this, "bot", "The root command for bot management.", "npc"),
|
||||
new AICommand(this, "ai", "The root command for bot AI training.")
|
||||
);
|
||||
}
|
||||
|
||||
private void registerCommands(CommandInstance... commands) {
|
||||
String fallback = plugin.getName().toLowerCase();
|
||||
SimpleCommandMap bukkitCommandMap = ((CraftServer) plugin.getServer()).getCommandMap();
|
||||
|
||||
for (CommandInstance command : commands) {
|
||||
commandMap.put(command.getName(), command);
|
||||
bukkitCommandMap.register(fallback, command);
|
||||
|
||||
Method[] methods = command.getClass().getDeclaredMethods();
|
||||
|
||||
for (Method method : methods) {
|
||||
if (method.isAnnotationPresent(Command.class)) {
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
} catch (SecurityException e) {
|
||||
Debugger.log("Failed to access method " + method.getName() + ".");
|
||||
continue;
|
||||
}
|
||||
|
||||
Command cmd = method.getAnnotation(Command.class);
|
||||
|
||||
String perm = "";
|
||||
if (method.isAnnotationPresent(Require.class)) {
|
||||
Require require = method.getAnnotation(Require.class);
|
||||
perm = require.value();
|
||||
}
|
||||
|
||||
String autofillName = cmd.autofill();
|
||||
Method autofiller = null;
|
||||
|
||||
if (!autofillName.isEmpty()) {
|
||||
for (Method m : methods) {
|
||||
if (m.getName().equals(autofillName)) {
|
||||
autofiller = m;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String methodName = cmd.name();
|
||||
CommandMethod commandMethod = new CommandMethod(methodName, Sets.newHashSet(cmd.aliases()), cmd.desc(), cmd.usage(), perm, command, method, autofiller);
|
||||
|
||||
command.addMethod(methodName, commandMethod);
|
||||
}
|
||||
}
|
||||
|
||||
setHelp(command);
|
||||
}
|
||||
}
|
||||
|
||||
public CommandInstance getCommand(String name) {
|
||||
return commandMap.get(name);
|
||||
}
|
||||
|
||||
public void sendRootInfo(CommandInstance commandInstance, CommandSender sender) {
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
sender.sendMessage(ChatColor.GOLD + plugin.getName() + ChatUtils.BULLET_FORMATTED + ChatColor.GRAY
|
||||
+ "[" + ChatColor.YELLOW + "/" + commandInstance.getName() + ChatColor.GRAY + "]");
|
||||
help.get(commandInstance.getName()).forEach(sender::sendMessage);
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
}
|
||||
|
||||
private void setHelp(CommandInstance commandInstance) {
|
||||
help.put(commandInstance.getName(), getCommandInfo(commandInstance));
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO
|
||||
* Eventually, this will be a LOT better, basically not having to have this method
|
||||
* at all (kind of like Drink), and even better, we won't even need a "usage" field
|
||||
* in the @Command annotation, and can create the usage message from the method parameters.
|
||||
*/
|
||||
public void sendUsage(CommandSender sender, CommandInstance command, String usage) {
|
||||
sender.sendMessage("Command Usage: " + ChatColor.YELLOW + "/" + command.getName() + " " + usage);
|
||||
}
|
||||
|
||||
private List<String> getCommandInfo(CommandInstance commandInstance) {
|
||||
List<String> output = new ArrayList<>();
|
||||
|
||||
for (CommandMethod method : commandInstance.getMethods().values()) {
|
||||
if (!method.getMethod().getAnnotation(Command.class).visible() || method.getName().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
output.add(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "/" + commandInstance.getName() + " " + method.getName()
|
||||
+ ChatUtils.BULLET_FORMATTED + method.getDescription());
|
||||
}
|
||||
|
||||
return output.stream().sorted().collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package net.nuggetmc.tplus.command;
|
||||
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.defaults.BukkitCommand;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class CommandInstance extends BukkitCommand {
|
||||
|
||||
protected final CommandHandler commandHandler;
|
||||
|
||||
private final Map<String, CommandMethod> methods;
|
||||
|
||||
private static final String MANAGE_PERMISSION = "terminatorplus.manage";
|
||||
|
||||
public CommandInstance(CommandHandler handler, String name, String description, @Nullable String... aliases) {
|
||||
super(name, description, "", aliases == null ? new ArrayList<>() : Arrays.asList(aliases));
|
||||
|
||||
this.commandHandler = handler;
|
||||
this.methods = new HashMap<>();
|
||||
}
|
||||
|
||||
public Map<String, CommandMethod> getMethods() {
|
||||
return methods;
|
||||
}
|
||||
|
||||
protected void addMethod(String name, CommandMethod method) {
|
||||
methods.put(name, method);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(@Nonnull CommandSender sender, @Nonnull String label, @Nonnull String[] args) {
|
||||
if (!sender.hasPermission(MANAGE_PERMISSION)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
CommandMethod method;
|
||||
|
||||
if (args.length == 0) {
|
||||
method = methods.get("");
|
||||
} else if (methods.containsKey(args[0])) {
|
||||
method = methods.get(args[0]);
|
||||
} else {
|
||||
method = methods.get("");
|
||||
}
|
||||
|
||||
if (method == null) {
|
||||
sender.sendMessage(ChatColor.RED + "There is no root command present for the " + ChatColor.YELLOW + getName() + ChatColor.RED + " command.");
|
||||
return true;
|
||||
}
|
||||
|
||||
List<String> arguments = new ArrayList<>(Arrays.asList(args));
|
||||
|
||||
if (arguments.size() > 0) {
|
||||
arguments.remove(0);
|
||||
}
|
||||
|
||||
try {
|
||||
method.getMethod().invoke(method.getHandler(), sender, arguments);
|
||||
} catch (InvocationTargetException | IllegalAccessException e) {
|
||||
sender.sendMessage(ChatColor.RED + "Failed to perform command.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> tabComplete(@Nonnull CommandSender sender, @Nonnull String label, @Nonnull String[] args) {
|
||||
if (args.length == 1) {
|
||||
return methods.keySet().stream().filter(c -> !c.isEmpty()).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
if (args.length > 1) {
|
||||
CommandMethod commandMethod = methods.get(args[0]);
|
||||
Method autofiller = commandMethod.getAutofiller();
|
||||
|
||||
if (autofiller != null) {
|
||||
try {
|
||||
return (List<String>) autofiller.invoke(commandMethod.getHandler(), sender, args);
|
||||
} catch (InvocationTargetException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
66
src/main/java/net/nuggetmc/tplus/command/CommandMethod.java
Normal file
66
src/main/java/net/nuggetmc/tplus/command/CommandMethod.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package net.nuggetmc.tplus.command;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Set;
|
||||
|
||||
public class CommandMethod {
|
||||
|
||||
private final String name;
|
||||
private final Set<String> aliases;
|
||||
private final String description;
|
||||
private final String usage;
|
||||
private final String permission;
|
||||
|
||||
private final CommandInstance handler;
|
||||
|
||||
private final Method method;
|
||||
private final Method autofiller;
|
||||
|
||||
public CommandMethod(String name, Set<String> aliases, String description, String usage, String permission, CommandInstance handler, Method method, Method autofiller) {
|
||||
this.name = name;
|
||||
this.aliases = aliases;
|
||||
this.description = description;
|
||||
this.usage = usage;
|
||||
this.permission = permission;
|
||||
this.handler = handler;
|
||||
this.method = method;
|
||||
this.autofiller = autofiller;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Set<String> getAliases() {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public String getUsage() {
|
||||
return usage;
|
||||
}
|
||||
|
||||
public String getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
public CommandInstance getHandler() {
|
||||
return handler;
|
||||
}
|
||||
|
||||
public Method getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public Method getAutofiller() {
|
||||
return autofiller;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + "{name=\"" + name + "\",aliases=" + aliases + ",description=\"" + description + "\",usage=\"" + usage + "\",permission=\"" + permission + "\",method=" + method + ",autofiller=" + autofiller + "}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package net.nuggetmc.tplus.command.annotation;
|
||||
|
||||
public @interface Autofill {
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package net.nuggetmc.tplus.command.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD})
|
||||
public @interface Command {
|
||||
String name() default "";
|
||||
|
||||
String[] aliases() default {};
|
||||
|
||||
String desc() default "Blank description.";
|
||||
|
||||
String usage() default "";
|
||||
|
||||
String autofill() default "";
|
||||
|
||||
boolean visible() default true;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package net.nuggetmc.tplus.command.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD})
|
||||
public @interface Require {
|
||||
String value();
|
||||
}
|
||||
224
src/main/java/net/nuggetmc/tplus/command/commands/AICommand.java
Normal file
224
src/main/java/net/nuggetmc/tplus/command/commands/AICommand.java
Normal file
@@ -0,0 +1,224 @@
|
||||
package net.nuggetmc.tplus.command.commands;
|
||||
|
||||
import net.nuggetmc.tplus.TerminatorPlus;
|
||||
import net.nuggetmc.tplus.bot.Bot;
|
||||
import net.nuggetmc.tplus.bot.BotManager;
|
||||
import net.nuggetmc.tplus.bot.agent.legacyagent.ai.IntelligenceAgent;
|
||||
import net.nuggetmc.tplus.bot.agent.legacyagent.ai.NeuralNetwork;
|
||||
import net.nuggetmc.tplus.command.CommandHandler;
|
||||
import net.nuggetmc.tplus.command.CommandInstance;
|
||||
import net.nuggetmc.tplus.command.annotation.Autofill;
|
||||
import net.nuggetmc.tplus.command.annotation.Command;
|
||||
import net.nuggetmc.tplus.utils.ChatUtils;
|
||||
import net.nuggetmc.tplus.utils.MathUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AICommand extends CommandInstance {
|
||||
|
||||
/*
|
||||
* ideas
|
||||
* ability to export neural network data to a text file, and also load from them
|
||||
* maybe also have a custom extension like .tplus and encrypt it in base64
|
||||
*/
|
||||
|
||||
private final TerminatorPlus plugin;
|
||||
private final BotManager manager;
|
||||
private final BukkitScheduler scheduler;
|
||||
|
||||
private IntelligenceAgent agent;
|
||||
|
||||
public AICommand(CommandHandler handler, String name, String description, String... aliases) {
|
||||
super(handler, name, description, aliases);
|
||||
|
||||
this.plugin = TerminatorPlus.getInstance();
|
||||
this.manager = plugin.getManager();
|
||||
this.scheduler = Bukkit.getScheduler();
|
||||
}
|
||||
|
||||
@Command
|
||||
public void root(CommandSender sender, List<String> args) {
|
||||
commandHandler.sendRootInfo(this, sender);
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "random",
|
||||
desc = "Create bots with random neural networks, collecting feed data.",
|
||||
usage = "<amount> <name> [skin]"
|
||||
)
|
||||
public void random(CommandSender sender, List<String> args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.size() < 2) {
|
||||
commandHandler.sendUsage(sender, this, "random <amount> <name> [skin]");
|
||||
return;
|
||||
}
|
||||
|
||||
String skin;
|
||||
|
||||
if (args.size() < 3) {
|
||||
skin = null;
|
||||
} else {
|
||||
skin = args.get(2);
|
||||
}
|
||||
|
||||
int n;
|
||||
|
||||
try {
|
||||
n = Integer.parseInt(args.get(0));
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage("The amount must be an integer!");
|
||||
return;
|
||||
}
|
||||
|
||||
manager.createBots((Player) sender, args.get(1), skin, n, NeuralNetwork.RANDOM);
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "reinforcement",
|
||||
desc = "Begin an AI training session.",
|
||||
usage = "<population-size> <name> [skin]"
|
||||
)
|
||||
public void reinforcement(CommandSender sender, List<String> args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
|
||||
if (args.size() < 2) {
|
||||
commandHandler.sendUsage(player, this, "reinforcement <amount> <name> [skin]");
|
||||
return;
|
||||
}
|
||||
|
||||
String skin;
|
||||
|
||||
if (args.size() < 3) {
|
||||
skin = null;
|
||||
} else {
|
||||
skin = args.get(2);
|
||||
}
|
||||
|
||||
int populationSize;
|
||||
|
||||
try {
|
||||
populationSize = Integer.parseInt(args.get(0));
|
||||
} catch (NumberFormatException e) {
|
||||
player.sendMessage("The population size must be an integer!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (agent != null) {
|
||||
player.sendMessage("A session is already active.");
|
||||
return;
|
||||
}
|
||||
|
||||
player.sendMessage("Starting a new session...");
|
||||
|
||||
agent = new IntelligenceAgent(this, populationSize, args.get(1), skin);
|
||||
agent.addUser(player);
|
||||
}
|
||||
|
||||
public IntelligenceAgent getSession() {
|
||||
return agent;
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "stop",
|
||||
desc = "End a currently running AI training session."
|
||||
)
|
||||
public void stop(CommandSender sender, List<String> args) {
|
||||
if (agent == null) {
|
||||
sender.sendMessage("No session is currently active.");
|
||||
return;
|
||||
}
|
||||
|
||||
sender.sendMessage("Stopping the current session...");
|
||||
String name = agent.getName();
|
||||
clearSession();
|
||||
|
||||
scheduler.runTaskLater(plugin, () -> sender.sendMessage("The session " + ChatColor.YELLOW + name + ChatColor.RESET + " has been closed."), 10);
|
||||
}
|
||||
|
||||
public void clearSession() {
|
||||
if (agent != null) {
|
||||
agent.stop();
|
||||
agent = null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasActiveSession() {
|
||||
return agent != null;
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "info",
|
||||
desc = "Display neural network information about a bot.",
|
||||
usage = "<name>",
|
||||
autofill = "infoAutofill"
|
||||
)
|
||||
public void info(CommandSender sender, List<String> args) {
|
||||
if (args.isEmpty()) {
|
||||
commandHandler.sendUsage(sender, this, "info <name>");
|
||||
return;
|
||||
}
|
||||
|
||||
String name = args.get(0);
|
||||
|
||||
sender.sendMessage("Processing request...");
|
||||
|
||||
scheduler.runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
Bot bot = manager.getFirst(name);
|
||||
|
||||
if (bot == null) {
|
||||
sender.sendMessage("Could not find bot " + ChatColor.GREEN + name + ChatColor.RESET + "!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!bot.hasNeuralNetwork()) {
|
||||
sender.sendMessage("The bot " + ChatColor.GREEN + name + ChatColor.RESET + " does not have a neural network!");
|
||||
return;
|
||||
}
|
||||
|
||||
NeuralNetwork network = bot.getNeuralNetwork();
|
||||
List<String> strings = new ArrayList<>();
|
||||
|
||||
network.nodes().forEach((nodeType, node) -> {
|
||||
strings.add("");
|
||||
strings.add(ChatColor.YELLOW + "\"" + nodeType.name().toLowerCase() + "\"" + ChatColor.RESET + ":");
|
||||
List<String> values = new ArrayList<>();
|
||||
node.getValues().forEach((dataType, value) -> values.add(ChatUtils.BULLET_FORMATTED + "node"
|
||||
+ dataType.getShorthand().toUpperCase() + ": " + ChatColor.RED + MathUtils.round2Dec(value)));
|
||||
strings.addAll(values);
|
||||
});
|
||||
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
sender.sendMessage(ChatColor.DARK_GREEN + "NeuralNetwork" + ChatUtils.BULLET_FORMATTED + ChatColor.GRAY + "[" + ChatColor.GREEN + name + ChatColor.GRAY + "]");
|
||||
strings.forEach(sender::sendMessage);
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
}
|
||||
|
||||
catch (Exception e) {
|
||||
sender.sendMessage(ChatUtils.EXCEPTION_MESSAGE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Autofill
|
||||
public List<String> infoAutofill(CommandSender sender, String[] args) {
|
||||
if (args.length == 2) {
|
||||
return manager.fetchNames();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
package net.nuggetmc.tplus.command.commands;
|
||||
|
||||
import net.nuggetmc.tplus.TerminatorPlus;
|
||||
import net.nuggetmc.tplus.bot.Bot;
|
||||
import net.nuggetmc.tplus.bot.BotManager;
|
||||
import net.nuggetmc.tplus.bot.agent.legacyagent.EnumTargetGoal;
|
||||
import net.nuggetmc.tplus.bot.agent.legacyagent.LegacyAgent;
|
||||
import net.nuggetmc.tplus.command.CommandHandler;
|
||||
import net.nuggetmc.tplus.command.CommandInstance;
|
||||
import net.nuggetmc.tplus.command.annotation.Autofill;
|
||||
import net.nuggetmc.tplus.command.annotation.Command;
|
||||
import net.nuggetmc.tplus.utils.ChatUtils;
|
||||
import net.nuggetmc.tplus.utils.Debugger;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class BotCommand extends CommandInstance {
|
||||
|
||||
private final TerminatorPlus plugin;
|
||||
private final CommandHandler handler;
|
||||
private final BotManager manager;
|
||||
private final LegacyAgent agent;
|
||||
private final BukkitScheduler scheduler;
|
||||
private final DecimalFormat formatter;
|
||||
|
||||
private AICommand aiManager;
|
||||
|
||||
public BotCommand(CommandHandler handler, String name, String description, String... aliases) {
|
||||
super(handler, name, description, aliases);
|
||||
|
||||
this.handler = commandHandler;
|
||||
this.plugin = TerminatorPlus.getInstance();
|
||||
this.manager = plugin.getManager();
|
||||
this.agent = (LegacyAgent) manager.getAgent();
|
||||
this.scheduler = Bukkit.getScheduler();
|
||||
this.formatter = new DecimalFormat("0.##");
|
||||
}
|
||||
|
||||
@Command
|
||||
public void root(CommandSender sender, List<String> args) {
|
||||
commandHandler.sendRootInfo(this, sender);
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "create",
|
||||
desc = "Create a bot.",
|
||||
usage = "<name> [skin]"
|
||||
)
|
||||
public void create(CommandSender sender, List<String> args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.isEmpty()) {
|
||||
commandHandler.sendUsage(sender, this, "create <name> [skin]");
|
||||
return;
|
||||
}
|
||||
|
||||
String skin;
|
||||
|
||||
if (args.size() < 2) {
|
||||
skin = null;
|
||||
} else {
|
||||
skin = args.get(1);
|
||||
}
|
||||
|
||||
manager.createBots((Player) sender, args.get(0), skin, 1);
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "multi",
|
||||
desc = "Create multiple bots at once.",
|
||||
usage = "<amount> <name> [skin]"
|
||||
)
|
||||
public void multi(CommandSender sender, List<String> args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.size() < 2) {
|
||||
commandHandler.sendUsage(sender, this, "multi <amount> <name> [skin]");
|
||||
return;
|
||||
}
|
||||
|
||||
String skin;
|
||||
|
||||
if (args.size() < 3) {
|
||||
skin = null;
|
||||
} else {
|
||||
skin = args.get(2);
|
||||
}
|
||||
|
||||
int n;
|
||||
|
||||
try {
|
||||
n = Integer.parseInt(args.get(0));
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage("The amount must be an integer!");
|
||||
return;
|
||||
}
|
||||
|
||||
manager.createBots((Player) sender, args.get(1), skin, n);
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "info",
|
||||
desc = "Information about loaded bots.",
|
||||
usage = "[name]",
|
||||
autofill = "infoAutofill"
|
||||
)
|
||||
public void info(CommandSender sender, List<String> args) {
|
||||
if (args.isEmpty()) {
|
||||
commandHandler.sendUsage(sender, this, "info <name>");
|
||||
return;
|
||||
}
|
||||
|
||||
String name = args.get(0);
|
||||
|
||||
if (name == null) {
|
||||
sender.sendMessage(ChatColor.YELLOW + "Bot GUI coming soon!");
|
||||
return;
|
||||
}
|
||||
|
||||
sender.sendMessage("Processing request...");
|
||||
|
||||
scheduler.runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
Bot bot = manager.getFirst(name);
|
||||
|
||||
if (bot == null) {
|
||||
sender.sendMessage("Could not find bot " + ChatColor.GREEN + name + ChatColor.RESET + "!");
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* time created
|
||||
* current life (how long it has lived for)
|
||||
* health
|
||||
* inventory
|
||||
* current target
|
||||
* current kills
|
||||
* skin
|
||||
* neural network values (network name if loaded, otherwise RANDOM)
|
||||
*/
|
||||
|
||||
String botName = bot.getName();
|
||||
String world = ChatColor.YELLOW + bot.getBukkitEntity().getWorld().getName();
|
||||
Location loc = bot.getLocation();
|
||||
String strLoc = ChatColor.YELLOW + formatter.format(loc.getBlockX()) + ", " + formatter.format(loc.getBlockY()) + ", " + formatter.format(loc.getBlockZ());
|
||||
Vector vel = bot.getVelocity();
|
||||
String strVel = ChatColor.AQUA + formatter.format(vel.getX()) + ", " + formatter.format(vel.getY()) + ", " + formatter.format(vel.getZ());
|
||||
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
sender.sendMessage(ChatColor.GREEN + botName);
|
||||
sender.sendMessage(ChatUtils.BULLET_FORMATTED + "World: " + world);
|
||||
sender.sendMessage(ChatUtils.BULLET_FORMATTED + "Position: " + strLoc);
|
||||
sender.sendMessage(ChatUtils.BULLET_FORMATTED + "Velocity: " + strVel);
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
}
|
||||
|
||||
catch (Exception e) {
|
||||
sender.sendMessage(ChatUtils.EXCEPTION_MESSAGE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Autofill
|
||||
public List<String> infoAutofill(CommandSender sender, String[] args) {
|
||||
return args.length == 2 ? manager.fetchNames() : null;
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "reset",
|
||||
desc = "Remove all loaded bots."
|
||||
)
|
||||
public void reset(CommandSender sender, List<String> args) {
|
||||
sender.sendMessage("Removing every bot...");
|
||||
int size = manager.fetch().size();
|
||||
manager.reset();
|
||||
sender.sendMessage("Removed " + ChatColor.RED + ChatUtils.NUMBER_FORMAT.format(size) + ChatColor.RESET + " entit" + (size == 1 ? "y" : "ies") + ".");
|
||||
|
||||
if (aiManager == null) {
|
||||
this.aiManager = (AICommand) handler.getCommand("ai");
|
||||
}
|
||||
|
||||
if (aiManager != null && aiManager.hasActiveSession()) {
|
||||
Bukkit.dispatchCommand(sender, "ai stop");
|
||||
}
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "settings",
|
||||
desc = "Make changes to the global configuration file and bot-specific settings.",
|
||||
aliases = "options",
|
||||
autofill = "settingsAutofill"
|
||||
)
|
||||
public void settings(CommandSender sender, List<String> args) {
|
||||
String arg1 = args.isEmpty() ? null : args.get(0);
|
||||
String arg2 = args.size() < 2 ? null : args.get(1);
|
||||
|
||||
String extra = ChatColor.GRAY + " [" + ChatColor.YELLOW + "/bot settings" + ChatColor.GRAY + "]";
|
||||
|
||||
if (arg1 == null || !arg1.equals("setgoal")) {
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
sender.sendMessage(ChatColor.GOLD + "Bot Settings" + extra);
|
||||
sender.sendMessage(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "setgoal" + ChatUtils.BULLET_FORMATTED + "Set the global bot target selection method.");
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
return;
|
||||
}
|
||||
|
||||
EnumTargetGoal goal = EnumTargetGoal.from(arg2 == null ? "" : arg2);
|
||||
|
||||
if (goal == null) {
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
sender.sendMessage(ChatColor.GOLD + "Goal Selection Types" + extra);
|
||||
Arrays.stream(EnumTargetGoal.values()).forEach(g -> sender.sendMessage(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + g.name().replace("_", "").toLowerCase()
|
||||
+ ChatUtils.BULLET_FORMATTED + g.description()));
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
return;
|
||||
}
|
||||
|
||||
agent.setTargetType(goal);
|
||||
|
||||
sender.sendMessage("The global bot goal has been set to " + ChatColor.BLUE + goal.name() + ChatColor.RESET + ".");
|
||||
}
|
||||
|
||||
@Autofill
|
||||
public List<String> settingsAutofill(CommandSender sender, String[] args) {
|
||||
List<String> output = new ArrayList<>();
|
||||
|
||||
// More settings:
|
||||
// setitem
|
||||
// tpall
|
||||
// tprandom
|
||||
// hidenametags or nametags <show/hide>
|
||||
// sitall
|
||||
// lookall
|
||||
|
||||
if (args.length == 2) {
|
||||
output.add("setgoal");
|
||||
}
|
||||
|
||||
else if (args.length == 3) {
|
||||
if (args[1].equalsIgnoreCase("setgoal")) {
|
||||
Arrays.stream(EnumTargetGoal.values()).forEach(goal -> output.add(goal.name().replace("_", "").toLowerCase()));
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
@Command(
|
||||
name = "debug",
|
||||
desc = "Debug plugin code.",
|
||||
usage = "<expression>",
|
||||
visible = false
|
||||
)
|
||||
public void debug(CommandSender sender, List<String> args) {
|
||||
if (args.isEmpty()) {
|
||||
commandHandler.sendUsage(sender, this, "debug <expression>");
|
||||
return;
|
||||
}
|
||||
|
||||
new Debugger(sender).execute(args.get(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package net.nuggetmc.tplus.command.commands;
|
||||
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
import net.md_5.bungee.api.chat.hover.content.Text;
|
||||
import net.nuggetmc.tplus.TerminatorPlus;
|
||||
import net.nuggetmc.tplus.command.CommandHandler;
|
||||
import net.nuggetmc.tplus.command.CommandInstance;
|
||||
import net.nuggetmc.tplus.command.annotation.Command;
|
||||
import net.nuggetmc.tplus.utils.ChatUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MainCommand extends CommandInstance {
|
||||
|
||||
private BaseComponent[] rootInfo;
|
||||
|
||||
public MainCommand(CommandHandler handler, String name, String description, String... aliases) {
|
||||
super(handler, name, description, aliases);
|
||||
}
|
||||
|
||||
@Command
|
||||
public void root(CommandSender sender, List<String> args) {
|
||||
if (rootInfo == null) {
|
||||
rootInfoSetup();
|
||||
}
|
||||
|
||||
sender.spigot().sendMessage(rootInfo);
|
||||
}
|
||||
|
||||
private void rootInfoSetup() {
|
||||
ComponentBuilder message = new ComponentBuilder();
|
||||
String pluginName = TerminatorPlus.getInstance().getName();
|
||||
|
||||
message.append(ChatUtils.LINE + "\n");
|
||||
message.append(ChatColor.GOLD + pluginName + ChatColor.GRAY + " [v" + TerminatorPlus.getVersion() + "]\n");
|
||||
message.append("\nPlugin Information:\n");
|
||||
message.append(ChatUtils.BULLET_FORMATTED + "Author" + ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "HorseNuggets\n");
|
||||
message.append(ChatUtils.BULLET_FORMATTED + "Links" + ChatUtils.BULLET_FORMATTED);
|
||||
message.append(ChatColor.RED + "YouTube");
|
||||
message.event(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://youtube.com/horsenuggets"));
|
||||
message.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("Click to visit HorseNuggets' " + ChatColor.RED + "YouTube" + ChatColor.RESET + "!")));
|
||||
message.append(", ");
|
||||
message.event((ClickEvent) null);
|
||||
message.event((HoverEvent) null);
|
||||
message.append(ChatColor.BLUE + "Discord");
|
||||
message.event(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://discord.gg/horsenuggets"));
|
||||
message.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("Click to visit HorseNuggets' " + ChatColor.BLUE + "Discord" + ChatColor.RESET + "!")));
|
||||
message.append("\n");
|
||||
message.event((ClickEvent) null);
|
||||
message.event((HoverEvent) null);
|
||||
message.append("\nPlugin Commands:\n");
|
||||
|
||||
commandHandler.getCommands().forEach((name, command) -> {
|
||||
if (!name.equalsIgnoreCase(pluginName)) {
|
||||
message.append(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "/" + name + ChatUtils.BULLET_FORMATTED + command.getDescription() + "\n");
|
||||
}
|
||||
});
|
||||
|
||||
message.append(ChatUtils.LINE);
|
||||
|
||||
this.rootInfo = message.create();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package net.nuggetmc.tplus.command.nms;
|
||||
|
||||
public class TPCommand {
|
||||
// this class (as well as some others like "give", "effect", etc.) will listen into the PlayerCommandPreProcessEvent
|
||||
// and will perform the actions accordingly to the bots as well as the real players
|
||||
}
|
||||
Reference in New Issue
Block a user