#Replace item entity after explosion event

33 messages · Page 1 of 1 (latest)

proven shell
#

Hey everyone!
I want to make a script which replaces exploded dirt block items with stone block items.

LevelEvents.afterExplosion(event => {
    const { x, y, z, level } = event
    level.getEntitiesWithin(AABB.of(x - event.size, y - event.size, z - event.size, x + event.size, y + event.size, z + event.size)).forEach(entity => {
        if (entity.item == 'minecraft:dirt') {
            //replace dirt with stone
        }
    })
})

Unfortunately when I tried to test this code with a counter that would count every entity that was within the event, it would always output 0. I don't really know what to do next. Thanks for the help in advance!

drowsy kilnBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

calm marlin
#

your first issue may be that you aren't checking if the entity type is an item, which you can do by:
if (entity.type != 'minecraft:item') return

#

most ideally, check this before checking what the item is

proven shell
#
LevelEvents.afterExplosion(event => {
    const { x, y, z, level } = event
    let count = 0
    level.getEntitiesWithin(AABB.of(x - event.size, y - event.size, z - event.size, x + event.size, y + event.size, z + event.size)).forEach(entity => {
        if (entity.type != 'minecraft:item') return
        count++
        if (entity.item == 'minecraft:dirt') {
            //replace dirt with stone
        }
    })
    event.server.tell(count)
})

The count is still 0

calm marlin
#

can you try using beforeExplosion instead?

#

i want to see how that goes

proven shell
#

Still 0

dreamy python
#

try setting the AABB first as a variable and check for AABB positions

#
let aabb = AABB.of(x - event.size, y - event.size, z - event.size, x + event.size, y + event.size, z + event.size)
console.log(aabb)
level.getEntitiesWithin(aabb)
proven shell
#

Variable aabb seems to be correct, its the size of the explosion. Count is still 0

LevelEvents.beforeExplosion(event => {
    const { x, y, z, level } = event
    let count = 0
    let aabb = AABB.of(x - event.size, y - event.size, z - event.size, x + event.size, y + event.size, z + event.size)
    event.server.tell(aabb)
    level.getEntitiesWithin(aabb).forEach(entity => {
        count++
        if (entity.type != 'minecraft:item') return
        if (entity.item == 'minecraft:dirt') {
            //replace dirt with stone
        }
    })
    event.server.tell(count)
})
dreamy python
proven shell
#

I deleted that line and I was flying away every time when I lit the tnt, that's why the count was 0. If I stood beside it it was 1.

dreamy python
proven shell
#

Did you use the exact same script? I mean of course without the item is dirt part

dreamy python
#
LevelEvents.beforeExplosion(event => {
    const { x, y, z, level, size } = event
    let count = 0
    let aabb = AABB.of(x - size, y - size, z - size, x + size, y + size, z + size)
    event.server.tell(aabb)
    level.getEntitiesWithin(aabb).forEach(entity => {
        count++
        event.server.tell(count)
        // if (entity.type != 'minecraft:item') return
        if (entity.item == 'minecraft:dirt') {
            event.server.tell('item is dirt')
            entity.item = 'minecraft:stone'
        }
    })
    event.server.tell(count)
})```
proven shell
#

I copypasted your code, but it did not work for me

dreamy python
#

this only checks for items that are there before the explosion... not blocks that turn into items

#

doesnt turn it into stone tho, as it still blows up heh

proven shell
#

oh damn, and how could I change so that it checks after the explosion?

#

to explode the dirt blocks into stone block items

dreamy python
#
LevelEvents.afterExplosion(event => {
    const { x, y, z, level} = event
    let size = 5 //Size doesn't work on afterExplosion
    let aabb = AABB.of(x - size, y - size, z - size, x + size, y + size, z + size)
    event.server.scheduleInTicks(1, event =>{
        level.getEntitiesWithin(aabb).forEach(entity => {
            if (entity.item == 'minecraft:dirt') {
                entity.item = 'minecraft:stone'
            }
        }) 
    })
})```


here ya go @proven shell
calm marlin
#

you may want to change entity.item = 'minecraft:stone' to entity.item.id = 'minecraft:stone'

#

and same goes for the if check

dreamy python
#

why... itworks tho?

calm marlin
#

it shouldn't... weird

dreamy python
proven shell
dreamy python
#

i think it works as long as the item.count is 1, but that doesnt matter as 1tick after the explosion the items aren't stacked yet

proven shell
#

@dreamy python thank you very much for the code!!

dreamy python
#

very welcome