Adjust when the EntityTrackingEvents are fired. ()

* fix EntityTrackingEvents at once

* use event in Attachment Sync API

* fix: remove removed mixin from mixins.json

(cherry picked from commit 89981351cc)
This commit is contained in:
Octol1ttle 2025-01-14 18:46:14 +05:00 committed by modmuss50
parent efd8abe398
commit cb3101de6a
5 changed files with 19 additions and 72 deletions
fabric-data-attachment-api-v1/src/main
java/net/fabricmc/fabric
impl/attachment/sync
mixin/attachment
resources
fabric-networking-api-v1/src/main/java/net/fabricmc/fabric

View file

@ -29,6 +29,7 @@ import net.minecraft.server.network.ServerPlayerEntity;
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;
@ -113,7 +114,14 @@ public class AttachmentSync implements ModInitializer {
}
});
// 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 {

View file

@ -1,59 +0,0 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.mixin.attachment;
import java.util.ArrayList;
import java.util.List;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.entity.Entity;
import net.minecraft.server.network.EntityTrackerEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.fabricmc.fabric.impl.attachment.AttachmentTargetImpl;
import net.fabricmc.fabric.impl.attachment.sync.AttachmentChange;
@Mixin(EntityTrackerEntry.class)
abstract class EntityTrackerEntryMixin {
@Shadow
@Final
private Entity entity;
@Inject(
method = "startTracking",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/entity/Entity;onStartedTrackingBy(Lnet/minecraft/server/network/ServerPlayerEntity;)V"
)
)
private void syncAttachmentsAfterSpawn(ServerPlayerEntity player, CallbackInfo ci) {
// mixin because the START_TRACKING event triggers before the spawn packet is sent to the client,
// whereas we want to modify the entity on the client
List<AttachmentChange> changes = new ArrayList<>();
((AttachmentTargetImpl) this.entity).fabric_computeInitialSyncChanges(player, changes::add);
if (!changes.isEmpty()) {
AttachmentChange.partitionAndSendPackets(changes, player);
}
}
}

View file

@ -11,7 +11,6 @@
"ClientConnectionMixin",
"CustomPayloadS2CPacketAccessor",
"EntityMixin",
"EntityTrackerEntryMixin",
"SerializedChunkMixin",
"ServerWorldMixin",
"VarIntsAccessor",

View file

@ -27,9 +27,9 @@ import net.fabricmc.fabric.api.event.EventFactory;
*/
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) {
@ -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) {
@ -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
@ -62,10 +61,10 @@ public final class EntityTrackingEvents {
@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);
}

View file

@ -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);
}