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

95 lines
2.8 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.PlayerAI;
2021-06-28 15:17:32 -05:00
import net.nuggetmc.ai.bot.agent.BotAgent;
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;
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 PlayerAI plugin;
2021-06-28 15:17:32 -05:00
private final BotAgent agent;
private final NumberFormat numberFormat;
2021-06-26 19:43:58 -05:00
private final Set<Bot> bots = new HashSet<>();
2021-06-26 19:43:58 -05:00
2021-06-30 02:18:31 -05:00
public BotManager(PlayerAI plugin) {
this.plugin = plugin;
this.agent = new BotAgent(this);
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) {
bots.add(bot);
2021-06-26 19:43:58 -05:00
}
2021-06-30 02:18:31 -05:00
public BotAgent getAgent() {
return agent;
2021-06-26 19:43:58 -05:00
}
public void createBots(Player sender, String name, String skin, int n) {
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);
if (skin != null && skin.length() > 16) skin = skin.substring(0, 16);
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
2021-06-28 15:17:32 -05:00
+ (skin == null ? "" : ChatColor.RESET + " and skin " + ChatColor.GREEN + skin)
+ ChatColor.RESET + "...");
skin = skin == null ? name : skin;
2021-06-28 15:17:32 -05:00
double f = n < 100 ? .004 * n : .4;
for (int i = 0; i < n; i++) {
Bot bot = Bot.createBot(loc, name, skin);
2021-06-28 15:17:32 -05:00
if (i > 0) 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 + ").");
}
2021-06-26 19:43:58 -05:00
public void reset() {
for (Bot bot : bots) {
2021-06-28 15:17:32 -05:00
bot.remove();
2021-06-26 19:43:58 -05:00
}
bots.clear();
2021-06-26 19:43:58 -05:00
}
@EventHandler
public void onJoin(PlayerJoinEvent event) {
PlayerConnection connection = ((CraftPlayer) event.getPlayer()).getHandle().playerConnection;
for (Bot bot : bots) {
bot.render(connection, true);
2021-06-26 19:43:58 -05:00
}
}
}