Files
behavior_packs
resource_packs
Code
14 658
behavior_packs/ai_villagers/scripts/main.js
import { ItemStack, system, world } from "@minecraft/server";
const POLICE = "ai_villagers:police_villager";
const MEDIC = "ai_villagers:medic_villager";
const HOUSE_TAG = "ai_villagers:house_built";
const BREAKABLE_BLOCKS = new Set([
"minecraft:short_grass",
"minecraft:tallgrass",
"minecraft:fern",
"minecraft:large_fern",
"minecraft:deadbush",
"minecraft:vine",
"minecraft:leaves",
"minecraft:oak_leaves",
"minecraft:birch_leaves",
"minecraft:spruce_leaves",
"minecraft:jungle_leaves",
"minecraft:acacia_leaves",
"minecraft:dark_oak_leaves",
"minecraft:mangrove_leaves",
"minecraft:cherry_leaves"
]);
function isRoleEntity(entity) {
return entity.typeId === POLICE || entity.typeId === MEDIC;
}
function placeBlock(dimension, x, y, z, typeId) {
try {
const block = dimension.getBlock({ x, y, z });
if (block && block.typeId !== "minecraft:bedrock") {
block.setType(typeId);
}
} catch (error) {
// A house can cross an unloaded chunk; the next work cycle will retry role AI.
}
}
function buildHouse(entity, role) {
if (entity.getTags().includes(HOUSE_TAG)) {
return;
}
const location = entity.location;
const dimension = entity.dimension;
const baseX = Math.floor(location.x) - 3;
const baseY = Math.floor(location.y);
const baseZ = Math.floor(location.z) - 3;
const wall = role === "police" ? "minecraft:cracked_stone_bricks" : "minecraft:mossy_cobblestone";
const trim = role === "police" ? "minecraft:stone_bricks" : "minecraft:oak_planks";
for (let x = baseX; x <= baseX + 6; x++) {
for (let z = baseZ; z <= baseZ + 6; z++) {
placeBlock(dimension, x, baseY, z, trim);
}
}
for (let y = baseY + 1; y <= baseY + 3; y++) {
for (let x = baseX; x <= baseX + 6; x++) {
for (let z = baseZ; z <= baseZ + 6; z++) {
const edge = x === baseX || x === baseX + 6 || z === baseZ || z === baseZ + 6;
if (!edge) {
continue;
}
const doorway = z === baseZ && x === baseX + 3 && y <= baseY + 2;
const window = y === baseY + 2 && ((z === baseZ || z === baseZ + 6) && (x === baseX + 1 || x === baseX + 5));
if (!doorway) {
placeBlock(dimension, x, y, z, window ? "minecraft:glass_pane" : wall);
}
}
}
}
for (let x = baseX - 1; x <= baseX + 7; x++) {
for (let z = baseZ - 1; z <= baseZ + 7; z++) {
placeBlock(dimension, x, baseY + 4, z, "minecraft:dark_oak_slab");
}
}
placeBlock(dimension, baseX + 3, baseY + 1, baseZ, "minecraft:air");
placeBlock(dimension, baseX + 3, baseY + 2, baseZ, "minecraft:air");
placeBlock(dimension, baseX + 2, baseY + 1, baseZ + 2, role === "police" ? "minecraft:smithing_table" : "minecraft:brewing_stand");
placeBlock(dimension, baseX + 4, baseY + 1, baseZ + 2, "minecraft:barrel");
placeBlock(dimension, baseX + 2, baseY + 1, baseZ + 4, "minecraft:bed");
placeBlock(dimension, baseX + 4, baseY + 1, baseZ + 4, role === "police" ? "minecraft:bell" : "minecraft:cauldron");
placeBlock(dimension, baseX + 1, baseY + 3, baseZ + 1, "minecraft:cobweb");
placeBlock(dimension, baseX + 5, baseY + 3, baseZ + 5, "minecraft:cobweb");
entity.addTag(HOUSE_TAG);
entity.nameTag = role === "police" ? "§9Police Villager" : "§cMedic Villager";
}
function clearNearbyPlant(entity) {
const location = entity.location;
const x = Math.floor(location.x) + Math.floor(Math.random() * 5) - 2;
const y = Math.floor(location.y) + Math.floor(Math.random() * 3);
const z = Math.floor(location.z) + Math.floor(Math.random() * 5) - 2;
try {
const block = entity.dimension.getBlock({ x, y, z });
if (block && BREAKABLE_BLOCKS.has(block.typeId)) {
block.setType("minecraft:air");
}
} catch (error) {
// The target may be outside the currently loaded area.
}
}
function buildPoliceBarricade(entity) {
const location = entity.location;
const x = Math.floor(location.x) + (Math.random() > 0.5 ? 3 : -3);
const y = Math.floor(location.y);
const z = Math.floor(location.z);
for (let height = 0; height < 2; height++) {
placeBlock(entity.dimension, x, y + height, z, "minecraft:cobblestone_wall");
}
}
function healNearestPlayer(entity) {
const players = entity.dimension.getEntities({
type: "minecraft:player",
location: entity.location,
maxDistance: 8
});
let closest = null;
let closestDistance = Number.MAX_VALUE;
for (const player of players) {
const dx = player.location.x - entity.location.x;
const dy = player.location.y - entity.location.y;
const dz = player.location.z - entity.location.z;
const distance = dx * dx + dy * dy + dz * dz;
if (distance < closestDistance) {
closest = player;
closestDistance = distance;
}
}
if (!closest) {
return;
}
try {
const health = closest.getComponent("minecraft:health");
if (health && health.currentValue < health.effectiveMax) {
health.resetToMax();
entity.dimension.spawnItem(new ItemStack("minecraft:paper", 1), {
x: entity.location.x,
y: entity.location.y + 1,
z: entity.location.z
});
}
} catch (error) {
// The player may leave the dimension between the query and the heal.
}
}
function craftMedicSupplies(entity) {
try {
entity.dimension.spawnItem(new ItemStack("minecraft:bread", 2), {
x: entity.location.x,
y: entity.location.y + 1,
z: entity.location.z
});
} catch (error) {
// Item spawning is skipped if the entity is removed during this work cycle.
}
}
function runRoleWork(entity) {
if (!isRoleEntity(entity)) {
return;
}
const role = entity.typeId === POLICE ? "police" : "medic";
buildHouse(entity, role);
if (role === "police") {
clearNearbyPlant(entity);
if (Math.random() > 0.45) {
buildPoliceBarricade(entity);
}
} else {
healNearestPlayer(entity);
if (Math.random() > 0.35) {
craftMedicSupplies(entity);
}
}
}
world.afterEvents.entitySpawn.subscribe((event) => {
if (isRoleEntity(event.entity)) {
system.run(() => runRoleWork(event.entity));
}
});
system.runInterval(() => {
for (const dimensionId of ["overworld", "nether", "the_end"]) {
let dimension;
try {
dimension = world.getDimension(dimensionId);
} catch (error) {
continue;
}