diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/EntityTrackingEvents.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/EntityTrackingEvents.java index 6e93cff1fc..918d8cb133 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/EntityTrackingEvents.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/EntityTrackingEvents.java @@ -26,14 +26,25 @@ * Events related to a tracking entities within a player's view distance. */ public final class EntityTrackingEvents { + /** * An event that is called before player starts tracking an entity. * Typically, this occurs when an entity enters a client's view distance. - * This event is called before the player's client is sent the entity's {@link Entity#createSpawnPacket() spawn packet}. + * This event is called before the player's client is sent the entity's {@link Entity#createSpawnPacket() spawn packet}. */ - public static final Event START_TRACKING = EventFactory.createArrayBacked(StartTracking.class, callbacks -> (trackedEntity, player) -> { - for (StartTracking callback : callbacks) { - callback.onStartTracking(trackedEntity, player); + public static final Event BEFORE_START_TRACKING = EventFactory.createArrayBacked(BeforeStartTracking.class, callbacks -> (trackedEntity, player) -> { + for (BeforeStartTracking callback : callbacks) { + callback.beforeStartTracking(trackedEntity, player); + } + }); + + /** + * An event that is called after player starts tracking an entity. + * This event is called after the player's client is sent the entity's {@link Entity#createSpawnPacket() spawn packet}. + */ + public static final Event AFTER_START_TRACKING = EventFactory.createArrayBacked(AfterStartTracking.class, callbacks -> (trackedEntity, player) -> { + for (AfterStartTracking callback : callbacks) { + callback.afterStartTracking(trackedEntity, player); } }); @@ -49,14 +60,24 @@ public final class EntityTrackingEvents { }); @FunctionalInterface - public interface StartTracking { + public interface BeforeStartTracking { /** * Called before an entity starts getting tracked by a player. * * @param trackedEntity the entity that will be tracked * @param player the player that will track the entity */ - void onStartTracking(Entity trackedEntity, ServerPlayerEntity player); + void beforeStartTracking(Entity trackedEntity, ServerPlayerEntity player); + } + + public interface AfterStartTracking { + /** + * Called after an entity starts getting tracked by a player. + * + * @param trackedEntity the entity that will be tracked + * @param player the player that will track the entity + */ + void afterStartTracking(Entity trackedEntity, ServerPlayerEntity player); } @FunctionalInterface @@ -72,4 +93,22 @@ public interface StopTracking { private EntityTrackingEvents() { } + + //-- + + /** + * @deprecated Use {#BEFORE_START_TRACKING} or {@link #AFTER_START_TRACKING} instead + */ + @Deprecated() + public static final Event START_TRACKING = EventFactory.createArrayBacked(StartTracking.class, callbacks -> (trackedEntity, player) -> { + for (StartTracking callback : callbacks) { + callback.onStartTracking(trackedEntity, player); + } + }); + + @Deprecated() + @FunctionalInterface + public interface StartTracking { + void onStartTracking(Entity trackedEntity, ServerPlayerEntity player); + } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/EntityTrackerEntryMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/EntityTrackerEntryMixin.java index 7e908734ef..026b2aa1df 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/EntityTrackerEntryMixin.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/EntityTrackerEntryMixin.java @@ -36,8 +36,14 @@ abstract class EntityTrackerEntryMixin { private Entity entity; @Inject(method = "startTracking", at = @At("HEAD")) - private void onStartTracking(ServerPlayerEntity player, CallbackInfo ci) { + private void onStartTrackingBefore(ServerPlayerEntity player, CallbackInfo ci) { EntityTrackingEvents.START_TRACKING.invoker().onStartTracking(this.entity, player); + EntityTrackingEvents.BEFORE_START_TRACKING.invoker().beforeStartTracking(this.entity, player); + } + + @Inject(method = "startTracking", at = @At("TAIL")) + private void onStartTrackingAfter(ServerPlayerEntity player, CallbackInfo ci) { + EntityTrackingEvents.AFTER_START_TRACKING.invoker().afterStartTracking(this.entity, player); } @Inject(method = "stopTracking", at = @At("TAIL"))