Files
behavior_pack
resource_pack
Code
7 304
behavior_pack/scripts/main.js
import { system, world } from "@minecraft/server";
const WAFFLES_ID = "waffle_pet:waffles";
const craftingTableTicks = new Map();
const inventoryCounts = new Map();
const hungerTimes = new Map();
function countInventory(player) {
const inventory = player.getComponent("minecraft:inventory");
const container = inventory?.container;
if (!container) return 0;
let count = 0;
for (let slot = 0; slot < container.size; slot++) {
count += container.getItem(slot)?.amount ?? 0;
}
return count;
}
function isNearWaffles(player) {
return player.dimension.getEntities({
type: WAFFLES_ID,
location: player.location,
maxDistance: 12
}).length > 0;
}
function eatNearbyBlock(waffles) {
const blockY = Math.floor(waffles.location.y) - 1;
const candidates = [];
for (let xOffset = -1; xOffset <= 1; xOffset++) {
for (let zOffset = -1; zOffset <= 1; zOffset++) {
if (xOffset === 0 && zOffset === 0) continue;
const block = waffles.dimension.getBlock({
x: Math.floor(waffles.location.x) + xOffset,
y: blockY,
z: Math.floor(waffles.location.z) + zOffset
});
if (block && block.typeId !== "minecraft:air") candidates.push(block);
}
}
if (candidates.length === 0) return;
const block = candidates[Math.floor(Math.random() * candidates.length)];
block.setType("minecraft:air");
world.sendMessage("Waffles ate a block!");
}
function eatNearbyMob(waffles) {
const candidates = waffles.dimension.getEntities({
families: ["mob"],
location: waffles.location,
maxDistance: 4,
excludeTypes: [WAFFLES_ID]
}).filter((mob) => mob.id !== waffles.id);
if (candidates.length === 0) return;
const mob = candidates[Math.floor(Math.random() * candidates.length)];
mob.kill();
}
world.afterEvents.entityHurt.subscribe((event) => {
if (event.hurtEntity.typeId === WAFFLES_ID) {
world.sendMessage("Waffles: Ouch!");
}
});
world.afterEvents.playerInteractWithBlock.subscribe((event) => {
if (event.block.typeId === "minecraft:crafting_table") {
craftingTableTicks.set(event.player.id, system.currentTick);
}
});
system.runInterval(() => {
const wafflesSeen = new Set();
for (const player of world.getAllPlayers()) {
const playerId = player.id;
const itemCount = countInventory(player);
const previousCount = inventoryCounts.get(playerId);
const craftingTick = craftingTableTicks.get(playerId);
if (
previousCount !== undefined &&
itemCount > previousCount &&
craftingTick !== undefined &&
system.currentTick - craftingTick <= 200 &&
isNearWaffles(player)
) {
world.sendMessage("Waffles: I hope it's food!");
craftingTableTicks.delete(playerId);
}
inventoryCounts.set(playerId, itemCount);
const nearby = player.dimension.getEntities({
type: WAFFLES_ID,
location: player.location,
maxDistance: 32
});
for (const waffles of nearby) {
if (wafflesSeen.has(waffles.id)) continue;
wafflesSeen.add(waffles.id);
const nextHunger = hungerTimes.get(waffles.id) ?? 0;
if (system.currentTick >= nextHunger) {
world.sendMessage("Waffles: I'm hungry!");
eatNearbyBlock(waffles);
if (Math.random() < 0.25) eatNearbyMob(waffles);
hungerTimes.set(waffles.id, system.currentTick + 1200 + Math.floor(Math.random() * 1200));
}
}
}
}, 20);