Skip to content

ServerEntityEvents.ALLOW_FRESH_LOAD and AFTER_FRESH_LOAD event#5206

Open
bleudev wants to merge 9 commits intoFabricMC:26.1from
bleudev:26.1
Open

ServerEntityEvents.ALLOW_FRESH_LOAD and AFTER_FRESH_LOAD event#5206
bleudev wants to merge 9 commits intoFabricMC:26.1from
bleudev:26.1

Conversation

@bleudev
Copy link

@bleudev bleudev commented Feb 14, 2026

Add two simple server-side events which calls before/after a living entity was added to level (spawned). It is useful for developers who wants do something before/after entity spawn.

Sample:

// Allow fresh load sample
ServerEntityEvents.ALLOW_FRESH_LOAD.register((entity, level) -> {
	if (entity instanceof Creeper creeper) {
		// Spawn with 25% chance zombie instead of creeper
		if (level.getRandom().nextFloat() <= 0.25f) {
			var zombie = EntityType.ZOMBIE.create(level, EntitySpawnReason.EVENT);
			if (zombie != null) {
				zombie.copyPosition(creeper);
				level.addFreshEntity(zombie);
				return false;
			}
		}
	}
	return true;
});

@modmuss50
Copy link
Member

What is the difference between this and ServerEntityEvents.ENTITY_LOAD?

@sylv256 sylv256 added the enhancement New feature or request label Feb 15, 2026
@bleudev
Copy link
Author

bleudev commented Feb 15, 2026

What is the difference between this and ServerEntityEvents.ENTITY_LOAD?

The entity load event is called when an entity is loaded upon creation, as well as when the server starts once the entity has been added. My event is called only when an entity is added to the server.

@modmuss50
Copy link
Member

Could you use data attachments to keep track of if its the first time the entity was added to the world? E.g store a marker to indicate that your mod has done what it needs to the entity?

@cassiancc
Copy link
Member

I'm personally happier with this than having to needlessly keep track of things myself - I've had to mixin in order to do this instead of having an equivalent event to the one on NeoForge. It'd save on unnecessary calls to mod logic by only running when needed instead of every time the entity is loaded/unloaded.

@modmuss50
Copy link
Member

At the very least please fix the build issues, and explain the documentation what its for, why it differs from the other and when its fired.

@Patbox
Copy link
Member

Patbox commented Feb 16, 2026

Would it maybe make more sense to run the event before entity is actually added to world rather than after? I think for cases where you want to modify it in some way it would be probably cleaner (and maybe allow canceling that within the event).

Maybe rename would be good, since just "adding" an entity feels little bit unspecific, maybe calling it "ON_FRESHLY_ADDED" would be better (since it's more descriptive). Probably would make sense to document it too that it's called when ServerLevel#addFreshEntity runs

@DennisOchulor
Copy link
Contributor

Could this be expanded to cover all entities? I'm thinking something like ServerEntityEvents.BEFORE_FRESH_ENTITY_LOAD that's cancellable.

Then ENTITY_LOAD would need some indicator of whether the entity was freshly added or not, similar to #5112 for chunks.
This is for use cases where you want to do some action only if the entity is definetly being spawned, which may not be the case in the cancellable event.

@bleudev
Copy link
Author

bleudev commented Feb 17, 2026

Could this be expanded to cover all entities? I'm thinking something like ServerEntityEvents.BEFORE_FRESH_ENTITY_LOAD that's cancellable.

Then ENTITY_LOAD would need some indicator of whether the entity was freshly added or not, similar to #5112 for chunks. This is for use cases where you want to do some action only if the entity is definetly being spawned, which may not be the case in the cancellable event.

Great idea, I thought about it before.

But parameters (similar to chuck load event) - i think no. It will be unnecessary breaking change

@bleudev
Copy link
Author

bleudev commented Feb 17, 2026

Could you use data attachments to keep track of if its the first time the entity was added to the world? E.g store a marker to indicate that your mod has done what it needs to the entity?

What do you mean? Sorry for my misunderstanding)

@bleudev
Copy link
Author

bleudev commented Feb 17, 2026

Would it maybe make more sense to run the event before entity is actually added to world rather than after? I think for cases where you want to modify it in some way it would be probably cleaner (and maybe allow canceling that within the event).

Maybe rename would be good, since just "adding" an entity feels little bit unspecific, maybe calling it "ON_FRESHLY_ADDED" would be better (since it's more descriptive). Probably would make sense to document it too that it's called when ServerLevel#addFreshEntity runs

Maybe i will do something like BEFORE_FRESH_LOAD and AFTER_FRESH_LOAD?

@bleudev bleudev changed the title ServerLivingEntityEvents.AFTER_ADD event ServerEntityEvents.ALLOW_FRESH_LOAD and AFTER_FRESH_LOAD event Feb 17, 2026
@bleudev
Copy link
Author

bleudev commented Feb 17, 2026

Done some recommendations. Can anyone review?

@bleudev
Copy link
Author

bleudev commented Feb 17, 2026

I can't check build issues, workflow awaiting approval

@DennisOchulor
Copy link
Contributor

Then ENTITY_LOAD would need some indicator of whether the entity was freshly added or not, similar to #5112 for chunks.

I still think it would be better to add to ENTITY_LOAD instead of a new AFTER_FRESH_LOAD event.

Yes it's a breaking change but if #5112 was considered fine I don't see why this would be unacceptable.

@bleudev
Copy link
Author

bleudev commented Feb 20, 2026

Yes it's a breaking change but if #5112 was considered fine I don't see why this would be unacceptable.

Maybe you're right. Probably, we can do breaking changes cause it's snapshots. But I'm waiting for @modmuss50. What he will say about it?

@bleudev
Copy link
Author

bleudev commented Feb 21, 2026

I've fixed all issues from checkStyleMain task. Can anyone approve workflow again?

@bleudev bleudev requested a review from modmuss50 February 25, 2026 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants