package micdoodle8.mods.galacticraft.core.tile;
import io.netty.buffer.ByteBuf;
import micdoodle8.mods.galacticraft.core.GalacticraftCore;
import micdoodle8.mods.galacticraft.core.blocks.BlockParaChest;
import micdoodle8.mods.galacticraft.core.entities.IScaleableFuelLevel;
import micdoodle8.mods.galacticraft.core.inventory.ContainerParaChest;
import micdoodle8.mods.galacticraft.core.inventory.IInventorySettable;
import micdoodle8.mods.galacticraft.core.network.PacketDynamicInventory;
import micdoodle8.mods.galacticraft.core.util.FluidUtil;
import micdoodle8.mods.galacticraft.core.util.GCCoreUtil;
import micdoodle8.mods.miccore.Annotations.NetworkedField;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fml.relauncher.Side;
import java.util.Iterator;
import java.util.List;
public class TileEntityParaChest extends TileEntityAdvanced implements IInventorySettable, IScaleableFuelLevel
{
private final int tankCapacity = 5000;
@NetworkedField(targetSide = Side.CLIENT)
public FluidTank fuelTank = new FluidTank(this.tankCapacity);
public ItemStack[] chestContents = new ItemStack[3];
public boolean adjacentChestChecked = false;
public float lidAngle;
public float prevLidAngle;
public int numUsingPlayers;
@NetworkedField(targetSide = Side.CLIENT)
public EnumDyeColor color = EnumDyeColor.RED;
public TileEntityParaChest()
{
this.color = EnumDyeColor.RED;
}
@Override
public void onLoad()
{
if (this.worldObj.isRemote)
{
//Request size + contents information from server
GalacticraftCore.packetPipeline.sendToServer(new PacketDynamicInventory(this));
}
}
@Override
public int getScaledFuelLevel(int i)
{
final double fuelLevel = this.fuelTank.getFluid() == null ? 0 : this.fuelTank.getFluid().amount;
return (int) (fuelLevel * i / this.tankCapacity);
}
@Override
public int getSizeInventory()
{
return this.chestContents.length;
}
@Override
public void setSizeInventory(int size)
{
if ((size - 3) % 18 != 0)
{
size += 18 - ((size - 3) % 18);
}
this.chestContents = new ItemStack[size];
}
@Override
public ItemStack getStackInSlot(int par1)
{
return this.chestContents[par1];
}
@Override
public ItemStack decrStackSize(int par1, int par2)
{
if (this.chestContents[par1] != null)
{
ItemStack itemstack;
if (this.chestContents[par1].stackSize <= par2)
{
itemstack = this.chestContents[par1];
this.chestContents[par1] = null;
this.markDirty();
return itemstack;
}
else
{
itemstack = this.chestContents[par1].splitStack(par2);
if (this.chestContents[par1].stackSize == 0)
{
this.chestContents[par1] = null;
}
this.markDirty();
return itemstack;
}
}
else
{
return null;
}
}
@Override
public ItemStack removeStackFromSlot(int par1)
{
if (this.chestContents[par1] != null)
{
ItemStack itemstack = this.chestContents[par1];
this.chestContents[par1] = null;
return itemstack;
}
else
{
return null;
}
}
@Override
public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
{
this.chestContents[par1] = par2ItemStack;
if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
{
par2ItemStack.stackSize = this.getInventoryStackLimit();
}
this.markDirty();
}
@Override
public String getName()
{
return GCCoreUtil.translate("container.parachest.name");
}
@Override
public boolean hasCustomName()
{
return true;
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
NBTTagList nbttaglist = nbt.getTagList("Items", 10);
int size = nbt.getInteger("chestContentLength");
if ((size - 3) % 18 != 0)
{
size += 18 - ((size - 3) % 18);
}
this.chestContents = new ItemStack[size];
for (int i = 0; i < nbttaglist.tagCount(); ++i)
{
NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
int j = nbttagcompound1.getByte("Slot") & 255;
if (j < this.chestContents.length)
{
this.chestContents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
}
}
if (nbt.hasKey("fuelTank"))
{
this.fuelTank.readFromNBT(nbt.getCompoundTag("fuelTank"));
}
if (nbt.hasKey("color"))
{
this.color = EnumDyeColor.byDyeDamage(nbt.getInteger("color"));
}
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setInteger("chestContentLength", this.chestContents.length);
NBTTagList nbttaglist = new NBTTagList();
for (int i = 0; i < this.chestContents.length; ++i)
{
if (this.chestContents[i] != null)
{
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setByte("Slot", (byte) i);
this.chestContents[i].writeToNBT(nbttagcompound1);
nbttaglist.appendTag(nbttagcompound1);
}
}
nbt.setTag("Items", nbttaglist);
if (this.fuelTank.getFluid() != null)
{
nbt.setTag("fuelTank", this.fuelTank.writeToNBT(new NBTTagCompound()));
}
nbt.setInteger("color", this.color.getDyeDamage());
}
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
{
return this.worldObj.getTileEntity(this.getPos()) == this && par1EntityPlayer.getDistanceSq(this.getPos().getX() + 0.5D, this.getPos().getY() + 0.5D, this.getPos().getZ() + 0.5D) <= 64.0D;
}
@Override
public void updateContainingBlockInfo()
{
super.updateContainingBlockInfo();
this.adjacentChestChecked = false;
}
@Override
public void update()
{
super.update();
float f;
if (!this.worldObj.isRemote && this.numUsingPlayers != 0 && (this.ticks + this.getPos().getX() + this.getPos().getY() + this.getPos().getZ()) % 200 == 0)
{
this.numUsingPlayers = 0;
f = 5.0F;
List<?> list = this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.fromBounds(this.getPos().getX() - f, this.getPos().getY() - f, this.getPos().getZ() - f, this.getPos().getX() + 1 + f, this.getPos().getY() + 1 + f, this.getPos().getZ() + 1 + f));
Iterator<?> iterator = list.iterator();
while (iterator.hasNext())
{
EntityPlayer entityplayer = (EntityPlayer) iterator.next();
if (entityplayer.openContainer instanceof ContainerParaChest)
{
++this.numUsingPlayers;
}
}
}
this.prevLidAngle = this.lidAngle;
f = 0.1F;
double d0;
if (this.numUsingPlayers > 0 && this.lidAngle == 0.0F)
{
double d1 = this.getPos().getX() + 0.5D;
d0 = this.getPos().getZ() + 0.5D;
this.worldObj.playSoundEffect(d1, this.getPos().getY() + 0.5D, d0, "random.chestopen", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
}
if (this.numUsingPlayers == 0 && this.lidAngle > 0.0F || this.numUsingPlayers > 0 && this.lidAngle < 1.0F)
{
float f1 = this.lidAngle;
if (this.numUsingPlayers > 0)
{
this.lidAngle += f;
}
else
{
this.lidAngle -= f;
}
if (this.lidAngle > 1.0F)
{
this.lidAngle = 1.0F;
}
float f2 = 0.5F;
if (this.lidAngle < f2 && f1 >= f2)
{
d0 = this.getPos().getX() + 0.5D;
double d2 = this.getPos().getZ() + 0.5D;
this.worldObj.playSoundEffect(d0, this.getPos().getY() + 0.5D, d2, "random.chestclosed", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
}
if (this.lidAngle < 0.0F)
{
this.lidAngle = 0.0F;
}
}
if (!this.worldObj.isRemote)
{
this.checkFluidTankTransfer(this.chestContents.length - 1, this.fuelTank);
}
}
private void checkFluidTankTransfer(int slot, FluidTank tank)
{
FluidUtil.tryFillContainerFuel(tank, this.chestContents, slot);
}
@Override
public boolean receiveClientEvent(int par1, int par2)
{
if (par1 == 1)
{
this.numUsingPlayers = par2;
return true;
}
else
{
return super.receiveClientEvent(par1, par2);
}
}
@Override
public void openInventory(EntityPlayer player)
{
if (this.numUsingPlayers < 0)
{
this.numUsingPlayers = 0;
}
++this.numUsingPlayers;
this.worldObj.addBlockEvent(this.getPos(), this.getBlockType(), 1, this.numUsingPlayers);
this.worldObj.notifyNeighborsOfStateChange(this.getPos(), this.getBlockType());
this.worldObj.notifyNeighborsOfStateChange(this.getPos().down(), this.getBlockType());
}
@Override
public void closeInventory(EntityPlayer player)
{
if (this.getBlockType() != null && this.getBlockType() instanceof BlockParaChest)
{
--this.numUsingPlayers;
this.worldObj.addBlockEvent(this.getPos(), this.getBlockType(), 1, this.numUsingPlayers);
this.worldObj.notifyNeighborsOfStateChange(this.getPos(), this.getBlockType());
this.worldObj.notifyNeighborsOfStateChange(this.getPos().down(), this.getBlockType());
}
}
@Override
public boolean isItemValidForSlot(int par1, ItemStack par2ItemStack)
{
return true;
}
@Override
public void invalidate()
{
super.invalidate();
this.updateContainingBlockInfo();
}
@Override
public double getPacketRange()
{
return 12.0D;
}
@Override
public int getPacketCooldown()
{
return 3;
}
@Override
public boolean isNetworkedTile()
{
return true;
}
@Override
public int getField(int id)
{
return 0;
}
@Override
public void setField(int id, int value)
{
}
@Override
public int getFieldCount()
{
return 0;
}
@Override
public void clear()
{
}
@Override
public IChatComponent getDisplayName()
{
return null;
}
@Override
public void decodePacketdata(ByteBuf buffer)
{
EnumDyeColor color = this.color;
super.decodePacketdata(buffer);
if (this.worldObj.isRemote && color != this.color)
{
this.worldObj.markBlockRangeForRenderUpdate(getPos(), getPos());
}
}
}