galacticr3

make a datapack that adds an explosive sword enchant, the enchant should make every mob hit with the sword that has this enchant explode.

Kodari AI
KodariJul 14 at 3:27 pm 1

I’ll create the ExplosiveSword data pack with a custom enchantment that makes hit mobs explode.

ExplosiveSwordDataPack
A vanilla data pack adding a custom "Explosive" sword enchantment. When a player hits any mob while holding a sword enchanted with Explosive, the target mob explodes in a TNT blast. The enchantment is obtainable through enchanting tables, villager trades, and loot. Compatible with Minecraft 1.21.4 vanilla data pack format.

Done. The Explosive Sword data pack adds a custom enchantment:

How it works:

  • The Explosive enchantment (gold, level 1) is available on swords via enchanting tables, villager trades, and loot chests

  • When a player hits any mob while holding a sword with this enchantment, the nearest non-player entity within 3 blocks (the mob they just hit) explodes

  • The explosion is a real TNT blast — damages the mob, nearby entities, and the terrain

To use:

  1. Extract the folder into world/datapacks/

  2. Run /reload

  3. Enchant a sword with Explosive (via enchanting table, anvil with enchanted book, or /enchant @s explosive_sword:explosive)

  4. Hit any mob — it explodes on contact

galacticr3

how do i download it?

Kodari AI
KodariJul 14 at 3:29 pm 1

I don’t have a built-in download/export button in this chat — you’ll need to create the files manually from the content I already displayed above. Here’s the quickest way:

1. Copy the folder structure: Create these files in a folder called explosive_sword_datapack:

Plain Text
explosive_sword_datapack/
├── pack.mcmeta
└── data/
    ├── explosive_sword/
    │   ├── enchantment/
    │   │   └── explosive.json
    │   ├── advancement/
    │   │   └── explosive_hit.json
    │   └── function/
    │       ├── trigger_explosion.mcfunction
    │       └── load.mcfunction
    └── minecraft/
        └── tag/
            ├── enchantment/
            │   ├── in_enchanting_table.json
            │   ├── tradeable.json
            │   └── on_random_loot.json
            └── function/
                └── load.json

2. Copy the file contents from my previous message — each <create> block shows the full content for that file.

3. Place it in your world:

  • Navigate to saves/<YourWorld>/datapacks/

  • Drop the explosive_sword_datapack folder in there

  • Launch Minecraft and run /reload

If you want, I can simplify this — I can remake the pack with fewer files (combining everything into fewer files) to make manual copying easier. Just say the word.

galacticr3

add a function for /enchant

Kodari AI
KodariJul 14 at 3:44 pm 1

Now run /function explosive_sword:enchant while holding a sword to add the Explosive enchantment.

galacticr3

doesn't work

Kodari AI
KodariJul 14 at 3:50 pm 1

Looking at all the files, I see several issues. Let me fix the whole system:

  1. The advancement conditions are too strict/complex

  2. The function needs to check the enchantment itself

  3. No repeatability (advancement fires once)

What changed:

  • Advancement now triggers on any hit (no conditions) — the enchantment check moved to the function

  • New predicate (has_explosive.json) checks if the player’s mainhand has the Explosive enchantment

  • Function uses the predicate, summons TNT at the nearest mob, then revokes the advancement so it can fire again on the next hit

Give it a try with /reload!

Kodari can make mistakes. Review important code before using.
Files
explosive_sword_datapack
Code
11 90
explosive_sword_datapack/data/explosive_sword/function/trigger_explosion.mcfunction
# Check if player has Explosive enchantment on mainhand
execute if predicate explosive_sword:has_explosive as @e[type=!player,distance=..5,limit=1,sort=nearest] at @s run summon minecraft:tnt ~ ~ ~ {Fuse:0}
# Allow repeat triggering
advancement revoke @s only explosive_sword:explosive_hit