package aeronicamc.mods.mxtune.events;

import aeronicamc.mods.mxtune.Reference;
import aeronicamc.mods.mxtune.entity.MusicSourceEntity;
import aeronicamc.mods.mxtune.managers.PlayManager;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Mod.EventBusSubscriber(modid = Reference.MOD_ID)
public class ModPlayerEvents
{
    private static final Logger LOGGER = LogManager.getLogger(ModPlayerEvents.class);
    @SubscribeEvent
    public static void event(PlayerEvent.StartTracking event)
    {
        if (!event.getPlayer().level.isClientSide() && ((event.getTarget() instanceof ServerPlayerEntity) || event.getTarget() instanceof MusicSourceEntity))
        {
            LOGGER.debug("{} Start Tracking {}", event.getPlayer(), event.getTarget());
            PlayManager.sendMusicTo((ServerPlayerEntity) event.getPlayer(), event.getTarget());
        }
    }

    @SubscribeEvent
    public static void event(PlayerEvent.StopTracking event)
    {
        if (!event.getPlayer().level.isClientSide() && ((event.getTarget() instanceof ServerPlayerEntity) || event.getTarget() instanceof MusicSourceEntity))
        {
            LOGGER.debug("{} Stop Tracking {}", event.getPlayer(), event.getTarget());
            PlayManager.stopListeningTo((ServerPlayerEntity) event.getPlayer(), event.getTarget());
        }
    }

    @SubscribeEvent
    public static void event(PlayerContainerEvent.Open event)
    {
        if(!event.getEntityLiving().getCommandSenderWorld().isClientSide())
            PlayManager.stopPlayingEntity(event.getEntityLiving());
    }
}

@@ -214,33 +214,201 @@
 ity.
-player.ServerPlayerEntity
+item.ItemEntity;%0Aimport net.minecraft.entity.player.ServerPlayerEntity;%0Aimport net.minecraftforge.event.entity.EntityJoinWorldEvent;%0Aimport net.minecraftforge.event.entity.EntityLeaveWorldEvent
 ;%0Aim
@@ -2034,11 +2034,613 @@
 );%0A    %7D
+%0A%0A    @SubscribeEvent%0A    public static void event(EntityJoinWorldEvent event)%0A    %7B%0A        if (!event.getWorld().isClientSide() && event.getEntity() instanceof ItemEntity)%0A        %7B%0A            LOGGER.debug(%22Entity Join World Event %7B%7D%22, ((ItemEntity) event.getEntity()).getItem());%0A        %7D%0A    %7D%0A%0A    @SubscribeEvent%0A    public static void event(EntityLeaveWorldEvent event)%0A    %7B%0A        if (!event.getWorld().isClientSide() && (event.getEntity() instanceof ItemEntity))%0A        %7B%0A            LOGGER.debug(%22Entity Leave World Event %7B%7D%22, ((ItemEntity) event.getEntity()).getItem());%0A        %7D%0A    %7D
 %0A%7D%0A

Add Entity Leave and Join world events.