Using Command Blocks would work if you put one with the /gamerule keepInventory False command into the entrance of the region of no keepInventory, hidden under a block that would activate it (such as a pressure plate). Be sure to reset this at the beginning and after this location with /gamerule keepInventory true.
There is no way to remove items if they are moved with the cursor, as far as I am aware, due to the fact that it doesn't have a slot id.
However clearing all other slots of a specific item and killing all dropped items is possible, using /replaceitem
. This would require 36 command blocks to clear items from every slot in the inventory apart from armor, 37 if you include offhand.
Inventory
/replaceitem entity @a slot.inventory.# minecraft:air
Replace #
with a number from 0 to 26, with 0 being top-left slot and 26 being bottom right slot of the inventory. This does not include the hotbar
Hotbar
/replaceitem entity @a slot.hotbar.# minecraft:air
Replace #
with a number from 0 to 8, 0 being far left hotbar slot, 8 being far right. This does not include offhand
Offhand
/replaceitem entity @a slot.weapon.offhand minecraft:air
To clear the entire inventory, use /clear <player>
I know its a late answer, but this will work. It may cause a bit of lag. You will not be able to use any other items, unless you find a way of specifying what item to replace. I have tested /clear
for its maxCount
parameter, but it specifies the maximum amount to be cleared, not the maximum items left.
Good luck!
Best Answer
Other thing you can do is clear both pants and then give the player a new set of pants in the appropriate armor slot.