Files
src
pom.xml
README.md
Code
14 1,445
src/main/java/dev/crowdsim/CrowdSimulator.java
package dev.crowdsim;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.*;
public final class CrowdSimulator extends JavaPlugin implements TabExecutor {
Names names; Skins skins; Manager mgr; Gui gui; Wand wand;
private final Map<UUID, March.Rec> recs = new HashMap<>();
private final Map<UUID, org.bukkit.block.Block> pend = new HashMap<>();
static void msg(CommandSender s, String t) {
s.sendMessage(Component.text("[Crowd] ", NamedTextColor.AQUA).append(Component.text(t, NamedTextColor.WHITE)));
}
@Override public void onEnable() {
getDataFolder().mkdirs();
names = new Names(System.nanoTime());
skins = new Skins(new java.io.File(getDataFolder(), "skins"));
mgr = new Manager(this); wand = new Wand(this); gui = new Gui(this);
var pm = getServer().getPluginManager();
pm.registerEvents(wand, this); pm.registerEvents(gui, this);
for (String c : List.of("crowdwand", "crowdgui", "marchingrecord", "motionrecordblock", "crowdteam")) getCommand(c).setExecutor(this);
var sch = getServer().getScheduler();
sch.runTaskTimer(this, mgr::tick, 20, 1);
sch.runTaskTimer(this, this::sample, 20, 2);
}
@Override public void onDisable() { mgr.clear(); }
boolean recording(Player p) { return recs.containsKey(p.getUniqueId()); }
void toggleRecord(Player p) {
if (recs.remove(p.getUniqueId()) != null) { msg(p, "Recording cancelled."); return; }
recs.put(p.getUniqueId(), new March.Rec(p.getWorld()));
msg(p, "Recording started. Walk your path, then /marchingrecord save <name>");
}
void playMarch(Player p, String name, int amount) {
March m = March.load(this, name);
if (m == null) { msg(p, "Unknown or invalid march: " + name); return; }
msg(p, "Marching " + mgr.play(m, amount, gui.pf(p)[1], gui.ck(p, gui.pf(p)[1])) + " bots along '" + name + "'.");
}
private static final NamedTextColor[] PAL = {NamedTextColor.RED, NamedTextColor.BLUE, NamedTextColor.GREEN, NamedTextColor.YELLOW,
NamedTextColor.LIGHT_PURPLE, NamedTextColor.AQUA, NamedTextColor.GOLD, NamedTextColor.WHITE};
Team createTeam(Player p, String name, int amount, int kit, String colorName, boolean hostile) {
if (name == null) { int i = mgr.teams.size() + 1; do name = "Team" + i++; while (mgr.team(name) != null); }
if (!March.valid(name) || mgr.team(name) != null) { msg(p, "Invalid or already used team name."); return null; }
NamedTextColor col = colorName == null ? null : NamedTextColor.NAMES.value(colorName.toLowerCase(Locale.ROOT));
if (col == null) col = PAL[mgr.teams.size() % PAL.length];
Location l = p.getLocation();
Team t = new Team(name, col, l.getWorld(), l.getX(), l.getY(), l.getZ());
t.kit = kit; t.ck = gui.ck(p, kit);
if (hostile) for (Team o : mgr.teams.values()) { t.enemies.add(o); o.enemies.add(t); }
mgr.teams.put(t.key, t);
msg(p, "Team '" + name + "': spawning " + mgr.addMembers(t, Math.max(1, Math.min(Manager.MAX, amount))) + " bots, leader auto-assigned. Home = here, patrol radius " + (int) t.radius + ".");
return t;
}
private Team tm(Player p, String n) {
Team t = mgr.team(n);
if (t == null) msg(p, "Unknown team: " + n);
return t;
}
private int parseKit(String s) {
for (int i = 0; i < 8; i++) if (Kits.N[i].equalsIgnoreCase(s)) return i;
return s.toLowerCase(Locale.ROOT).matches("custom[1-3]") ? 7 + (s.charAt(6) - '0') : -1;
}
private void team(Player p, String[] a) {
String sub = a.length == 0 ? "" : a[0].toLowerCase(Locale.ROOT);
Team t = null;
if (a.length > 1 && !sub.equals("create")) { t = tm(p, a[1]); if (t == null) return; }
switch (sub) {
case "create" -> {
if (a.length < 2) { msg(p, "Usage: /crowdteam create <name> [amount] [color]"); return; }
createTeam(p, a[1], a.length > 2 ? Integer.parseInt(a[2]) : 20, gui.pf(p)[8], a.length > 3 ? a[3] : null, false);
}
case "add" -> msg(p, "Adding " + mgr.addMembers(t, a.length > 2 ? Integer.parseInt(a[2]) : 10) + " bots to " + t.name + ".");
case "kit" -> {
int k = a.length > 2 ? parseKit(a[2]) : -1;
if (k < 0) { msg(p, "Kit: none, leather, chain, iron, gold, diamond, netherite, mixed, custom1-3"); return; }
t.kit = k; t.ck = gui.ck(p, k); mgr.equipTeam(t);
msg(p, "Team " + t.name + " kit set to " + a[2] + ".");
}
case "leader" -> {
Bot b = mgr.botOf(p.getTargetEntity(50));
if (b == null || b.team != t) { msg(p, "Look at a member of " + t.name + " (within 50 blocks)."); return; }
mgr.setLeader(t, b);
msg(p, b.name + " is now leader of " + t.name + ".");
}
case "enemy", "peace" -> {
if (a.length < 3) { msg(p, "Usage: /crowdteam " + sub + " <team> <team|player>"); return; }
boolean add = sub.equals("enemy");
Team o = mgr.team(a[2]);
if (o != null) {
if (add) { t.enemies.add(o); o.enemies.add(t); } else { t.enemies.remove(o); o.enemies.remove(t); }
} else {
Player q = Bukkit.getPlayerExact(a[2]);
if (q == null) { msg(p, "No team or online player named " + a[2]); return; }
if (add) t.enemyPlayers.add(q.getUniqueId()); else t.enemyPlayers.remove(q.getUniqueId());
}
msg(p, t.name + (add ? " is now hostile to " : " is now at peace with ") + a[2] + ".");
}
case "patrol" -> {
if (a.length < 3) { msg(p, "Usage: /crowdteam patrol <team> <radius 10-100>"); return; }
t.radius = Math.max(10, Math.min(100, Integer.parseInt(a[2])));
msg(p, t.name + " patrol radius " + (int) t.radius + " (max 100 from home).");
}
case "home" -> { Location l = p.getLocation(); t.hx = l.getX(); t.hy = l.getY(); t.hz = l.getZ(); msg(p, t.name + " home moved here."); }
case "remove" -> { mgr.removeTeam(t); msg(p, "Team " + t.name + " disbanded."); }
case "list" -> {
if (mgr.teams.isEmpty()) msg(p, "No teams.");
for (Team x : mgr.teams.values()) {
List<String> en = new ArrayList<>();
for (Team e : x.enemies) en.add(e.name);
msg(p, x.name + ": " + x.members.size() + " bots, leader " + (x.leader == null ? "none" : x.leader.name) + ", radius " + (int) x.radius + ", enemies " + en + ", enemy players " + x.enemyPlayers.size());
}
}
default -> msg(p, "Usage: /crowdteam [create <name> [amount] [color] | add | kit | leader | enemy | peace | patrol | home | remove | list] <team> ...");
}
}
void playDest(Player p, String name, int amount, int dist) {
Dest d = Dest.load(this, name);
if (d == null) { msg(p, "Unknown destination: " + name); return; }
int[] f = gui.pf(p);
msg(p, "Sending " + mgr.playDest(d, amount, f[5], gui.ck(p, f[5]), f[7] == 1, dist) + " bots to '" + name + "' (wander " + (f[7] == 1 ? "on" : "off") + ").");
}
private void dest(Player p, String[] a) {
String sub = a.length == 0 ? "" : a[0].toLowerCase(Locale.ROOT);
switch (sub) {
case "record" -> {
var b = p.getTargetBlockExact(150);
if (b == null) b = p.getLocation().subtract(0, 1, 0).getBlock();
pend.put(p.getUniqueId(), b);
msg(p, "Destination block " + b.getX() + ", " + b.getY() + ", " + b.getZ() + " captured. Now /motionrecordblock save <name>");
}
case "save" -> {
var b = pend.get(p.getUniqueId());
if (a.length < 2 || !March.valid(a[1])) { msg(p, "Usage: /motionrecordblock save <name> (letters, digits, _ -)"); return; }
if (b == null) { msg(p, "Look at a block and use /motionrecordblock record first."); return; }
try { Dest.save(this, a[1], b); pend.remove(p.getUniqueId()); msg(p, "Saved destination '" + a[1] + "'."); }
catch (Exception ex) { msg(p, "Save failed: " + ex.getMessage()); }
}
case "play" -> {
if (a.length < 3) { msg(p, "Usage: /motionrecordblock play <name> <amount> [spawn-distance]"); return; }
try { playDest(p, a[1], Math.max(1, Math.min(Manager.MAX, Integer.parseInt(a[2]))), a.length > 3 ? Math.max(10, Math.min(250, Integer.parseInt(a[3]))) : 0); }
catch (NumberFormatException ex) { msg(p, "Amount/distance must be numbers."); }
}
case "wander" -> {
if (a.length < 2) { msg(p, "Usage: /motionrecordblock wander <on|off>"); return; }
gui.pf(p)[7] = a[1].equalsIgnoreCase("on") ? 1 : 0;
msg(p, "Wander at destination: " + (gui.pf(p)[7] == 1 ? "ON" : "OFF"));
}
default -> msg(p, "Usage: /motionrecordblock [record | save <name> | play <name> <amount> [distance] | wander <on|off>]");
}
}
private void sample() {
recs.forEach((id, r) -> {
Player p = Bukkit.getPlayer(id);
if (p == null || r.pts.size() >= 9000 || !p.getWorld().equals(r.w)) return;
Location l = p.getLocation();
r.pts.add(new double[]{l.getX(), l.getY(), l.getZ(), l.getYaw()});
});
}
@Override public boolean onCommand(CommandSender s, Command c, String l, String[] a) {
if (!(s instanceof Player p)) { msg(s, "Players only."); return true; }
switch (c.getName()) {
case "crowdwand" -> { p.getInventory().addItem(wand.item()); msg(p, "Left-click = corner 1, right-click = corner 2 (lowest surface level)."); }
case "crowdgui" -> gui.open(p, 0);
case "motionrecordblock" -> dest(p, a);
case "crowdteam" -> { try { team(p, a); } catch (NumberFormatException ex) { msg(p, "Amount/radius must be a number."); } }
default -> march(p, a);
}
return true;
}
private void march(Player p, String[] a) {
String sub = a.length == 0 ? "" : a[0].toLowerCase(Locale.ROOT);
switch (sub) {
case "record" -> { recs.remove(p.getUniqueId()); toggleRecord(p); }
case "save" -> {
March.Rec r = recs.get(p.getUniqueId());
if (a.length < 2 || !March.valid(a[1])) { msg(p, "Usage: /marchingrecord save <name> (letters, digits, _ -)"); return; }
if (r == null || r.pts.size() < 2) { msg(p, "Nothing recorded. Use /marchingrecord record first."); return; }