Skip to content
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

[1.20.4] Alter StartTracking with Before and After Variants #3696

Open
wants to merge 2 commits into
base: 1.20.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,26 @@
/**
* Events related to a tracking entities within a player's view distance.
*/
public final class EntityTrackingEvents {

Check failure on line 28 in fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/EntityTrackingEvents.java

View workflow job for this annotation

GitHub Actions / build (21-ubuntu)

blank line after {

/**
* 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 <strong>before</strong> the player's client is sent the entity's {@link Entity#createSpawnPacket() spawn packet}.
*/
public static final Event<BeforeStartTracking> 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 <strong>after</strong> the player's client is sent the entity's {@link Entity#createSpawnPacket() spawn packet}.
*/
public static final Event<StartTracking> START_TRACKING = EventFactory.createArrayBacked(StartTracking.class, callbacks -> (trackedEntity, player) -> {
for (StartTracking callback : callbacks) {
callback.onStartTracking(trackedEntity, player);
public static final Event<AfterStartTracking> AFTER_START_TRACKING = EventFactory.createArrayBacked(AfterStartTracking.class, callbacks -> (trackedEntity, player) -> {
for (AfterStartTracking callback : callbacks) {
callback.afterStartTracking(trackedEntity, player);
}
});

Expand All @@ -49,14 +60,24 @@
});

@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
Expand All @@ -72,4 +93,23 @@

private EntityTrackingEvents() {
}

//--

/**
* @deprecated Use {#BEFORE_START_TRACKING} or {@link #AFTER_START_TRACKING} instead
*/
@Deprecated(forRemoval = true)
modmuss50 marked this conversation as resolved.
Show resolved Hide resolved
public static final Event<BeforeStartTracking> START_TRACKING = BEFORE_START_TRACKING;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prob just leave this a tottaly seperate event, and keep it the same as it was before.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little clarification, do you mean:

  1. Keep the original Event as it was but with deprecation while also adding the specific before and after variants
    OR
  2. Keep the original Event but add the after-variant as the only addition

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think it's no. 2; start tracking and after start tracking events.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, sorry. I was more thinking about 1:

Keep the current event untouched, no need to try and be clever about ensuring backwards compatibility just duplicate it. You would then end up with:

  • BEFORE_START_TRACKING
  • START_TRACKING (existing, deprecated)
  • AFTER_START_TRACKING

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did ensure the code was backward compatible but made the desired adjustment to the PR.


@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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Loading