How To Stop Vanilla Mobs From Despawning When Idle

Started by Smol on

Topic category: Help with Minecraft modding (Java Edition)

Joined May 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How To Stop Vanilla Mobs From Despawning When Idle

I am wondering if there is a way to stop vanilla mobs (primarily hostile) from despawning when the player goes far away or simply after some time of being idle. 

I have made a custom mob that acts as a lure for zombies. I put the mob on a block and dug a sort of trench around it. the aim is for zombies to walk into the trench and get stuck there. then I return and kill them later on. So like a mob or xp farm. 

 

only problem is zombies despawn fairly quickly when idle, especially is there is a few of them in one area. 

I've done some research and found out some things like with animal mobs, as long as they are trapped/fenced in and cant move 20 blocks in any direction, they shouldn't despawn - however the same logic does not seem to apply to hostile mobs such as zombies. 

I am not very good with coding but I'm willing to give it a go and learn. I recently managed to learn how to get Vanilla Minecraft zombies to attack my custom mob using a code snippet in procedures. 

Any help would be greatly appreciated, thankyou!!! :D

 

Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hostile mobs have to despawn…
Tue, 09/19/2023 - 22:47

Hostile mobs have to despawn; otherwise they can keep accumulating indefinitely in dark areas and lag out the game. Passive mobs don't despawn because they only ever spawn on world generation. (With some exceptions, like bats and fish.) So you definitely don't want to make all hostiles permanent! 

Naming mobs also prevents them from despawning- it's possible you could make your custom mob change the display names of zombies, which would solve the problem, but probably not look great. 

...A potentially more complicated alternative is to make another custom mob that doesn't despawn when idle, but doesn't have any AI or rendering. Whenever a zombie despawns in proximity to your custom block, spawn in one of these, and then have the thing spawn a new zombie when the area is loaded again. ...This will also, admittedly, probably cause some issues.

The best solution might just be to make a custom, modded version of the zombie, and swap out the real zombies with modded ones when they're targeted by your custom mob. The zombie AI should be pretty easy to replicate, and Mcreator has built in Biped models- you could lock the entity's code and replace their texture with the vanilla zombie texture if you want it to be compatible with resource packs, or take it as an excuse to add new zombie textures that indicate the zombies have been trapped.