#Item explode when picked up
26 messages · Page 1 of 1 (latest)
Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!
Paste version of pipe_bomb.js from @fringe matrix
and also how to delete the item when picked up
or do i just spawn tnt
and i would also need to make the event happen when the item enters the inventory if possible
bro
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
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
ItemEvents.canPickUp("kubejs:pipe_bomb", event=>{
let {itemEntity ,itemEntity:{x, y, z}, level} = event
//create explosion
level.createExplosion(x, y, z)
.explosionMode("block") //block, mob, tnt.
.causesFire(false) // true to create fire, false to not
.strength(3)
.explode()
//to prevent actually picking up.
itemEntity.remove("discarded")
event.cancel()
})
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
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()
})
Oh nvm i forgot i had the gamerule to false so this wont be necessary, also just wanna know what does the mode change
mob and block seems no apparent difference
Update: it seems setting mode to none cause the explosion to stop destroying blocks.
oh great then!
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
There's PlayerEvents.InventoryChanged
oh well thats a lot more simple than i thought it would be