Files
Tplus/src/main/java/net/nuggetmc/ai/bot/BotManager.java

106 lines
3.2 KiB
Java
Raw Normal View History

package net.nuggetmc.ai.bot;
2021-06-26 19:43:58 -05:00
import net.minecraft.server.v1_16_R3.PlayerConnection;
import net.nuggetmc.ai.bot.agent.Agent;
import net.nuggetmc.ai.bot.agent.legacyagent.LegacyAgent;
import net.nuggetmc.ai.utils.MojangAPI;
2021-07-21 13:52:21 -05:00
import org.bukkit.*;
2021-06-26 19:43:58 -05:00
import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer;
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;
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;
public class BotManager implements Listener {
2021-06-26 19:43:58 -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-21 13:52:21 -05:00
public boolean joinMessages = false;
2021-07-19 17:35:28 -05:00
public boolean removeOnDeath = true;
2021-06-26 19:43:58 -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);
}
public Set<Bot> fetch() {
return bots;
2021-06-26 19:43:58 -05:00
}
public void add(Bot bot) {
2021-07-21 13:52:21 -05:00
if (joinMessages) {
Bukkit.broadcastMessage(ChatColor.YELLOW + bot.getName() + " joined the game");
}
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;
}
public Agent getAgent() {
2021-06-30 02:18:31 -05:00
return agent;
2021-06-26 19:43:58 -05:00
}
public void createBots(Player sender, String name, String skinName, int n) {
long timestamp = System.currentTimeMillis();
if (n < 1) n = 1;
World world = sender.getWorld();
Location loc = sender.getLocation();
2021-06-28 15:17:32 -05:00
sender.sendMessage("Creating " + (n == 1 ? "new bot" : ChatColor.RED + numberFormat.format(n) + ChatColor.RESET + " new bots")
+ " with name " + ChatColor.GREEN + name
+ (skinName == null ? "" : ChatColor.RESET + " and skin " + ChatColor.GREEN + skinName)
2021-06-28 15:17:32 -05:00
+ ChatColor.RESET + "...");
skinName = skinName == null ? name : skinName;
2021-06-28 15:17:32 -05:00
double f = n < 100 ? .004 * n : .4;
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));
}
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 + ").");
}
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
}
}