Added console support, added location args and removed spawnLoc setting

This commit is contained in:
Badbird5907
2023-03-01 10:02:47 -05:00
parent c02f88dd94
commit 82b354e7d6
5 changed files with 145 additions and 126 deletions

View File

@@ -3,6 +3,7 @@ package net.nuggetmc.tplus.api;
import net.nuggetmc.tplus.api.agent.Agent;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
@@ -10,10 +11,6 @@ import java.util.Set;
import java.util.UUID;
public interface BotManager {
Location getSpawnLoc();
void setSpawnLoc(Location loc);
Set<Terminator> fetch();
Agent getAgent();
@@ -24,9 +21,9 @@ public interface BotManager {
List<String> fetchNames();
void createBots(Player sender, String name, String skinName, int n);
void createBots(CommandSender sender, String name, String skinName, int n, Location location);
void createBots(Player sender, String name, String skinName, int n, NeuralNetwork network);
void createBots(CommandSender sender, String name, String skinName, int n, NeuralNetwork network, Location location);
Set<Terminator> createBots(Location loc, String name, String[] skin, List<NeuralNetwork> networks);

View File

@@ -10,6 +10,7 @@ import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork;
import net.nuggetmc.tplus.api.event.BotDeathEvent;
import net.nuggetmc.tplus.api.utils.MojangAPI;
import org.bukkit.*;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_19_R2.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
@@ -36,7 +37,6 @@ public class BotManagerImpl implements BotManager, Listener {
public boolean joinMessages = false;
private boolean mobTarget = false;
private boolean addPlayerList = false;
private Location spawnLoc;
public BotManagerImpl() {
this.agent = new LegacyAgent(this, TerminatorPlus.getInstance());
@@ -44,16 +44,6 @@ public class BotManagerImpl implements BotManager, Listener {
this.numberFormat = NumberFormat.getInstance(Locale.US);
}
@Override
public Location getSpawnLoc() {
return spawnLoc;
}
@Override
public void setSpawnLoc(Location loc) {
spawnLoc = loc;
}
@Override
public Set<Terminator> fetch() {
return bots;
@@ -70,16 +60,16 @@ public class BotManagerImpl implements BotManager, Listener {
@Override
public Terminator getFirst(String name, Location target) {
if (target != null) {
Terminator closest = null;
for (Terminator bot : bots) {
if (name.equals(bot.getBotName()) && (closest == null
|| target.distanceSquared(bot.getLocation()) < target.distanceSquared(closest.getLocation()))) {
closest = bot;
}
}
return closest;
}
if (target != null) {
Terminator closest = null;
for (Terminator bot : bots) {
if (name.equals(bot.getBotName()) && (closest == null
|| target.distanceSquared(bot.getLocation()) < target.distanceSquared(closest.getLocation()))) {
closest = bot;
}
}
return closest;
}
for (Terminator bot : bots) {
if (name.equals(bot.getBotName())) {
return bot;
@@ -104,12 +94,12 @@ public class BotManagerImpl implements BotManager, Listener {
}
@Override
public void createBots(Player sender, String name, String skinName, int n) {
createBots(sender, name, skinName, n, null);
public void createBots(CommandSender sender, String name, String skinName, int n, Location loc) {
createBots(sender, name, skinName, n, null, loc);
}
@Override
public void createBots(Player sender, String name, String skinName, int n, NeuralNetwork network) {
public void createBots(CommandSender sender, String name, String skinName, int n, NeuralNetwork network, Location location) {
long timestamp = System.currentTimeMillis();
if (n < 1) n = 1;
@@ -121,18 +111,17 @@ public class BotManagerImpl implements BotManager, Listener {
skinName = skinName == null ? name : skinName;
if (spawnLoc != null) {
sender.sendMessage("The spawn location is "
+ ChatColor.BLUE + String.format("(%s, %s, %s)", spawnLoc.getX(), spawnLoc.getY(), spawnLoc.getZ()) + ChatColor.RESET
+ ". This will be reset to the player location next time.");
Location loc = sender.getLocation().clone();
loc.setX(spawnLoc.getX());
loc.setY(spawnLoc.getY());
loc.setZ(spawnLoc.getZ());
createBots(loc, name, MojangAPI.getSkin(skinName), n, network);
spawnLoc = null;
} else
createBots(sender.getLocation(), name, MojangAPI.getSkin(skinName), n, network);
if (location != null) {
createBots(location, name, MojangAPI.getSkin(skinName), n, network);
} else {
if (sender instanceof Player player)
createBots(player.getLocation(), name, MojangAPI.getSkin(skinName), n, network);
else {
Location l = new Location(Bukkit.getWorlds().get(0), 0, 0, 0);
sender.sendMessage(ChatColor.RED + "No location specified, defaulting to " + l + ".");
createBots(l, name, MojangAPI.getSkin(skinName), n, network);
}
}
sender.sendMessage("Process completed (" + ChatColor.RED + ((System.currentTimeMillis() - timestamp) / 1000D) + "s" + ChatColor.RESET + ").");
}
@@ -265,8 +254,8 @@ public class BotManagerImpl implements BotManager, Listener {
@EventHandler
public void onMobTarget(EntityTargetLivingEntityEvent event) {
if (mobTarget || event.getTarget() == null)
return;
if (mobTarget || event.getTarget() == null)
return;
Bot bot = (Bot) getBot(event.getTarget().getUniqueId());
if (bot != null) {
event.setCancelled(true);

View File

@@ -10,12 +10,11 @@ import net.nuggetmc.tplus.api.utils.MathUtils;
import net.nuggetmc.tplus.bot.BotManagerImpl;
import net.nuggetmc.tplus.command.CommandHandler;
import net.nuggetmc.tplus.command.CommandInstance;
import net.nuggetmc.tplus.command.annotation.Arg;
import net.nuggetmc.tplus.command.annotation.Autofill;
import net.nuggetmc.tplus.command.annotation.Command;
import net.nuggetmc.tplus.command.annotation.OptArg;
import net.nuggetmc.tplus.command.annotation.*;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
@@ -51,16 +50,48 @@ public class AICommand extends CommandInstance implements AIManager {
}
@Command(
name = "random",
desc = "Create bots with random neural networks, collecting feed data."
name = "random",
desc = "Create bots with random neural networks, collecting feed data."
)
public void random(Player sender, @Arg("amount") int amount, @Arg("name") String name, @OptArg("skin") String skin) {
manager.createBots(sender, name, skin, amount, NeuralNetwork.RANDOM);
public void random(CommandSender sender, List<String> args, @Arg("amount") int amount, @Arg("name") String name, @OptArg("skin") String skin, @OptArg("loc") @TextArg String loc) {
if (sender instanceof Player && args.size() < 2) {
sender.sendMessage(ChatColor.RED + "Usage: /ai random <amount> <name> [skin] [spawnLoc: [player Player]/[x,y,z]]");
return;
}
Location location = (sender instanceof Player) ? ((Player) sender).getLocation() : new Location(Bukkit.getWorlds().get(0), 0, 0, 0);
if (loc != null && !loc.isEmpty()) {
Player player = Bukkit.getPlayer(loc);
if (player != null) {
location = player.getLocation();
} else {
String[] split = loc.split(" ");
if (split.length >= 3) {
try {
double x = Double.parseDouble(split[0]);
double y = Double.parseDouble(split[1]);
double z = Double.parseDouble(split[2]);
World world = Bukkit.getWorld(split.length >= 4 ? split[3] : location.getWorld().getName());
location = new Location(world, x, y, z);
} catch (NumberFormatException e) {
sender.sendMessage("The location '" + ChatColor.YELLOW + loc + ChatColor.RESET + "' is not valid!");
return;
}
} else {
sender.sendMessage("The location '" + ChatColor.YELLOW + loc + ChatColor.RESET + "' is not valid!");
return;
}
}
} else {
if (!(sender instanceof Player)) {
sender.sendMessage("Spawning bot at 0, 0, 0 in world " + location.getWorld().getName() + " because no location was specified.");
}
}
manager.createBots(sender, name, skin, amount, NeuralNetwork.RANDOM, location);
}
@Command(
name = "reinforcement",
desc = "Begin an AI training session."
name = "reinforcement",
desc = "Begin an AI training session."
)
public void reinforcement(Player sender, @Arg("population-size") int populationSize, @Arg("name") String name, @OptArg("skin") String skin) {
//FIXME: Sometimes, bots will become invisible, or just stop working if they're the last one alive, this has been partially fixed (invis part) see Terminator#removeBot, which removes the bot.
@@ -83,8 +114,8 @@ public class AICommand extends CommandInstance implements AIManager {
}
@Command(
name = "stop",
desc = "End a currently running AI training session."
name = "stop",
desc = "End a currently running AI training session."
)
public void stop(CommandSender sender) {
if (agent == null) {
@@ -112,9 +143,9 @@ public class AICommand extends CommandInstance implements AIManager {
}
@Command(
name = "info",
desc = "Display neural network information about a bot.",
autofill = "infoAutofill"
name = "info",
desc = "Display neural network information about a bot.",
autofill = "infoAutofill"
)
public void info(CommandSender sender, @Arg("bot-name") String name) {
sender.sendMessage("Processing request...");
@@ -149,9 +180,7 @@ public class AICommand extends CommandInstance implements AIManager {
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) {
} catch (Exception e) {
sender.sendMessage(ChatUtils.EXCEPTION_MESSAGE);
}
});

View File

@@ -8,15 +8,9 @@ import net.nuggetmc.tplus.api.utils.ChatUtils;
import net.nuggetmc.tplus.bot.BotManagerImpl;
import net.nuggetmc.tplus.command.CommandHandler;
import net.nuggetmc.tplus.command.CommandInstance;
import net.nuggetmc.tplus.command.annotation.Arg;
import net.nuggetmc.tplus.command.annotation.Autofill;
import net.nuggetmc.tplus.command.annotation.Command;
import net.nuggetmc.tplus.command.annotation.OptArg;
import net.nuggetmc.tplus.command.annotation.*;
import net.nuggetmc.tplus.utils.Debugger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.*;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
@@ -64,16 +58,72 @@ public class BotCommand extends CommandInstance {
name = "create",
desc = "Create a bot."
)
public void create(Player sender, @Arg("name") String name, @OptArg("skin") String skin) {
manager.createBots(sender, name, skin, 1);
public void create(CommandSender sender, @Arg("name") String name, @OptArg("skin") String skin, @TextArg @OptArg("loc") String loc) {
Location location = (sender instanceof Player) ? ((Player) sender).getLocation() : new Location(Bukkit.getWorlds().get(0), 0, 0, 0);
if (loc != null && !loc.isEmpty()) {
Player player = Bukkit.getPlayer(loc);
if (player != null) {
location = player.getLocation();
} else {
String[] split = loc.split(" ");
if (split.length >= 3) {
try {
double x = Double.parseDouble(split[0]);
double y = Double.parseDouble(split[1]);
double z = Double.parseDouble(split[2]);
World world = Bukkit.getWorld(split.length >= 4 ? split[3] : location.getWorld().getName());
location = new Location(world, x, y, z);
} catch (NumberFormatException e) {
sender.sendMessage("The location '" + ChatColor.YELLOW + loc + ChatColor.RESET + "' is not valid!");
return;
}
} else {
sender.sendMessage("The location '" + ChatColor.YELLOW + loc + ChatColor.RESET + "' is not valid!");
return;
}
}
} else {
if (!(sender instanceof Player)) {
sender.sendMessage("Spawning bot at 0, 0, 0 in world " + location.getWorld().getName() + " because no location was specified.");
}
}
manager.createBots(sender, name, skin, 1, location);
}
@Command(
name = "multi",
desc = "Create multiple bots at once."
)
public void multi(Player sender, @Arg("amount") int amount, @Arg("name") String name, @OptArg("skin") String skin) {
manager.createBots(sender, name, skin, amount);
public void multi(CommandSender sender, @Arg("amount") int amount, @Arg("name") String name, @OptArg("skin") String skin, @TextArg @OptArg("loc") String loc) {
Location location = (sender instanceof Player) ? ((Player) sender).getLocation() : new Location(Bukkit.getWorlds().get(0), 0, 0, 0);
if (loc != null && !loc.isEmpty()) {
Player player = Bukkit.getPlayer(loc);
if (player != null) {
location = player.getLocation();
} else {
String[] split = loc.split(" ");
if (split.length >= 3) {
try {
double x = Double.parseDouble(split[0]);
double y = Double.parseDouble(split[1]);
double z = Double.parseDouble(split[2]);
World world = Bukkit.getWorld(split.length >= 4 ? split[3] : location.getWorld().getName());
location = new Location(world, x, y, z);
} catch (NumberFormatException e) {
sender.sendMessage("The location '" + ChatColor.YELLOW + loc + ChatColor.RESET + "' is not valid!");
return;
}
} else {
sender.sendMessage("The location '" + ChatColor.YELLOW + loc + ChatColor.RESET + "' is not valid!");
return;
}
}
} else {
if (!(sender instanceof Player)) {
sender.sendMessage("Spawning bot at 0, 0, 0 in world " + location.getWorld().getName() + " because no location was specified.");
}
}
manager.createBots(sender, name, skin, amount, location);
}
@Command(
@@ -156,8 +206,7 @@ public class BotCommand extends CommandInstance {
ItemStack[] armor = armorTiers.get(tier);
manager.fetch().forEach(bot -> {
if (bot.getBukkitEntity() instanceof Player) {
Player botPlayer = (Player) bot.getBukkitEntity();
if (bot.getBukkitEntity() instanceof Player botPlayer) {
botPlayer.getInventory().setArmorContents(armor);
botPlayer.updateInventory();
@@ -289,11 +338,10 @@ public class BotCommand extends CommandInstance {
String extra = ChatColor.GRAY + " [" + ChatColor.YELLOW + "/bot settings" + ChatColor.GRAY + "]";
if (arg1 == null || (!arg1.equalsIgnoreCase("spawnloc") && !arg1.equalsIgnoreCase("setgoal") && !arg1.equalsIgnoreCase("mobtarget") && !arg1.equalsIgnoreCase("playertarget")
if (arg1 == null || (!arg1.equalsIgnoreCase("setgoal") && !arg1.equalsIgnoreCase("mobtarget") && !arg1.equalsIgnoreCase("playertarget")
&& !arg1.equalsIgnoreCase("addplayerlist") && !arg1.equalsIgnoreCase("region"))) {
sender.sendMessage(ChatUtils.LINE);
sender.sendMessage(ChatColor.GOLD + "Bot Settings" + extra);
sender.sendMessage(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "spawnloc" + ChatUtils.BULLET_FORMATTED + "Set the location where the bots should spawn. This will be reset after a spawn command is executed.");
sender.sendMessage(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "setgoal" + ChatUtils.BULLET_FORMATTED + "Set the global bot target selection method.");
sender.sendMessage(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "mobtarget" + ChatUtils.BULLET_FORMATTED + "Allow all bots to be targeted by hostile mobs.");
sender.sendMessage(ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "playertarget" + ChatUtils.BULLET_FORMATTED + "Sets a player name for spawned bots to focus on if the goal is PLAYER.");
@@ -302,50 +350,7 @@ public class BotCommand extends CommandInstance {
sender.sendMessage(ChatUtils.LINE);
return;
}
if (arg1.equalsIgnoreCase("spawnloc")) {
if (arg2 == null) {
if (manager.getSpawnLoc() == null)
sender.sendMessage("No custom spawn location has been set. The bots will spawn at the player location.");
else {
Location loc = manager.getSpawnLoc();
sender.sendMessage("The next spawn location will be at " + ChatColor.BLUE + String.format("(%s, %s, %s)", loc.getX(), loc.getY(), loc.getZ()) + ChatColor.RESET + ".");
}
return;
}
if (arg2.equalsIgnoreCase("clear")) {
manager.setSpawnLoc(null);
sender.sendMessage("The spawn location has been reset to the player location.");
return;
}
if (arg2.equalsIgnoreCase("playerloc")) {
if (!(sender instanceof Player)) {
sender.sendMessage("You must be a player to do this!");
return;
}
Location loc = ((Player)sender).getLocation();
manager.setSpawnLoc(loc.clone());
sender.sendMessage("The spawn location has been set to " + ChatColor.BLUE + formatter.format(loc.getX()) + ", " + formatter.format(loc.getY()) + ", " + formatter.format(loc.getZ()) + ChatColor.RESET + ".");
return;
}
if (args.size() != 4) {
sender.sendMessage("Incorrect argument size. Correct syntax: " + ChatColor.YELLOW + "/bot settings spawnloc <x> <y> <z>" + ChatColor.RESET);
sender.sendMessage("Additionally, to specify a spawnloc at the current player position: " + ChatColor.YELLOW + "/bot settings spawnloc playerloc" + ChatColor.RESET);
return;
}
double x, y, z;
try {
x = Double.parseDouble(args.get(1));
y = Double.parseDouble(args.get(2));
z = Double.parseDouble(args.get(3));
} catch (NumberFormatException e) {
sender.sendMessage("The block coordinates must be doubles!");
sender.sendMessage("Correct syntax: " + ChatColor.YELLOW + "/bot settings spawnloc <x> <y> <z>" + ChatColor.RESET);
return;
}
manager.setSpawnLoc(new Location(null, x, y, z));
sender.sendMessage("The next spawn location has been set to " + ChatColor.BLUE + String.format("(%s, %s, %s)", x, y, z) + ChatColor.RESET + ".");
} else if (arg1.equalsIgnoreCase("setgoal")) {
else if (arg1.equalsIgnoreCase("setgoal")) {
if (arg2 == null) {
sender.sendMessage("The global bot goal is currently " + ChatColor.BLUE + agent.getTargetType() + ChatColor.RESET + ".");
return;
@@ -486,7 +491,6 @@ public class BotCommand extends CommandInstance {
// lookall
if (args.length == 2) {
output.add("spawnloc");
output.add("setgoal");
output.add("mobtarget");
output.add("playertarget");

View File

@@ -3,4 +3,4 @@ plugins {
}
group = "net.nuggetmc"
version = "4.1.0-BETA"
version = "4.2.0-BETA"