const getAABBofRadius = (centerX, centerY, centerZ, radius) => {
const [minX, minY, minZ, maxX, maxY, maxZ] = [-1, 1].map(c => [centerX, centerY, centerZ].map(n => n + radius * c)).reduce((a, b) => a.concat(b))
const aabb = AABB.of(minX, minY, minZ, maxX, maxY, maxZ)
return aabb
}
const wormHeadEntityType = "alexsmobs:void_worm"
const wormPartEntityType = "alexsmobs:void_worm_part"
const wormsUUIDmap = {}
const getWormTail = (level, tickCount, aabb) => {
let uuidHashcode
wormsUUIDmap[tickCount].forEach(n => uuidHashcode = n)
return level.getEntitiesWithin(aabb).filter(entity => entity.nbt.UUID.hashCode() == uuidHashcode)[0]
}
EntityEvents.spawned([wormHeadEntityType, wormPartEntityType], event=>{
const {entity:{nbt, nbt:{UUID:wormUUID}, type, x, y, z}, server, server:{tickCount:rawTickCount}, level} = event
let tickCount = rawTickCount
if(type == wormHeadEntityType) tickCount += 1
wormsUUIDmap[tickCount] = wormsUUIDmap[tickCount] || new Set()
wormsUUIDmap[tickCount].add(wormUUID.hashCode())
const ParentUUID = nbt.ParentUUID
if(!ParentUUID) return
function deleteHavingParent (ParentUUID){
server.scheduleInTicks(1, _ => {
wormsUUIDmap[tickCount].delete(ParentUUID.hashCode())
})
}
deleteHavingParent(ParentUUID)
})
BlockEvents.placed(event=>{
let {block, server, server:{tickCount}, level} = event
let worm = block.createEntity("alexsmobs:void_worm")
let {x, y, z} = worm
worm.spawn()
server.scheduleInTicks(3, _=>{
let wormTail = getWormTail(level, tickCount + 1, getAABBofRadius(x, y, z, 50))
console.log(wormTail.nbt)
let newWormPart
for(let i = 0; i < 10; i++){
newWormPart = level.createEntity(wormPartEntityType)
newWormPart.setPosition(x, y + 10, z)
newWormPart.mergeNbt({ParentUUID: wormTail.nbt.UUID, BodyIndex: wormTail.nbt.BodyIndex + 1, TailPart: true})
newWormPart.spawn()
console.log(newWormPart.nbt)
wormTail = newWormPart
}
})
})