Minecraft Commands – How to Use /kill to Only Kill Mobs

minecraft-commandsminecraft-java-edition

I have a Redstone Power loop that connects to a Command Block that is set to kill all entities except for myself (/kill @e[type=!Player]), because I'm building a creative map, but don't want slimes all over the place. Problem is if I want to place an Item Frame than the Command Block kills it.

So, is there anyway to exclude items from getting deleted, or make it so it will only target mobs?

Best Answer

The simplest way to handle this is with a gamerule. If you don't want any mobs bothering you in your creative world, just turn off spawns with /gamerule doMobSpawning false. No mobs will spawn, which will allow you to have a quiet creative world, free from cows, pigs, sheep, chickens, slimes, etc, while also allowing yourself to do science on mobs at will. Mobs will spawn only when you want them to.

If you're really committed to the idea of doing this with /kill, however, you can do this one of two ways. You can set up individual command blocks to kill each type of mob, which might be a small set of command blocks if your main problem is slimes.

Alternatively, you can use the methods described in this answer to select exceptions. It uses a scoreboard to set a score for all entities to 1, and simultaneously sets the score of all exceptions to 0. From there, you can select the set of all mobs excluding any exceptions you want with @e[score_select_min=1].

The simplest and most flexible way to keep slimes from bothering you though is to turn off mob spawning altogether.