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

173 lines
5 KiB
Java
Raw Normal View History

2015-06-07 04:11:13 +02:00
package techreborn.tiles;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
2016-03-13 17:08:30 +01:00
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.util.EnumFacing;
2015-11-08 13:15:45 +01:00
import reborncore.api.IListInfoProvider;
import reborncore.api.tile.IInventoryProvider;
2016-10-08 21:46:16 +02:00
import reborncore.common.IWrenchable;
import reborncore.common.tile.TileMachineBase;
2015-11-08 13:15:45 +01:00
import reborncore.common.util.Inventory;
import reborncore.common.util.ItemUtils;
2015-06-07 04:11:13 +02:00
import techreborn.init.ModBlocks;
import java.util.List;
2015-06-07 04:11:13 +02:00
2016-10-08 21:46:16 +02:00
public class TileDigitalChest extends TileMachineBase implements IInventoryProvider, IWrenchable, IListInfoProvider {
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 = 32767;
public Inventory inventory = new Inventory(3, "TileDigitalChest", storage, this);
@Override
2016-10-08 21:46:16 +02:00
public void updateEntity() {
if (!worldObj.isRemote) {
if (storedItem != null) {
ItemStack fakeStack = storedItem.copy();
fakeStack.stackSize = 1;
setInventorySlotContents(2, fakeStack);
2016-10-08 21:46:16 +02:00
} else if (storedItem == null && getStackInSlot(1) != null) {
ItemStack fakeStack = getStackInSlot(1).copy();
fakeStack.stackSize = 1;
setInventorySlotContents(2, fakeStack);
2016-10-08 21:46:16 +02:00
} else {
setInventorySlotContents(2, null);
}
2016-10-08 21:46:16 +02:00
if (getStackInSlot(0) != null) {
if (storedItem == null) {
storedItem = getStackInSlot(0);
setInventorySlotContents(0, null);
2016-10-08 21:46:16 +02:00
} else if (ItemUtils.isItemEqual(storedItem, getStackInSlot(0), true, true)) {
if (storedItem.stackSize <= storage - getStackInSlot(0).stackSize) {
storedItem.stackSize += getStackInSlot(0).stackSize;
decrStackSize(0, getStackInSlot(0).stackSize);
}
}
}
2016-10-08 21:46:16 +02:00
if (storedItem != null && getStackInSlot(1) == null) {
ItemStack itemStack = storedItem.copy();
itemStack.stackSize = itemStack.getMaxStackSize();
setInventorySlotContents(1, itemStack);
storedItem.stackSize -= itemStack.getMaxStackSize();
2016-10-08 21:46:16 +02:00
} else if (ItemUtils.isItemEqual(getStackInSlot(1), storedItem, true, true)) {
int wanted = getStackInSlot(1).getMaxStackSize() - getStackInSlot(1).stackSize;
2016-10-08 21:46:16 +02:00
if (storedItem.stackSize >= wanted) {
decrStackSize(1, -wanted);
storedItem.stackSize -= wanted;
2016-10-08 21:46:16 +02:00
} else {
decrStackSize(1, -storedItem.stackSize);
storedItem = null;
}
}
}
}
2016-03-25 10:47:34 +01:00
@Override
2016-10-08 21:46:16 +02:00
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
2016-03-25 10:47:34 +01:00
worldObj.markBlockRangeForRenderUpdate(getPos().getX(), getPos().getY(), getPos().getZ(), getPos().getX(),
2016-10-08 21:46:16 +02:00
getPos().getY(), getPos().getZ());
2016-03-25 10:47:34 +01:00
readFromNBT(packet.getNbtCompound());
}
@Override
2016-10-08 21:46:16 +02:00
public void readFromNBT(NBTTagCompound tagCompound) {
2016-03-25 10:47:34 +01:00
super.readFromNBT(tagCompound);
readFromNBTWithoutCoords(tagCompound);
}
2016-10-08 21:46:16 +02:00
public void readFromNBTWithoutCoords(NBTTagCompound tagCompound) {
2016-03-25 10:47:34 +01:00
storedItem = null;
2016-10-08 21:46:16 +02:00
if (tagCompound.hasKey("storedStack")) {
2016-03-25 10:47:34 +01:00
storedItem = ItemStack.loadItemStackFromNBT((NBTTagCompound) tagCompound.getTag("storedStack"));
}
2016-10-08 21:46:16 +02:00
if (storedItem != null) {
2016-03-25 10:47:34 +01:00
storedItem.stackSize = tagCompound.getInteger("storedQuantity");
}
}
@Override
2016-10-08 21:46:16 +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-10-08 21:46:16 +02:00
public NBTTagCompound writeToNBTWithoutCoords(NBTTagCompound tagCompound) {
if (storedItem != null) {
2016-03-25 10:47:34 +01:00
tagCompound.setTag("storedStack", storedItem.writeToNBT(new NBTTagCompound()));
tagCompound.setInteger("storedQuantity", storedItem.stackSize);
} else
tagCompound.setInteger("storedQuantity", 0);
2016-05-18 17:53:54 +02:00
return tagCompound;
2016-03-25 10:47:34 +01:00
}
@Override
2016-10-08 21:46:16 +02:00
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, EnumFacing side) {
2016-03-25 10:47:34 +01:00
return false;
}
@Override
2016-10-08 21:46:16 +02:00
public EnumFacing getFacing() {
2016-03-25 10:47:34 +01:00
return getFacingEnum();
}
@Override
2016-10-08 21:46:16 +02:00
public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
return entityPlayer.isSneaking();
2016-03-25 10:47:34 +01:00
}
@Override
2016-10-08 21:46:16 +02:00
public float getWrenchDropRate() {
2016-03-25 10:47:34 +01:00
return 1F;
}
@Override
2016-10-08 21:46:16 +02:00
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
2016-03-25 10:47:34 +01:00
return getDropWithNBT();
}
2016-10-08 21:46:16 +02:00
public ItemStack getDropWithNBT() {
2016-03-25 10:47:34 +01:00
NBTTagCompound tileEntity = new NBTTagCompound();
ItemStack dropStack = new ItemStack(ModBlocks.digitalChest, 1);
writeToNBTWithoutCoords(tileEntity);
dropStack.setTagCompound(new NBTTagCompound());
dropStack.getTagCompound().setTag("tileEntity", tileEntity);
return dropStack;
}
@Override
2016-10-08 21:46:16 +02:00
public void addInfo(List<String> info, boolean isRealTile) {
2016-03-25 10:47:34 +01:00
int size = 0;
String name = "of nothing";
2016-10-08 21:46:16 +02:00
if (storedItem != null) {
2016-03-25 10:47:34 +01:00
name = storedItem.getDisplayName();
size += storedItem.stackSize;
}
2016-10-08 21:46:16 +02:00
if (getStackInSlot(1) != null) {
2016-03-25 10:47:34 +01:00
name = getStackInSlot(1).getDisplayName();
size += getStackInSlot(1).stackSize;
}
info.add(size + " " + name);
}
@Override
public Inventory getInventory() {
return inventory;
2016-03-25 10:47:34 +01:00
}
2015-06-07 04:11:13 +02:00
}