Flint and steel TNT
Right-click any block with flint and steel and it turns into lit TNT.
In this lesson you build a flint and steel that turns whatever block you click into lit TNT. Stone, dirt, your friend's house: a right click, a hiss, and four seconds later it is gone.
Listen for clicks
Clicking is when a player interacts. It runs for every click, left or right, on a block or into the air, and brings six event values with it:
| Value | What it is |
|---|---|
player | Who clicked |
action | What kind of click: RIGHT_CLICK_BLOCK, LEFT_CLICK_AIR and so on |
clickedBlock | The block they clicked, empty when they clicked into the air |
blockFace | Which side of the block they clicked |
hand | Which hand this run is for |
material | What they are holding in that hand |
Only with flint and steel
Nobody wants every click to blow things up, so first check what the player is holding. material is a
Material, and as you saw in Types, you compare it with a Material from the
dropdown, not with text:
This already makes blocks disappear. It also has a bug: right-click into the air with flint and steel and the script stops with an error, because there is no clicked block to set to air. Using a value that is empty stops the script, and the server log says why.
Only right clicks on a block
action tells you what kind of click it was. and lets you ask for both things at once:
That is the whole trick. The block is replaced with air, and spawn puts lit TNT in its place. EntityType.TNT
is TNT that is already burning, the same as when you light a TNT block by hand, so it goes off after four seconds.
Why offset by 0.5, 0, 0.5? A block's location is its corner, the point with the smallest x, y and z. Spawn the TNT there and it sits on the edge between four blocks. Half a block along x and z puts it in the middle of the hole.
Why the click does not count twice
A right click runs when a player interacts once for each hand. The second run is for the other hand, and
material is then whatever is in that hand, usually nothing. The material check skips that run for free.
Make it better
A few extras: a fuse sound, some flames, and a rule that bedrock stays put.
Notice that the bedrock check comes first and ends with return, before anything is changed. Checking first and leaving early keeps the rest of the script free of extra if blocks.
Try it yourself
- Cluster bomb. Use a blaze rod instead, and drop three TNT one after the other with a repeat and a wait inside it. The clicked block stays where it is this time, so the TNT lands on top of it.
- Lightning rod. Swap the TNT for strike lightning.
- Creative only. Check the player's game mode so only builders in creative mode can use the TNT lighter.
The variable spot in the cluster bomb keeps its location across the waits. Variables hold their values for the
rest of the run, and start empty again the next time the event happens. Building bigger things
uses them a lot more.