#how to make itemUse not run when placing block

1 messages · Page 1 of 1 (latest)

timid hornet
#
import { system, world } from "@minecraft/server";

world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack;
    const player = data.source;
    if (item.typeId === "minecraft:tnt") {
        system.run(() => {
            player.runCommand("clear @s tnt 0 1")
            player.runCommand("playsound random.fuse @s ~~~ 1")
            let location = { x: player.location.x + player.getViewDirection().x * 0.5, y: player.location.y + 1.5 + player.getViewDirection().y * 0.5, z: player.location.z + player.getViewDirection().z * 0.5 }
            let tnt = player.dimension.spawnEntity("minecraft:tnt", location)
            tnt.applyImpulse(player.getViewDirection())
        })
    }
})

world.afterEvents.playerPlaceBlock.subscribe(data => {
    if (data.block.typeId == "minecraft:tnt") {
        data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} air`)
        data.player.runCommandAsync(`summon tnt ${data.block.x} ${data.block.y} ${data.block.z}`)
    }
})

i want make it like hive boom box .'

marsh valve
#

do you mean the player can't place the tnt?

#

or do you mean when a tnt block gets place its should not summon a tnt

#

world.beforeEvents.playerPlaceBlock.subscribe(data => {
    if (data.itemStack.typeId == "minecraft:tnt") {
        data.cancel = true
        
    }
})
```if you mean cancel the the place block
#

here the other way

let map = new Map()
world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack;
    const player = data.source;
    if (item.typeId === "minecraft:tnt") {
        system.run(() => {
            if (!map.get(player.id)) {
                player.runCommand("clear @s tnt 0 1")
                player.runCommand("playsound random.fuse @s ~~~ 1")
                let location = { x: player.location.x + player.getViewDirection().x * 0.5, y: player.location.y + 1.5 + player.getViewDirection().y * 0.5, z: player.location.z + player.getViewDirection().z * 0.5 }
                let tnt = player.dimension.spawnEntity("minecraft:tnt", location)
                tnt.applyImpulse(player.getViewDirection())
            } else {
                map.set(player.id, false)
            }
        })
    }
})



world.beforeEvents.playerPlaceBlock.subscribe(data => {
    if (data.itemStack.typeId == "minecraft:tnt") {
        if (map.get(data.player.id) == false) {
            data.player.runCommandAsync("clear @s tnt 0 1")
            data.player.runCommandAsync("playsound random.fuse @s ~~~ 1")
            data.player.runCommandAsync(`summon tnt ${data.block.x} ${data.block.y +1} ${data.block.z}`)
        }
        data.cancel = true
        map.set(data.player.id, true)
    }
})


timid hornet
timid hornet
#

How to check if there is a certain block and run command for that block. For example, if there is a tnt block in world, it will run command

timid hornet
#
import { system, world } from "@minecraft/server";

let map = new Map()

world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack;
    const player = data.source;
    if (item.typeId === "minecraft:tnt") {
        system.run(() => {
            if (!map.get(player.id)) {
            player.runCommand("clear @s tnt 0 1")
            player.runCommand("playsound random.fuse @s ~~~ 1")
            let location = { x: player.location.x + player.getViewDirection().x * 0.5, y: player.location.y + 1.5 + player.getViewDirection().y * 0.5, z: player.location.z + player.getViewDirection().z * 0.5 }
            let tnt = player.dimension.spawnEntity("minecraft:tnt", location)
            tnt.applyImpulse(player.getViewDirection())
            } else {
                map.set(player.id, false)
            }
        })
    }
})

world.afterEvents.playerPlaceBlock.subscribe(data => {
    if (data.block.typeId == "minecraft:tnt") {
        if (map.get(data.player.id) == false) {
        data.player.runCommandAsync(`setblock ${data.block.x} ${data.block.y} ${data.block.z} add:tnt`)
        }
        map.set(data.player.id, true)
    }
})
#

@marsh valve that code work good but sometimes there are some tnts that don't run the command

#

add:tnt is custom block i add, when player place or use setblock i also run command i use json minecraft:on_placed to run command when it place

naive jolt
#

try switching those two event listeners maybe?
So, blockPlace is on the top, and itemUse is on the bottom.

#

And hopefully, blockPlace will take priority to fire first, and send that information to itemUse, preventing it from running.

#

Or the other way... You do a raycast if there's any block on the player view. (This will not work for mobile players)

mystic spruce
#

@marsh valve

#

I have a problem

#

@naive jolt

#

Why are the buttons I created deleted when I exit and return from the game?

winter shell
#

that gets resetted upon world /reload or rejoining

#

if you want to store the data actually use dynamicproperties

#

or scoreboard database

mystic spruce
#

Send it please

#

@winter shell

timber robinBOT
#

Description
Database

Credits
These scripts were written by iBlqzed

mystic spruce
#

There are a lot of them

timid hornet
#

is it possible to check the block and run command for it, like json

#

like this

#
"minecraft:on_placed": {
                 "event": "tnt"
            }
        },
        "events": {
            "tnt": {
                "run_command": {
                    "command": [
                        "setblock ~~~ air",
                        "summon tnt ~~-0.60~"
                    ]
                }
            }
        }
    }
}
marsh valve
# timid hornet ```js import { system, world } from "@minecraft/server"; let map = new Map() w...

here

let map = new Map()

world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack;
    const player = data.source;
    if (item.typeId === "minecraft:tnt") {
        system.run(() => {
            if (!map.get(player.id)) {
                player.runCommand("clear @s tnt 0 1")
                player.runCommand("playsound random.fuse @s ~~~ 1")
                let location = { x: player.location.x + player.getViewDirection().x * 0.5, y: player.location.y + 1.5 + player.getViewDirection().y * 0.5, z: player.location.z + player.getViewDirection().z * 0.5 }
                let tnt = player.dimension.spawnEntity("minecraft:tnt", location)
                tnt.applyImpulse(player.getViewDirection())
            } else {
                map.set(player.id, false)
            }
        })
    }
})

world.beforeEvents.playerPlaceBlock.subscribe(data => {
    let { player, block, itemStack } = data
    if (itemStack.typeId == "minecraft:tnt") {
        if (map.get(player.id) == false) {
            let { x,y,z } = facing(player)
            player.runCommandAsync("clear @s tnt 0 1")
            player.runCommandAsync("playsound random.fuse @s ~~~ 1")
            player.runCommandAsync(`summon tnt ${x} ${y} ${z}`)
        }
        map.set(player.id, true)
        data.cancel = true
    }
})

function facing(player, offset = 1) {
    let { x, y, z } = player.getBlockFromViewDirection().block.location;
    let face = player.getBlockFromViewDirection().face;

    switch (face) {
        case 'Down':
            y -= offset;
            break;
        case 'Up':
            y += offset;
            break;
        case 'East':
            x += offset;
            break;
        case 'West':
            x -= offset;
            break;
        case 'North':
            z -= offset;
            break;
        case 'South':
            z += offset;
            break;
    }

    return { x, y, z };
}
#

@timid hornet

naive jolt
marsh valve
#

oh you right

#

I forgot about that

#

XD

marsh valve
#

I don't have any idea idk

#

I have this ```js
world.beforeEvents.playerPlaceBlock.subscribe(data => {
let { player, block, itemStack } = data
if (itemStack.typeId == "minecraft:tnt") {
if (map.get(player.id) == false) {
let { x, y, z } = facing(block)
player.runCommandAsync("clear @s tnt 0 1")
player.runCommandAsync("playsound random.fuse @s ~~~ 1")
player.runCommandAsync(summon tnt ${x} ${y} ${z})
}
map.set(player.id, true)
data.cancel = true
}
})
function facing(block, offset = 1) {
let { x, y, z } = block.location;

if (block.above(1).typeId == "minecraft:air") {
    y += offset;
} 
else if (block.below(1).typeId == "minecraft:air") {
    y -= offset;
}
else if (block.east(1).typeId == "minecraft:air") {
    x += offset;
} 
else if (block.west(1).typeId == "minecraft:air") {
    x -= offset;
} 
else if (block.north(1).typeId == "minecraft:air") {
    z -= offset;
} 
else if (block.south(1).typeId == "minecraft:air") {
    z += offset;
}
return { x, y, z };

}

but the problem is here when the wall is 1x1x2+ its spawn the tnt the nearst block air and its always the same direction
marsh valve
mystic spruce
#

@marsh valve

#

Oh brother, the buttons that were created from the list when I leave the world and come back, the buttons are deleted.

naive jolt
#

Honestly, I'll wrap itemUse into a system.runTimeout(), and check the boolean value returned by the blockPlace event listener from there.

That way, blockPlace will have the firing order prioritized, and have enough time to send necessary toggle/information to the itemUse event listener.

marsh valve
#

bro please stop pinging me

marsh valve
#
let map = new Map()

world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack;
    const player = data.source;
    if (item.typeId === "minecraft:tnt") {
        system.run(() => {
            if (!map.get(player.id)) {
                player.runCommand("clear @s tnt 0 1")
                player.runCommand("playsound random.fuse @s ~~~ 1")
                let location = { x: player.location.x + player.getViewDirection().x * 0.5, y: player.location.y + 1.5 + player.getViewDirection().y * 0.5, z: player.location.z + player.getViewDirection().z * 0.5 }
                let tnt = player.dimension.spawnEntity("minecraft:tnt", location)
                tnt.applyImpulse(player.getViewDirection())
            } else {
                map.set(player.id, false)
            }
        })
    }
})

world.beforeEvents.itemUseOn.subscribe(data => {
    let { source: player, blockFace, itemStack, block } = data
    if (itemStack.typeId == "minecraft:tnt") {
        if (map.get(player.id) == false) {
            let { x,y,z } = facing(block, blockFace)
            player.runCommandAsync("clear @s tnt 0 1")
            player.runCommandAsync("playsound random.fuse @s ~~~ 1")
            player.runCommandAsync(`summon tnt ${x} ${y} ${z}`)
        }
        map.set(player.id, true)
        data.cancel = true
    }
})

function facing(block, blockFace, offset = 1) {
    let { x, y, z } = block.location

    switch (blockFace) {
        case 'Down':
            y -= offset;
            break;
        case 'Up':
            y += offset;
            break;
        case 'East':
            x += offset;
            break;
        case 'West':
            x -= offset;
            break;
        case 'North':
            z -= offset;
            break;
        case 'South':
            z += offset;
            break;
    }

    return { x, y, z };
}

@timid hornet

#

@naive jolt thx

#

@mystic spruce now I can help you

mystic spruce
#

Good

mystic spruce
#

The script we learned about yesterday worked, but when I created buttons, exited the world, and returned, the buttons were deleted.

mystic spruce
#

I made it but no one responded

storm wigeon
#

where is it

marsh valve
#

#1189184628980994158

#

here

mystic spruce
#

Yes

timid hornet
timid hornet
#

i need something liks this but script and run command for block want

#
"minecraft:on_placed": {
                 "event": "tnt"
            }
        },
        "events": {
            "tnt": {
                "run_command": {
                    "command": [
                        "setblock ~~~ air",
                        "summon tnt ~~-0.60~"
                    ]
                }
            }
        }
    }
}
marsh valve
# timid hornet Sometimes, when setting tnt, it doesn't run the command but it disappears, in su...

try this

let map = new Map()

world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack;
    const player = data.source;
    if (item.typeId === "minecraft:tnt") {
        system.run(() => {
            if (!map.get(player.id)) {
                player.runCommand("clear @s tnt 0 1")
                player.runCommand("playsound random.fuse @s ~~~ 1")
                let location = { x: player.location.x + player.getViewDirection().x * 0.5, y: player.location.y + 1.5 + player.getViewDirection().y * 0.5, z: player.location.z + player.getViewDirection().z * 0.5 }
                let tnt = player.dimension.spawnEntity("minecraft:tnt", location)
                tnt.applyImpulse(player.getViewDirection())
            } else {
                map.set(player.id, null)
            }
        })
    }
})



world.beforeEvents.itemUseOn.subscribe(data => {
    let { source: player, blockFace, itemStack, block } = data
    if (itemStack.typeId == "minecraft:tnt") {
        if (!map.get(player.id)) {
            let { x,y,z } = facing(block, blockFace)
            player.runCommandAsync("clear @s tnt 0 1")
            player.runCommandAsync("playsound random.fuse @s ~~~ 1")
            player.runCommandAsync(`summon tnt ${x} ${y} ${z}`)
        }
        map.set(player.id, true)
        data.cancel = true
    }
})
timid hornet
marsh valve
#

bc you didn't used the function

marsh valve
#

for me it works

#

idk

timid hornet
marsh valve
#

is this a custom block?

timid hornet
#

texture

#

is vanilla tnt

marsh valve
#

for me its working fine

timid hornet
#

hmm

marsh valve
#

you right nvm

#

its the only gamemode that buggy

#

try with adventure

#

it should work also for creative mode

timid hornet
#

adventure work

#

but i want survival mode;-;

marsh valve
#

hmm idk how to fix that

timid hornet
marsh valve
#

you can use this

let map = new Map()

world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack;
    const player = data.source;
    if (item.typeId === "minecraft:tnt") {
        system.run(() => {
            if (map.get(player.id) == true) {
                player.runCommand("clear @s tnt 0 1")
                player.runCommand("playsound random.fuse @s ~~~ 1")
                let location = { x: player.location.x + player.getViewDirection().x * 0.5, y: player.location.y + 1.5 + player.getViewDirection().y * 0.5, z: player.location.z + player.getViewDirection().z * 0.5 }
                let tnt = player.dimension.spawnEntity("minecraft:tnt", location)
                tnt.applyImpulse(player.getViewDirection())
            } else {
                map.set(player.id, null)
            }
        })
    }
})

world.beforeEvents.itemUseOn.subscribe(data => {
    let { source: player, blockFace, itemStack, block } = data
    if (itemStack.typeId == "minecraft:tnt") {
        if (map.get(player.id) == true) {
            let { x,y,z } = facing(block, blockFace)
            player.runCommandAsync("clear @s tnt 0 1")
            player.runCommandAsync("playsound random.fuse @s ~~~ 1")
            player.runCommandAsync(`summon tnt ${x} ${y} ${z}`)
        }
        map.set(player.id, true)
        data.cancel = true
    }
})

but the first block that you placed get canceled

#

but it works in survival

timid hornet
#

hmm, i think i fixed it

marsh valve
#

how

timid hornet
marsh valve
#

@timid hornet

timid hornet
# timid hornet

The reason I need a script that can run commands for the block, it use command replace tnt = add:tnt

#
"minecraft:on_placed": {
                 "event": "tnt"
            }
        },
        "events": {
            "tnt": {
                "run_command": {
                    "command": [
                        "setblock ~~~ air",
                        "summon tnt ~~-0.60~"
                    ]
                }
            }
        }
    }
}
#

so do you know how run command for block?

marsh valve
#

no sorry

timid hornet
#

ok thank you, i think should use command

marsh valve
#

wait I think I have a block that use run command

#

I need to search for it

timid hornet
marsh valve
#

you mean script oh I thought you mean json

#

xD

timid hornet
#

.-

marsh valve
#

?

timid hornet
#
{
    "format_version": "1.16.100",
    "minecraft:block": {
        "description": {
            "identifier": "add:tnt"
        },
        "components": {
            "minecraft:loot": "loot_tables/empty.json",
            "minecraft:geometry": "geometry.empty",
            "minecraft:destroy_time": 10,
            "minecraft:block_light_emission": 0,
            "minecraft:pick_collision": {
                "origin": [-1, 0, -1],
                "size": [0, 0, 0]
            },
            "minecraft:entity_collision": {
                "origin": [-1, 0, -1],
                "size": [0, 0, 0]
            },
            "minecraft:material_instances": {
               "*": {
                 "texture": "invisibility",
                 "render_method": "blend"
              }
            },
            "minecraft:on_placed": {
                 "event": "tnt"
            }
        },
        "events": {
            "tnt": {
                "run_command": {
                    "command": [
                        "setblock ~~~ air",
                        "summon tnt ~~-0.60~"
                    ]
                }
            }
        }
    }
}
marsh valve
#

does it lite automaticlly on?

timid hornet
#

my block

marsh valve
#

If you set your block does it ignites automatically

#

@timid hornet

#

like /setblock

timid hornet
#

/execute as @a at @s run fill ~10 ~-10 ~10 ~-10 ~10 ~-10 add:tnt replace tnt

#

it auto ignites tnt

marsh valve
#

oh ok wait I think it should work with script

#

do you mean like this?

world.beforeEvents.itemUseOn.subscribe(data => {
    let { source: player, blockFace, itemStack, block } = data
    if (itemStack.typeId == "minecraft:tnt") {
        let { x, y, z } = facing(block, blockFace)
        player.runCommandAsync(`setblock ${x} ${y} ${z} add:tnt`)
        player.runCommandAsync(`setblock ${x} ${y} ${z} air`)
        data.cancel = true
    }
})
#
{
    "format_version": "1.16.100",
    "minecraft:block": {
        "description": {
            "identifier": "add:tnt"
        },
        "components": {
            "minecraft:loot": "loot_tables/empty.json",
            "minecraft:geometry": "geometry.empty",
            "minecraft:destroy_time": 10,
            "minecraft:block_light_emission": 0,
            "minecraft:pick_collision": {
                "origin": [
                    -1,
                    0,
                    -1
                ],
                "size": [
                    0,
                    0,
                    0
                ]
            },
            "minecraft:entity_collision": {
                "origin": [
                    -1,
                    0,
                    -1
                ],
                "size": [
                    0,
                    0,
                    0
                ]
            },
            "minecraft:material_instances": {
                "*": {
                    "texture": "invisibility",
                    "render_method": "blend"
                }
            },
            "minecraft:on_placed": {
                "event": "tnt"
            }
        },
        "events": {
            "tnt": {
                "run_command": {
                    "command": [
                        "summon tnt ~ ~ ~"
                    ]
                }
            }
        }
    }
}
marsh valve
timid hornet
#

Run the command on the vanilla block with script

#

I have to go now. See you later

#

Many thanks for your help!

marsh valve
#

You can use this it works fine but the problem is that it has a cooldown when you throw the tnt

let map = new Map();

world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack;
    const player = data.source;
    if (item.typeId === "minecraft:tnt") {
        system.run(() => {
            if (!map.get(player.id)) {
                player.runCommand("clear @s tnt 0 1");
                player.runCommand("playsound random.fuse @s ~~~ 1");
                let location = {
                    x: player.location.x + player.getViewDirection().x * 0.5,
                    y: player.location.y + 1.5 + player.getViewDirection().y * 0.5,
                    z: player.location.z + player.getViewDirection().z * 0.5,
                };
                let tnt = player.dimension.spawnEntity("minecraft:tnt", location);
                tnt.applyImpulse(player.getViewDirection());
            }
            map.set(player.id, !map.get(player.id));
        });
    }
});

world.beforeEvents.itemUseOn.subscribe((data) => {
    let { source: player, blockFace, itemStack, block } = data;
    if (itemStack.typeId == "minecraft:tnt") {
        let { x, y, z } = facing(block, blockFace);
        player.runCommandAsync("clear @s tnt 0 1");
        player.runCommandAsync("playsound random.fuse @s ~~~ 1");
        player.runCommandAsync(`summon tnt ${x} ${y} ${z}`);
        map.set(player.id, !map.get(player.id));
        data.cancel = true;
    }
});
timid hornet
#

@marsh valve do you know how to make this tnt have owner ? ...

storm wigeon
timid hornet
storm wigeon
#

yeah it is

storm wigeon
timid hornet
storm wigeon
#

its the entity

#

that summons a tnt

timid hornet
#

Oh but i need vanilla tnt for all

storm wigeon
#

it is?

timid hornet
#

So i throw traye:tnt_spawner when it hìt someone or block it summon tnt with owner?

storm wigeon
#

no its a entity not a item

timid hornet
#

Yes

storm wigeon
#

if u summon traye:tnt_spawner it will hit someone with a owner

timid hornet
#

I mean when throw that entity hit some and summon tnt? Right?

storm wigeon
#
let map = new Map();

world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack;
    const player = data.source;
    if (item.typeId === "minecraft:tnt") {
        system.run(() => {
            if (!map.get(player.id)) {
                player.runCommand("clear @s tnt 0 1");
                player.runCommand("playsound random.fuse @s ~~~ 1");
                let location = {
                    x: player.location.x + player.getViewDirection().x * 0.5,
                    y: player.location.y + 1.5 + player.getViewDirection().y * 0.5,
                    z: player.location.z + player.getViewDirection().z * 0.5,
                };
                let tnt = player.dimension.spawnEntity("traye:tnt_spawner", location);
                tnt.applyImpulse(player.getViewDirection());
            }
            map.set(player.id, !map.get(player.id));
        });
    }
});

world.beforeEvents.itemUseOn.subscribe((data) => {
    let { source: player, blockFace, itemStack, block } = data;
    if (itemStack.typeId == "minecraft:tnt") {
        let { x, y, z } = facing(block, blockFace);
        player.runCommandAsync("clear @s tnt 0 1");
        player.runCommandAsync("playsound random.fuse @s ~~~ 1");
        player.runCommandAsync(`summon tnt ${x} ${y} ${z}`);
        map.set(player.id, !map.get(player.id));
        data.cancel = true;
    }
});```
timid hornet
#

It also have models tnt?

storm wigeon
#

yes?

#

it summons tnt entity?

timid hornet
#

When i throw that entity

#

I mean when it's in the air

storm wigeon
#

the entity transforms into tnt

#

so yes

#

it models tnt

timid hornet
#

Ok

storm wigeon
#

bc it is tnt

timid hornet
#

Ok i'll try it later thank you

timid hornet
#

also event entity

storm wigeon
#

did u paste both?

timid hornet
#

yes

storm wigeon
#

where did u paste them

timid hornet
#

to entities

storm wigeon
#

then what did u do to get the spawner in?

timid hornet
#

i mov emy player.json and replace it

storm wigeon
#

no like how did u spawn it?

timid hornet
storm wigeon
#

/summon doesnt work with it

timid hornet
#

and after i use event

#

event entity

storm wigeon
#

because u gotta use event entity @s traye:tnt_spawner

timid hornet
#

yes i use it

#

but not work

storm wigeon
#

did u reload

#

ur world

timid hornet
#

i said i was kill by block of tnt

#

hmm

#

ok

storm wigeon
#

if u want more info u can look at this

timid hornet
timid hornet
storm wigeon
#

hm intresting

#

well i had a owner issue aswell but i made a post and this solved it

timid hornet
#

hmm

storm wigeon
#

what was the issue?

timid hornet
#

use "component_groups": {
"minecraft:spawn_tnt": {
"minecraft:spawn_entity": {
"entities": {
"min_wait_time": 0,
"max_wait_time": 0,
"spawn_entity": "minecraft:tnt",
"single_use": true,
"num_to_spawn": 1
}
}
},

#

use vanilla tnt

#

instead add entity

storm wigeon
#

wdym

timid hornet
storm wigeon
#

u just gotta wait like 1 tick for it to spawn

#

then applyimpulse

timid hornet
#

can ex?

#

code

storm wigeon
#

what are u sayinng?

timid hornet
#

EXample

storm wigeon
#

sure

timid hornet
#

ok

storm wigeon
#
import { world, system } from '@minecraft/server';

let map = new Map();

world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack;
    const player = data.source;
    if (item.typeId === "minecraft:tnt") {
        system.run(() => {
            if (!map.get(player.id)) {
                player.runCommand("clear @s tnt 0 1");
                player.runCommand("playsound random.fuse @s ~~~ 1");
                let location = {
                    x: player.location.x + player.getViewDirection().x * 0.5,
                    y: player.location.y + 1.5 + player.getViewDirection().y * 0.5,
                    z: player.location.z + player.getViewDirection().z * 0.5,
                };
                let tnt = player.dimension.spawnEntity("minecraft:tnt", location);
                system.runTimeout(() => {
                tnt.applyImpulse(player.getViewDirection())},1);
            }
            map.set(player.id, !map.get(player.id));
        });
    }
});

world.beforeEvents.itemUseOn.subscribe((data) => {
    let { source: player, blockFace, itemStack, block } = data;
    if (itemStack.typeId == "minecraft:tnt") {
        let { x, y, z } = facing(block, blockFace);
        player.runCommandAsync("clear @s tnt 0 1");
        player.runCommandAsync("playsound random.fuse @s ~~~ 1");
        player.runCommandAsync(`summon tnt ${x} ${y} ${z}`);
        map.set(player.id, !map.get(player.id));
        data.cancel = true;
    }
});```
timid hornet
#

i mean throw entity summon by event

storm wigeon
#

just run as the player

timid hornet
#

player.triggerEvent("minecraft:spawn_tnt")

#

after it summon knockback it

#

can?

storm wigeon
#

yeah

#

this?

import { world, system } from '@minecraft/server';

let map = new Map();

world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack;
    const player = data.source;
    if (item.typeId === "minecraft:tnt") {
        system.run(() => {
            if (!map.get(player.id)) {
                player.runCommand("clear @s tnt 0 1");
                player.runCommand("playsound random.fuse @s ~~~ 1");
                let location = {
                    x: player.location.x + player.getViewDirection().x * 0.5,
                    y: player.location.y + 1.5 + player.getViewDirection().y * 0.5,
                    z: player.location.z + player.getViewDirection().z * 0.5,
                };
                player.triggerEvent("eventName")
                const tnt = world.getDimension("overworld").getEntities({type:"minecraft:tnt"})[0]
                system.runTimeout(() => {
                tnt.applyImpulse(player.getViewDirection())},1);
            }
            map.set(player.id, !map.get(player.id));
        });
    }
});

world.beforeEvents.itemUseOn.subscribe((data) => {
    let { source: player, blockFace, itemStack, block } = data;
    if (itemStack.typeId == "minecraft:tnt") {
        let { x, y, z } = facing(block, blockFace);
        player.runCommandAsync("clear @s tnt 0 1");
        player.runCommandAsync("playsound random.fuse @s ~~~ 1");
        player.runCommandAsync(`summon tnt ${x} ${y} ${z}`);
        map.set(player.id, !map.get(player.id));
        data.cancel = true;
    }
});```
timid hornet
#

[Scripting][error]-TypeError: cannot read property 'applyImpulse' of undefined at <anonymous> (boom_box.js:23)

storm wigeon
#

wait

#

what is ur code for player.json

timid hornet
storm wigeon
#

it works but i have no idea why apply impulse isnt working