package advsolar.common.tiles; import advsolar.common.AdvancedSolarPanel; import advsolar.common.container.ContainerMolecularTransformer; import advsolar.network.IReceiveServerEvents; import advsolar.network.PacketChangeState; import advsolar.utils.MTRecipeManager; import advsolar.utils.MTRecipeRecord; import cpw.mods.fml.common.eventhandler.EventBus; import ic2.api.energy.event.EnergyTileLoadEvent; import ic2.api.energy.event.EnergyTileUnloadEvent; import ic2.api.energy.tile.IEnergySink; import ic2.api.energy.tile.IEnergyTile; import ic2.api.network.INetworkDataProvider; import ic2.api.network.INetworkTileEntityEventListener; import ic2.api.network.INetworkUpdateListener; import ic2.api.tile.IWrenchable; import java.util.Arrays; import java.util.List; import java.util.Random; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.network.NetworkManager; import net.minecraft.network.Packet; import net.minecraft.network.play.server.S35PacketUpdateTileEntity; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.ForgeDirection; public class TileEntityMolecularTransformer extends TileEntityBase implements IEnergyTile, IWrenchable, IInventory, IEnergySink, ISidedInventory, INetworkDataProvider, INetworkUpdateListener, INetworkTileEntityEventListener, IReceiveServerEvents { private static final int[] slots_top = { 0 }; private static final int[] slots_bottom = { 1, 1 }; private static final int[] slots_sides = { 1 }; public static Random randomizer = new Random(); public int ticker; public boolean initialized; private short facing; public boolean addedToEnergyNet; private boolean created; public boolean doWork; public boolean waitOutputSlot; public ItemStack lastRecipeInput; public ItemStack lastRecipeOutput; public int lastRecipeEnergyUsed; public int lastRecipeEnergyPerOperation; public int lastRecipeNumber; private int machineTire; private ItemStack[] workSlots; private int lastX; private int lastY; private int lastZ; private Boolean deactiveTimer = Boolean.valueOf(false); private int deactiveTicker = 0; private int deactiveTickrate = 40; private int energyTicker = 0; private int energyTickRate = 60; public short lastProgress; public int energyBuffer; private boolean isActive; private boolean prevActiveState; public int inputEU; public boolean loaded = false; public TileEntityMolecularTransformer() { this.created = false; this.facing = 2; this.workSlots = new ItemStack[2]; this.initialized = false; this.machineTire = Integer.MAX_VALUE; this.ticker = randomizer.nextInt(tickRate()); this.lastX = this.field_145851_c; this.lastY = this.field_145848_d; this.lastZ = this.field_145849_e; } public void func_145829_t() { super.func_145829_t(); if ((func_145837_r()) || (!this.field_145850_b.func_72899_e(this.field_145851_c, this.field_145848_d, this.field_145849_e))) { return; } onLoaded(); } public void func_145843_s() { if (this.loaded) { onUnloaded(); } super.func_145843_s(); } public void onLoaded() { if (!this.field_145850_b.field_72995_K) { MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this)); this.addedToEnergyNet = true; } this.loaded = true; } public void onChunkUnload() { if (this.loaded) { onUnloaded(); } super.onChunkUnload(); } public void onUnloaded() { if ((!this.field_145850_b.field_72995_K) && (this.addedToEnergyNet)) { MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this)); this.addedToEnergyNet = false; } this.loaded = false; } public void intialize() { updateVisibility(); this.initialized = true; if (!this.addedToEnergyNet) { onLoaded(); } } public void func_145845_h() { super.func_145845_h(); if ((!this.initialized) && (this.field_145850_b != null)) { intialize(); } if (!AdvancedSolarPanel.isSimulating()) { return; } if ((this.lastX != this.field_145851_c) || (this.lastZ != this.field_145849_e) || (this.lastY != this.field_145848_d)) { this.lastX = this.field_145851_c; this.lastY = this.field_145848_d; this.lastZ = this.field_145849_e; onUnloaded(); intialize(); } flushSlots(); if ((!this.doWork) && (this.workSlots[0] != null)) { if (canSmelt()) { this.workSlots[0].field_77994_a -= this.lastRecipeInput.field_77994_a; this.lastRecipeEnergyUsed = 0; this.waitOutputSlot = false; this.lastProgress = 0; this.doWork = true; this.deactiveTimer = Boolean.valueOf(false); } } else if (this.doWork) { if (this.energyBuffer > 0) { this.energyTicker = 0; if (!this.waitOutputSlot) { setActive(true, true); } } else { this.energyTicker += 1; if (this.energyTicker >= this.energyTickRate) { this.energyTicker = 0; if (this.isActive) { setActive(false, true); } } } this.energyBuffer = gainFuel(this.energyBuffer); } if (this.deactiveTimer.booleanValue() == true) { checkDeactivateMachine(); } } public void checkDeactivateMachine() { if (this.deactiveTimer.booleanValue()) { this.deactiveTicker += 1; if (this.deactiveTicker == this.deactiveTickrate) { this.deactiveTicker = 0; this.deactiveTimer = Boolean.valueOf(false); setActive(false, true); } } } private boolean canSmelt() { if (this.workSlots[0] == null) { return false; } for (int i = 0; i < MTRecipeManager.transformerRecipes.size(); i++) { ItemStack tmpItemStack = ((MTRecipeRecord)MTRecipeManager.transformerRecipes.get(i)).inputStack; if (isItemEqual(this.workSlots[0], tmpItemStack)) { if (this.workSlots[1] != null) { if (isItemEqual(this.workSlots[1], ((MTRecipeRecord)MTRecipeManager.transformerRecipes.get(i)).outputStack)) { if (this.workSlots[1].field_77994_a + ((MTRecipeRecord)MTRecipeManager.transformerRecipes.get(i)).outputStack.field_77994_a > this.workSlots[1].func_77976_d()) { return false; } } else { return false; } } if (this.workSlots[0].field_77994_a < ((MTRecipeRecord)MTRecipeManager.transformerRecipes.get(i)).inputStack.field_77994_a) { return false; } this.lastRecipeInput = ((MTRecipeRecord)MTRecipeManager.transformerRecipes.get(i)).inputStack.func_77946_l(); this.lastRecipeOutput = ((MTRecipeRecord)MTRecipeManager.transformerRecipes.get(i)).outputStack.func_77946_l(); this.lastRecipeEnergyPerOperation = ((MTRecipeRecord)MTRecipeManager.transformerRecipes.get(i)).energyPerOperation; this.lastRecipeNumber = i; return true; } } return false; } public int gainFuel(int energyPacket) { int energyLeft = energyPacket; if (energyPacket >= 0) { if (this.lastRecipeEnergyPerOperation - this.lastRecipeEnergyUsed > energyPacket) { energyLeft = 0; this.lastRecipeEnergyUsed += energyPacket; } else { energyLeft = energyPacket - (this.lastRecipeEnergyPerOperation - this.lastRecipeEnergyUsed); this.lastRecipeEnergyUsed = this.lastRecipeEnergyPerOperation; this.lastProgress = 100; this.waitOutputSlot = true; if (this.workSlots[1] != null) { if (isItemEqual(this.lastRecipeOutput, this.workSlots[1])) { if (this.workSlots[1].func_77976_d() >= this.workSlots[1].field_77994_a + this.lastRecipeOutput.field_77994_a) { this.workSlots[1].field_77994_a += this.lastRecipeOutput.field_77994_a; this.doWork = false; this.deactiveTicker = 0; this.deactiveTimer = Boolean.valueOf(true); super.func_70296_d(); this.waitOutputSlot = false; } else if (isActive()) { setActive(false, true); } } } else { this.workSlots[1] = this.lastRecipeOutput.func_77946_l(); this.doWork = false; this.deactiveTicker = 0; this.deactiveTimer = Boolean.valueOf(true); this.waitOutputSlot = false; super.func_70296_d(); } } } updateProgress(); return energyLeft; } private void updateProgress() { if (this.doWork) { float tmpProgress = this.lastRecipeEnergyUsed / this.lastRecipeEnergyPerOperation * 100.0F; this.lastProgress = ((short)Math.round(tmpProgress)); if (this.lastRecipeEnergyUsed == this.lastRecipeEnergyPerOperation) { this.lastProgress = 100; } } else { this.lastProgress = 0; if (this.isActive) { this.deactiveTicker = 0; this.deactiveTimer = Boolean.valueOf(true); } } } private void flushSlots() { if ((this.workSlots[0] != null) && (this.workSlots[0].field_77994_a <= 0)) { this.workSlots[0] = null; } if ((this.workSlots[1] != null) && (this.workSlots[1].field_77994_a <= 0)) { this.workSlots[1] = null; } } private boolean isItemEqual(ItemStack inputStack, ItemStack outputStack) { if ((inputStack.func_77973_b() == outputStack.func_77973_b()) && (inputStack.func_77960_j() == outputStack.func_77960_j())) { return true; } return false; } public void updateVisibility() {} public void func_145839_a(NBTTagCompound nbttagcompound) { super.func_145839_a(nbttagcompound); this.lastX = nbttagcompound.func_74762_e("lastX"); this.lastY = nbttagcompound.func_74762_e("lastY"); this.lastZ = nbttagcompound.func_74762_e("lastZ"); this.doWork = nbttagcompound.func_74767_n("doWork"); this.lastRecipeEnergyUsed = nbttagcompound.func_74762_e("lastRecipeEnergyUsed"); this.lastRecipeEnergyPerOperation = nbttagcompound.func_74762_e("lastRecipeEnergyPerOperation"); this.lastProgress = nbttagcompound.func_74765_d("lastProgress"); NBTTagList nbttaglist_recipe = nbttagcompound.func_150295_c("Recipes", 10); for (int i = 0; i < nbttaglist_recipe.func_74745_c(); i++) { NBTTagCompound nbttagcompound_recipe = nbttaglist_recipe.func_150305_b(i); if (i == 0) { this.lastRecipeInput = ItemStack.func_77949_a(nbttagcompound_recipe); } if (i == 1) { this.lastRecipeOutput = ItemStack.func_77949_a(nbttagcompound_recipe); } } if ((this.lastRecipeInput == null) || (this.lastRecipeOutput == null)) { this.lastRecipeNumber = 0; this.doWork = false; this.lastProgress = 0; this.lastRecipeEnergyUsed = 0; } else { int i = searchRecipeNumber(this.lastRecipeInput, this.lastRecipeOutput); if (i < 0) { this.lastRecipeNumber = 0; this.doWork = false; this.lastProgress = 0; this.lastRecipeEnergyUsed = 0; } else { this.lastRecipeNumber = i; } } NBTTagList nbttaglist = nbttagcompound.func_150295_c("Items", 10); this.workSlots = new ItemStack[func_70302_i_()]; for (int i = 0; i < nbttaglist.func_74745_c(); i++) { NBTTagCompound nbttagcompound1 = nbttaglist.func_150305_b(i); int j = nbttagcompound1.func_74771_c("Slot") & 0xFF; if ((j >= 0) && (j < this.workSlots.length)) { this.workSlots[j] = ItemStack.func_77949_a(nbttagcompound1); } } } public int searchRecipeNumber(ItemStack inputStack, ItemStack outputStack) { for (int i = 0; i < MTRecipeManager.transformerRecipes.size(); i++) { ItemStack tmpInputStack = ((MTRecipeRecord)MTRecipeManager.transformerRecipes.get(i)).inputStack; ItemStack tmpOutputStack = ((MTRecipeRecord)MTRecipeManager.transformerRecipes.get(i)).outputStack; if ((isItemEqual(inputStack, tmpInputStack)) && (isItemEqual(outputStack, tmpOutputStack))) { return i; } } return -1; } public void func_145841_b(NBTTagCompound nbttagcompound) { super.func_145841_b(nbttagcompound); NBTTagList nbttaglist = new NBTTagList(); NBTTagList nbttaglist_recipe = new NBTTagList(); nbttagcompound.func_74768_a("lastX", this.lastX); nbttagcompound.func_74768_a("lastY", this.lastY); nbttagcompound.func_74768_a("lastZ", this.lastZ); nbttagcompound.func_74757_a("doWork", this.doWork); nbttagcompound.func_74768_a("lastRecipeEnergyUsed", this.lastRecipeEnergyUsed); nbttagcompound.func_74768_a("lastRecipeEnergyPerOperation", this.lastRecipeEnergyPerOperation); nbttagcompound.func_74777_a("lastProgress", this.lastProgress); nbttagcompound.func_74768_a("lastRecipeNumber", this.lastRecipeNumber); NBTTagCompound nbttagcompound_recipe = new NBTTagCompound(); if (this.lastRecipeInput != null) { nbttagcompound_recipe.func_74757_a("lastRecipeInput", true); this.lastRecipeInput.func_77955_b(nbttagcompound_recipe); } else { nbttagcompound_recipe.func_74757_a("lastRecipeInput", false); } nbttaglist_recipe.func_74742_a(nbttagcompound_recipe); nbttagcompound_recipe = new NBTTagCompound(); if (this.lastRecipeOutput != null) { nbttagcompound_recipe.func_74757_a("lastRecipeOutput", true); this.lastRecipeOutput.func_77955_b(nbttagcompound_recipe); } else { nbttagcompound_recipe.func_74757_a("lastRecipeOutput", false); } nbttaglist_recipe.func_74742_a(nbttagcompound_recipe); nbttagcompound.func_74782_a("Recipes", nbttaglist_recipe); for (int i = 0; i < this.workSlots.length; i++) { if (this.workSlots[i] != null) { NBTTagCompound nbttagcompound1 = new NBTTagCompound(); nbttagcompound1.func_74774_a("Slot", (byte)i); this.workSlots[i].func_77955_b(nbttagcompound1); nbttaglist.func_74742_a(nbttagcompound1); } } nbttagcompound.func_74782_a("Items", nbttaglist); } public boolean isAddedToEnergyNet() { return this.addedToEnergyNet; } public boolean isActive() { return this.isActive; } public void setActive(boolean activeState, boolean network) { if ((network) && (activeState != this.isActive)) { PacketChangeState.issue(this, activeState == true ? 1 : 0, null); } if (activeState != this.isActive) { this.field_145850_b.func_147471_g(this.field_145851_c, this.field_145848_d, this.field_145849_e); } this.isActive = activeState; if (!activeState) { this.inputEU = 0; } } public int gaugeFuelScaled(int i) { return i; } public boolean func_70300_a(EntityPlayer entityplayer) { return entityplayer.func_70011_f(this.field_145851_c + 0.5D, this.field_145848_d + 0.5D, this.field_145849_e + 0.5D) <= 64.0D; } public int tickRate() { return 20; } public short getFacing() { return this.facing; } public void setFacing(short facing) { this.facing = facing; } public boolean wrenchCanSetFacing(EntityPlayer entityplayer, int i) { return false; } public boolean wrenchCanRemove(EntityPlayer entityplayer) { return true; } public float getWrenchDropRate() { return 1.0F; } public ItemStack getWrenchDrop(EntityPlayer entityPlayer) { return new ItemStack(this.field_145850_b.func_147439_a(this.field_145851_c, this.field_145848_d, this.field_145849_e), 1, this.field_145850_b.func_72805_g(this.field_145851_c, this.field_145848_d, this.field_145849_e)); } public ItemStack[] getContents() { return this.workSlots; } public int func_70302_i_() { return this.workSlots.length; } public ItemStack func_70301_a(int i) { return this.workSlots[i]; } public ItemStack func_70298_a(int index, int amount) { ItemStack itemStack = func_70301_a(index); if (itemStack == null) { return null; } if (amount >= itemStack.field_77994_a) { func_70299_a(index, null); return itemStack; } itemStack.field_77994_a -= amount; ItemStack ret = itemStack.func_77946_l(); ret.field_77994_a = amount; return ret; } public void func_70299_a(int i, ItemStack itemstack) { this.workSlots[i] = itemstack; if ((itemstack != null) && (itemstack.field_77994_a > func_70297_j_())) { itemstack.field_77994_a = func_70297_j_(); } } public int func_70297_j_() { return 64; } public Container getGuiContainer(InventoryPlayer inventoryplayer) { return new ContainerMolecularTransformer(inventoryplayer, this); } public ItemStack func_70304_b(int var1) { if (this.workSlots[var1] != null) { ItemStack var2 = this.workSlots[var1]; this.workSlots[var1] = null; return var2; } return null; } private static List fields = Arrays.asList(new String[0]); public void onNetworkUpdate(String field) {} public List getNetworkedFields() { return fields; } public void onServerEvent(int event, NBTTagCompound nbtData) { Thread.getAllStackTraces(); switch (event) { case 0: setActive(false, false); break; case 1: setActive(true, false); } } public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) { NBTTagCompound nbt = pkt.func_148857_g(); this.isActive = nbt.func_74767_n("active"); } public Packet func_145844_m() { NBTTagCompound nbttagcompound = new NBTTagCompound(); nbttagcompound.func_74757_a("active", this.isActive); func_145841_b(nbttagcompound); return new S35PacketUpdateTileEntity(this.field_145851_c, this.field_145848_d, this.field_145849_e, 3, nbttagcompound); } public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction) { return true; } public double getDemandedEnergy() { if (!this.doWork) { this.inputEU = 0; return 0.0D; } if (this.lastRecipeEnergyPerOperation - this.lastRecipeEnergyUsed >= 0) { return this.lastRecipeEnergyPerOperation - this.lastRecipeEnergyUsed; } return 0.0D; } public double injectEnergy(ForgeDirection directionFrom, double amount, double voltage) { this.inputEU = ((int)amount); if (!this.doWork) { return amount; } if (this.lastRecipeEnergyPerOperation - this.lastRecipeEnergyUsed >= amount) { this.energyBuffer = ((int)(this.energyBuffer + amount)); return 0.0D; } this.energyBuffer = ((int)(this.energyBuffer + (amount - (this.lastRecipeEnergyPerOperation - this.lastRecipeEnergyUsed)))); return amount - (this.lastRecipeEnergyPerOperation - this.lastRecipeEnergyUsed); } public int getSinkTier() { return this.machineTire; } public boolean func_94041_b(int i, ItemStack itemstack) { return i != 1; } public int[] func_94128_d(int side) { return side == 1 ? slots_top : side == 0 ? slots_bottom : slots_sides; } public boolean func_102007_a(int index, ItemStack itemStack, int side) { return index != 1; } public boolean func_102008_b(int index, ItemStack itemStack, int side) { return true; } public String func_145825_b() { return "Molecular Transformer"; } public boolean func_145818_k_() { return false; } public void func_70295_k_() {} public void func_70305_f() {} }