Skip to content

Commit

Permalink
Adjust when the EntityTrackingEvents are fired. (#4369)
Browse files Browse the repository at this point in the history
* fix EntityTrackingEvents at once

* use event in Attachment Sync API

* fix: remove removed mixin from mixins.json
  • Loading branch information
Octol1ttle authored Jan 14, 2025
1 parent 20da279 commit 8998135
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.minecraft.util.Identifier;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.EntityTrackingEvents;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking;
Expand Down Expand Up @@ -113,7 +114,14 @@ public void onInitialize() {
}
});

// entity tracking handled in EntityTrackerEntryMixin instead, see comment
EntityTrackingEvents.START_TRACKING.register((trackedEntity, player) -> {
List<AttachmentChange> changes = new ArrayList<>();
((AttachmentTargetImpl) trackedEntity).fabric_computeInitialSyncChanges(player, changes::add);

if (!changes.isEmpty()) {
AttachmentChange.partitionAndSendPackets(changes, player);
}
});
}

private record AttachmentSyncTask() implements ServerPlayerConfigurationTask {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"ClientConnectionMixin",
"CustomPayloadS2CPacketAccessor",
"EntityMixin",
"EntityTrackerEntryMixin",
"SerializedChunkMixin",
"ServerWorldMixin",
"VarIntsAccessor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
*/
public final class EntityTrackingEvents {
/**
* An event that is called before player starts tracking an entity.
* An event that is called after a player has started 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 {@linkplain Entity#createSpawnPacket spawn packet}.
* This event is called after the entity's {@linkplain Entity#createSpawnPacket spawn packet} is sent to the player.
*/
public static final Event<StartTracking> START_TRACKING = EventFactory.createArrayBacked(StartTracking.class, callbacks -> (trackedEntity, player) -> {
for (StartTracking callback : callbacks) {
Expand All @@ -38,9 +38,8 @@ public final class EntityTrackingEvents {
});

/**
* An event that is called after a player has stopped tracking an entity.
* The client at this point was sent a packet to {@link net.minecraft.network.packet.s2c.play.EntitiesDestroyS2CPacket destroy} the entity on the client.
* The entity still exists on the server.
* An event that is called before a player stops tracking an entity.
* The entity still exists on the server and on the player's client.
*/
public static final Event<StopTracking> STOP_TRACKING = EventFactory.createArrayBacked(StopTracking.class, callbacks -> (trackedEntity, player) -> {
for (StopTracking callback : callbacks) {
Expand All @@ -51,7 +50,7 @@ public final class EntityTrackingEvents {
@FunctionalInterface
public interface StartTracking {
/**
* Called before an entity starts getting tracked by a player.
* Called after a player has started tracking an entity.
*
* @param trackedEntity the entity that will be tracked
* @param player the player that will track the entity
Expand All @@ -62,10 +61,10 @@ public interface StartTracking {
@FunctionalInterface
public interface StopTracking {
/**
* Called after an entity stops getting tracked by a player.
* Called before an entity stops getting tracked by a player.
*
* @param trackedEntity the entity that is no longer being tracked
* @param player the player that is no longer tracking the entity
* @param trackedEntity the entity that is about to stop being tracked
* @param player the player that is about to stop tracking the entity
*/
void onStopTracking(Entity trackedEntity, ServerPlayerEntity player);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ abstract class EntityTrackerEntryMixin {
@Final
private Entity entity;

@Inject(method = "startTracking", at = @At("HEAD"))
@Inject(method = "startTracking", at = @At("TAIL"))
private void onStartTracking(ServerPlayerEntity player, CallbackInfo ci) {
EntityTrackingEvents.START_TRACKING.invoker().onStartTracking(this.entity, player);
}

@Inject(method = "stopTracking", at = @At("TAIL"))
@Inject(method = "stopTracking", at = @At("HEAD"))
private void onStopTracking(ServerPlayerEntity player, CallbackInfo ci) {
EntityTrackingEvents.STOP_TRACKING.invoker().onStopTracking(this.entity, player);
}
Expand Down

0 comments on commit 8998135

Please sign in to comment.