TechReborn/src/main/java/techreborn/tiles/TileQuantumChest.java

239 lines
5.7 KiB
Java
Raw Normal View History

2015-04-11 12:30:16 +02:00
package techreborn.tiles;
2016-05-12 20:16:40 +02:00
import reborncore.common.IWrenchable;
2015-04-11 12:30:16 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
2016-03-13 17:08:30 +01:00
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.util.EnumFacing;
import powercrystals.minefactoryreloaded.api.IDeepStorageUnit;
2015-11-08 13:15:45 +01:00
import reborncore.api.IListInfoProvider;
import reborncore.api.tile.IInventoryProvider;
import reborncore.common.tile.TileMachineBase;
2015-11-08 13:15:45 +01:00
import reborncore.common.util.Inventory;
import reborncore.common.util.ItemUtils;
2015-04-11 19:14:57 +02:00
import techreborn.init.ModBlocks;
import java.util.List;
2015-04-11 12:30:16 +02:00
2016-03-25 10:47:34 +01:00
public class TileQuantumChest extends TileMachineBase
implements IInventoryProvider, IWrenchable, IDeepStorageUnit, IListInfoProvider
2016-03-25 10:47:34 +01:00
{
2016-03-25 10:47:34 +01:00
// Slot 0 = Input
// Slot 1 = Output
// Slot 2 = Fake Item
2016-03-25 10:47:34 +01:00
public ItemStack storedItem;
// TODO use long so we can have 9,223,372,036,854,775,807 items instead of
// 2,147,483,647
int storage = (int) Double.MAX_VALUE;
public Inventory inventory = new Inventory(3, "TileQuantumChest", storage, this);
@Override
2016-03-25 10:47:34 +01:00
public void updateEntity()
{
2016-03-25 10:47:34 +01:00
if (!worldObj.isRemote)
{
2016-03-25 10:47:34 +01:00
if (storedItem != null)
{
ItemStack fakeStack = storedItem.copy();
fakeStack.stackSize = 1;
setInventorySlotContents(2, fakeStack);
2016-03-25 10:47:34 +01:00
} else if (storedItem == null && getStackInSlot(1) != null)
{
ItemStack fakeStack = getStackInSlot(1).copy();
fakeStack.stackSize = 1;
setInventorySlotContents(2, fakeStack);
2016-03-25 10:47:34 +01:00
} else
{
setInventorySlotContents(2, null);
}
2016-03-25 10:47:34 +01:00
if (getStackInSlot(0) != null)
{
2016-03-25 10:47:34 +01:00
if (storedItem == null)
{
storedItem = getStackInSlot(0);
setInventorySlotContents(0, null);
2016-03-25 10:47:34 +01:00
} else if (ItemUtils.isItemEqual(storedItem, getStackInSlot(0), true, true))
{
2016-03-25 10:47:34 +01:00
if (storedItem.stackSize <= storage - getStackInSlot(0).stackSize)
{
storedItem.stackSize += getStackInSlot(0).stackSize;
decrStackSize(0, getStackInSlot(0).stackSize);
}
}
}
2016-03-25 10:47:34 +01:00
if (storedItem != null && getStackInSlot(1) == null)
{
ItemStack itemStack = storedItem.copy();
itemStack.stackSize = itemStack.getMaxStackSize();
setInventorySlotContents(1, itemStack);
storedItem.stackSize -= itemStack.getMaxStackSize();
2016-03-25 10:47:34 +01:00
} else if (ItemUtils.isItemEqual(getStackInSlot(1), storedItem, true, true))
{
int wanted = getStackInSlot(1).getMaxStackSize() - getStackInSlot(1).stackSize;
2016-03-25 10:47:34 +01:00
if (storedItem.stackSize >= wanted)
{
decrStackSize(1, -wanted);
storedItem.stackSize -= wanted;
2016-03-25 10:47:34 +01:00
} else
{
decrStackSize(1, -storedItem.stackSize);
storedItem = null;
}
}
}
}
2016-03-25 10:47:34 +01:00
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet)
{
worldObj.markBlockRangeForRenderUpdate(getPos().getX(), getPos().getY(), getPos().getZ(), getPos().getX(),
getPos().getY(), getPos().getZ());
readFromNBT(packet.getNbtCompound());
}
@Override
public void readFromNBT(NBTTagCompound tagCompound)
{
super.readFromNBT(tagCompound);
readFromNBTWithoutCoords(tagCompound);
}
public void readFromNBTWithoutCoords(NBTTagCompound tagCompound)
{
storedItem = null;
if (tagCompound.hasKey("storedStack"))
{
storedItem = ItemStack.loadItemStackFromNBT((NBTTagCompound) tagCompound.getTag("storedStack"));
}
if (storedItem != null)
{
storedItem.stackSize = tagCompound.getInteger("storedQuantity");
}
}
@Override
2016-05-18 17:53:54 +02:00
public NBTTagCompound writeToNBT(NBTTagCompound tagCompound)
2016-03-25 10:47:34 +01:00
{
super.writeToNBT(tagCompound);
writeToNBTWithoutCoords(tagCompound);
2016-05-18 17:53:54 +02:00
return tagCompound;
2016-03-25 10:47:34 +01:00
}
2016-05-18 17:53:54 +02:00
public NBTTagCompound writeToNBTWithoutCoords(NBTTagCompound tagCompound)
2016-03-25 10:47:34 +01:00
{
if (storedItem != null)
{
tagCompound.setTag("storedStack", storedItem.writeToNBT(new NBTTagCompound()));
tagCompound.setInteger("storedQuantity", storedItem.stackSize);
2016-05-18 17:53:54 +02:00
} else{
2016-03-25 10:47:34 +01:00
tagCompound.setInteger("storedQuantity", 0);
2016-05-18 17:53:54 +02:00
}
return tagCompound;
2016-03-25 10:47:34 +01:00
}
@Override
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, EnumFacing side)
{
return false;
}
@Override
public EnumFacing getFacing()
{
return getFacingEnum();
}
@Override
public boolean wrenchCanRemove(EntityPlayer entityPlayer)
{
return entityPlayer.isSneaking();
2016-03-25 10:47:34 +01:00
}
@Override
public float getWrenchDropRate()
{
return 1F;
}
@Override
public ItemStack getWrenchDrop(EntityPlayer entityPlayer)
{
return getDropWithNBT();
}
public ItemStack getDropWithNBT()
{
NBTTagCompound tileEntity = new NBTTagCompound();
ItemStack dropStack = new ItemStack(ModBlocks.quantumChest, 1);
writeToNBTWithoutCoords(tileEntity);
dropStack.setTagCompound(new NBTTagCompound());
dropStack.getTagCompound().setTag("tileEntity", tileEntity);
return dropStack;
}
@Override
public ItemStack getStoredItemType()
{
return this.storedItem;
}
@Override
public void setStoredItemCount(int amount)
{
this.storedItem.stackSize = 0;
this.storedItem.stackSize += (amount);
this.markDirty();
}
@Override
public void setStoredItemType(ItemStack type, int amount)
{
this.storedItem = type;
this.storedItem.stackSize = amount;
this.markDirty();
}
@Override
public int getMaxStoredCount()
{
return this.storage;
}
@Override
public void addInfo(List<String> info, boolean isRealTile)
{
if (isRealTile)
{
int size = 0;
String name = "of nothing";
if (storedItem != null)
{
name = storedItem.getDisplayName();
size += storedItem.stackSize;
}
if (getStackInSlot(1) != null)
{
name = getStackInSlot(1).getDisplayName();
size += getStackInSlot(1).stackSize;
}
info.add(size + " " + name);
}
}
@Override
public Inventory getInventory() {
return inventory;
}
2015-04-11 12:30:16 +02:00
}