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;
|
2016-01-07 11:46:13 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2015-11-08 13:15:45 +01:00
|
|
|
import reborncore.api.IListInfoProvider;
|
2016-04-11 18:43:54 +02:00
|
|
|
import reborncore.api.tile.IInventoryProvider;
|
2016-10-08 21:46:16 +02:00
|
|
|
import reborncore.common.IWrenchable;
|
2016-11-04 21:03:05 +01:00
|
|
|
import reborncore.common.tile.TileLegacyMachineBase;
|
2016-02-20 17:59:31 +01:00
|
|
|
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;
|
2016-04-11 18:50:00 +02:00
|
|
|
|
|
|
|
import java.util.List;
|
2015-06-07 04:11:13 +02:00
|
|
|
|
2016-11-04 21:03:05 +01:00
|
|
|
public class TileDigitalChest extends TileLegacyMachineBase implements IInventoryProvider, IWrenchable, IListInfoProvider {
|
2015-08-09 12:05:32 +02:00
|
|
|
|
2016-03-25 10:47:34 +01:00
|
|
|
// Slot 0 = Input
|
|
|
|
// Slot 1 = Output
|
|
|
|
// Slot 2 = Fake Item
|
2016-03-24 01:41:47 +01:00
|
|
|
|
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);
|
2015-08-09 12:05:32 +02:00
|
|
|
|
2015-11-11 22:37:31 +01:00
|
|
|
@Override
|
2016-10-08 21:46:16 +02:00
|
|
|
public void updateEntity() {
|
|
|
|
if (!worldObj.isRemote) {
|
|
|
|
if (storedItem != null) {
|
2015-11-11 22:37:31 +01:00
|
|
|
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) {
|
2015-11-11 22:37:31 +01:00
|
|
|
ItemStack fakeStack = getStackInSlot(1).copy();
|
|
|
|
fakeStack.stackSize = 1;
|
|
|
|
setInventorySlotContents(2, fakeStack);
|
2016-10-08 21:46:16 +02:00
|
|
|
} else {
|
2015-11-11 22:37:31 +01:00
|
|
|
setInventorySlotContents(2, null);
|
|
|
|
}
|
2015-08-09 12:05:32 +02:00
|
|
|
|
2016-10-08 21:46:16 +02:00
|
|
|
if (getStackInSlot(0) != null) {
|
|
|
|
if (storedItem == null) {
|
2015-11-11 22:37:31 +01:00
|
|
|
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) {
|
2015-11-11 22:37:31 +01:00
|
|
|
storedItem.stackSize += getStackInSlot(0).stackSize;
|
|
|
|
decrStackSize(0, getStackInSlot(0).stackSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-09 12:05:32 +02:00
|
|
|
|
2016-10-08 21:46:16 +02:00
|
|
|
if (storedItem != null && getStackInSlot(1) == null) {
|
2015-11-11 22:37:31 +01:00
|
|
|
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)) {
|
2015-11-11 22:37:31 +01:00
|
|
|
int wanted = getStackInSlot(1).getMaxStackSize() - getStackInSlot(1).stackSize;
|
2016-10-08 21:46:16 +02:00
|
|
|
if (storedItem.stackSize >= wanted) {
|
2015-11-11 22:37:31 +01:00
|
|
|
decrStackSize(1, -wanted);
|
|
|
|
storedItem.stackSize -= wanted;
|
2016-10-08 21:46:16 +02:00
|
|
|
} else {
|
2015-11-11 22:37:31 +01:00
|
|
|
decrStackSize(1, -storedItem.stackSize);
|
|
|
|
storedItem = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-09 12:05:32 +02:00
|
|
|
|
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) {
|
2016-04-03 15:34:39 +02:00
|
|
|
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
|
2016-04-11 18:43:54 +02:00
|
|
|
public Inventory getInventory() {
|
|
|
|
return inventory;
|
2016-03-25 10:47:34 +01:00
|
|
|
}
|
2015-06-07 04:11:13 +02:00
|
|
|
}
|