Entity changing?

Hello i create an armor stand who is created with the code, and i keep it in a hashmap to get an action on rightclick.
But i have a problem, when i go away too far and i come back, it is not the “same” armorstand and i can’t keep it. How can i do to preserve it?

what is the best way?

I found this website yesterday to generate armor stands :

And it has an option “Persistent” thought I have no idea if it can help you.

You shouldn’t be storing Entities directly, instead store their UUID and get them back with World#getEntity(entityUUID);

To expand on what @RysingDragon said:
An entity object represents the loaded, visible object in the world. This is merely a view of the data on the disk. When the entity is unloaded, the object is forgotten. When the entity is killed, the object is forgotten (even if it’s a player). If the entity is a player and they log out, the object is forgotten. Whether the data is still existent or not is irrelevant; a new view will be constructed once the entity is loaded again. To store an entity object directly in a container is a Bad Thing, because it will very easily cause a memory leak. However, a UUID is simply a number and can be stored directly with no worry of leakage, and can be used to obtain the relevant entity.
If you absolutely MUST hold an entity in a map, use MapMaker#weakValues.

1 Like