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

352 lines
12 KiB
Java
Raw Normal View History

package techreborn.tiles;
import ic2.api.energy.prefab.BasicSink;
2015-04-15 22:11:34 +02:00
import ic2.api.item.IC2Items;
2015-04-14 13:57:43 +02:00
import ic2.api.tile.IWrenchable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
2015-04-14 22:23:45 +02:00
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import techreborn.api.CentrifugeRecipie;
import techreborn.api.TechRebornAPI;
2015-04-14 22:23:45 +02:00
import techreborn.config.ConfigTechReborn;
2015-04-14 20:23:16 +02:00
import techreborn.init.ModBlocks;
import techreborn.util.Inventory;
import techreborn.util.ItemUtils;
2015-04-14 22:23:45 +02:00
public class TileCentrifuge extends TileMachineBase implements IInventory, IWrenchable, ISidedInventory {
public BasicSink energy;
public Inventory inventory = new Inventory(6, "TileCentrifuge", 64);
public boolean isRunning;
public int tickTime;
public CentrifugeRecipie currentRecipe;
public boolean hasTakenCells = false;
public boolean hasTakenItem = false;
2015-04-14 22:23:45 +02:00
public int euTick = ConfigTechReborn.CentrifugeInputTick;
public TileCentrifuge() {
2015-04-14 22:23:45 +02:00
energy = new BasicSink(this, ConfigTechReborn.CentrifugeCharge, ConfigTechReborn.CentrifugeTier);
}
@Override
public void updateEntity() {
super.updateEntity();
energy.updateEntity();
2015-04-15 17:23:12 +02:00
if (!worldObj.isRemote) {
if (isRunning) {
if (ItemUtils.isItemEqual(currentRecipe.getInputItem(), getStackInSlot(0), true, true)) {
if (!hasTakenCells) {
2015-04-15 22:11:34 +02:00
if (getStackInSlot(1) != null && getStackInSlot(1) != null && ItemUtils.isItemEqual(getStackInSlot(1), IC2Items.getItem("cell"), false, false)) {
2015-04-15 17:23:12 +02:00
if (getStackInSlot(1).stackSize >= currentRecipe.getCells()) {
decrStackSize(1, currentRecipe.getCells());
hasTakenCells = true;
}
}
}
2015-04-15 17:23:12 +02:00
if (hasTakenCells && hasTakenItem) {
if (tickTime == currentRecipe.getTickTime()) {
if (areAnyOutputsFull()) {
return;
}
2015-04-15 17:23:12 +02:00
if (areOutputsEmpty()) {
setOutput(1, currentRecipe.getOutput1());
setOutput(2, currentRecipe.getOutput2());
setOutput(3, currentRecipe.getOutput3());
setOutput(4, currentRecipe.getOutput4());
2015-04-15 17:23:12 +02:00
} else if (areOutputsEqual()) {
if (currentRecipe.getOutput1() != null)
increacseItemStack(1, 1);
2015-04-15 17:23:12 +02:00
if (currentRecipe.getOutput2() != null)
increacseItemStack(2, 1);
2015-04-15 17:23:12 +02:00
if (currentRecipe.getOutput3() != null)
increacseItemStack(3, 1);
2015-04-15 17:23:12 +02:00
if (currentRecipe.getOutput4() != null)
increacseItemStack(4, 1);
}
tickTime = 0;
isRunning = false;
hasTakenCells = false;
hasTakenItem = false;
syncWithAll();
return;
}
2015-04-15 17:23:12 +02:00
if (energy.canUseEnergy(euTick)) {
if (getStackInSlot(0) != null && ItemUtils.isItemEqual(getStackInSlot(0), currentRecipe.getInputItem(), true, true)) {
if (energy.useEnergy(5)) {
tickTime++;
}
}
}
}
}
} else {
//sets a new recipe
2015-04-15 17:23:12 +02:00
if (getStackInSlot(0) != null && currentRecipe == null) {
for (CentrifugeRecipie recipie : TechRebornAPI.centrifugeRecipies) {
if (ItemUtils.isItemEqual(recipie.getInputItem(), getStackInSlot(0), true, true)) {
currentRecipe = new CentrifugeRecipie(recipie);
}
}
}
2015-04-15 17:23:12 +02:00
if (!isRunning && currentRecipe != null) {
if (areOutputsEqual() || !areAnyOutputsFull()) {
if (getStackInSlot(0) != null && currentRecipe.getInputItem().stackSize <= getStackInSlot(0).stackSize) {
if (energy.canUseEnergy(euTick)) {
decrStackSize(0, currentRecipe.getInputItem().stackSize);
hasTakenItem = true;
tickTime = 0;
isRunning = true;
}
}
}
}
}
syncWithAll();
}
}
2015-04-15 17:23:12 +02:00
public boolean areOutputsEqual() {
if (currentRecipe == null) {
return false;
}
boolean boo = false;
2015-04-15 17:23:12 +02:00
if (currentRecipe.getOutput1() != null && ItemUtils.isItemEqual(getOutputItemStack(1), currentRecipe.getOutput1(), false, true)) {
boo = true;
}
2015-04-15 17:23:12 +02:00
if (currentRecipe.getOutput2() != null && ItemUtils.isItemEqual(getOutputItemStack(2), currentRecipe.getOutput2(), false, true)) {
boo = true;
}
2015-04-15 17:23:12 +02:00
if (currentRecipe.getOutput3() != null && ItemUtils.isItemEqual(getOutputItemStack(3), currentRecipe.getOutput3(), false, true)) {
boo = true;
}
2015-04-15 17:23:12 +02:00
if (currentRecipe.getOutput4() != null && ItemUtils.isItemEqual(getOutputItemStack(4), currentRecipe.getOutput4(), false, true)) {
boo = true;
}
return boo;
}
2015-04-15 17:23:12 +02:00
public boolean areOutputsEmpty() {
return getOutputItemStack(1) == null && getOutputItemStack(2) == null && getOutputItemStack(3) == null && getOutputItemStack(4) == null;
}
2015-04-15 17:23:12 +02:00
public boolean areAnyOutputsFull() {
if (currentRecipe.getOutput1() != null && getOutputItemStack(1) != null && getOutputItemStack(1).stackSize + currentRecipe.getOutput1().stackSize > currentRecipe.getOutput1().getMaxStackSize()) {
return true;
}
2015-04-15 17:23:12 +02:00
if (currentRecipe.getOutput2() != null && getOutputItemStack(2) != null && getOutputItemStack(2).stackSize + currentRecipe.getOutput2().stackSize > currentRecipe.getOutput1().getMaxStackSize()) {
return true;
}
2015-04-15 17:23:12 +02:00
if (currentRecipe.getOutput3() != null && getOutputItemStack(3) != null && getOutputItemStack(3).stackSize + currentRecipe.getOutput3().stackSize > currentRecipe.getOutput1().getMaxStackSize()) {
return true;
}
2015-04-15 17:23:12 +02:00
if (currentRecipe.getOutput4() != null && getOutputItemStack(4) != null && getOutputItemStack(4).stackSize + currentRecipe.getOutput4().stackSize > currentRecipe.getOutput1().getMaxStackSize()) {
return true;
}
return false;
}
2015-04-15 17:23:12 +02:00
public ItemStack getOutputItemStack(int slot) {
return getStackInSlot(slot + 1);
}
public void increacseItemStack(int slot, int amount) {
2015-04-15 17:23:12 +02:00
if (getOutputItemStack(slot) == null) {
return;
}
decrStackSize(slot + 1, -amount);
}
2015-04-15 17:23:12 +02:00
public void setOutput(int slot, ItemStack stack) {
if (stack == null) {
return;
}
setInventorySlotContents(slot + 1, stack);
}
@Override
public void readFromNBT(NBTTagCompound tagCompound) {
super.readFromNBT(tagCompound);
inventory.readFromNBT(tagCompound);
String recipeName = tagCompound.getString("recipe");
2015-04-15 17:23:12 +02:00
for (CentrifugeRecipie recipie : TechRebornAPI.centrifugeRecipies) {
if (recipie.getInputItem().getUnlocalizedName().equals(recipeName)) {
currentRecipe = new CentrifugeRecipie(recipie);
}
}
isRunning = tagCompound.getBoolean("isRunning");
tickTime = tagCompound.getInteger("tickTime");
hasTakenCells = tagCompound.getBoolean("hasTakenCells");
hasTakenItem = tagCompound.getBoolean("hasTakenItem");
}
@Override
public void writeToNBT(NBTTagCompound tagCompound) {
super.writeToNBT(tagCompound);
inventory.writeToNBT(tagCompound);
writeUpdateToNBT(tagCompound);
}
public void writeUpdateToNBT(NBTTagCompound tagCompound) {
2015-04-15 17:23:12 +02:00
if (currentRecipe != null) {
tagCompound.setString("recipe", currentRecipe.getInputItem().getUnlocalizedName());
} else {
tagCompound.setString("recipe", "none");
}
tagCompound.setBoolean("isRunning", isRunning);
tagCompound.setInteger("tickTime", tickTime);
tagCompound.setBoolean("hasTakenCells", hasTakenCells);
tagCompound.setBoolean("hasTakenItem", hasTakenItem);
}
@Override
public int getSizeInventory() {
return inventory.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int p_70301_1_) {
return inventory.getStackInSlot(p_70301_1_);
}
@Override
public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_) {
return inventory.decrStackSize(p_70298_1_, p_70298_2_);
}
@Override
public ItemStack getStackInSlotOnClosing(int p_70304_1_) {
return inventory.getStackInSlotOnClosing(p_70304_1_);
}
@Override
public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_) {
inventory.setInventorySlotContents(p_70299_1_, p_70299_2_);
}
@Override
public String getInventoryName() {
return inventory.getInventoryName();
}
@Override
public boolean hasCustomInventoryName() {
return inventory.hasCustomInventoryName();
}
@Override
public int getInventoryStackLimit() {
return inventory.getInventoryStackLimit();
}
@Override
public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
return inventory.isUseableByPlayer(p_70300_1_);
}
@Override
public void openInventory() {
inventory.openInventory();
}
@Override
public void closeInventory() {
inventory.closeInventory();
}
@Override
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
return inventory.isItemValidForSlot(p_94041_1_, p_94041_2_);
}
@Override
public void invalidate() {
energy.invalidate();
}
@Override
public void onChunkUnload() {
energy.onChunkUnload();
}
public Packet getDescriptionPacket() {
NBTTagCompound nbtTag = new NBTTagCompound();
writeToNBT(nbtTag);
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag);
}
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
worldObj.markBlockRangeForRenderUpdate(xCoord, yCoord, zCoord, xCoord, yCoord, zCoord);
readFromNBT(packet.func_148857_g());
}
2015-04-14 13:57:43 +02:00
2015-04-14 20:23:16 +02:00
@Override
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) {
return false;
}
@Override
public short getFacing() {
return 0;
}
@Override
2015-04-15 17:23:12 +02:00
public void setFacing(short facing) {
}
2015-04-14 20:23:16 +02:00
@Override
public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
return true;
}
@Override
public float getWrenchDropRate() {
return 1.0F;
}
@Override
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
return new ItemStack(ModBlocks.centrifuge, 1);
}
2015-04-14 22:23:45 +02:00
@Override
public int[] getAccessibleSlotsFromSide(int side) {
//Top
2015-04-15 17:23:12 +02:00
if (side == 1) {
2015-04-14 22:23:45 +02:00
return new int[]{0};
}
//Bottom
2015-04-15 17:23:12 +02:00
if (side == 0) {
2015-04-14 22:23:45 +02:00
return new int[]{1};
}
//Not bottom or top
return new int[]{2, 3, 4, 5};
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, int side) {
//Bottom
2015-04-15 17:23:12 +02:00
if (side == 0) {
2015-04-15 22:11:34 +02:00
return ItemUtils.isItemEqual(stack, IC2Items.getItem("cell"), false, false);
2015-04-14 22:23:45 +02:00
}
//Not bottom or top
2015-04-15 17:23:12 +02:00
if (side >= 2) {
2015-04-14 22:23:45 +02:00
return false;
}
return true;
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side) {
//Only output slots and sides
return slot >= 2 && side >= 2;
}
}