From 9d16b13d6ef0de1b34d41d94fe631cc551e2a88a Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Thu, 18 Jun 2015 18:34:39 +0100 Subject: [PATCH] initial work on making it work, now to fix the broken gui --- .../techreborn/blocks/storage/BlockIDSU.java | 14 +- .../java/techreborn/tiles/idsu/TileIDSU.java | 252 +++++++++++++++++- 2 files changed, 263 insertions(+), 3 deletions(-) diff --git a/src/main/java/techreborn/blocks/storage/BlockIDSU.java b/src/main/java/techreborn/blocks/storage/BlockIDSU.java index d705e73b4..1881cf819 100644 --- a/src/main/java/techreborn/blocks/storage/BlockIDSU.java +++ b/src/main/java/techreborn/blocks/storage/BlockIDSU.java @@ -3,7 +3,9 @@ package techreborn.blocks.storage; import cpw.mods.fml.common.FMLCommonHandler; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.IIcon; import net.minecraft.world.World; @@ -13,6 +15,7 @@ import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import techreborn.client.GuiHandler; import techreborn.packets.PacketHandler; +import techreborn.tiles.TileAesu; import techreborn.tiles.idsu.IDSUManager; import techreborn.tiles.idsu.TileIDSU; @@ -55,7 +58,7 @@ public class BlockIDSU extends BlockMachineBase { @Override public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) { - return new TileIDSU(); + return new TileIDSU(5, 2048, 100000000); } @@ -71,4 +74,13 @@ public class BlockIDSU extends BlockMachineBase { z); return true; } + + @Override + public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase player, ItemStack itemstack) { + super.onBlockPlacedBy(world, x, y, z, player, itemstack); + TileEntity tile = world.getTileEntity(x, y, z); + if(tile instanceof TileIDSU){ + ((TileIDSU) tile).setFacing((short) world.getBlockMetadata(x, y, z)); + } + } } diff --git a/src/main/java/techreborn/tiles/idsu/TileIDSU.java b/src/main/java/techreborn/tiles/idsu/TileIDSU.java index e0431838e..759aa96aa 100644 --- a/src/main/java/techreborn/tiles/idsu/TileIDSU.java +++ b/src/main/java/techreborn/tiles/idsu/TileIDSU.java @@ -1,10 +1,28 @@ package techreborn.tiles.idsu; +import ic2.api.energy.EnergyNet; +import ic2.api.energy.event.EnergyTileLoadEvent; +import ic2.api.energy.event.EnergyTileUnloadEvent; +import ic2.api.energy.tile.IEnergySink; +import ic2.api.energy.tile.IEnergySource; +import ic2.api.network.INetworkClientTileEntityEventListener; +import ic2.api.tile.IEnergyStorage; +import ic2.core.IC2; +import ic2.core.block.TileEntityBlock; +import ic2.core.init.MainConfig; +import ic2.core.util.ConfigUtil; +import ic2.core.util.StackUtil; +import ic2.core.util.Util; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.StatCollector; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.common.util.ForgeDirection; import techreborn.packets.PacketHandler; -import techreborn.tiles.TileMachineBase; -public class TileIDSU extends TileMachineBase { +public class TileIDSU extends TileEntityBlock implements IEnergySink, IEnergySource, INetworkClientTileEntityEventListener, IEnergyStorage { public int channelID = 0; @@ -18,4 +36,234 @@ public class TileIDSU extends TileMachineBase { } } } + + public double getEnergy(){ + return IDSUManager.INSTANCE.getSaveDataForWorld(worldObj, channelID).storedPower; + } + + public void setEnergy(double energy){ + IDSUManager.INSTANCE.getSaveDataForWorld(worldObj, channelID).storedPower = energy; + } + + public int tier; + public int output; + public double maxStorage; + public boolean hasRedstone = false; + public byte redstoneMode = 0; + public static byte redstoneModes = 7; + private boolean isEmittingRedstone = false; + private int redstoneUpdateInhibit = 5; + public boolean addedToEnergyNet = false; + + public TileIDSU(int tier1, int output1, int maxStorage1) { + this.tier = tier1; + this.output = output1; + this.maxStorage = maxStorage1; + } + + public float getChargeLevel() { + float ret = (float)this.getEnergy() / (float)this.maxStorage; + if(ret > 1.0F) { + ret = 1.0F; + } + + return ret; + } + + public void readFromNBT(NBTTagCompound nbttagcompound) { + super.readFromNBT(nbttagcompound); + setEnergy(Util.limit(nbttagcompound.getDouble("energy"), 0.0D, (double) this.maxStorage + EnergyNet.instance.getPowerFromTier(this.tier))); + this.redstoneMode = nbttagcompound.getByte("redstoneMode"); + } + + public void writeToNBT(NBTTagCompound nbttagcompound) { + super.writeToNBT(nbttagcompound); + nbttagcompound.setDouble("energy", this.getEnergy()); + nbttagcompound.setByte("redstoneMode", this.redstoneMode); + } + + public void onLoaded() { + super.onLoaded(); + if(IC2.platform.isSimulating()) { + MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this)); + this.addedToEnergyNet = true; + } + + } + + public void onUnloaded() { + if(IC2.platform.isSimulating() && this.addedToEnergyNet) { + MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this)); + this.addedToEnergyNet = false; + } + + super.onUnloaded(); + } + + public boolean enableUpdateEntity() { + return IC2.platform.isSimulating(); + } + + public void updateEntity() { + super.updateEntity(); + boolean needsInvUpdate = false; + + if(this.redstoneMode == 5 || this.redstoneMode == 6) { + this.hasRedstone = this.worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord); + } + + boolean shouldEmitRedstone1 = this.shouldEmitRedstone(); + if(shouldEmitRedstone1 != this.isEmittingRedstone) { + this.isEmittingRedstone = shouldEmitRedstone1; + this.setActive(this.isEmittingRedstone); + this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord)); + } + + if(needsInvUpdate) { + this.markDirty(); + } + + } + + public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction) { + return !this.facingMatchesDirection(direction); + } + + public boolean emitsEnergyTo(TileEntity receiver, ForgeDirection direction) { + return this.facingMatchesDirection(direction); + } + + public boolean facingMatchesDirection(ForgeDirection direction) { + return direction.ordinal() == this.getFacing(); + } + + public double getOfferedEnergy() { + return this.getEnergy() >= (double)this.output && (this.redstoneMode != 5 || !this.hasRedstone) && (this.redstoneMode != 6 || !this.hasRedstone || this.getEnergy() >= (double)this.maxStorage)?Math.min(this.getEnergy(), (double)this.output):0.0D; + } + + public void drawEnergy(double amount) { + setEnergy(getEnergy() - amount); + } + + public double getDemandedEnergy() { + return (double)this.maxStorage - this.getEnergy(); + } + + public double injectEnergy(ForgeDirection directionFrom, double amount, double voltage) { + if(this.getEnergy() >= (double)this.maxStorage) { + return amount; + } else { + setEnergy(this.getEnergy() + amount); + return 0.0D; + } + } + + public int getSourceTier() { + return this.tier; + } + + public int getSinkTier() { + return this.tier; + } + + + + public void onGuiClosed(EntityPlayer entityPlayer) { + } + + public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) { + return this.getFacing() != side; + } + + public void setFacing(short facing) { + if(this.addedToEnergyNet) { + MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this)); + } + + super.setFacing(facing); + if(this.addedToEnergyNet) { + this.addedToEnergyNet = false; + MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this)); + this.addedToEnergyNet = true; + } + + } + + public boolean shouldEmitRedstone() { + boolean shouldEmitRedstone = false; + switch(this.redstoneMode) { + case 1: + shouldEmitRedstone = this.getEnergy() >= (double)(this.maxStorage - this.output * 20); + break; + case 2: + shouldEmitRedstone = this.getEnergy() > (double)this.output && this.getEnergy() < (double)this.maxStorage; + break; + case 3: + shouldEmitRedstone = this.getEnergy() > (double)this.output && this.getEnergy() < (double)this.maxStorage || this.getEnergy() < (double)this.output; + break; + case 4: + shouldEmitRedstone = this.getEnergy() < (double)this.output; + } + + if(this.isEmittingRedstone != shouldEmitRedstone && this.redstoneUpdateInhibit != 0) { + --this.redstoneUpdateInhibit; + return this.isEmittingRedstone; + } else { + this.redstoneUpdateInhibit = 5; + return shouldEmitRedstone; + } + } + + public void onNetworkEvent(EntityPlayer player, int event) { + ++this.redstoneMode; + if(this.redstoneMode >= redstoneModes) { + this.redstoneMode = 0; + } + + IC2.platform.messagePlayer(player, this.getredstoneMode(), new Object[0]); + } + + public String getredstoneMode() { + return this.redstoneMode <= 6 && this.redstoneMode >= 0? StatCollector.translateToLocal("ic2.EUStorage.gui.mod.redstone" + this.redstoneMode):""; + } + + public ItemStack getWrenchDrop(EntityPlayer entityPlayer) { + ItemStack ret = super.getWrenchDrop(entityPlayer); + float energyRetainedInStorageBlockDrops = ConfigUtil.getFloat(MainConfig.get(), "balance/energyRetainedInStorageBlockDrops"); + if(energyRetainedInStorageBlockDrops > 0.0F) { + NBTTagCompound nbttagcompound = StackUtil.getOrCreateNbtData(ret); + nbttagcompound.setDouble("energy", this.getEnergy() * (double)energyRetainedInStorageBlockDrops); + } + + return ret; + } + + public int getStored() { + return (int)this.getEnergy(); + } + + public int getCapacity() { + return (int) this.maxStorage; + } + + public int getOutput() { + return this.output; + } + + public double getOutputEnergyUnitsPerTick() { + return (double)this.output; + } + + public void setStored(int energy1) { + setEnergy(energy1); + } + + public int addEnergy(int amount) { + setEnergy(getEnergy() + amount); + return amount; + } + + public boolean isTeleporterCompatible(ForgeDirection side) { + return true; + } }