package com.lothrazar.potionstorage;

import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.PotionEffect;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class PotionButtonPacket implements IMessage , IMessageHandler<PotionButtonPacket, IMessage>
{
	public PotionButtonPacket() {}
	NBTTagCompound tags = new NBTTagCompound(); 
	
	public PotionButtonPacket(NBTTagCompound ptags)
	{
		tags = ptags;
	}

	@Override
	public void fromBytes(ByteBuf buf) 
	{
		tags = ByteBufUtils.readTag(buf);
	}

	@Override
	public void toBytes(ByteBuf buf) 
	{
		ByteBufUtils.writeTag(buf, this.tags);
	}

	@Override
	public IMessage onMessage(PotionButtonPacket message, MessageContext ctx)
	{
		EntityPlayer p = ctx.getServerHandler().playerEntity;
		
		//encode potions as csv, store in metadata for player and clear them

		InventoryPersistProperty storage = InventoryPersistProperty.get(p);
		
		if(storage.countPotionEffects() == 0)
		{
			storage.savePotionEffects();
	        p.clearActivePotions();
		}
		else
		{
			for(PotionEffect pot : storage.getSavedPotionEffects())
			{
				//make a copy
				p.addPotionEffect(new PotionEffect(pot));
			}
		}
        
       // p.closeScreen();
        
		return null;
	}
}

@@ -63,16 +63,62 @@
 yteBuf;%0A
+import net.minecraft.entity.EntityLivingBase;%0A
 import n
@@ -199,16 +199,52 @@
 mpound;%0A
+import net.minecraft.potion.Potion;%0A
 import n
@@ -983,24 +983,66 @@
 .tags);%0A%09%7D%0A%0A
+%09public static boolean allowMerge = true;%0A
 %09@Override%0A%09
@@ -1536,118 +1536,843 @@
 %09%09%09%09
-p.addPotionEffect(new PotionEffect(pot));%0A%09%09%09%7D%0A%09%09%7D%0A        %0A       // p.closeScreen();%0A        %0A%09%09return null;
+%0A%09%09%09%09//wait dont we want to merge%0A%09%09%09%09%0A%09%09%09%09//%0A%09%09%09%09if(allowMerge)%0A%09%09%09%09%09addOrMergePotionEffect(p, pot);%0A%09%09%09%09else%0A%09%09%09%09%09p.addPotionEffect(new PotionEffect(pot));%0A%09%09%09%7D%0A%09%09%7D%0A        %0A       // p.closeScreen();%0A        %0A%09%09return null;%0A%09%7D%0A%09%0A%09//copied  from my PowerPApples mod%0A%09public static void addOrMergePotionEffect(EntityLivingBase player, PotionEffect newp)%0A%09%7B%0A%09%09if(player.isPotionActive(newp.getPotionID()))%0A%09%09%7B%0A%09%09%09//do not use built in 'combine' function, just add up duration myself%0A%09%09%09PotionEffect p = player.getActivePotionEffect(Potion.potionTypes%5Bnewp.getPotionID()%5D);%0A%09%09%09%0A%09%09%09int ampMax = Math.max(p.getAmplifier(), newp.getAmplifier());%0A%09%09%0A%09%09%09player.addPotionEffect(new PotionEffect(newp.getPotionID()%0A%09%09%09%09%09,newp.getDuration() + p.getDuration()%0A%09%09%09%09%09,ampMax));%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09player.addPotionEffect(new PotionEffect(newp));%0A%09%09%7D
 %0A%09%7D%0A

enable potion effect merging