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.


Solution 1:

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.

Solution 2:

I currently am working on a server-side Fabric mod which would entirely focus on such a matter. And even though it still only is in beta, you can still set a config to make it work properly, since making it work properly at first was the alpha version challenge.

You can actually find that mod right here. I'd also soon be publishing updates, as soon as I'll reach some real step in the development.

Here is a helpful description from the mod:

When any player dies, if the keepInventory vanilla gamerule is set to true, and the death matches any condition set in the blacklist, the player's inventory drops anyway. If set to false, the mod searches for matching death into the whitelist, and if it finds any, the player gets to keep its inventory. But here comes the second gamerule, conditionalDoVanishing, which makes it so that, when the player gets to keep its inventory despite the vanilla keepInventoory, and this gamerule is set to true, all the items with the Curse of Vanishing in their inventory still get to disappear in thin air.  Finally, when the server stops, the mod saves the config to the config file to make sure it matches the gamerules.