private final Map<UUID, List<UUID>> playerVillagers = new ConcurrentHashMap<>();
private final Map<UUID, BuildTask> activeBuilds = new ConcurrentHashMap<>();
private final Map<UUID, List<WanderingVillager>> wanderingVillagers = new ConcurrentHashMap<>();
private static final Pattern BRACE_PATTERN = Pattern.compile("\\{([^}]+)\\}");
@Override
public void onEnable() {
saveDefaultConfig();
getServer().getPluginManager().registerEvents(this, this);
getLogger().info("§aAI Villager Builder enabled! Type {command} in chat.");
}
@Override
public void onDisable() {
playerVillagers.values().forEach(list -> list.forEach(id -> {
Entity e = getServer().getEntity(id);
if (e != null) e.remove();
}));
playerVillagers.clear();
wanderingVillagers.values().forEach(list -> list.forEach(v -> {
if (v.entity != null) v.entity.remove();
}));
wanderingVillagers.clear();
activeBuilds.values().forEach(BuildTask::cancel);
activeBuilds.clear();
}
@EventHandler
public void onChat(AsyncPlayerChatEvent event) {
Player player = event.getPlayer();
String message = event.getMessage();
Matcher matcher = BRACE_PATTERN.matcher(message);
if (!matcher.find()) return;
event.setCancelled(true);
String command = matcher.group(1).trim().toLowerCase();
new BukkitRunnable() {
@Override
public void run() {
interpretCommand(player, command);
}
}.runTask(this);
}
private void interpretCommand(Player player, String cmd) {
UUID pid = player.getUniqueId();
if (cmd.contains("come here") || cmd.contains("follow") || cmd.equals("come")) {
summonVillagers(player, 3);
player.sendMessage("§aVillagers are coming!");
}
else if (cmd.contains("build me a city") || cmd.contains("build a city") || cmd.contains("build city")) {
spawnVillagers(player, 10);
startBuild(player, "city");
}
else if (cmd.contains("build me a castle") || cmd.contains("build a castle") || cmd.contains("build castle")) {
spawnVillagers(player, 8);
startBuild(player, "castle");
}
else if (cmd.contains("build me a house") || cmd.contains("build a house") || cmd.contains("build house")) {
spawnVillagers(player, 3);
startBuild(player, "house");
}
else if (cmd.contains("build me a tower") || cmd.contains("build a tower") || cmd.contains("build tower")) {
spawnVillagers(player, 4);
startBuild(player, "tower");
}
else if (cmd.contains("build me a wall") || cmd.contains("build a wall") || cmd.contains("build wall")) {
spawnVillagers(player, 5);
startBuild(player, "wall");
}
else if (cmd.contains("build me a bridge") || cmd.contains("build a bridge") || cmd.contains("build bridge")) {
spawnVillagers(player, 4);
startBuild(player, "bridge");
}
else if (cmd.contains("build me a farm") || cmd.contains("build a farm") || cmd.contains("build farm")) {
spawnVillagers(player, 3);
startBuild(player, "farm");
}
else if (cmd.contains("stop") || cmd.contains("go away") || cmd.contains("dismiss")) {
BuildTask task = activeBuilds.remove(pid);
if (task != null) {
task.cancel();
player.sendMessage("§eBuild stopped.");
}
despawnVillagers(player);
player.sendMessage("§eVillagers dismissed.");
}
else if (cmd.contains("dance") || cmd.contains("party") || cmd.contains("celebrate")) {
makeVillagersDance(player);
}
else if (cmd.contains("attack") || cmd.contains("fight") || cmd.contains("defend")) {
spawnVillagers(player, 4);
Entity target = findNearestMonster(player);
if (target != null) {
sendVillagersToAttack(player, target);
player.sendMessage("§cVillagers attacking " + target.getName() + "!");
} else {
player.sendMessage("§eNo monsters nearby.");
}
}
else if (cmd.contains("wander") || cmd.contains("explore") || cmd.contains("roam")) {
spawnVillagers(player, 3);
startWandering(player);
player.sendMessage("§eVillagers are exploring the area.");
}
else if (cmd.contains("mine") || cmd.contains("dig") || cmd.contains("excavate")) {
spawnVillagers(player, 5);
startMining(player);
player.sendMessage("§eVillagers start digging!");
}
else {
if (cmd.startsWith("build ")) {
String thing = cmd.replace("build ", "").trim();
spawnVillagers(player, 4);
if (thing.contains("castle")) {
startBuild(player, "castle");
} else if (thing.contains("city")) {
startBuild(player, "city");
} else if (thing.contains("tower")) {
startBuild(player, "tower");
} else if (thing.contains("house") || thing.contains("home")) {
startBuild(player, "house");
} else if (thing.contains("wall")) {
startBuild(player, "wall");
} else if (thing.contains("bridge")) {
startBuild(player, "bridge");
} else if (thing.contains("farm")) {
startBuild(player, "farm");
} else {
startBuild(player, "house");
}
player.sendMessage("§aVillagers are building a " + thing + "!");
} else {
player.sendMessage("§eTry: {villagers come here}, {build me a castle}, {dance}, {attack}, {stop}");
}
}
}
private void spawnVillagers(Player player, int count) {
UUID pid = player.getUniqueId();
List<UUID> existing = playerVillagers.getOrDefault(pid, new ArrayList<>());
Location loc = player.getLocation().add(2, 0, 0);
for (int i = 0; i < count; i++) {
Location spawnLoc = loc.clone().add(i * 2 - count, 0, 0);
Villager v = (Villager) player.getWorld().spawnEntity(spawnLoc, EntityType.VILLAGER);
v.setCustomName("§b" + player.getName() + "'s Builder");
v.setCustomNameVisible(true);
v.setAI(true);
v.setCollidable(false);
v.setInvulnerable(true);
v.setProfession(Villager.Profession.NITWIT);
existing.add(v.getUniqueId());
}
playerVillagers.put(pid, existing);
}
private void despawnVillagers(Player player) {
UUID pid = player.getUniqueId();
List<UUID> list = playerVillagers.remove(pid);
if (list != null) {
list.forEach(id -> {
Entity e = getServer().getEntity(id);
if (e != null) e.remove();
});
}
List<WanderingVillager> wanderers = wanderingVillagers.remove(pid);
if (wanderers != null) {
wanderers.forEach(w -> { if (w.entity != null) w.entity.remove(); });
}
}
private void summonVillagers(Player player, int count) {
spawnVillagers(player, count);
UUID pid = player.getUniqueId();
List<UUID> list = playerVillagers.get(pid);
if (list == null) return;
new BukkitRunnable() {
int ticks = 0;
@Override
public void run() {
if (ticks > 200 || !player.isOnline()) {
this.cancel();
return;
}
list.forEach(id -> {
Entity e = getServer().getEntity(id);
if (e instanceof Villager v && v.isValid()) {
Location target = player.getLocation();
Location vLoc = v.getLocation();
if (vLoc.distance(target) > 3) {
Vector dir = target.toVector().subtract(vLoc.toVector()).normalize().multiply(0.3);
v.setVelocity(dir);
vLoc.getWorld().spawnParticle(Particle.HEART, vLoc.add(0, 1.5, 0), 1, 0.2, 0.2, 0.2, 0);
}
}
});
ticks++;
}
}.runTaskTimer(this, 0L, 2L);
}
private void startBuild(Player player, String type) {
UUID pid = player.getUniqueId();
BuildTask existing = activeBuilds.get(pid);
if (existing != null) existing.cancel();
List<UUID> list = playerVillagers.get(pid);
if (list == null || list.isEmpty()) {
player.sendMessage("§cNo villagers available. Say {villagers come here} first.");
return;
}
Location origin = player.getLocation().getBlock().getLocation().add(5, 0, 5);
List<BlockData> blocks = generateStructure(type, origin);
if (blocks.isEmpty()) {
player.sendMessage("§cUnknown structure type: " + type);
return;
}
player.sendMessage("§aBuilding " + type + "...");
activeBuilds.put(pid, new BuildTask(player, type, origin, blocks, list));
}
private List<BlockData> generateStructure(String type, Location origin) {
List<BlockData> blocks = new ArrayList<>();
int bx = origin.getBlockX(), by = origin.getBlockY(), bz = origin.getBlockZ();
World w = origin.getWorld();
if (w == null) return blocks;
switch (type) {
case "house":
for (int y = 0; y < 4; y++)
for (int x = 0; x < 7; x++)
for (int z = 0; z < 6; z++) {
if (y == 0)
blocks.add(w.getBlockAt(bx + x, by, bz + z).getBlockData());
else if (y == 3 && (x == 0 || x == 6 || z == 0 || z == 5))
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
else if (y > 0 && y < 3 && (x == 0 || x == 6 || z == 0 || z == 5))
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
}
break;
case "tower":
for (int y = 0; y < 10; y++)
for (int x = 0; x < 5; x++)
for (int z = 0; z < 5; z++) {
if (y == 0)
blocks.add(w.getBlockAt(bx + x, by, bz + z).getBlockData());
else if (x == 0 || x == 4 || z == 0 || z == 4)
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
}
break;
case "wall":
for (int y = 0; y < 4; y++)
for (int x = 0; x < 20; x++)
for (int z = 0; z < 2; z++) {
if (y == 0 || (y == 3 && x % 3 == 0 && z == 0))
continue;
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
}
break;
case "bridge":
for (int x = 0; x < 15; x++)
for (int z = 0; z < 3; z++) {
blocks.add(w.getBlockAt(bx + x, by, bz + z).getBlockData());
if (x == 0 || x == 14)
for (int y = 1; y <= 2; y++)
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
}
break;
case "farm":
for (int x = 0; x < 10; x++)
for (int z = 0; z < 10; z++) {
blocks.add(w.getBlockAt(bx + x, by, bz + z).getBlockData());
if (x == 0 || x == 9 || z == 0 || z == 9)
for (int y = 1; y <= 1; y++)
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
}
break;
case "castle":
// Main keep
for (int y = 0; y < 8; y++)
for (int x = 0; x < 12; x++)
for (int z = 0; z < 10; z++) {
if (y == 0)
blocks.add(w.getBlockAt(bx + x, by, bz + z).getBlockData());
else if (x == 0 || x == 11 || z == 0 || z == 9)
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
}
// Corner towers
for (int y = 0; y < 10; y++)
for (int x = -3; x < 0; x++)
for (int z = -3; z < 0; z++) {
if (y == 0 || x == -3 || x == -1 || z == -3 || z == -1)
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
}
for (int y = 0; y < 10; y++)
for (int x = 12; x < 15; x++)
for (int z = -3; z < 0; z++) {
if (y == 0 || x == 12 || x == 14 || z == -3 || z == -1)
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
}
for (int y = 0; y < 10; y++)
for (int x = -3; x < 0; x++)
for (int z = 10; z < 13; z++) {
if (y == 0 || x == -3 || x == -1 || z == 10 || z == 12)
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
}
for (int y = 0; y < 10; y++)
for (int x = 12; x < 15; x++)
for (int z = 10; z < 13; z++) {
if (y == 0 || x == 12 || x == 14 || z == 10 || z == 12)
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
}
break;
case "city":
for (int ci = 0; ci < 3; ci++)
for (int cj = 0; cj < 3; cj++) {
int ox = ci * 12, oz = cj * 12;
for (int y = 0; y < 4; y++)
for (int x = 0; x < 7; x++)
for (int z = 0; z < 6; z++) {
if (y == 0)
blocks.add(w.getBlockAt(bx + ox + x, by, bz + oz + z).getBlockData());
else if (y == 3 && (x == 0 || x == 6 || z == 0 || z == 5))
blocks.add(w.getBlockAt(bx + ox + x, by + y, bz + oz + z).getBlockData());
else if (y > 0 && y < 3 && (x == 0 || x == 6 || z == 0 || z == 5))
blocks.add(w.getBlockAt(bx + ox + x, by + y, bz + oz + z).getBlockData());
}
}
// Roads between buildings
for (int x = 0; x < 36; x++) {
blocks.add(w.getBlockAt(bx + x, by, bz + 6).getBlockData());
blocks.add(w.getBlockAt(bx + x, by, bz + 18).getBlockData());
}
for (int z = 0; z < 24; z++) {
blocks.add(w.getBlockAt(bx + 7, by, bz + z).getBlockData());
blocks.add(w.getBlockAt(bx + 19, by, bz + z).getBlockData());
}
break;
default:
// Simple house as default
for (int y = 0; y < 4; y++)
for (int x = 0; x < 7; x++)
for (int z = 0; z < 6; z++) {
if (y == 0)
blocks.add(w.getBlockAt(bx + x, by, bz + z).getBlockData());
else if (y == 3 && (x == 0 || x == 6 || z == 0 || z == 5))
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
else if (y > 0 && y < 3 && (x == 0 || x == 6 || z == 0 || z == 5))
blocks.add(w.getBlockAt(bx + x, by + y, bz + z).getBlockData());
}
break;
}
return blocks;
}
private void makeVillagersDance(Player player) {
UUID pid = player.getUniqueId();
List<UUID> list = playerVillagers.get(pid);
if (list == null || list.isEmpty()) {
spawnVillagers(player, 3);
list = playerVillagers.get(pid);
}
if (list == null) return;
final List<UUID> finalList = list;
new BukkitRunnable() {
int ticks = 0;
@Override
public void run() {
if (ticks > 100 || !player.isOnline()) {
this.cancel();
return;
}
finalList.forEach(id -> {
Entity e = getServer().getEntity(id);
if (e instanceof Villager v && v.isValid()) {
v.setVelocity(new Vector(
Math.sin(ticks * 0.3 + v.getEntityId()) * 0.2,
0.05 * Math.sin(ticks * 0.2 + v.getEntityId()),
Math.cos(ticks * 0.3 + v.getEntityId()) * 0.2
));
Location loc = v.getLocation().add(0, 1, 0);
loc.getWorld().spawnParticle(Particle.NOTE, loc, 1, 0.3, 0.3, 0.3, 0);
}
});
ticks++;
}
}.runTaskTimer(this, 0L, 1L);
player.sendMessage("§dVillagers are dancing! 🎵");
}
private Entity findNearestMonster(Player player) {
return player.getNearbyEntities(20, 10, 20).stream()
.filter(e -> e instanceof Monster && e.isValid())
.min(Comparator.comparing(e -> e.getLocation().distance(player.getLocation())))
.orElse(null);
}
private void sendVillagersToAttack(Player player, Entity target) {
UUID pid = player.getUniqueId();
List<UUID> list = playerVillagers.get(pid);
if (list == null) return;
new BukkitRunnable() {
int ticks = 0;
@Override
public void run() {
if (ticks > 200 || !player.isOnline() || !target.isValid()) {
this.cancel();
return;
}
list.forEach(id -> {
Entity e = getServer().getEntity(id);
if (e instanceof Villager v && v.isValid()) {
Location tLoc = target.getLocation();
Location vLoc = v.getLocation();
if (vLoc.distance(tLoc) > 2) {
Vector dir = tLoc.toVector().subtract(vLoc.toVector()).normalize().multiply(0.4);
v.setVelocity(dir);
} else {
target.getWorld().strikeLightningEffect(target.getLocation());
}
}
});
ticks++;
}
}.runTaskTimer(this, 0L, 2L);
}
private void startWandering(Player player) {
UUID pid = player.getUniqueId();
List<UUID> list = playerVillagers.get(pid);
if (list == null) return;
List<WanderingVillager> wanderers = new ArrayList<>();
Random rand = new Random();
for (UUID id : list) {
Entity e = getServer().getEntity(id);
if (e instanceof Villager v) {
WanderingVillager wv = new WanderingVillager(v, player.getLocation().clone());
wanderers.add(wv);
}
}
wanderingVillagers.put(pid, wanderers);
new BukkitRunnable() {
int ticks = 0;
@Override
public void run() {
if (ticks > 400 || !player.isOnline()) {
this.cancel();
List<WanderingVillager> ws = wanderingVillagers.remove(pid);
if (ws != null) ws.forEach(w -> {
if (w.entity != null && w.entity.isValid()) {
w.entity.setVelocity(new Vector(0, 0, 0));
}
});
return;
}
wanderers.forEach(w -> {
if (w.entity != null && w.entity.isValid()) {
if (w.entity.getLocation().distance(w.center) > 15) {
Vector back = w.center.toVector().subtract(w.entity.getLocation().toVector()).normalize().multiply(0.3);
w.entity.setVelocity(back);
} else {
w.entity.setVelocity(new Vector(
(rand.nextDouble() - 0.5) * 0.4,
0,
(rand.nextDouble() - 0.5) * 0.4
));
}
}
});
ticks++;
}
}.runTaskTimer(this, 0L, 5L);
}
private void startMining(Player player) {
UUID pid = player.getUniqueId();
List<UUID> list = playerVillagers.get(pid);
if (list == null) return;
Location center = player.getLocation().getBlock().getLocation();
new BukkitRunnable() {
int ticks = 0;
int index = 0;
@Override
public void run() {
if (ticks > 300 || !player.isOnline()) {
this.cancel();
player.sendMessage("§eMining complete!");
return;
}
list.forEach(uuid -> {
Entity e = getServer().getEntity(uuid);
if (e instanceof Villager v && v.isValid()) {
int cx = center.getBlockX() + (index % 10) - 5;
int cy = center.getBlockY() - 1;
int cz = center.getBlockZ() + ((index / 10) % 10) - 5;
Block b = center.getWorld().getBlockAt(cx, cy, cz);
if (b.getType() != Material.AIR && b.getType() != Material.BEDROCK) {
Location bLoc = b.getLocation().add(0.5, 0.5, 0.5);
v.getWorld().spawnParticle(Particle.CRIT, bLoc, 5, 0.3, 0.3, 0.3, 0);
v.getWorld().playSound(bLoc, Sound.BLOCK_STONE_BREAK, 0.5f, 1.0f);
b.setType(Material.AIR);
}
Location vLoc = v.getLocation();
Vector to = b.getLocation().add(0.5, 0, 0.5).toVector().subtract(vLoc.toVector()).normalize().multiply(0.3);
v.setVelocity(to);
}
});
index++;
ticks++;
}
}.runTaskTimer(this, 0L, 3L);
}
private class BuildTask {
final Player player;
final String type;
final Location origin;
final List<BlockData> blocks;
final List<UUID> villagers;
BukkitRunnable task;
int index = 0;
BuildTask(Player player, String type, Location origin, List<BlockData> blocks, List<UUID> villagers) {
this.player = player;
this.type = type;
this.origin = origin;
this.blocks = blocks;
this.villagers = villagers;
start();
}
void start() {
task = new BukkitRunnable() {
@Override
public void run() {
if (!player.isOnline() || index >= blocks.size()) {
if (index >= blocks.size()) {
player.sendMessage("§a" + type.substring(0, 1).toUpperCase() + type.substring(1) + " complete!");
}
activeBuilds.remove(player.getUniqueId());
this.cancel();
return;
}
for (int i = 0; i < 3 && index < blocks.size(); i++, index++) {
BlockData data = blocks.get(index);
Block b = data.getPlacementLocation().getBlock();
Material mat = getBuildMaterial(type);
b.setType(mat);
b.getWorld().spawnParticle(Particle.HAPPY_VILLAGER, b.getLocation().add(0.5, 0.5, 0.5), 3, 0.2, 0.2, 0.2, 0);
b.getWorld().playSound(b.getLocation(), Sound.BLOCK_STONE_PLACE, 0.3f, 1.0f);
// Animate a villager to the block
if (!villagers.isEmpty()) {
UUID vid = villagers.get(index % villagers.size());
Entity e = getServer().getEntity(vid);
if (e instanceof Villager v && v.isValid()) {
Location target = b.getLocation().add(0.5, 0, 0.5);
Vector dir = target.toVector().subtract(v.getLocation().toVector()).normalize().multiply(0.5);
v.setVelocity(dir);
}
}
}
}
};
task.runTaskTimer(AIVillagerBuilder.this, 0L, getConfig().getInt("build-speed", 3));
}
void cancel() {
if (task != null) task.cancel();
}
}
private Material getBuildMaterial(String type) {
switch (type) {
case "castle": return Material.STONE_BRICKS;
case "house": return Material.OAK_PLANKS;
case "tower": return Material.STONE_BRICKS;
case "wall": return Material.COBBLESTONE;
case "bridge": return Material.OAK_PLANKS;
case "farm": return Material.GRASS_BLOCK;
case "city": return Material.STONE_BRICKS;
default: return Material.OAK_PLANKS;
}
}
private static class WanderingVillager {
final Villager entity;
final Location center;
WanderingVillager(Villager entity, Location center) {
this.entity = entity;
this.center = center;
}
}