This commit is contained in:
batchprogrammer314
2021-08-21 13:36:10 -05:00
parent d86a42eab6
commit 84f3918c25
20 changed files with 317 additions and 186 deletions

View File

@@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2021 batchprogrammer314 Copyright (c) 2021 HorseNuggets
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@@ -43,6 +43,14 @@
</transformers> </transformers>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>D:\Minecraft Server\plugins</outputDirectory>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>

View File

@@ -2,6 +2,8 @@ package net.nuggetmc.ai.bot;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.datafixers.util.Pair; import com.mojang.datafixers.util.Pair;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import net.minecraft.server.v1_16_R3.Chunk; import net.minecraft.server.v1_16_R3.Chunk;
import net.minecraft.server.v1_16_R3.*; import net.minecraft.server.v1_16_R3.*;
import net.nuggetmc.ai.TerminatorPlus; import net.nuggetmc.ai.TerminatorPlus;
@@ -27,6 +29,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Objects; import java.util.Objects;
@@ -105,7 +108,13 @@ public class Bot extends EntityPlayer {
Bot bot = new Bot(nmsServer, nmsWorld, profile, interactManager); Bot bot = new Bot(nmsServer, nmsWorld, profile, interactManager);
bot.playerConnection = new PlayerConnection(nmsServer, new NetworkManager(EnumProtocolDirection.CLIENTBOUND), bot); bot.playerConnection = new PlayerConnection(nmsServer, new NetworkManager(EnumProtocolDirection.CLIENTBOUND) {
@Override
public void sendPacket(Packet<?> packet, @Nullable GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) { }
}, bot);
bot.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch()); bot.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
bot.getBukkitEntity().setNoDamageTicks(0); bot.getBukkitEntity().setNoDamageTicks(0);
nmsWorld.addEntity(bot); nmsWorld.addEntity(bot);
@@ -234,7 +243,7 @@ public class Bot extends EntityPlayer {
Chunk chunk = world.getChunkAt(i, j); Chunk chunk = world.getChunkAt(i, j);
if (!chunk.loaded) { if (!chunk.loaded) {
chunk.setLoaded(true); chunk.loaded = true;
} }
} }
} }
@@ -400,8 +409,10 @@ public class Bot extends EntityPlayer {
faceLocation(entity.getLocation()); faceLocation(entity.getLocation());
punch(); punch();
double damage = ItemUtils.getLegacyAttackDamage(defaultItem);
if (entity instanceof Damageable) { if (entity instanceof Damageable) {
((Damageable) entity).damage(ItemUtils.getLegacyAttackDamage(defaultItem), getBukkitEntity()); ((Damageable) entity).damage(damage, getBukkitEntity());
} }
} }
@@ -472,6 +483,8 @@ public class Bot extends EntityPlayer {
} }
private void setDead() { private void setDead() {
sendPacket(new PacketPlayOutEntityDestroy(getId()));
this.dead = true; this.dead = true;
this.defaultContainer.b(this); this.defaultContainer.b(this);
if (this.activeContainer != null) { if (this.activeContainer != null) {
@@ -481,7 +494,7 @@ public class Bot extends EntityPlayer {
private void dieCheck() { private void dieCheck() {
if (removeOnDeath) { if (removeOnDeath) {
scheduler.runTask(plugin, () -> plugin.getManager().remove(this)); scheduler.runTask(plugin, () -> plugin.getManager().remove(this)); // maybe making this later will fix the concurrentmodificationexception?
scheduler.runTaskLater(plugin, this::setDead, 30); scheduler.runTaskLater(plugin, this::setDead, 30);
this.removeTab(); this.removeTab();

View File

@@ -111,7 +111,7 @@ public class BotManager implements Listener {
bot.setNeuralNetwork(network == NeuralNetwork.RANDOM ? NeuralNetwork.generateRandomNetwork() : network); bot.setNeuralNetwork(network == NeuralNetwork.RANDOM ? NeuralNetwork.generateRandomNetwork() : network);
bot.setShield(true); bot.setShield(true);
bot.setDefaultItem(new ItemStack(Material.WOODEN_AXE)); bot.setDefaultItem(new ItemStack(Material.WOODEN_AXE));
bot.setRemoveOnDeath(false); //bot.setRemoveOnDeath(false);
} }
if (network != null) { if (network != null) {
@@ -140,11 +140,12 @@ public class BotManager implements Listener {
} }
public void reset() { public void reset() {
if (!bots.isEmpty()) {
bots.forEach(Bot::removeVisually); bots.forEach(Bot::removeVisually);
bots.clear(); // Not always necessary, but a good security measure bots.clear(); // Not always necessary, but a good security measure
agent.stopAllTasks(); }
System.gc(); agent.stopAllTasks();
} }
public Bot getBot(Player player) { // potentially memory intensive public Bot getBot(Player player) { // potentially memory intensive

View File

@@ -55,9 +55,11 @@ public abstract class Agent {
} }
public void stopAllTasks() { public void stopAllTasks() {
if (!taskList.isEmpty()) {
taskList.stream().filter(t -> !t.isCancelled()).forEach(BukkitRunnable::cancel); taskList.stream().filter(t -> !t.isCancelled()).forEach(BukkitRunnable::cancel);
taskList.clear(); taskList.clear();
} }
}
public void setDrops(boolean enabled) { public void setDrops(boolean enabled) {
this.drops = enabled; this.drops = enabled;

View File

@@ -1,32 +1,38 @@
package net.nuggetmc.ai.bot.agent.legacyagent; package net.nuggetmc.ai.bot.agent.legacyagent;
public enum EnumTargetGoal { // TODO USE ORDINAL!!!!! import java.util.HashMap;
NEAREST_REAL_VULNERABLE_PLAYER, import java.util.Map;
NEAREST_REAL_PLAYER,
NEAREST_BOT_DIFFER,
NEAREST_BOT,
NEAREST_BOT_DIFFER_ALPHA,
NONE;
public static EnumTargetGoal of(int n) { public enum EnumTargetGoal {
switch (n) { NEAREST_VULNERABLE_PLAYER("Locate the nearest real player that is in either Survival or Adventure mode."),
default: NEAREST_PLAYER("Locate the nearest real online player, despite the gamemode."),
return NONE; NEAREST_BOT("Locate the nearest bot."),
NEAREST_BOT_DIFFER("Locate the nearest bot with a different username."),
NEAREST_BOT_DIFFER_ALPHA("Locate the nearest bot with a different username after filtering out non-alpha characters."),
NONE("No target goal.");
case 1: private static final Map<String, EnumTargetGoal> VALUES = new HashMap<String, EnumTargetGoal>() {
return NEAREST_REAL_VULNERABLE_PLAYER; {
this.put("none", NONE);
this.put("nearestvulnerableplayer", NEAREST_VULNERABLE_PLAYER);
this.put("nearestplayer", NEAREST_PLAYER);
this.put("nearestbot", NEAREST_BOT);
this.put("nearestbotdiffer", NEAREST_BOT_DIFFER);
this.put("nearestbotdifferalpha", NEAREST_BOT_DIFFER_ALPHA);
}
};
case 2: private final String description;
return NEAREST_REAL_PLAYER;
case 3: EnumTargetGoal(String description) {
return NEAREST_BOT_DIFFER; this.description = description;
}
case 4: public static EnumTargetGoal from(String name) {
return NEAREST_BOT; return VALUES.get(name);
}
case 5: public String description() {
return NEAREST_BOT_DIFFER_ALPHA; return description;
}
} }
} }

View File

@@ -11,7 +11,6 @@ import net.nuggetmc.ai.bot.agent.legacyagent.ai.NeuralNetwork;
import net.nuggetmc.ai.bot.event.BotDamageByPlayerEvent; import net.nuggetmc.ai.bot.event.BotDamageByPlayerEvent;
import net.nuggetmc.ai.bot.event.BotDeathEvent; import net.nuggetmc.ai.bot.event.BotDeathEvent;
import net.nuggetmc.ai.bot.event.BotFallDamageEvent; import net.nuggetmc.ai.bot.event.BotFallDamageEvent;
import net.nuggetmc.ai.bot.event.BotKilledByPlayerEvent;
import net.nuggetmc.ai.utils.MathUtils; import net.nuggetmc.ai.utils.MathUtils;
import net.nuggetmc.ai.utils.PlayerUtils; import net.nuggetmc.ai.utils.PlayerUtils;
import org.bukkit.*; import org.bukkit.*;
@@ -35,12 +34,14 @@ public class LegacyAgent extends Agent {
private final LegacyBlockCheck blockCheck; private final LegacyBlockCheck blockCheck;
private EnumTargetGoal goal;
public boolean offsets = true; public boolean offsets = true;
public LegacyAgent(BotManager manager) { public LegacyAgent(BotManager manager) {
super(manager); super(manager);
this.goal = EnumTargetGoal.NEAREST_REAL_VULNERABLE_PLAYER; this.goal = EnumTargetGoal.NEAREST_VULNERABLE_PLAYER;
this.blockCheck = new LegacyBlockCheck(this); this.blockCheck = new LegacyBlockCheck(this);
} }
@@ -93,17 +94,18 @@ public class LegacyAgent extends Agent {
} }
private void tickBot(Bot bot) { private void tickBot(Bot bot) {
if (!bot.isAlive()) return; if (!bot.isAlive()) {
return;
}
if (bot.tickDelay(20)) { if (bot.tickDelay(20)) {
center(bot); center(bot);
} }
Location loc = bot.getLocation(); Location loc = bot.getLocation();
Player player = locateTarget(bot, loc);
Player player = nearestPlayer(bot, loc);
if (player == null) { if (player == null) {
// LESSLAG if (bot.tickDelay(20))
stopMining(bot); stopMining(bot);
return; return;
} }
@@ -114,33 +116,28 @@ public class LegacyAgent extends Agent {
miscellaneousChecks(bot, player); miscellaneousChecks(bot, player);
Player botPlayer = bot.getBukkitEntity(); Player botPlayer = bot.getBukkitEntity();
Location target = offsets ? player.getLocation().add(bot.getOffset()) : player.getLocation(); Location target = offsets ? player.getLocation().add(bot.getOffset()) : player.getLocation();
NeuralNetwork network;
boolean ai = bot.hasNeuralNetwork(); boolean ai = bot.hasNeuralNetwork();
if (ai) { NeuralNetwork network = ai ? bot.getNeuralNetwork() : null;
BotData data = BotData.generate(bot, player);
network = bot.getNeuralNetwork(); if (ai) {
network.feed(data); network.feed(BotData.generate(bot, player));
} else {
network = null;
} }
if (bot.tickDelay(3) && !miningAnim.containsKey(botPlayer)) { if (bot.tickDelay(3) && !miningAnim.containsKey(botPlayer)) {
Location a = botPlayer.getEyeLocation(); Location botEyeLoc = botPlayer.getEyeLocation();
Location b = player.getEyeLocation(); Location playerEyeLoc = player.getEyeLocation();
Location c1 = player.getLocation(); Location playerLoc = player.getLocation();
if (ai) { // force unable to block if they are more than 6/7 blocks away if (ai) {
if (network.check(BotNode.BLOCK) && loc.distance(player.getLocation()) < 6) { if (network.check(BotNode.BLOCK) && loc.distance(player.getLocation()) < 6) {
bot.block(10, 10); bot.block(10, 10);
} }
} }
if (LegacyUtils.checkFreeSpace(a, b) || LegacyUtils.checkFreeSpace(a, c1)) { if (LegacyUtils.checkFreeSpace(botEyeLoc, playerEyeLoc) || LegacyUtils.checkFreeSpace(botEyeLoc, playerLoc)) {
attack(bot, player, loc); attack(bot, player, loc);
} }
} }
@@ -1124,117 +1121,86 @@ public class LegacyAgent extends Agent {
bot.attack(player); bot.attack(player);
} }
private EnumTargetGoal goal;
public void setTargetType(EnumTargetGoal goal) { public void setTargetType(EnumTargetGoal goal) {
this.goal = goal; this.goal = goal;
} }
private Player nearestPlayer(Bot bot, Location loc) { public Player locateTarget(Bot bot, Location loc) {
Player result = null;
switch (goal) { switch (goal) {
case NEAREST_REAL_VULNERABLE_PLAYER:
return nearestRealVulnerablePlayer(loc);
case NEAREST_REAL_PLAYER:
return nearestRealPlayer(loc);
case NEAREST_BOT_DIFFER:
return nearestBotDiffer(bot, loc);
case NEAREST_BOT_DIFFER_ALPHA:
return nearestBotDifferAlpha(bot, loc);
case NEAREST_BOT:
return nearestBot(bot, loc);
default: default:
return null; return null;
}
}
private Player nearestRealPlayer(Location loc) {
Player result = null;
case NEAREST_PLAYER: {
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
if (loc.getWorld() != player.getWorld() || player.isDead()) continue; if (validateCloserPlayer(player, loc, result)) {
if (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation())) {
result = player; result = player;
} }
} }
return result; break;
} }
private Player nearestRealVulnerablePlayer(Location loc) { case NEAREST_VULNERABLE_PLAYER: {
Player result = null;
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
if (PlayerUtils.isInvincible(player.getGameMode()) || loc.getWorld() != player.getWorld() || player.isDead()) continue; if (!PlayerUtils.isInvincible(player.getGameMode()) && validateCloserPlayer(player, loc, result)) {
if (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation())) {
result = player; result = player;
} }
} }
return result; break;
} }
private Player nearestBot(Bot bot, Location loc) { case NEAREST_BOT: {
Player result = null;
for (Bot otherBot : manager.fetch()) { for (Bot otherBot : manager.fetch()) {
if (bot == otherBot) continue; if (bot != otherBot) {
Player player = otherBot.getBukkitEntity(); Player player = otherBot.getBukkitEntity();
if (loc.getWorld() != player.getWorld() || player.isDead()) continue; if (validateCloserPlayer(player, loc, result)) {
if (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation())) {
result = player; result = player;
} }
} }
return result;
} }
private Player nearestBotDiffer(Bot bot, Location loc) { break;
Player result = null; }
case NEAREST_BOT_DIFFER: {
String name = bot.getName();
for (Bot otherBot : manager.fetch()) { for (Bot otherBot : manager.fetch()) {
if (bot == otherBot) continue; if (bot != otherBot) {
Player player = otherBot.getBukkitEntity(); Player player = otherBot.getBukkitEntity();
if (!bot.getName().equals(otherBot.getName())) { if (!name.equals(otherBot.getName()) && validateCloserPlayer(player, loc, result)) {
if (loc.getWorld() != player.getWorld() || player.isDead()) continue;
if (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation())) {
result = player; result = player;
} }
} }
} }
return result; break;
} }
private Player nearestBotDifferAlpha(Bot bot, Location loc) { case NEAREST_BOT_DIFFER_ALPHA: {
Player result = null; String name = bot.getName().replaceAll("[^A-Za-z]+", "");
for (Bot otherBot : manager.fetch()) { for (Bot otherBot : manager.fetch()) {
if (bot == otherBot) continue; if (bot != otherBot) {
Player player = otherBot.getBukkitEntity(); Player player = otherBot.getBukkitEntity();
if (!bot.getName().replaceAll("[^A-Za-z]+", "").equals(otherBot.getName().replaceAll("[^A-Za-z]+", ""))) { if (!name.equals(otherBot.getName().replaceAll("[^A-Za-z]+", "")) && validateCloserPlayer(player, loc, result)) {
if (loc.getWorld() != player.getWorld() || player.isDead()) continue;
if (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation())) {
result = player; result = player;
} }
} }
} }
}
}
return result; return result;
} }
private boolean validateCloserPlayer(Player player, Location loc, Player result) {
return loc.getWorld() == player.getWorld() && !player.isDead() && (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation()));
}
} }

View File

@@ -5,6 +5,10 @@ import org.bukkit.Material;
public class LegacyWorldManager { public class LegacyWorldManager {
/*
* This is where the respawning queue will be managed
*/
public static boolean aboveGround(Location loc) { public static boolean aboveGround(Location loc) {
int y = 1; int y = 1;

View File

@@ -29,6 +29,7 @@ public class IntelligenceAgent {
* commands /ai stop and /ai pause * commands /ai stop and /ai pause
* if a session with name already exists keep adding underscores * if a session with name already exists keep adding underscores
* /ai conclude or /ai finish * /ai conclude or /ai finish
* default anchor location, /ai relocateanchor
*/ */
private final TerminatorPlus plugin; private final TerminatorPlus plugin;
@@ -54,7 +55,6 @@ public class IntelligenceAgent {
private Player primary; private Player primary;
private final Set<CommandSender> users; private final Set<CommandSender> users;
private final Map<Integer, Set<Map<BotNode, Map<BotDataType, Double>>>> genProfiles; private final Map<Integer, Set<Map<BotNode, Map<BotDataType, Double>>>> genProfiles;
public IntelligenceAgent(AICommand aiManager, int populationSize, String name, String skin) { public IntelligenceAgent(AICommand aiManager, int populationSize, String name, String skin) {
@@ -215,7 +215,7 @@ public class IntelligenceAgent {
Set<Map<BotNode, Map<BotDataType, Double>>> profiles = new HashSet<>(); Set<Map<BotNode, Map<BotDataType, Double>>> profiles = new HashSet<>();
double mutationSize = MathUtils.getMutationSize(generation); double mutationSize = Math.pow(Math.E, 2); //MathUtils.getMutationSize(generation);
for (int j = 0; j < populationSize; j++) { for (int j = 0; j < populationSize; j++) {
Map<BotNode, Map<BotDataType, Double>> profile = new HashMap<>(); Map<BotNode, Map<BotDataType, Double>> profile = new HashMap<>();
@@ -272,6 +272,8 @@ public class IntelligenceAgent {
} }
public void addUser(CommandSender sender) { public void addUser(CommandSender sender) {
if (users.contains(sender)) return;
users.add(sender); users.add(sender);
print(sender.getName() + " has been added to the userlist."); print(sender.getName() + " has been added to the userlist.");
@@ -322,7 +324,14 @@ public class IntelligenceAgent {
} }
private void clearBots() { private void clearBots() {
print("Removing all current bots..."); if (!bots.isEmpty()) {
print("Removing all cached bots...");
bots.values().forEach(Bot::removeVisually);
bots.clear();
}
/*print("Removing all current bots...");
int size = manager.fetch().size(); int size = manager.fetch().size();
manager.reset(); manager.reset();
@@ -330,6 +339,6 @@ public class IntelligenceAgent {
String formatted = NumberFormat.getNumberInstance(Locale.US).format(size); String formatted = NumberFormat.getNumberInstance(Locale.US).format(size);
print("Removed " + ChatColor.RED + formatted + ChatColor.RESET + " entit" + (size == 1 ? "y" : "ies") + "."); print("Removed " + ChatColor.RED + formatted + ChatColor.RESET + " entit" + (size == 1 ? "y" : "ies") + ".");
bots.clear(); bots.clear();*/
} }
} }

View File

@@ -3,6 +3,7 @@ package net.nuggetmc.ai.bot.agent.legacyagent.ai;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
public class NodeConnections { public class NodeConnections {
@@ -30,7 +31,7 @@ public class NodeConnections {
} }
private double generateValue() { private double generateValue() {
return Math.random() * 20 - 10; return ThreadLocalRandom.current().nextDouble(-10, 10);
} }
public boolean check() { public boolean check() {

View File

@@ -37,6 +37,7 @@ public class CommandHandler {
this.commandMap = new HashMap<>(); this.commandMap = new HashMap<>();
this.registerCommands(); this.registerCommands();
this.drink.registerCommands(); this.drink.registerCommands();
this.commandMap.values().forEach(CommandInstance::onLoad);
} }
private void registerCommands() { private void registerCommands() {
@@ -52,7 +53,7 @@ public class CommandHandler {
setHelp(handler.getClass()); setHelp(handler.getClass());
} }
public CommandInstance getComand(String name) { public CommandInstance getCommand(String name) {
return commandMap.get(name); return commandMap.get(name);
} }

View File

@@ -21,4 +21,6 @@ public abstract class CommandInstance {
public CommandHandler getCommandHandler() { public CommandHandler getCommandHandler() {
return commandHandler; return commandHandler;
} }
protected void onLoad() { }
} }

View File

@@ -1,5 +1,6 @@
package net.nuggetmc.ai.command.commands; package net.nuggetmc.ai.command.commands;
import com.jonahseguin.drink.annotation.Autofill;
import com.jonahseguin.drink.annotation.Command; import com.jonahseguin.drink.annotation.Command;
import com.jonahseguin.drink.annotation.OptArg; import com.jonahseguin.drink.annotation.OptArg;
import com.jonahseguin.drink.annotation.Sender; import com.jonahseguin.drink.annotation.Sender;
@@ -82,6 +83,10 @@ public class AICommand extends CommandInstance {
agent.addUser(sender); agent.addUser(sender);
} }
public IntelligenceAgent getSession() {
return agent;
}
@Command( @Command(
name = "stop", name = "stop",
desc = "End a currently running AI training session." desc = "End a currently running AI training session."
@@ -157,6 +162,7 @@ public class AICommand extends CommandInstance {
}); });
} }
@Autofill
public List<String> infoAutofill(CommandSender sender, String[] args) { public List<String> infoAutofill(CommandSender sender, String[] args) {
if (args.length == 2) { if (args.length == 2) {
return manager.fetchNames(); return manager.fetchNames();

View File

@@ -1,16 +1,16 @@
package net.nuggetmc.ai.command.commands; package net.nuggetmc.ai.command.commands;
import com.jonahseguin.drink.annotation.Command; import com.jonahseguin.drink.annotation.*;
import com.jonahseguin.drink.annotation.OptArg;
import com.jonahseguin.drink.annotation.Sender;
import com.jonahseguin.drink.annotation.Text;
import com.jonahseguin.drink.utils.ChatUtils; import com.jonahseguin.drink.utils.ChatUtils;
import net.nuggetmc.ai.TerminatorPlus; import net.nuggetmc.ai.TerminatorPlus;
import net.nuggetmc.ai.bot.Bot; import net.nuggetmc.ai.bot.Bot;
import net.nuggetmc.ai.bot.BotManager; import net.nuggetmc.ai.bot.BotManager;
import net.nuggetmc.ai.bot.agent.legacyagent.EnumTargetGoal;
import net.nuggetmc.ai.bot.agent.legacyagent.LegacyAgent;
import net.nuggetmc.ai.command.CommandHandler; import net.nuggetmc.ai.command.CommandHandler;
import net.nuggetmc.ai.command.CommandInstance; import net.nuggetmc.ai.command.CommandInstance;
import net.nuggetmc.ai.utils.Debugger; import net.nuggetmc.ai.utils.Debugger;
import net.nuggetmc.ai.utils.StringUtilities;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
@@ -20,15 +20,16 @@ import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale;
public class BotCommand extends CommandInstance { public class BotCommand extends CommandInstance {
private final TerminatorPlus plugin; private final TerminatorPlus plugin;
private final CommandHandler handler;
private final BotManager manager; private final BotManager manager;
private final LegacyAgent agent;
private final BukkitScheduler scheduler; private final BukkitScheduler scheduler;
private final DecimalFormat formatter; private final DecimalFormat formatter;
@@ -37,12 +38,17 @@ public class BotCommand extends CommandInstance {
public BotCommand(CommandHandler commandHandler) { public BotCommand(CommandHandler commandHandler) {
super(commandHandler); super(commandHandler);
this.handler = commandHandler;
this.plugin = TerminatorPlus.getInstance(); this.plugin = TerminatorPlus.getInstance();
this.manager = plugin.getManager(); this.manager = plugin.getManager();
this.agent = (LegacyAgent) manager.getAgent();
this.scheduler = Bukkit.getScheduler(); this.scheduler = Bukkit.getScheduler();
this.formatter = new DecimalFormat("0.##"); this.formatter = new DecimalFormat("0.##");
}
scheduler.runTask(plugin, () -> aiManager = (AICommand) plugin.getHandler().getComand("ai")); @Override
public void onLoad() {
this.aiManager = (AICommand) handler.getCommand("ai");
} }
@Command( @Command(
@@ -101,7 +107,7 @@ public class BotCommand extends CommandInstance {
* current target * current target
* current kills * current kills
* skin * skin
* neural network values * neural network values (network name if loaded, otherwise RANDOM)
*/ */
String botName = bot.getName(); String botName = bot.getName();
@@ -125,12 +131,9 @@ public class BotCommand extends CommandInstance {
}); });
} }
@Autofill
public List<String> infoAutofill(CommandSender sender, String[] args) { public List<String> infoAutofill(CommandSender sender, String[] args) {
if (args.length == 2) { return args.length == 2 ? manager.fetchNames() : null;
return manager.fetchNames();
} else {
return null;
}
} }
@Command( @Command(
@@ -139,12 +142,9 @@ public class BotCommand extends CommandInstance {
) )
public void reset(@Sender CommandSender sender) { public void reset(@Sender CommandSender sender) {
sender.sendMessage("Removing every bot..."); sender.sendMessage("Removing every bot...");
int size = manager.fetch().size(); int size = manager.fetch().size();
manager.reset(); manager.reset();
sender.sendMessage("Removed " + ChatColor.RED + StringUtilities.NUMBER_FORMAT.format(size) + ChatColor.RESET + " entit" + (size == 1 ? "y" : "ies") + ".");
String formatted = NumberFormat.getNumberInstance(Locale.US).format(size);
sender.sendMessage("Removed " + ChatColor.RED + formatted + ChatColor.RESET + " entit" + (size == 1 ? "y" : "ies") + ".");
if (aiManager.hasActiveSession()) { if (aiManager.hasActiveSession()) {
Bukkit.dispatchCommand(sender, "ai stop"); Bukkit.dispatchCommand(sender, "ai stop");
@@ -152,26 +152,58 @@ public class BotCommand extends CommandInstance {
} }
@Command( @Command(
name = "options", name = "settings",
desc = "Make changes to the global configuration file and bot-specific settings.", desc = "Make changes to the global configuration file and bot-specific settings.",
aliases = "settings", aliases = "options",
autofill = "optionsAutofill" autofill = "settingsAutofill"
) )
public void options(@Sender CommandSender sender) { public void settings(@Sender CommandSender sender, @OptArg String arg1, @OptArg String arg2) {
sender.sendMessage(ChatColor.YELLOW + "This feature is coming soon!"); 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;
} }
public List<String> optionsAutofill(CommandSender sender, String[] args) { 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<>(); List<String> output = new ArrayList<>();
// More settings:
// setitem
// tpall
// tprandom
// hidenametags or nametags <show/hide>
// sitall
// lookall
if (args.length == 2) { if (args.length == 2) {
output.add("setgoal"); output.add("setgoal");
output.add("setitem"); }
output.add("tpall");
output.add("tprandom"); else if (args.length == 3) {
output.add("hidenametags"); if (args[1].equalsIgnoreCase("setgoal")) {
output.add("sitall"); Arrays.stream(EnumTargetGoal.values()).forEach(goal -> output.add(goal.name().replace("_", "").toLowerCase()));
output.add("lookall"); }
} }
return output; return output;

View File

@@ -57,7 +57,7 @@ public class MainCommand extends CommandInstance {
message.event((ClickEvent) null); message.event((ClickEvent) null);
message.event((HoverEvent) null); message.event((HoverEvent) null);
message.append(ChatColor.BLUE + "Discord"); message.append(ChatColor.BLUE + "Discord");
message.event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "lol okay this isn't actually ready yet")); 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.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("Click to visit HorseNuggets' " + ChatColor.BLUE + "Discord" + ChatColor.RESET + "!")));
message.append("\n"); message.append("\n");
message.event((ClickEvent) null); message.event((ClickEvent) null);

View File

@@ -0,0 +1,6 @@
package net.nuggetmc.ai.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
}

View File

@@ -4,8 +4,9 @@ import net.minecraft.server.v1_16_R3.EntityLiving;
import net.nuggetmc.ai.TerminatorPlus; import net.nuggetmc.ai.TerminatorPlus;
import net.nuggetmc.ai.bot.Bot; import net.nuggetmc.ai.bot.Bot;
import net.nuggetmc.ai.bot.agent.Agent; import net.nuggetmc.ai.bot.agent.Agent;
import net.nuggetmc.ai.bot.agent.legacyagent.EnumTargetGoal;
import net.nuggetmc.ai.bot.agent.legacyagent.LegacyAgent; import net.nuggetmc.ai.bot.agent.legacyagent.LegacyAgent;
import net.nuggetmc.ai.bot.agent.legacyagent.ai.IntelligenceAgent;
import net.nuggetmc.ai.command.commands.AICommand;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
@@ -96,6 +97,41 @@ public class Debugger {
* DEBUGGER METHODS * DEBUGGER METHODS
*/ */
public void colorTest() {
Player player = (Player) sender;
Location loc = player.getLocation();
String[] skin = MojangAPI.getSkin("Kubepig");
TerminatorPlus plugin = TerminatorPlus.getInstance();
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
for (int n = 1; n <= 40; n++) {
int wait = (int) (Math.pow(1.05, 130 - n) + 100);
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
e.printStackTrace();
}
Bukkit.getScheduler().runTask(plugin, () -> Bot.createBot(PlayerUtils.findBottom(loc.clone().add(Math.random() * 20 - 10, 0, Math.random() * 20 - 10)), ChatColor.GREEN + "-$26.95", skin));
player.playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, 1, 1);
}
});
}
public void tpall() {
Player player = (Player) sender;
TerminatorPlus.getInstance().getManager().fetch().stream().filter(EntityLiving::isAlive).forEach(bot -> bot.getBukkitEntity().teleport(player));
}
public void viewsession() {
IntelligenceAgent session = ((AICommand) TerminatorPlus.getInstance().getHandler().getCommand("ai")).getSession();
Bukkit.getOnlinePlayers().stream().filter(ServerOperator::isOp).forEach(session::addUser);
}
public void block() { public void block() {
TerminatorPlus.getInstance().getManager().fetch().forEach(bot -> bot.block(10, 10)); TerminatorPlus.getInstance().getManager().fetch().forEach(bot -> bot.block(10, 10));
} }
@@ -234,18 +270,7 @@ public class Debugger {
} }
public void setTarget(int n) { public void setTarget(int n) {
Agent agent = TerminatorPlus.getInstance().getManager().getAgent(); print("This has been established as a feature as \"" + ChatColor.AQUA + "/bot settings setgoal" + ChatColor.RESET + "\"!");
if (!(agent instanceof LegacyAgent)) {
print("This method currently only supports " + ChatColor.AQUA + "LegacyAgent" + ChatColor.RESET + ".");
return;
}
LegacyAgent legacyAgent = (LegacyAgent) agent;
EnumTargetGoal goal = EnumTargetGoal.of(n);
legacyAgent.setTargetType(goal);
print("The goal has been set to " + ChatColor.BLUE + goal.name() + ChatColor.RESET + ".");
} }
public void fire(boolean b) { public void fire(boolean b) {

View File

@@ -67,4 +67,22 @@ public class PlayerUtils {
return loc.clone().add(0, amount, 0); return loc.clone().add(0, amount, 0);
} }
} }
public static Location findBottom(Location loc) {
loc.setY(loc.getBlockY());
for (int i = 0; i < 255; i++) {
Location check = loc.clone().add(0, -i, 0);
if (check.getY() <= 0) {
break;
}
if (check.getBlock().getType().isSolid()) {
return check.add(0, 1, 0);
}
}
return loc;
}
} }

View File

@@ -0,0 +1,26 @@
package net.nuggetmc.ai.utils;
public class Singularity {
private Object value;
public Singularity(Object value) {
this.value = value;
}
public Singularity() {
this.value = null;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public boolean hasValue() {
return value != null;
}
}

View File

@@ -2,8 +2,13 @@ package net.nuggetmc.ai.utils;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import java.text.NumberFormat;
import java.util.Locale;
public class StringUtilities { public class StringUtilities {
public static final NumberFormat NUMBER_FORMAT = NumberFormat.getNumberInstance(Locale.US);
public static final String ON = ChatColor.GREEN.toString(); public static final String ON = ChatColor.GREEN.toString();
public static final String OFF = ChatColor.GRAY.toString(); public static final String OFF = ChatColor.GRAY.toString();