Files
Lumberjack_BP
Lumberjack_RP
build_addon.py
Code
10 567
Lumberjack_BP/scripts/main.js
import { world, system, ItemStack } from "@minecraft/server";
const LUMBERJACK_ID = "kodari:lumberjack";
const AXE_ID = "minecraft:iron_axe";
const SEARCH_RADIUS = 8;
const BREAK_RANGE = 2.8;
const BREAK_TICKS = 12;
const WOOD_TYPES = new Set([
"minecraft:oak_log",
"minecraft:spruce_log",
"minecraft:birch_log",
"minecraft:jungle_log",
"minecraft:acacia_log",
"minecraft:dark_oak_log",
"minecraft:mangrove_log",
"minecraft:cherry_log",
"minecraft:pale_oak_log",
"minecraft:stripped_oak_log",
"minecraft:stripped_spruce_log",
"minecraft:stripped_birch_log",
"minecraft:stripped_jungle_log",
"minecraft:stripped_acacia_log",
"minecraft:stripped_dark_oak_log",
"minecraft:stripped_mangrove_log",
"minecraft:stripped_cherry_log",
"minecraft:stripped_pale_oak_log",
"minecraft:oak_wood",
"minecraft:spruce_wood",
"minecraft:birch_wood",
"minecraft:jungle_wood",
"minecraft:acacia_wood",
"minecraft:dark_oak_wood",
"minecraft:mangrove_wood",
"minecraft:cherry_wood",
"minecraft:pale_oak_wood",
"minecraft:stripped_oak_wood",
"minecraft:stripped_spruce_wood",
"minecraft:stripped_birch_wood",
"minecraft:stripped_jungle_wood",
"minecraft:stripped_acacia_wood",
"minecraft:stripped_dark_oak_wood",
"minecraft:stripped_mangrove_wood",
"minecraft:stripped_cherry_wood",
"minecraft:stripped_pale_oak_wood",
"minecraft:crimson_stem",
"minecraft:warped_stem",
"minecraft:stripped_crimson_stem",
"minecraft:stripped_warped_stem",
"minecraft:crimson_hyphae",
"minecraft:warped_hyphae",
"minecraft:stripped_crimson_hyphae",
"minecraft:stripped_warped_hyphae",
"minecraft:bamboo_block",
"minecraft:stripped_bamboo_block"
]);
const lumberjacks = new Map();
function isWood(typeId) {
return WOOD_TYPES.has(typeId);
}
function distanceSquared(a, b) {
const x = a.x - b.x;
const y = a.y - b.y;
const z = a.z - b.z;
return x * x + y * y + z * z;
}
function setChopping(entity, value) {
try {
entity.setProperty("kodari:is_chopping", value);
} catch (error) {
// The entity may have been removed during the same tick.
}
}
function equipAxe(entity) {
try {
entity.runCommand(`replaceitem entity @s slot.weapon.mainhand 0 ${AXE_ID} 1`);
entity.nameTag = "Lenhador";
} catch (error) {
// The entity can still chop if the server has no replaceitem command.
}
}
function findNearestWood(entity) {
const origin = entity.location;
let nearest = undefined;
let nearestDistance = Number.MAX_VALUE;
for (let x = -SEARCH_RADIUS; x <= SEARCH_RADIUS; x++) {
for (let y = -2; y <= 5; y++) {
for (let z = -SEARCH_RADIUS; z <= SEARCH_RADIUS; z++) {
const position = {
x: Math.floor(origin.x) + x,
y: Math.floor(origin.y) + y,
z: Math.floor(origin.z) + z
};
try {
const block = entity.dimension.getBlock(position);
if (!block || !isWood(block.typeId)) {
continue;
}
const blockDistance = distanceSquared(origin, position);
if (blockDistance < nearestDistance) {
nearestDistance = blockDistance;
nearest = { position, typeId: block.typeId };
}
} catch (error) {
// Unloaded or inaccessible chunks are ignored.
}
}
}
}
return nearest;
}
function moveTowards(entity, position) {
const origin = entity.location;
const x = position.x + 0.5 - origin.x;
const z = position.z + 0.5 - origin.z;
const length = Math.sqrt(x * x + z * z);
if (length < 0.01) {
return;
}
try {
entity.applyImpulse({ x: (x / length) * 0.035, y: 0, z: (z / length) * 0.035 });
} catch (error) {
// Movement is retried on the next tick.
}
}
function breakTarget(entity, state) {
const target = state.target;
try {
const block = entity.dimension.getBlock(target.position);
if (!block || block.typeId !== target.typeId || !isWood(block.typeId)) {
state.target = undefined;
setChopping(entity, false);
return;
}
block.setType("minecraft:air");
state.collected.set(target.typeId, (state.collected.get(target.typeId) ?? 0) + 1);
} catch (error) {
// A block changed by another player is simply skipped.
}
state.target = undefined;
setChopping(entity, false);
}
function dropCollectedWood(entity, state) {
for (const [typeId, amount] of state.collected) {
let remaining = amount;
while (remaining > 0) {
const stackSize = Math.min(remaining, 64);
try {
entity.dimension.spawnItem(new ItemStack(typeId, stackSize), entity.location);
} catch (error) {
// Invalid items are not allowed to stop the other drops.
}
remaining -= stackSize;
}
}
}
world.afterEvents.entitySpawn.subscribe(({ entity }) => {
if (entity.typeId !== LUMBERJACK_ID) {
return;
}
lumberjacks.set(entity.id, {
entity,
target: undefined,
targetStartedAt: 0,
collected: new Map(),
nextSearchAt: 0
});
equipAxe(entity);
});
world.afterEvents.entityDie.subscribe(({ deadEntity }) => {
if (deadEntity.typeId !== LUMBERJACK_ID) {
return;
}
const state = lumberjacks.get(deadEntity.id);
if (state) {
dropCollectedWood(deadEntity, state);