import { world, system } from "@minecraft/server"; let dimension = null; let hasSubscribedToRemoveEvent = false; // Main ticker function function ticker() { if (!dimension) { dimension = world.getDimension("overworld"); if (system.EntityDieAfterEventSignal) { // Check if the event signal exists system.EntityDieAfterEventSignal.subscribe((event) => { const nameParts = event.deadEntity.getName().split(" x"); if (nameParts.length === 2) { system.triggerEvent("entity.killed", { entity: event.deadEntity, dimension: dimension }); } }); } else { console.error("EntityDieAfterEventSignal is not available in this version of the API."); } return; } processEntities(dimension); } system.runInterval(ticker, 10); function processEntities(dimension) { const entities = dimension.getEntities(); function getBlockKey(entity) { const x = Math.round(entity.location.x); const y = Math.round(entity.location.y); const z = Math.round(entity.location.z); return `${entity.dimension.id.replace("minecraft:", "")}_${x}_${y}_${z}`; } let locationGroups = {}; entities.forEach((entity) => { if (entity.typeId !== "minecraft:item" && entity.typeId !== "minecraft:armor_stand" && entity.typeId !== "minecraft:player") { const key = getBlockKey(entity); locationGroups[key] = locationGroups[key] || []; locationGroups[key].push(entity); } }); for (let key in locationGroups) { const group = locationGroups[key]; stackEntitiesByType(group, dimension); } } function stackEntitiesByType(entities, dimension) { const entityTypeGroups = {}; entities.forEach(entity => { entityTypeGroups[entity.typeId] = entityTypeGroups[entity.typeId] || []; entityTypeGroups[entity.typeId].push(entity); }); if (!hasSubscribedToRemoveEvent) { for (let type in entityTypeGroups) { const group = entityTypeGroups[type]; if (group.length > 1) { const mainEntity = group[0]; for (let i = 1; i < group.length; i++) { system.EntityRemoveAfterEventSignal.subscribe((event) => { if (event.removedEntityId === group[i].id) { // Entity has been removed, no further action required } }); } mainEntity.setName(`${type.replace("minecraft:", "")} x${group.length}`); } } hasSubscribedToRemoveEvent = true; } } // Custom event system system._eventListeners = {}; system.registerEvent = function(eventName, callback) { this._eventListeners[eventName] = this._eventListeners[eventName] || []; this._eventListeners[eventName].push(callback); }; system.triggerEvent = function(eventName, data) { if (this._eventListeners[eventName]) { this._eventListeners[eventName].forEach(callback => callback(data)); } }; // Register for the 'entity.killed' event system.registerEvent("entity.killed", (event) => { const entity = event.entity; const dimension = event.dimension; const nameParts = entity.getName().split(" x"); if (nameParts.length === 2) { const type = nameParts[0]; const count = parseInt(nameParts[1]); if (count > 1) { const newEntity = dimension.spawnEntity(type, entity.location); newEntity.setName(`${type} x${count - 1}`); } } });