package squeek.appleskin.network;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.WorldTickEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side;
import squeek.appleskin.ModInfo;
import squeek.appleskin.helpers.HungerHelper;

public class SyncHandler
{
	public static final SimpleNetworkWrapper CHANNEL = NetworkRegistry.INSTANCE.newSimpleChannel(ModInfo.MODID);

	public static void init()
	{
		CHANNEL.registerMessage(MessageExhaustionSync.class, MessageExhaustionSync.class, 1, Side.CLIENT);
		CHANNEL.registerMessage(MessageSaturationSync.class, MessageSaturationSync.class, 2, Side.CLIENT);

		MinecraftForge.EVENT_BUS.register(new SyncHandler());
	}

	/*
	 * Sync saturation (vanilla MC only syncs when it hits 0)
	 * Sync exhaustion (vanilla MC does not sync it at all)
	 */
	private float lastSaturationLevel = 0;
	private float lastExhaustionLevel = 0;

	@SubscribeEvent
	public void onLivingUpdateEvent(LivingUpdateEvent event)
	{
		if (!(event.getEntity() instanceof EntityPlayerMP))
			return;

		EntityPlayerMP player = (EntityPlayerMP) event.getEntity();

		if (this.lastSaturationLevel != player.getFoodStats().getSaturationLevel())
		{
			CHANNEL.sendTo(new MessageSaturationSync(player.getFoodStats().getSaturationLevel()), player);
			this.lastSaturationLevel = player.getFoodStats().getSaturationLevel();
		}

		float exhaustionLevel = HungerHelper.getExhaustion(player);
		if (Math.abs(this.lastExhaustionLevel - exhaustionLevel) >= 0.01f)
		{
			CHANNEL.sendTo(new MessageExhaustionSync(exhaustionLevel), player);
			this.lastExhaustionLevel = exhaustionLevel;
		}
	}
}

@@ -83,91 +83,8 @@
 MP;%0A
-import net.minecraft.world.EnumDifficulty;%0Aimport net.minecraft.world.WorldServer;%0A
 impo
@@ -322,85 +322,14 @@
 ent.
-TickEvent;%0Aimport net.minecraftforge.fml.common.gameevent.TickEvent.WorldTick
+Player
 Even
@@ -598,16 +598,88 @@
 elper;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.UUID;%0A%0A
 public c
@@ -1231,29 +1231,53 @@
 */%0A%09private 
-f
+static final Map%3CUUID, F
 loat
+%3E
  lastSaturat
@@ -1288,28 +1288,78 @@
 evel
+s
  = 
-0;%0A%09private f
+new HashMap%3CUUID, Float%3E();%0A%09private static final Map%3CUUID, F
 loat
+%3E
  las
@@ -1378,12 +1378,38 @@
 evel
+s
  = 
-0
+new HashMap%3CUUID, Float%3E()
 ;%0A%0A%09
@@ -1617,20 +1617,202 @@
 ();%0A
-%0A%09%09if (this.
+%09%09Float lastSaturationLevel = lastSaturationLevels.get(player.getUniqueID());%0A%09%09Float lastExhaustionLevel = lastExhaustionLevels.get(player.getUniqueID());%0A%0A%09%09if (lastSaturationLevel == null %7C%7C 
 last
@@ -1975,29 +1975,24 @@
 player);%0A%09%09%09
-this.
 lastSaturati
@@ -1998,18 +1998,43 @@
 ionLevel
- =
+s.put(player.getUniqueID(),
  player.
@@ -2068,16 +2068,17 @@
 nLevel()
+)
 ;%0A%09%09%7D%0A%0A%09
@@ -2144,16 +2144,47 @@
 ;%0A%09%09if (
+lastExhaustionLevel == null %7C%7C 
 Math.abs
@@ -2184,21 +2184,16 @@
 ath.abs(
-this.
 lastExha
@@ -2315,55 +2315,348 @@
 %0A%09%09%09
-this.lastExhaustionLevel = exhaustionLevel;%0A%09%09%7D
+lastExhaustionLevels.put(player.getUniqueID(), exhaustionLevel);%0A%09%09%7D%0A%09%7D%0A%0A%09@SubscribeEvent%0A%09public void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event)%0A%09%7B%0A%09%09if (!(event.player instanceof EntityPlayerMP))%0A%09%09%09return;%0A%0A%09%09lastSaturationLevels.remove(event.player.getUniqueID());%0A%09%09lastExhaustionLevels.remove(event.player.getUniqueID());
 %0A%09%7D%0A

Fix sending *way* too many saturation/exhaustion sync packets - See https://github.com/squeek502/AppleCore/issues/82