2021-06-27 00:26:45 -05:00
|
|
|
package net.nuggetmc.ai.bot;
|
2021-06-26 19:43:58 -05:00
|
|
|
|
|
|
|
|
import net.minecraft.server.v1_16_R3.PlayerConnection;
|
2021-07-12 18:20:17 -05:00
|
|
|
import net.nuggetmc.ai.bot.agent.Agent;
|
|
|
|
|
import net.nuggetmc.ai.bot.agent.legacyagent.LegacyAgent;
|
2021-07-10 23:51:14 -05:00
|
|
|
import net.nuggetmc.ai.utils.MojangAPI;
|
2021-06-28 13:18:49 -05:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
|
import org.bukkit.Location;
|
|
|
|
|
import org.bukkit.Particle;
|
|
|
|
|
import org.bukkit.World;
|
2021-06-26 19:43:58 -05:00
|
|
|
import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer;
|
2021-06-28 13:18:49 -05:00
|
|
|
import org.bukkit.entity.Player;
|
2021-06-26 19:43:58 -05:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
2021-06-28 13:18:49 -05:00
|
|
|
import org.bukkit.util.Vector;
|
2021-06-26 19:43:58 -05:00
|
|
|
|
2021-06-28 15:17:32 -05:00
|
|
|
import java.text.NumberFormat;
|
2021-06-26 19:43:58 -05:00
|
|
|
import java.util.HashSet;
|
2021-06-28 15:17:32 -05:00
|
|
|
import java.util.Locale;
|
2021-06-26 19:43:58 -05:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
2021-06-27 00:26:45 -05:00
|
|
|
public class BotManager implements Listener {
|
2021-06-26 19:43:58 -05:00
|
|
|
|
2021-07-12 18:20:17 -05:00
|
|
|
private final Agent agent;
|
2021-07-16 03:41:21 -05:00
|
|
|
private final Set<Bot> bots;
|
2021-06-28 15:17:32 -05:00
|
|
|
private final NumberFormat numberFormat;
|
2021-06-26 19:43:58 -05:00
|
|
|
|
2021-07-19 17:35:28 -05:00
|
|
|
public boolean removeOnDeath = true;
|
2021-06-26 19:43:58 -05:00
|
|
|
|
2021-07-12 18:20:17 -05:00
|
|
|
public BotManager() {
|
|
|
|
|
this.agent = new LegacyAgent(this);
|
2021-07-16 03:41:21 -05:00
|
|
|
this.bots = new HashSet<>();
|
2021-06-30 02:18:31 -05:00
|
|
|
this.numberFormat = NumberFormat.getInstance(Locale.US);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-27 00:26:45 -05:00
|
|
|
public Set<Bot> fetch() {
|
|
|
|
|
return bots;
|
2021-06-26 19:43:58 -05:00
|
|
|
}
|
|
|
|
|
|
2021-06-27 00:26:45 -05:00
|
|
|
public void add(Bot bot) {
|
|
|
|
|
bots.add(bot);
|
2021-06-26 19:43:58 -05:00
|
|
|
}
|
|
|
|
|
|
2021-07-19 17:35:28 -05:00
|
|
|
public Bot getFirst(String name) {
|
|
|
|
|
for (Bot bot : bots) {
|
|
|
|
|
if (name.equals(bot.getName())) {
|
|
|
|
|
return bot;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 18:20:17 -05:00
|
|
|
public Agent getAgent() {
|
2021-06-30 02:18:31 -05:00
|
|
|
return agent;
|
2021-06-26 19:43:58 -05:00
|
|
|
}
|
|
|
|
|
|
2021-07-10 23:51:14 -05:00
|
|
|
public void createBots(Player sender, String name, String skinName, int n) {
|
2021-06-28 13:18:49 -05:00
|
|
|
long timestamp = System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
if (n < 1) n = 1;
|
|
|
|
|
|
|
|
|
|
World world = sender.getWorld();
|
|
|
|
|
Location loc = sender.getLocation();
|
|
|
|
|
|
|
|
|
|
if (name.length() > 16) name = name.substring(0, 16);
|
2021-07-10 23:51:14 -05:00
|
|
|
if (skinName != null && skinName.length() > 16) skinName = skinName.substring(0, 16);
|
2021-06-28 13:18:49 -05:00
|
|
|
|
2021-06-28 15:17:32 -05:00
|
|
|
sender.sendMessage("Creating " + (n == 1 ? "new bot" : ChatColor.RED + numberFormat.format(n) + ChatColor.RESET + " new bots")
|
2021-06-28 13:18:49 -05:00
|
|
|
+ " with name " + ChatColor.GREEN + name
|
2021-07-10 23:51:14 -05:00
|
|
|
+ (skinName == null ? "" : ChatColor.RESET + " and skin " + ChatColor.GREEN + skinName)
|
2021-06-28 15:17:32 -05:00
|
|
|
+ ChatColor.RESET + "...");
|
2021-06-28 13:18:49 -05:00
|
|
|
|
2021-07-10 23:51:14 -05:00
|
|
|
skinName = skinName == null ? name : skinName;
|
2021-06-28 13:18:49 -05:00
|
|
|
|
2021-06-28 15:17:32 -05:00
|
|
|
double f = n < 100 ? .004 * n : .4;
|
|
|
|
|
|
2021-07-10 23:51:14 -05:00
|
|
|
String[] skin = MojangAPI.getSkin(skinName);
|
|
|
|
|
|
2021-07-19 17:35:28 -05:00
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
|
Bot bot = Bot.createBot(loc, name.replace("%", String.valueOf(i)), skin, removeOnDeath);
|
|
|
|
|
if (i > 1) bot.setVelocity(new Vector(Math.random() - 0.5, 0.5, Math.random() - 0.5).normalize().multiply(f));
|
2021-06-28 13:18:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
world.spawnParticle(Particle.CLOUD, loc, 100, 1, 1, 1, 0.5);
|
|
|
|
|
|
2021-06-28 15:17:32 -05:00
|
|
|
sender.sendMessage("Process completed (" + ChatColor.RED + ((System.currentTimeMillis() - timestamp) / 1000D) + "s" + ChatColor.RESET + ").");
|
2021-06-28 13:18:49 -05:00
|
|
|
}
|
|
|
|
|
|
2021-07-12 18:20:17 -05:00
|
|
|
public void remove(Bot bot) {
|
|
|
|
|
bots.remove(bot);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 19:43:58 -05:00
|
|
|
public void reset() {
|
2021-07-16 03:41:21 -05:00
|
|
|
bots.forEach(Bot::removeVisually);
|
|
|
|
|
bots.clear(); // Not always necessary, but a good security measure
|
2021-07-19 17:35:28 -05:00
|
|
|
agent.stopAllTasks();
|
2021-06-26 19:43:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
|
public void onJoin(PlayerJoinEvent event) {
|
|
|
|
|
PlayerConnection connection = ((CraftPlayer) event.getPlayer()).getHandle().playerConnection;
|
2021-07-19 17:35:28 -05:00
|
|
|
bots.forEach(bot -> bot.render(connection, true));
|
2021-06-26 19:43:58 -05:00
|
|
|
}
|
|
|
|
|
}
|