Minecraft – Keeping inventory conditionally

minecraft-java-editionminecraft-java-edition-server

I want to make a PvP data pack for a Minecraft server, and I want it to function as follows: if a player is killed within half a minute of him hitting another player, they should drop all their items, but if they're killed without hitting another player they should keep their inventory. Is this possible? Also, if possible, I would want to do this without using command blocks.

Best Answer

The gamerule keepInventory can only be applied to the whole Minecraft world. But luckily there's also the /clear command. You can set /gamerule keepInventory true and then handle the inventory clearing with commands:

You can detect players who hit someone/got hit like this: Detect when player attacks another player

Then you can use a scoreboard of the deathCount type to find players who have died (and respawned) and use /clear to clear their inventory.

If you also want to clear their XP, you can use /xp set <player> 0.