Files
Tplus/src/main/java/net/nuggetmc/tplus/command/commands/BotCommand.java

297 lines
9.8 KiB
Java
Raw Normal View History

2021-08-24 19:54:46 -05:00
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;
2021-07-21 13:52:21 -05:00
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
2021-08-25 02:49:31 +01:00
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
2021-07-21 13:52:21 -05:00
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector;
import java.text.DecimalFormat;
2021-07-21 17:18:36 -05:00
import java.util.ArrayList;
2021-08-21 13:36:10 -05:00
import java.util.Arrays;
2021-07-21 17:18:36 -05:00
import java.util.List;
2021-07-21 13:52:21 -05:00
public class BotCommand extends CommandInstance {
private final TerminatorPlus plugin;
2021-08-21 13:36:10 -05:00
private final CommandHandler handler;
2021-07-21 13:52:21 -05:00
private final BotManager manager;
2021-08-21 13:36:10 -05:00
private final LegacyAgent agent;
2021-07-21 13:52:21 -05:00
private final BukkitScheduler scheduler;
private final DecimalFormat formatter;
2021-07-24 23:34:07 -05:00
private AICommand aiManager;
2021-08-24 19:54:46 -05:00
public BotCommand(CommandHandler handler, String name, String description, String... aliases) {
super(handler, name, description, aliases);
2021-07-21 13:52:21 -05:00
2021-08-21 13:36:10 -05:00
this.handler = commandHandler;
2021-07-21 13:52:21 -05:00
this.plugin = TerminatorPlus.getInstance();
this.manager = plugin.getManager();
2021-08-21 13:36:10 -05:00
this.agent = (LegacyAgent) manager.getAgent();
2021-07-21 13:52:21 -05:00
this.scheduler = Bukkit.getScheduler();
this.formatter = new DecimalFormat("0.##");
2021-08-21 13:36:10 -05:00
}
2021-07-24 23:34:07 -05:00
2021-08-24 19:54:46 -05:00
@Command
public void root(CommandSender sender, List<String> args) {
2021-07-21 17:18:36 -05:00
commandHandler.sendRootInfo(this, sender);
2021-07-21 13:52:21 -05:00
}
@Command(
name = "create",
desc = "Create a bot.",
usage = "<name> [skin]"
)
2021-08-24 19:54:46 -05:00
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);
2021-07-21 13:52:21 -05:00
}
@Command(
name = "multi",
desc = "Create multiple bots at once.",
usage = "<amount> <name> [skin]"
)
2021-08-24 19:54:46 -05:00
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);
2021-07-21 13:52:21 -05:00
}
2021-08-25 02:49:31 +01:00
@Command(
name = "give",
desc = "Gives specified item to all bots.",
usage = "<item>"
)
public void give(CommandSender sender, List<String> args) {
String i = args.get(0);
Material item = Material.matchMaterial(i);
ItemStack itemToGive = new ItemStack(item);
if (item == null) {
sender.sendMessage(ChatColor.RED + "Failed to give all bots a " + ChatColor.YELLOW + item);
} else {
TerminatorPlus.getInstance().getManager().fetch().forEach(b -> b.setDefaultItem(itemToGive));
sender.sendMessage(ChatColor.GREEN + "Successfully gave all bots a " + ChatColor.BLUE + item);
}
2021-08-25 02:49:31 +01:00
}
2021-07-21 13:52:21 -05:00
@Command(
name = "info",
desc = "Information about loaded bots.",
2021-07-24 23:34:07 -05:00
usage = "[name]",
autofill = "infoAutofill"
2021-07-21 13:52:21 -05:00
)
2021-08-24 19:54:46 -05:00
public void info(CommandSender sender, List<String> args) {
if (args.isEmpty()) {
commandHandler.sendUsage(sender, this, "info <name>");
return;
}
String name = args.get(0);
2021-07-21 13:52:21 -05:00
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
2021-08-21 13:36:10 -05:00
* neural network values (network name if loaded, otherwise RANDOM)
2021-07-21 13:52:21 -05:00
*/
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) {
2021-07-24 23:34:07 -05:00
sender.sendMessage(ChatUtils.EXCEPTION_MESSAGE);
2021-07-21 13:52:21 -05:00
}
});
}
2021-08-21 13:36:10 -05:00
@Autofill
2021-07-24 23:34:07 -05:00
public List<String> infoAutofill(CommandSender sender, String[] args) {
2021-08-21 13:36:10 -05:00
return args.length == 2 ? manager.fetchNames() : null;
2021-07-24 23:34:07 -05:00
}
2021-07-21 13:52:21 -05:00
@Command(
name = "reset",
desc = "Remove all loaded bots."
)
2021-08-24 19:54:46 -05:00
public void reset(CommandSender sender, List<String> args) {
2021-07-21 13:52:21 -05:00
sender.sendMessage("Removing every bot...");
int size = manager.fetch().size();
manager.reset();
2021-08-24 19:54:46 -05:00
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");
}
2021-07-24 23:34:07 -05:00
2021-08-24 19:54:46 -05:00
if (aiManager != null && aiManager.hasActiveSession()) {
2021-07-24 23:34:07 -05:00
Bukkit.dispatchCommand(sender, "ai stop");
}
2021-07-21 13:52:21 -05:00
}
2021-07-21 17:18:36 -05:00
@Command(
2021-08-21 13:36:10 -05:00
name = "settings",
2021-07-21 17:18:36 -05:00
desc = "Make changes to the global configuration file and bot-specific settings.",
2021-08-21 13:36:10 -05:00
aliases = "options",
autofill = "settingsAutofill"
2021-07-21 17:18:36 -05:00
)
2021-08-24 19:54:46 -05:00
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);
2021-08-21 13:36:10 -05:00
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 + ".");
2021-07-21 17:18:36 -05:00
}
2021-08-21 13:36:10 -05:00
@Autofill
public List<String> settingsAutofill(CommandSender sender, String[] args) {
2021-07-21 17:18:36 -05:00
List<String> output = new ArrayList<>();
2021-08-21 13:36:10 -05:00
// More settings:
// setitem
// tpall
// tprandom
// hidenametags or nametags <show/hide>
// sitall
// lookall
2021-07-21 17:18:36 -05:00
if (args.length == 2) {
output.add("setgoal");
2021-08-21 13:36:10 -05:00
}
else if (args.length == 3) {
if (args[1].equalsIgnoreCase("setgoal")) {
Arrays.stream(EnumTargetGoal.values()).forEach(goal -> output.add(goal.name().replace("_", "").toLowerCase()));
}
2021-07-21 17:18:36 -05:00
}
return output;
}
@Command(
name = "debug",
desc = "Debug plugin code.",
usage = "<expression>",
visible = false
)
2021-08-24 19:54:46 -05:00
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));
2021-07-21 17:18:36 -05:00
}
2021-07-21 13:52:21 -05:00
}