#Item explode when picked up

26 messages · Page 1 of 1 (latest)

fringe matrix
#

I want to make an item explode when picked up, I just don't know what the explode command would be

tough waspBOT
#

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

indigo stumpBOT
#

Paste version of pipe_bomb.js from @fringe matrix

fringe matrix
#

and also how to delete the item when picked up

fringe matrix
#

or do i just spawn tnt

#

and i would also need to make the event happen when the item enters the inventory if possible

fringe matrix
#

bro

fringe matrix
#

I don't think i know anything said on the pages, would you mind explaining how I would integrate this code into my JS file?

#

I'm a complete begginer

fringe matrix
#

i have migrated to 1.20.1 now and dont really wanna open a new issue becuse it would just be a duplicate thing so im hoping for a new solution

#

i have edited the tags

coarse matrix
fringe matrix
#

So, if i want my server to only have it blow up so that no blocks get destroyed, how would i go about that?

#

Is it just setting the mode to mob

coarse matrix
# fringe matrix So, if i want my server to only have it blow up so that no blocks get destroyed,...

Unfortunately, whether native explosion causes block destruction is only depending on gamerule. But we can mimic an explosion effect which causes no block destrcution. The mimic is not 100% accurate though.

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
}

/**
 * 
 * @param {Internal.Level_} level 
 * @param {number} x 
 * @param {number} y 
 * @param {number} z 
 * @param {number} strength 
 */
function createPseudoExplosion(level, x ,y, z, power, exploderName){
    const aabb = getAABBofRadius(x, y, z, power * 2)
    const explosionCenter = new Vec3d(x, y, z)
    const marker = level.createEntity("marker")
    marker.setCustomName(exploderName)
    marker.spawn()
    level.spawnParticles("minecraft:explosion", false, x, y, z, 0, 0, 0, power/3, 1)
    level.runCommandSilent(`playsound minecraft:entity.generic.explode master @a ${x} ${y} ${z} 1 1`)
    level.getEntitiesWithin(aabb).forEach(entity => {
        const entityPosisition = entity.position()
        const distance = explosionCenter.distanceTo(entityPosisition)
        const normalizedVec = entityPosisition.subtract(explosionCenter).normalize()
        const exposure =
        Array.from(Array(6).keys())
        .map(n => explosionCenter.add(normalizedVec.scale(n + 1)))
        .map(vec => level.getBlock(vec.x(), vec.y(), vec.z()).blockState.air)
        .map(b => b ? 1 : 0)
        .reduce((a, b) => a + b) / 6
        const impact = (1 - distance / (2 * power)) * exposure
        const damageAmount = impact * (impact + 1) * power * 7 + 1
        entity.attack(
            entity.damageSources().explosion(marker, marker), 
            damageAmount
        )
        if(entity.alive){
            let velocity = normalizedVec.scale(exposure)
            entity.setMotion(velocity.x(), velocity.y(), velocity.z())
        }
    })
    marker.remove("discarded")
}

ItemEvents.canPickUp("kubejs:pipe_bomb", event=>{
    let {itemEntity ,itemEntity:{x, y, z}, level} = event    
    createPseudoExplosion(level, x, y, z, 3, event.item.hoverName)
    
    //to prevent actually picking up.
    itemEntity.remove("discarded")
    event.cancel()
})
fringe matrix
#

Oh nvm i forgot i had the gamerule to false so this wont be necessary, also just wanna know what does the mode change

coarse matrix
#

mob and block seems no apparent difference

coarse matrix
#

Update: it seems setting mode to none cause the explosion to stop destroying blocks.

fringe matrix
#

oh great then!

fringe matrix
#

I'm back again, the exploding's now not the problem but the fact I want to make it explode when it enters the inventory because the exact mod this is supposed to be made for doesn't trigger the event

#

if there is a way I would like to know, but otherwise I will be using this code for now

coarse matrix
#

There's PlayerEvents.InventoryChanged

fringe matrix
#

oh well thats a lot more simple than i thought it would be