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

224 lines
6.4 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
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 = Integer.MAX_VALUE;
2016-03-25 10:47:34 +01:00
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 (this.getStackInSlot(0) != ItemStack.EMPTY) {
if (this.getStoredItemType().isEmpty() || (this.storedItem.isEmpty()
&& ItemUtils.isItemEqual(this.getStackInSlot(0), this.getStackInSlot(1), true, true))) {
this.storedItem = this.getStackInSlot(0);
this.setInventorySlotContents(0, ItemStack.EMPTY);
this.syncWithAll();
} else if (ItemUtils.isItemEqual(this.getStoredItemType(), this.getStackInSlot(0), true, true)) {
this.setStoredItemCount(this.getStackInSlot(0).getCount());
this.setInventorySlotContents(0, ItemStack.EMPTY);
this.syncWithAll();
}
}
if (this.storedItem != ItemStack.EMPTY) {
if (this.getStackInSlot(1) == ItemStack.EMPTY) {
ItemStack delivered = this.storedItem.copy();
delivered.setCount(Math.min(this.storedItem.getCount(), delivered.getMaxStackSize()));
this.storedItem.shrink(delivered.getCount());
if (this.storedItem.isEmpty())
this.storedItem = ItemStack.EMPTY;
this.setInventorySlotContents(1, delivered);
this.syncWithAll();
} else if (ItemUtils.isItemEqual(this.storedItem, this.getStackInSlot(1), true, true)
&& this.getStackInSlot(1).getCount() < this.getStackInSlot(1).getMaxStackSize()) {
int wanted = Math.min(this.storedItem.getCount(),
this.getStackInSlot(1).getMaxStackSize() - this.getStackInSlot(1).getCount());
this.getStackInSlot(1).setCount(this.getStackInSlot(1).getCount() + wanted);
this.storedItem.shrink(wanted);
if (this.storedItem.isEmpty())
this.storedItem = ItemStack.EMPTY;
this.syncWithAll();
}
}
if (this.getStackInSlot(2) == ItemStack.EMPTY
&& (!this.storedItem.isEmpty() || !this.getStackInSlot(1).isEmpty())) {
ItemStack fake = storedItem.isEmpty() ? this.getStackInSlot(1).copy() : storedItem.copy();
fake.setCount(1);
this.setInventorySlotContents(2, fake);
} else if (!ItemUtils.isItemEqual(this.getStackInSlot(2), this.storedItem, true, true)) {
ItemStack fake = storedItem.isEmpty() ? this.getStackInSlot(1).copy() : storedItem.copy();
fake.setCount(1);
this.setInventorySlotContents(2, fake);
}
}
}
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() {
return this.storedItem.isEmpty() ? this.getStackInSlot(1) : this.storedItem;
2016-03-25 10:47:34 +01:00
}
@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;
}
public int getStoredCount() {
return this.storedItem.getCount();
}
2016-03-25 10:47:34 +01:00
@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
}