package portables.common.util;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.play.server.S3FPacketCustomPayload;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import portables.common.core.CommonProxy;
import portables.common.core.SimplePortables;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
public class Util
{
public static void updateNBT(ItemStack item)
{
if (item.stackTagCompound == null)
{
item.stackTagCompound = new NBTTagCompound();
}
if (!item.stackTagCompound.hasKey("info"))
{
item.stackTagCompound.setTag("info", new NBTTagCompound());
}
}
public static void clearNBT(ItemStack item)
{
item.stackTagCompound.getCompoundTag("info").setInteger("blockID", 0);
item.stackTagCompound.getCompoundTag("info").setString("blockName", "");
item.stackTagCompound.getCompoundTag("info").setBoolean("linked", false);
item.stackTagCompound.getCompoundTag("info").setInteger("tileX", 0);
item.stackTagCompound.getCompoundTag("info").setInteger("tileY", 0);
item.stackTagCompound.getCompoundTag("info").setInteger("tileZ", 0);
item.stackTagCompound.getCompoundTag("info").setInteger("side", 0);
item.stackTagCompound.getCompoundTag("info").setFloat("hitX", 0);
item.stackTagCompound.getCompoundTag("info").setFloat("hitY", 0);
item.stackTagCompound.getCompoundTag("info").setFloat("hitZ", 0);
}
public static void openGui(int ID, EntityPlayer player)
{
player.openGui(SimplePortables.instance, ID, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
}
public static void damageItem(int damage, EntityPlayer player)
{
ItemStack currentItem = player.getCurrentEquippedItem();
if (currentItem != null)
{
if (currentItem.getItem() == CommonProxy.portableItem)
{
currentItem.damageItem(damage, player);
}
}
}
public static void handleRemoteGUI(ItemStack item, EntityPlayer player, World world)
{
if (item.stackTagCompound.getCompoundTag("info").getBoolean("linked"))
{
if (!world.isRemote)
{
Block block = world.getBlock(item.stackTagCompound.getCompoundTag("info").getInteger("tileX"), item.stackTagCompound.getCompoundTag("info").getInteger("tileY"), item.stackTagCompound.getCompoundTag("info").getInteger("tileZ"));
TileEntity tile = world.getTileEntity(item.stackTagCompound.getCompoundTag("info").getInteger("tileX"), item.stackTagCompound.getCompoundTag("info").getInteger("tileY"), item.stackTagCompound.getCompoundTag("info").getInteger("tileZ"));
if (Loader.isModLoaded("EnderStorage"))
{
if (tile.getClass().getName().equals("codechicken.enderstorage.storage.item.TileEnderChest"))
{
Method activate = null;
try
{
activate = tile.getClass().getMethod("activate", EntityPlayer.class, int.class);
activate.invoke(tile, player, 0);
} catch (Exception e)
{
e.printStackTrace();
}
}
} else
{
block.onBlockActivated(world, item.stackTagCompound.getCompoundTag("info").getInteger("tileX"), item.stackTagCompound.getCompoundTag("info").getInteger("tileY"), item.stackTagCompound.getCompoundTag("info").getInteger("tileZ"), player, item.stackTagCompound.getCompoundTag("info").getInteger("side"), item.stackTagCompound.getCompoundTag("info").getFloat("hitX"), item.stackTagCompound.getCompoundTag("info").getFloat("hitY"), item.stackTagCompound.getCompoundTag("info").getFloat("hitZ"));
}
}
}
}
public static void sendModPacket(int id)
{
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
DataOutputStream dataStream = new DataOutputStream(byteStream);
try
{
dataStream.write((byte) id);
SimplePortables.channel.sendToServer(new FMLProxyPacket(new S3FPacketCustomPayload("SimplePortables", byteStream.toByteArray())));
} catch (IOException e)
{
}
}
}