Files
behavior_packs
resource_packs
Code
5 190
behavior_packs/him_bp/scripts/main.js
import { system, world } from "@minecraft/server";
const HIM_TYPE = "herobrine:him";
const APPEARED_PROPERTY = "herobrine:him_appeared";
const RETRY_TICKS = 200;
const DIMENSIONS = ["overworld", "nether", "the_end"];
let attemptScheduled = false;
function hasAppeared() {
return world.getDynamicProperty(APPEARED_PROPERTY) === true;
}
function findExistingHim() {
for (const dimensionId of DIMENSIONS) {
const entities = world.getDimension(dimensionId).getEntities({ type: HIM_TYPE });
if (entities.length > 0) {
world.setDynamicProperty(APPEARED_PROPERTY, true);
return true;
}
}
return false;
}
function isSafeSpawnBlock(dimension, x, groundY, z) {
try {
const ground = dimension.getBlock({ x, y: groundY, z });
const feet = dimension.getBlock({ x, y: groundY + 1, z });
const head = dimension.getBlock({ x, y: groundY + 2, z });
return Boolean(
ground && ground.isSolid &&
feet && feet.isAir && !feet.isLiquid &&
head && head.isAir && !head.isLiquid
);
} catch {
return false;
}
}
function trySpawnHim() {
if (hasAppeared() || findExistingHim()) return;
const players = world.getPlayers();
if (players.length === 0) return;
const player = players[Math.floor(Math.random() * players.length)];
const dimension = player.dimension;
const location = player.location;
const baseX = Math.floor(location.x);
const baseY = Math.floor(location.y);
const baseZ = Math.floor(location.z);
for (let attempt = 0; attempt < 48; attempt++) {
const angle = Math.random() * Math.PI * 2;
const radius = 5 + Math.random() * 2;
const x = Math.floor(location.x + Math.cos(angle) * radius);
const z = Math.floor(location.z + Math.sin(angle) * radius);
const distance = Math.hypot(x + 0.5 - location.x, z + 0.5 - location.z);
if (distance < 5 || distance > 7.5) continue;
const verticalOffset = Math.floor(Math.random() * 7) - 3;
const groundY = baseY - 1 + verticalOffset;
if (!isSafeSpawnBlock(dimension, x, groundY, z)) continue;
dimension.spawnEntity(HIM_TYPE, { x: x + 0.5, y: groundY + 1, z: z + 0.5 });
world.setDynamicProperty(APPEARED_PROPERTY, true);
return;
}
}
function scheduleAttempt() {
if (attemptScheduled || hasAppeared()) return;
attemptScheduled = true;
system.runTimeout(() => {
attemptScheduled = false;
try {
trySpawnHim();
} catch {
// A failed spawn or unavailable location is retried without interrupting the script runtime.
}
if (!hasAppeared()) scheduleAttempt();
}, RETRY_TICKS);
}
world.afterEvents.playerSpawn.subscribe((event) => {
if (!event.initialSpawn || hasAppeared()) return;
scheduleAttempt();
});