I tried to figure it out and the only solution I could come up with was a command block for each slot. I'm currently making a one command where it will force armor on you but I want to make it so if you try to take it off it removes from your inventory.
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.
/replaceitem entity @a slot.inventory.# minecraft:air
# 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
/replaceitem entity @a slot.hotbar.# minecraft:air
# with a number from 0 to 8, 0 being far left hotbar slot, 8 being far right. This does not include offhand
/replaceitem entity @a slot.weapon.offhand minecraft:air
To clear the entire inventory, use
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.