Minecraft – How to prevent certain mobs from spawning in vanilla minecraft

minecraft-java-edition

I like the /gamerule command, but how would I make it so only zombies or ghasts don't spawn? Ghasts are really annoying… I'm sure you understand.

Best Answer

There is no vanilla game rule that can disable zombie and ghast spawning, but you can achieve something similar with a custom game rule and a killing clock.

The kill clock

First, set up a fast clock that triggers the following commands to teleport all Zombies and Ghasts into the void, killing them without them dropping any loot (This will prevent you from ever making Regeneration Potions).

/tp @e[type=Zombie] ~ ~-600 ~
/tp @e[type=Ghast] ~ ~-600 ~

Setting up a custom gamerule

Now, if you want to be able to toggle this, we need to create a custom gamerule, by running

/gamerule noZombieOrGhast 1

As of now, this does absolutely nothing. Let's get ourselves a dummy armorstand called #handle (replace however you like) and a dummy objective:

/summon ArmorStand ~ ~ ~ {CustomName:#handle,Invulnerable:1,NoGravity:1,Marker:1,Invisible:1}
/scoreboard objectives add noZombieOrGhast dummy

Now create another clock to query the value of the custom GameRule noZombieOrGhast

/gamerule noZombieOrGhast

Make the command block output the queried result to the armor stand (x y z is the coordinates for the last command block:

/stats block x y z set QueryResult @e[name=#handle] noZombieOrGhast

Finally, place a third command block into this clock to test for the armor stand with the score of 0:

/testfor @e[name=#handle,score_noZombieOrGhast=0]

Put a comparator on this last command block, and make the output disable the killing clock.

Set the custom gamerule to 0 or 1 to allow or disallow Zombie and Ghast spawning, respectively.

/gamerule noZombieOrGhast 0
/gamerule noZombieOrGhast 1