From ddda82e07fda23c178b7d509daa656f1b4d25821 Mon Sep 17 00:00:00 2001 From: Dragon-Seeker Date: Tue, 9 Apr 2024 16:51:53 -0500 Subject: [PATCH 1/2] Alter EntityTrackingEvents with Before and After Variant for StartTracking - Keep binary compatibility with the old field marking such as deprecated --- .../networking/v1/EntityTrackingEvents.java | 52 ++++++++++++++++--- .../networking/EntityTrackerEntryMixin.java | 9 +++- 2 files changed, 53 insertions(+), 8 deletions(-) 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..c6b4757f37 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 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 START_TRACKING = EventFactory.createArrayBacked(StartTracking.class, callbacks -> (trackedEntity, player) -> { - for (StartTracking callback : callbacks) { - callback.onStartTracking(trackedEntity, player); + 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,23 @@ public interface StopTracking { private EntityTrackingEvents() { } + + //-- + + /** + * @deprecated Use {#BEFORE_START_TRACKING} or {@link #AFTER_START_TRACKING} instead + */ + @Deprecated(forRemoval = true) + public static final Event START_TRACKING = BEFORE_START_TRACKING; + + @Deprecated(forRemoval = true) + @FunctionalInterface + public interface StartTracking extends BeforeStartTracking { + void onStartTracking(Entity trackedEntity, ServerPlayerEntity player); + + @Override + default void beforeStartTracking(Entity trackedEntity, ServerPlayerEntity player) { + onStartTracking(trackedEntity, 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..dcd54aea68 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,13 @@ abstract class EntityTrackerEntryMixin { private Entity entity; @Inject(method = "startTracking", at = @At("HEAD")) - private void onStartTracking(ServerPlayerEntity player, CallbackInfo ci) { - EntityTrackingEvents.START_TRACKING.invoker().onStartTracking(this.entity, player); + private void onStartTrackingBefore(ServerPlayerEntity player, CallbackInfo ci) { + 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")) From 69eb8e04c9158a6e6a7226dee450c724f6d453fe Mon Sep 17 00:00:00 2001 From: Dragon-Seeker Date: Sat, 13 Apr 2024 14:26:52 -0500 Subject: [PATCH 2/2] Remove attempt at backwards compat and add back event --- .../api/networking/v1/EntityTrackingEvents.java | 17 ++++++++--------- .../networking/EntityTrackerEntryMixin.java | 1 + 2 files changed, 9 insertions(+), 9 deletions(-) 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 c6b4757f37..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 @@ -99,17 +99,16 @@ private EntityTrackingEvents() { /** * @deprecated Use {#BEFORE_START_TRACKING} or {@link #AFTER_START_TRACKING} instead */ - @Deprecated(forRemoval = true) - public static final Event START_TRACKING = BEFORE_START_TRACKING; + @Deprecated() + public static final Event START_TRACKING = EventFactory.createArrayBacked(StartTracking.class, callbacks -> (trackedEntity, player) -> { + for (StartTracking callback : callbacks) { + callback.onStartTracking(trackedEntity, player); + } + }); - @Deprecated(forRemoval = true) + @Deprecated() @FunctionalInterface - public interface StartTracking extends BeforeStartTracking { + public interface StartTracking { void onStartTracking(Entity trackedEntity, ServerPlayerEntity player); - - @Override - default void beforeStartTracking(Entity trackedEntity, ServerPlayerEntity player) { - onStartTracking(trackedEntity, 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 dcd54aea68..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 @@ -37,6 +37,7 @@ abstract class EntityTrackerEntryMixin { @Inject(method = "startTracking", at = @At("HEAD")) private void onStartTrackingBefore(ServerPlayerEntity player, CallbackInfo ci) { + EntityTrackingEvents.START_TRACKING.invoker().onStartTracking(this.entity, player); EntityTrackingEvents.BEFORE_START_TRACKING.invoker().beforeStartTracking(this.entity, player); }