package aeronicamc.mods.mxtune.network.messages;

import aeronicamc.mods.mxtune.Reference;
import aeronicamc.mods.mxtune.caps.LivingEntityModCapProvider;
import net.minecraft.entity.LivingEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraft.world.World;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.LogicalSidedProvider;
import net.minecraftforge.fml.network.NetworkEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Optional;
import java.util.function.Supplier;

public class LivingEntityModCapSync extends AbstractMessage<LivingEntityModCapSync>
{
    private static final Logger LOGGER = LogManager.getLogger(Reference.MOD_NAME);
    int playId = -1;

    public LivingEntityModCapSync() { /* NOP */ }

    public LivingEntityModCapSync(int playId)
    {
        this.playId = playId;
    }

    @Override
    public LivingEntityModCapSync decode(final PacketBuffer buffer)
    {
        final int playId = buffer.readInt();
        LOGGER.debug("playId: {}", playId);
        return new LivingEntityModCapSync(playId);
    }

    @Override
    public void encode(final LivingEntityModCapSync message, final PacketBuffer buffer)
    {
        LOGGER.debug("playId: {}", message.playId);
        buffer.writeInt(message.playId);
    }

    @Override
    public void handle(final LivingEntityModCapSync message, final Supplier<NetworkEvent.Context> ctx)
    {
        if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT)
            ctx.get().enqueueWork(() ->
                {
                    final Optional<World> optionalWorld = LogicalSidedProvider.CLIENTWORLD.get(ctx.get().getDirection().getReceptionSide());
                    final LivingEntity livingEntity = ctx.get().getSender();
                    if (livingEntity != null)
                        optionalWorld.ifPresent(world -> LivingEntityModCapProvider.getLivingEntityModCap(livingEntity).ifPresent(livingEntityCap ->
                            livingEntityCap.setPlayId(message.playId)));
                });
        ctx.get().setPacketHandled(true);
    }
}

@@ -139,32 +139,71 @@
 ModCapProvider;%0A
+import net.minecraft.client.Minecraft;%0A
 import net.minec
@@ -352,60 +352,8 @@
 de;%0A
-import net.minecraftforge.fml.LogicalSidedProvider;%0A
 impo
@@ -489,35 +489,8 @@
 r;%0A%0A
-import java.util.Optional;%0A
 impo
@@ -695,16 +695,24 @@
 E);%0A    
+private 
 int play
@@ -717,13 +717,34 @@
 ayId
- = -1
+;%0A    private int entityId
 ;%0A%0A 
@@ -837,16 +837,30 @@
 t playId
+, int entityId
 )%0A    %7B%0A
@@ -885,24 +885,58 @@
 d = playId;%0A
+        this.entityId = entityId;%0A
     %7D%0A%0A    @
@@ -1059,24 +1059,71 @@
 .readInt();%0A
+        final int entityId = buffer.readInt();%0A
         LOGG
@@ -1198,24 +1198,34 @@
 pSync(playId
+, entityId
 );%0A    %7D%0A%0A  
@@ -1419,24 +1419,67 @@
 ge.playId);%0A
+        buffer.writeInt(message.entityId);%0A
     %7D%0A%0A    @
@@ -1757,206 +1757,226 @@
     
-final Optional%3CWorld%3E optionalWorld = LogicalSidedProvider.CLIENTWORLD.get(ctx.get().getDirection().getReceptionSide());%0A                    final LivingEntity livingEntity = ctx.get().getSender();%0A
+World level = Minecraft.getInstance().level;%0A                    if (level != null)%0A                    %7B%0A                        final LivingEntity livingEntity = (LivingEntity) level.getEntity(message.entityId);%0A    
     
@@ -2045,40 +2045,11 @@
     
-optionalWorld.ifPresent(world -%3E
+   
  Liv
@@ -2122,27 +2122,13 @@
 ent(
-livingEntityCap -%3E%0A
+%0A    
     
@@ -2150,16 +2150,35 @@
         
+ livingEntityCap -%3E
  livingE
@@ -2212,18 +2212,39 @@
 playId))
-)
 ;
+%0A                    %7D
 %0A       

Add entity id to sync message. Remove incorrect use of getSender. It used server side, not client side.