Minecraft – Making player invincible to mobs, but not other players

minecraft-java-edition

Is there some way that I could make players invincible to mobs, but still be able to be harmed by players? I have already tried using this command, but it makes the player invincible to other players too. Is there anyway I can do this? Here is the command I tried:

/effect @a 11 240 372000

Best Answer

Yes, there is a way in the 1.8 snapshots.

Using a RedStone clock give all entities that damage the player a huge weakness effect:

/effect @e[mob id] 18 9999 127

Note that @e targets players and thus so that only mobs get a weakness effect you have to put in an ID for the entity type. You will have to have a few command blocks each for each type of hostile entity(One each for zombies, skeletons, endermen, creepers{change gamerule doMobGriefing to false}, zombie pigmen, ghasts and blazes). This prevents them from doing damage to the player. Note also that if you give them a weakness level of more than 127, it has a negative effect, meaning it will give them basically 127 strength.

Another method that can be used in 1.7.x is giving all the players a resistance effect like you did but weaker, and then giving players a strength effect that overpowers the resistance.
Strength:

/effect @a 5 9999 6

Resistance:

/effect @a 11 9999 4

This will give all players resistance 4 and this is enough to make them invincible to mobs. They will still take hits but will not take any damage. Note: I am not actually sure if Strength 6 is enough to overpower Resistance 4. If it isn't, just give a higher strength. If the Resistance is too low, give them a higher resistance and higher strength. Just look for the balance. :)

A few questions:

  1. Is there a reason you are not making it on Peaceful mode? (I imagine it is so that players don't regenerate health that quickly but you can turn the gamerule naturalRegeneration off)
  2. Is there a reason, if you don't want to turn it to peaceful, why you want to keep gamerule doMobSpawning on?
  3. Are you manually spawning the mobs, i.e. with command blocks? If yes you can modify their attributes to not allow them to damage the player.

So yes, there are ways to do it in vanilla Minecraft.

Hope I helped!