-
Notifications
You must be signed in to change notification settings - Fork 522
ServerEntityEvents.ALLOW_FRESH_LOAD and AFTER_FRESH_LOAD event #5206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 26.1
Are you sure you want to change the base?
Changes from 10 commits
1beeb69
7805258
fd135b9
b2936b3
3ffa035
72c330a
473bee4
07c6549
15a5ca1
4fe21ff
8a14770
75b38b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,56 @@ | |
| } | ||
| }); | ||
|
|
||
| /** | ||
| * Called before an Entity is added to a ServerLevel. | ||
| * | ||
| * <p>If return value is {@code false} entity will not be added to a server.</p> | ||
| * | ||
| * <p>Should be used when you want to add another entity instead of added or to block adding specific entities.</p> | ||
|
||
| * | ||
| * {@snippet : | ||
| * ServerEntityEvents.ALLOW_FRESH_LOAD.register((entity, level) -> { | ||
| * // Spawn with 25% chance zombie instead of creeper | ||
| * if (entity instanceof Creeper && level.getRandom().nextFloat() <= 0.25f) { | ||
| * var zombie = EntityType.ZOMBIE.create(level, null, entity.blockPosition(), EntitySpawnReason.EVENT, true, false); | ||
| * if (zombie != null) return !level.addFreshEntity(zombie); | ||
| * } | ||
| * return true; | ||
| * }); | ||
| * } | ||
| */ | ||
| public static final Event<ServerEntityEvents.AllowFreshLoad> ALLOW_FRESH_LOAD = EventFactory.createArrayBacked(ServerEntityEvents.AllowFreshLoad.class, callbacks -> (entity, level) -> { | ||
| boolean bl = true; | ||
|
|
||
| for (AllowFreshLoad callback : callbacks) { | ||
| bl = bl && callback.onFreshLoad(entity, level); | ||
| } | ||
|
|
||
| return bl; | ||
| }); | ||
|
|
||
| /** | ||
| * Called when an Entity is added to a ServerLevel. | ||
| * | ||
| * <p>When this event is called, the entity is already in the level.</p> | ||
| * | ||
| * <p>Should be used when you need to do something after entity summon, naturally spawn or any other add reason.</p> | ||
| * <p>If you need to do something after entity every entity load (not the first one) use ENTITY_LOAD event.</p> | ||
|
||
| * | ||
| * {@snippet : | ||
| * ServerEntityEvents.AFTER_FRESH_LOAD.register((entity, level) -> { | ||
| * if (entity instanceof Creeper) { | ||
| * level.players().forEach(player -> player.sendSystemMessage(Component.literal("Creeper was added"))); | ||
| * } | ||
| * }); | ||
| * } | ||
| */ | ||
| public static final Event<ServerEntityEvents.AfterFreshLoad> AFTER_FRESH_LOAD = EventFactory.createArrayBacked(ServerEntityEvents.AfterFreshLoad.class, callbacks -> (entity, level) -> { | ||
| for (AfterFreshLoad callback : callbacks) { | ||
| callback.afterFreshLoad(entity, level); | ||
| } | ||
| }); | ||
|
|
||
| /** | ||
| * Called when an Entity is unloaded from a ServerLevel. | ||
| * | ||
|
|
@@ -68,6 +118,16 @@ | |
| void onLoad(Entity entity, ServerLevel level); | ||
| } | ||
|
|
||
| @FunctionalInterface | ||
| public interface AllowFreshLoad { | ||
| boolean onFreshLoad(Entity entity, ServerLevel level); | ||
| } | ||
|
|
||
| @FunctionalInterface | ||
| public interface AfterFreshLoad { | ||
| void afterFreshLoad(Entity entity, ServerLevel level); | ||
| } | ||
|
|
||
| @FunctionalInterface | ||
| public interface Unload { | ||
| void onUnload(Entity entity, ServerLevel level); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.