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;
|
|
|
|
|
import net.nuggetmc.ai.PlayerAI;
|
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
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
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-06-26 19:26:46 -07:00
|
|
|
private final PlayerAI plugin;
|
2021-06-26 19:43:58 -05:00
|
|
|
|
2021-06-27 00:26:45 -05:00
|
|
|
private final Set<Bot> bots = new HashSet<>();
|
2021-06-26 19:43:58 -05:00
|
|
|
|
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-06-27 00:26:45 -05:00
|
|
|
public BotManager(PlayerAI plugin) {
|
2021-06-26 19:26:46 -07:00
|
|
|
this.plugin = plugin;
|
2021-06-26 19:43:58 -05:00
|
|
|
}
|
|
|
|
|
|
2021-06-28 13:18:49 -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);
|
|
|
|
|
|
|
|
|
|
sender.sendMessage("Creating " + (n == 1 ? "new bot" : ChatColor.RED + String.valueOf(n) + ChatColor.RESET + " new bots")
|
|
|
|
|
+ " with name " + ChatColor.GREEN + name
|
|
|
|
|
+ (skin == null ? "" : ChatColor.RESET + " and skin " + ChatColor.GREEN + skin) + ChatColor.RESET + "...");
|
|
|
|
|
|
|
|
|
|
skin = skin == null ? name : skin;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
Bot bot = Bot.createBot(loc, name, skin);
|
|
|
|
|
if (i > 0) bot.setVelocity(new Vector(Math.random() - 0.5, 0.5, Math.random() - 0.5).normalize().multiply(0.4));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
world.spawnParticle(Particle.CLOUD, loc, 100, 1, 1, 1, 0.5);
|
|
|
|
|
|
|
|
|
|
double time = (System.currentTimeMillis() - timestamp) / 1000D;
|
|
|
|
|
|
|
|
|
|
sender.sendMessage("Process completed (" + ChatColor.RED + time + "s" + ChatColor.RESET + ").");
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 19:43:58 -05:00
|
|
|
public void reset() {
|
2021-06-27 00:26:45 -05:00
|
|
|
for (Bot bot : bots) {
|
|
|
|
|
bot.despawn();
|
2021-06-26 19:43:58 -05:00
|
|
|
}
|
|
|
|
|
|
2021-06-27 00:26:45 -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;
|
|
|
|
|
|
2021-06-27 00:26:45 -05:00
|
|
|
for (Bot bot : bots) {
|
|
|
|
|
bot.render(connection, true);
|
2021-06-26 19:43:58 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|