diff --git a/src/main/java/techreborn/tiles/TileMatterFabricator.java b/src/main/java/techreborn/tiles/TileMatterFabricator.java index 69c198e48..ff0a78435 100644 --- a/src/main/java/techreborn/tiles/TileMatterFabricator.java +++ b/src/main/java/techreborn/tiles/TileMatterFabricator.java @@ -2,6 +2,7 @@ package techreborn.tiles; import ic2.api.energy.prefab.BasicSink; import ic2.api.energy.tile.IEnergyTile; +import ic2.api.recipe.Recipes; import ic2.api.tile.IWrenchable; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; @@ -11,93 +12,89 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.ForgeDirection; import techreborn.config.ConfigTechReborn; import techreborn.init.ModBlocks; +import techreborn.init.ModItems; import techreborn.util.Inventory; +import techreborn.util.ItemUtils; + +import java.util.Iterator; +import java.util.Map.Entry; public class TileMatterFabricator extends TileMachineBase implements IWrenchable, IEnergyTile, IInventory, ISidedInventory { - + + public static int sMatterFabricationRate = 166666; public int tickTime; public BasicSink energy; public Inventory inventory = new Inventory(7, "TileMatterFabricator", 64); + private int mAmplifier = 0; + private int mProgresstime = 0; public TileMatterFabricator() { //TODO configs - energy = new BasicSink(this, 1000, + energy = new BasicSink(this, 100000, ConfigTechReborn.CentrifugeTier); } @Override - public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) - { + public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) { return false; } @Override - public short getFacing() - { + public short getFacing() { return 0; } @Override - public void setFacing(short facing) - { + public void setFacing(short facing) { } @Override - public boolean wrenchCanRemove(EntityPlayer entityPlayer) - { - if (entityPlayer.isSneaking()) - { + public boolean wrenchCanRemove(EntityPlayer entityPlayer) { + if (entityPlayer.isSneaking()) { return true; } return false; } @Override - public float getWrenchDropRate() - { + public float getWrenchDropRate() { return 1.0F; } @Override - public ItemStack getWrenchDrop(EntityPlayer entityPlayer) - { + public ItemStack getWrenchDrop(EntityPlayer entityPlayer) { return new ItemStack(ModBlocks.MatterFabricator, 1); } - public boolean isComplete() - { + public boolean isComplete() { return false; } + @Override + public void readFromNBT(NBTTagCompound tagCompound) { + super.readFromNBT(tagCompound); + inventory.readFromNBT(tagCompound); + energy.readFromNBT(tagCompound); + } - @Override - public void readFromNBT(NBTTagCompound tagCompound) - { - super.readFromNBT(tagCompound); - inventory.readFromNBT(tagCompound); - energy.readFromNBT(tagCompound); - } + @Override + public void writeToNBT(NBTTagCompound tagCompound) { + super.writeToNBT(tagCompound); + inventory.writeToNBT(tagCompound); + energy.writeToNBT(tagCompound); + } - @Override - public void writeToNBT(NBTTagCompound tagCompound) - { - super.writeToNBT(tagCompound); - inventory.writeToNBT(tagCompound); - energy.writeToNBT(tagCompound); - } + @Override + public void invalidate() { + energy.invalidate(); + super.invalidate(); + } - @Override - public void invalidate() - { - energy.invalidate(); - super.invalidate(); - } - @Override - public void onChunkUnload() - { - energy.onChunkUnload(); - super.onChunkUnload(); - } + @Override + public void onChunkUnload() { + energy.onChunkUnload(); + super.onChunkUnload(); + } @Override public int getSizeInventory() { @@ -158,26 +155,98 @@ public class TileMatterFabricator extends TileMachineBase implements IWrenchable public boolean isItemValidForSlot(int slot, ItemStack stack) { return inventory.isItemValidForSlot(slot, stack); } - - // ISidedInventory + + // ISidedInventory @Override - public int[] getAccessibleSlotsFromSide(int side) - { - return side == ForgeDirection.DOWN.ordinal() ? new int[]{0, 1, 2, 3, 4, 5, 6} : new int[]{0, 1, 2, 3, 4, 5, 6}; + public int[] getAccessibleSlotsFromSide(int side) { + return side == ForgeDirection.DOWN.ordinal() ? new int[]{0, 1, 2, 3, 4, 5, 6} : new int[]{0, 1, 2, 3, 4, 5, 6}; } @Override - public boolean canInsertItem(int slotIndex, ItemStack itemStack, int side) - { + public boolean canInsertItem(int slotIndex, ItemStack itemStack, int side) { if (slotIndex == 6) return false; - return isItemValidForSlot(slotIndex, itemStack); + return isItemValidForSlot(slotIndex, itemStack); } @Override - public boolean canExtractItem(int slotIndex, ItemStack itemStack, int side) - { - return slotIndex == 6; + public boolean canExtractItem(int slotIndex, ItemStack itemStack, int side) { + return slotIndex == 6; + } + + public int maxProgresstime() { + return sMatterFabricationRate; + } + + @Override + public void updateEntity() { + super.updateEntity(); + if (!super.worldObj.isRemote) { + + if (this.energy.getEnergyStored() > 0 && !super.worldObj.isBlockIndirectlyGettingPowered(super.xCoord, super.yCoord, super.zCoord)) { + Iterator tIterator = Recipes.matterAmplifier.getRecipes().entrySet().iterator(); + + while (this.mAmplifier < 100000 && tIterator.hasNext()) { + Entry tEntry = (Entry) tIterator.next(); + ItemStack tStack = (ItemStack) tEntry.getKey(); + int tValue = (int) ((long) ((Integer) tEntry.getValue()).intValue() * (long) this.maxProgresstime() / 166666L); + if (tValue > 0) { + for (int i = 0; this.mAmplifier < 100000 && i < 5; ++i) { + if (inventory.getStackInSlot(i) != null && ItemUtils.isItemEqual(inventory.getStackInSlot(i), tStack, true, false)) { + this.mAmplifier += tValue; + inventory.decrStackSize(i, 1); + } + } + } + } + + if (this.mAmplifier > 0) { + if (this.mAmplifier > this.energy.getEnergyStored()) { + this.mProgresstime += this.energy.getEnergyStored(); + this.mAmplifier -= this.energy.getEnergyStored(); + this.decreaseStoredEnergy(this.energy.getEnergyStored(), true); + } else { + this.mProgresstime += this.mAmplifier; + this.decreaseStoredEnergy(this.mAmplifier, true); + this.mAmplifier = 0; + } + } + + if (this.mProgresstime > this.maxProgresstime() && this.spaceForOutput()) { + this.mProgresstime -= this.maxProgresstime(); + this.addOutputProducts(); + } + } + } + + } + + private boolean spaceForOutput() { + return inventory.getStackInSlot(6) == null || ItemUtils.isItemEqual(inventory.getStackInSlot(6), new ItemStack(ModItems.uuMatter), true, true) && inventory.getStackInSlot(6).stackSize < 64; + } + + private void addOutputProducts() { + + if (inventory.getStackInSlot(6) == null) { + inventory.setInventorySlotContents(6, new ItemStack(ModItems.uuMatter)); + } else if (ItemUtils.isItemEqual(inventory.getStackInSlot(6), new ItemStack(ModItems.uuMatter), true, true)) { + inventory.getStackInSlot(6).stackSize = Math.min(64, 1 + inventory.getStackInSlot(6).stackSize); + } + } + + + public boolean decreaseStoredEnergy(double aEnergy, boolean aIgnoreTooLessEnergy) { + if (energy.getEnergyStored() - aEnergy < 0 && !aIgnoreTooLessEnergy) { + return false; + } else { + energy.setEnergyStored(energy.getEnergyStored() - aEnergy); + if (energy.getEnergyStored() < 0) { + energy.setEnergyStored(0); + return false; + } else { + return true; + } + } } }