Carpet

Carpet

2M Downloads

Entity going through Nether Portal fires `entity_load_handler` with `new` being true

James103 opened this issue ยท 3 comments

commented

Currently (Carpet mod 1.4.45 and Minecraft 1.17.1), when you make an entity teleport across dimensions, the teleportation fires entity_load_handler events for that entity with new being true instead of false.

This means that entities that go through a Nether or End portal or are teleported to a different dimension using commands will be treated as if they were newly spawned entities for the purposes of Scarpet entity load handlers, even if they have been already initialized and tracked by the system.

Example code:

__on_start() -> (
	// Construct entity load handlers.
	entity_load_handler('*', _(e, new) -> (
		if(new,
			// Newly spawned entity, initialize as such
			if(query(e, 'has_scoreboard_tag', 'TEST-1'),
				// Will likely trigger when going through a Nether Portal
				logger('warn', str('An already initialized entity (%s) was marked as new again', e));
				return();
			);
			initialize_new_entity(e),
			
			// Entity being loaded from memory, check if entity not initialized
			if(!query(e, 'has_scoreboard_tag', 'TEST-1'),
				initialize_new_entity(e);
			);
		);
	));
	entity_types_handled += 1;
);

initialize_new_entity(e) -> (
	run(str('tag %s add TEST-1', e~'command_name'));
)
commented

I have a sneaky de ja vu I already responded to this before, but I can't find it.
Its up to the programmer what they do with the entities referenced in their code. Once the entity is removed, it still can be used, but its no longer tracked, marked as removed and invalid. At some point I was debating holding a world reference and entity id only, and not the entity direct pointer, but for simplicity stayed with entity references. Changing it now to world_ref+id could have some weird side effects. I guess some note in the docs about entity reference management could be of use. If you stop using a reference to an entity by re-referencing a variable, gc should free it at some point.

commented

This actually means that all previous entity references saved in variables will point to "nothing", and saving them forever could cause a memory leak. Except for players, those are special cased (both in minecraft, where the entity is not recreated when changing dimensions, and in scarpet, where a value pointing to a no longer valid player (like when they disconnected) will try to replace it with a valid reference if they have a new entity instance available).

commented

Well this is because entities are actually recreated when they change dimensions, aka the same entity object isn't reused, but a new one is created.