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

203 lines
5.7 KiB
Java
Raw Normal View History

2015-04-11 12:30:16 +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;
import powercrystals.minefactoryreloaded.api.IDeepStorageUnit;
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;
2016-11-04 21:03:05 +01:00
import reborncore.common.tile.TileLegacyMachineBase;
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 javax.annotation.Nonnull;
import java.util.List;
2015-04-11 12:30:16 +02:00
2016-11-04 21:03:05 +01:00
public class TileQuantumChest extends TileLegacyMachineBase
2016-10-08 21:46:16 +02:00
implements IInventoryProvider, IWrenchable, IDeepStorageUnit, IListInfoProvider {
2016-03-25 10:47:34 +01:00
// Slot 0 = Input
// Slot 1 = Output
// Slot 2 = Fake Item
@Nonnull
public ItemStack storedItem = ItemStack.EMPTY;
2016-03-25 10:47:34 +01:00
// 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-10-08 21:46:16 +02:00
public void updateEntity() {
2016-11-19 13:50:08 +01:00
if (!world.isRemote) {
if (storedItem != ItemStack.EMPTY) {
ItemStack fakeStack = storedItem.copy();
fakeStack.setCount(1);
setInventorySlotContents(2, fakeStack);
} else if (storedItem == ItemStack.EMPTY && getStackInSlot(1) != ItemStack.EMPTY) {
ItemStack fakeStack = getStackInSlot(1).copy();
fakeStack.setCount(1);
setInventorySlotContents(2, fakeStack);
2016-10-08 21:46:16 +02:00
} else {
setInventorySlotContents(2, ItemStack.EMPTY);
}
if (getStackInSlot(0) != ItemStack.EMPTY) {
if (storedItem == ItemStack.EMPTY) {
storedItem = getStackInSlot(0);
setInventorySlotContents(0, ItemStack.EMPTY);
2016-10-08 21:46:16 +02:00
} else if (ItemUtils.isItemEqual(storedItem, getStackInSlot(0), true, true)) {
2016-11-19 13:50:08 +01:00
if (storedItem.getCount() <= storage - getStackInSlot(0).getCount()) {
storedItem.grow(getStackInSlot(0).getCount());
2016-11-19 13:50:08 +01:00
decrStackSize(0, getStackInSlot(0).getCount());
}
}
}
if (storedItem != ItemStack.EMPTY && getStackInSlot(1) == ItemStack.EMPTY) {
ItemStack itemStack = storedItem.copy();
itemStack.setCount(itemStack.getMaxStackSize());
setInventorySlotContents(1, itemStack);
storedItem.shrink(itemStack.getMaxStackSize());
2016-10-08 21:46:16 +02:00
} else if (ItemUtils.isItemEqual(getStackInSlot(1), storedItem, true, true)) {
2016-11-19 13:50:08 +01:00
int wanted = getStackInSlot(1).getMaxStackSize() - getStackInSlot(1).getCount();
if (storedItem.getCount() >= wanted) {
decrStackSize(1, -wanted);
storedItem.shrink(wanted);
2016-10-08 21:46:16 +02:00
} else {
2016-11-19 13:50:08 +01:00
decrStackSize(1, -storedItem.getCount());
storedItem = ItemStack.EMPTY;
}
}
}
}
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-11-19 13:50:08 +01:00
world.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 = ItemStack.EMPTY;
2016-03-25 10:47:34 +01:00
2016-10-08 21:46:16 +02:00
if (tagCompound.hasKey("storedStack")) {
storedItem = new ItemStack((NBTTagCompound) tagCompound.getTag("storedStack"));
2016-03-25 10:47:34 +01:00
}
if (storedItem != ItemStack.EMPTY) {
storedItem.setCount(tagCompound.getInteger("storedQuantity"));
2016-03-25 10:47:34 +01:00
}
}
@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 != ItemStack.EMPTY) {
2016-03-25 10:47:34 +01:00
tagCompound.setTag("storedStack", storedItem.writeToNBT(new NBTTagCompound()));
2016-11-19 13:50:08 +01:00
tagCompound.setInteger("storedQuantity", storedItem.getCount());
2016-10-08 21:46:16 +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
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.quantumChest, 1);
writeToNBTWithoutCoords(tileEntity);
dropStack.setTagCompound(new NBTTagCompound());
dropStack.getTagCompound().setTag("tileEntity", tileEntity);
return dropStack;
}
@Override
2016-10-08 21:46:16 +02:00
public ItemStack getStoredItemType() {
2016-03-25 10:47:34 +01:00
return this.storedItem;
}
@Override
2016-10-08 21:46:16 +02:00
public void setStoredItemCount(int amount) {
storedItem.grow(amount);
2016-03-25 10:47:34 +01:00
this.markDirty();
}
@Override
2016-10-08 21:46:16 +02:00
public void setStoredItemType(ItemStack type, int amount) {
2016-03-25 10:47:34 +01:00
this.storedItem = type;
storedItem.setCount(amount);
2016-03-25 10:47:34 +01:00
this.markDirty();
}
@Override
2016-10-08 21:46:16 +02:00
public int getMaxStoredCount() {
2016-03-25 10:47:34 +01:00
return this.storage;
}
@Override
2016-10-08 21:46:16 +02:00
public void addInfo(List<String> info, boolean isRealTile) {
if (isRealTile) {
2016-03-25 10:47:34 +01:00
int size = 0;
String name = "of nothing";
if (storedItem != ItemStack.EMPTY) {
2016-03-25 10:47:34 +01:00
name = storedItem.getDisplayName();
2016-11-19 13:50:08 +01:00
size += storedItem.getCount();
2016-03-25 10:47:34 +01:00
}
if (getStackInSlot(1) != ItemStack.EMPTY) {
2016-03-25 10:47:34 +01:00
name = getStackInSlot(1).getDisplayName();
2016-11-19 13:50:08 +01:00
size += getStackInSlot(1).getCount();
2016-03-25 10:47:34 +01:00
}
info.add(size + " " + name);
}
}
@Override
public Inventory getInventory() {
return inventory;
}
2015-04-11 12:30:16 +02:00
}