diff --git a/src/main/java/basiccomponents/client/gui/GuiBatteryBox.java b/src/main/java/basiccomponents/client/gui/GuiBatteryBox.java index e5bc2ad..04f3142 100644 --- a/src/main/java/basiccomponents/client/gui/GuiBatteryBox.java +++ b/src/main/java/basiccomponents/client/gui/GuiBatteryBox.java @@ -8,8 +8,9 @@ import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; +import universalelectricity.api.energy.UnitDisplay; + import org.lwjgl.opengl.GL11; -import universalelectricity.core.electricity.ElectricityDisplay; @SideOnly(Side.CLIENT) public class GuiBatteryBox extends GuiContainer { @@ -27,8 +28,8 @@ public class GuiBatteryBox extends GuiContainer { @Override protected void drawGuiContainerForegroundLayer(int par1, int par2) { this.fontRendererObj.drawString(this.tileEntity.getInventoryName(), 65, 6, 4210752); - String displayJoules = ElectricityDisplay.getDisplayShort(this.tileEntity.getJoules(), ElectricityDisplay.ElectricUnit.JOULES); - String displayMaxJoules = ElectricityDisplay.getDisplay(this.tileEntity.getMaxJoules(), ElectricityDisplay.ElectricUnit.JOULES); + String displayJoules = UnitDisplay.getDisplayShort(this.tileEntity.getJoules(), UnitDisplay.Unit.JOULES); + String displayMaxJoules = UnitDisplay.getDisplay(this.tileEntity.getMaxJoules(), UnitDisplay.Unit.JOULES); if(this.tileEntity.isDisabled()) { displayMaxJoules = "Disabled"; } diff --git a/src/main/java/basiccomponents/client/gui/GuiCoalGenerator.java b/src/main/java/basiccomponents/client/gui/GuiCoalGenerator.java index 41d179c..6566673 100644 --- a/src/main/java/basiccomponents/client/gui/GuiCoalGenerator.java +++ b/src/main/java/basiccomponents/client/gui/GuiCoalGenerator.java @@ -8,8 +8,9 @@ import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; +import universalelectricity.api.energy.UnitDisplay; + import org.lwjgl.opengl.GL11; -import universalelectricity.core.electricity.ElectricityDisplay; @SideOnly(Side.CLIENT) public class GuiCoalGenerator extends GuiContainer { @@ -36,7 +37,7 @@ public class GuiCoalGenerator extends GuiContainer { } else if(this.tileEntity.generateWatts < 100.0D) { displayText = "Hull Heat: " + (int)(this.tileEntity.generateWatts / 100.0D * 100.0D) + "%"; } else { - displayText = ElectricityDisplay.getDisplay(this.tileEntity.generateWatts, ElectricityDisplay.ElectricUnit.WATT); + displayText = UnitDisplay.getDisplay(this.tileEntity.generateWatts, UnitDisplay.Unit.WATT); } this.fontRendererObj.drawString(displayText, (int)(100.0D - (double)displayText.length() * 1.25D), 45, 4210752); diff --git a/src/main/java/basiccomponents/client/gui/GuiElectricFurnace.java b/src/main/java/basiccomponents/client/gui/GuiElectricFurnace.java index 31a18da..d2b4714 100644 --- a/src/main/java/basiccomponents/client/gui/GuiElectricFurnace.java +++ b/src/main/java/basiccomponents/client/gui/GuiElectricFurnace.java @@ -8,8 +8,9 @@ import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; +import universalelectricity.api.energy.UnitDisplay; + import org.lwjgl.opengl.GL11; -import universalelectricity.core.electricity.ElectricityDisplay; @SideOnly(Side.CLIENT) public class GuiElectricFurnace extends GuiContainer { @@ -39,8 +40,8 @@ public class GuiElectricFurnace extends GuiContainer { } this.fontRendererObj.drawString("Status: " + displayText, 82, 45, 4210752); - this.fontRendererObj.drawString(ElectricityDisplay.getDisplay(10000.0D, ElectricityDisplay.ElectricUnit.WATT), 82, 56, 4210752); - this.fontRendererObj.drawString(ElectricityDisplay.getDisplay(this.tileEntity.getVoltage(), ElectricityDisplay.ElectricUnit.VOLTAGE), 82, 68, 4210752); + this.fontRendererObj.drawString(UnitDisplay.getDisplay(10000.0D, UnitDisplay.Unit.WATT), 82, 56, 4210752); + this.fontRendererObj.drawString(UnitDisplay.getDisplay(this.tileEntity.getVoltage(), UnitDisplay.Unit.VOLTAGE), 82, 68, 4210752); this.fontRendererObj.drawString(StatCollector.translateToLocal("container.inventory"), 8, this.ySize - 96 + 2, 4210752); } diff --git a/src/main/java/basiccomponents/common/item/ItemBlockCopperWire.java b/src/main/java/basiccomponents/common/item/ItemBlockCopperWire.java index 211bae9..b19641c 100644 --- a/src/main/java/basiccomponents/common/item/ItemBlockCopperWire.java +++ b/src/main/java/basiccomponents/common/item/ItemBlockCopperWire.java @@ -12,7 +12,7 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.util.IIcon; -import universalelectricity.core.electricity.ElectricityDisplay; +import universalelectricity.api.energy.UnitDisplay; public class ItemBlockCopperWire extends ItemBlock { @@ -38,7 +38,7 @@ public class ItemBlockCopperWire extends ItemBlock { } public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4) { - par3List.add("Resistance: " + ElectricityDisplay.getDisplay(TileEntityCopperWire.RESISTANCE, ElectricityDisplay.ElectricUnit.RESISTANCE)); - par3List.add("Max Amps: " + ElectricityDisplay.getDisplay(TileEntityCopperWire.MAX_AMPS, ElectricityDisplay.ElectricUnit.AMPERE)); + par3List.add("Resistance: " + UnitDisplay.getDisplay(TileEntityCopperWire.RESISTANCE, UnitDisplay.Unit.RESISTANCE)); + par3List.add("Max Amps: " + UnitDisplay.getDisplay(TileEntityCopperWire.MAX_AMPS, UnitDisplay.Unit.AMPERE)); } } diff --git a/src/main/java/universalelectricity/api/CompatibilityModule.java b/src/main/java/universalelectricity/api/CompatibilityModule.java new file mode 100644 index 0000000..fafcaad --- /dev/null +++ b/src/main/java/universalelectricity/api/CompatibilityModule.java @@ -0,0 +1,314 @@ +package universalelectricity.api; + +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Set; + +import net.minecraft.item.ItemStack; +import net.minecraftforge.common.util.ForgeDirection; + +/** A module to extend for compatibility with other energy systems. */ +public abstract class CompatibilityModule +{ + public static final Set loadedModules = new LinkedHashSet<>(); + + /** A cache to know which module to use with when facing objects with a specific class. */ + public static final HashMap, CompatibilityModule> energyHandlerCache = new HashMap<>(); + public static final HashMap, CompatibilityModule> energyStorageCache = new HashMap<>(); + + public static void register(CompatibilityModule module) + { + loadedModules.add(module); + } + + /** Can the handler connect to this specific direction? */ + public static boolean canConnect(Object handler, ForgeDirection direction, Object source) + { + if (isHandler(handler)) + { + return energyHandlerCache.get(handler.getClass()).doCanConnect(handler, direction, source); + } + + return false; + } + + /** + * Make the handler receive energy. + * + * @return The actual energy that was used. + */ + public static double receiveEnergy(Object handler, ForgeDirection direction, double energy, boolean doReceive) + { + if (isHandler(handler)) + { + return energyHandlerCache.get(handler.getClass()).doReceiveEnergy(handler, direction, energy, doReceive); + } + + return 0; + } + + /** + * Make the handler extract energy. + * + * @return The actual energy that was extract. + */ + public static double extractEnergy(Object handler, ForgeDirection direction, double energy, boolean doReceive) + { + if (isHandler(handler)) + { + return energyHandlerCache.get(handler.getClass()).doExtractEnergy(handler, direction, energy, doReceive); + } + + return 0; + } + + /** + * Gets the energy stored in the handler. + */ + public static double getEnergy(Object handler, ForgeDirection direction) + { + if (isEnergyContainer(handler)) + { + return energyStorageCache.get(handler.getClass()).doGetEnergy(handler, direction); + } + + return 0; + } + + /** + * Charges an item + * + * @return The actual energy that was accepted. + */ + public static double chargeItem(ItemStack itemStack, double energy, boolean doCharge) + { + if (itemStack != null && isHandler(itemStack.getItem())) + { + return energyHandlerCache.get(itemStack.getItem().getClass()).doChargeItem(itemStack, energy, doCharge); + } + + return 0; + } + + /** + * Discharges an item + * + * @return The actual energy that was removed. + */ + public static double dischargeItem(ItemStack itemStack, double energy, boolean doCharge) + { + if (itemStack != null && isHandler(itemStack.getItem())) + { + return energyHandlerCache.get(itemStack.getItem().getClass()).doDischargeItem(itemStack, energy, doCharge); + } + + return 0; + } + + /** + * Gets the itemStack with a specific charge. + * + * @return ItemStack of electrical/energy item. + */ + public static ItemStack getItemWithCharge(ItemStack itemStack, double energy) + { + if (itemStack != null && isHandler(itemStack.getItem())) + { + return energyHandlerCache.get(itemStack.getItem().getClass()).doGetItemWithCharge(itemStack, energy); + } + + return null; + } + + /** + * Is this object a valid energy handler? + * + * @param True if the handler can store energy. This can be for items and blocks. + */ + public static boolean isHandler(Object handler) + { + if (handler != null) + { + Class clazz = handler.getClass(); + + if (energyHandlerCache.containsKey(clazz)) + { + return true; + } + + for (CompatibilityModule module : CompatibilityModule.loadedModules) + { + if (module.doIsHandler(handler)) + { + energyHandlerCache.put(clazz, module); + return true; + } + } + } + + return false; + } + + /** + * Is this object able to store energy? + * + * @param handler + * @return True if the handler can store energy. The handler MUST be a block. + */ + public static boolean isEnergyContainer(Object handler) + { + if (handler != null) + { + Class clazz = handler.getClass(); + + if (energyStorageCache.containsKey(clazz)) + { + return true; + } + + for (CompatibilityModule module : CompatibilityModule.loadedModules) + { + if (module.doIsEnergyContainer(handler)) + { + energyStorageCache.put(clazz, module); + return true; + } + } + } + + return false; + } + + /** + * Blocks only + */ + public static double getMaxEnergy(Object handler, ForgeDirection direction) + { + if (isEnergyContainer(handler)) + { + return energyStorageCache.get(handler.getClass()).doGetMaxEnergy(handler, direction); + } + + return 0; + } + + public static double getVoltage(Object handler) + { + if (isHandler(handler)) + { + return energyHandlerCache.get(handler.getClass()).doGetVoltage(handler); + } + + return 0; + } + + public static boolean canReceive(Object handler, ForgeDirection side) + { + if (isHandler(handler)) + { + return energyHandlerCache.get(handler.getClass()).doCanReceive(handler, side); + } + + return false; + } + + public static boolean canExtract(Object handler, ForgeDirection side) + { + if (isHandler(handler)) + { + return energyHandlerCache.get(handler.getClass()).doCanExtract(handler, side); + } + + return false; + } + + public static double getDemandedJoules(Object handler) + { + if (isHandler(handler)) + { + return energyHandlerCache.get(handler.getClass()).doGetDemandedJoules(handler); + } + + return 0; + } + + public static double getProvidedJoules(Object handler) + { + if (isHandler(handler)) + { + return energyHandlerCache.get(handler.getClass()).doGetProvidedJoules(handler); + } + + return 0; + } + + public static double getEnergyItem(ItemStack itemStack) + { + if (itemStack != null && isHandler(itemStack.getItem())) + { + return energyHandlerCache.get(itemStack.getItem().getClass()).doGetEnergyItem(itemStack); + } + + return 0; + } + + public static double getMaxEnergyItem(ItemStack itemStack) + { + if (itemStack != null && isHandler(itemStack.getItem())) + { + return energyHandlerCache.get(itemStack.getItem().getClass()).doGetMaxEnergyItem(itemStack); + } + + return 0; + } + + public abstract double doReceiveEnergy(Object handler, ForgeDirection direction, double energy, boolean doReceive); + + public abstract double doExtractEnergy(Object handler, ForgeDirection direction, double energy, boolean doExtract); + + /** + * Charges an item with the given energy + * + * @param itemStack - item stack that is the item + * @param joules - input energy + * @param docharge - do the action + * @return amount of energy accepted + */ + public abstract double doChargeItem(ItemStack itemStack, double joules, boolean docharge); + + /** + * discharges an item with the given energy + * + * @param itemStack - item stack that is the item + * @param joules - input energy + * @param docharge - do the action + * @return amount of energy that was removed + */ + public abstract double doDischargeItem(ItemStack itemStack, double joules, boolean doDischarge); + + public abstract boolean doIsHandler(Object obj); + + public abstract boolean doIsEnergyContainer(Object obj); + + public abstract double doGetEnergy(Object obj, ForgeDirection direction); + + public abstract boolean doCanConnect(Object obj, ForgeDirection direction, Object source); + + public abstract ItemStack doGetItemWithCharge(ItemStack itemStack, double energy); + + public abstract double doGetMaxEnergy(Object handler, ForgeDirection direction); + + public abstract double doGetEnergyItem(ItemStack is); + + public abstract double doGetMaxEnergyItem(ItemStack is); + + public abstract double doGetVoltage(Object handler); + + public abstract boolean doCanReceive(Object handler, ForgeDirection side); + + public abstract boolean doCanExtract(Object handler, ForgeDirection side); + + public abstract double doGetDemandedJoules(Object handler); + + public abstract double doGetProvidedJoules(Object handler); +} diff --git a/src/main/java/universalelectricity/api/CompatibilityType.java b/src/main/java/universalelectricity/api/CompatibilityType.java new file mode 100644 index 0000000..51a9a5a --- /dev/null +++ b/src/main/java/universalelectricity/api/CompatibilityType.java @@ -0,0 +1,73 @@ +package universalelectricity.api; + +import cpw.mods.fml.common.Loader; + +/** + * ORDER OF MAGNITUDE: + * A coal in Universal Electricity (based on an estimate in real life) is worth 4MJ. + * A fission reactor should make around 4-9GW. + * A fusion reactor would go into the tera-watts. + * Reika's conversion: IC2[22512], BC[56280], RF[5628] + * @author Calclavia + */ +public enum CompatibilityType +{ + REDSTONE_FLUX("universalelectricity", "RedstoneFlux", "Redstone Flux", "RF", 2.5), + INDUSTRIALCRAFT("IC2", "IndustrialCraft", "Electrical Unit", "EU", 10); + + public final String modID; + public final String moduleName; + public final String fullUnit; + public final String unit; + + /** + * Multiply UE energy by this ratio to convert it to the forgien ratio. + */ + public double ratio; + + /** + * Multiply the forgien energy by this ratio to convert it into UE energy. + */ + public double reciprocal_ratio; + + /** + * The Universal Electricity Loader will change this value to indicate if the module is + * loaded or not. + */ + public boolean isModuleEnabled; + + /** + * @param modID - The Forge mod ID. + * @param moduleName - The name of the module, used for config and ASM + * @param fullUnit - The unit used + * @param unit - The unit short form used + * @param ratio - How much UE energy equates to the forgien energy? + */ + CompatibilityType(String modID, String moduleName, String fullUnit, String unit, double ratio) + { + this.modID = modID; + this.moduleName = moduleName; + this.fullUnit = fullUnit; + this.unit = unit; + this.ratio = 1.0 / ratio; + this.reciprocal_ratio = ratio; + } + + public boolean isLoaded() + { + return Loader.isModLoaded(this.modID); + } + + public static CompatibilityType get(String moduleName) + { + for (CompatibilityType type : values()) + { + if (moduleName.equals(type.moduleName)) + { + return type; + } + } + + return null; + } +} diff --git a/src/main/java/universalelectricity/api/electricity/IElectricalNetwork.java b/src/main/java/universalelectricity/api/electricity/IElectricalNetwork.java new file mode 100644 index 0000000..62a27b5 --- /dev/null +++ b/src/main/java/universalelectricity/api/electricity/IElectricalNetwork.java @@ -0,0 +1,18 @@ +package universalelectricity.api.electricity; + +import net.minecraft.tileentity.TileEntity; +import universalelectricity.api.energy.IEnergyNetwork; +import universalelectricity.core.electricity.ElectricityPack; + +/** + * Extended version of the energy network that properly implements amperage and voltage. + * If you want amps get last buffer and divide it by the voltage + * + * @author DarkGuardsman + */ +public interface IElectricalNetwork extends IEnergyNetwork +{ + /** Gets the current voltage of the network at this point. */ + public double getVoltage(); + +} diff --git a/src/main/java/universalelectricity/api/electricity/IVoltageInput.java b/src/main/java/universalelectricity/api/electricity/IVoltageInput.java new file mode 100644 index 0000000..3cd7cab --- /dev/null +++ b/src/main/java/universalelectricity/api/electricity/IVoltageInput.java @@ -0,0 +1,18 @@ +package universalelectricity.api.electricity; + +import net.minecraftforge.common.util.ForgeDirection; + +/** + * Implement this on your TileEntity if it has a voltage based energy input. You machine will still + * need to run its own voltage checks as this doesn't do anything on its own. + * + * @author DarkGuardsman + */ +public interface IVoltageInput +{ + /** Voltage input on the side mainly used for meters */ + public double getVoltageInput(ForgeDirection direction); + + /** Called when the network voltage doesn't equal the input */ + public void onWrongVoltage(ForgeDirection direction, double voltage); +} diff --git a/src/main/java/universalelectricity/api/electricity/IVoltageOutput.java b/src/main/java/universalelectricity/api/electricity/IVoltageOutput.java new file mode 100644 index 0000000..6f36b76 --- /dev/null +++ b/src/main/java/universalelectricity/api/electricity/IVoltageOutput.java @@ -0,0 +1,20 @@ +package universalelectricity.api.electricity; + +import net.minecraftforge.common.util.ForgeDirection; + +/** + * Applied to electrical machines that are designed to act as sources of power in an electrical + * network. Mainly used to calculate the over all voltage of a network correctly. + * + * @author DarkGuardsman + */ +public interface IVoltageOutput +{ + /** + * Can this machine emit voltage on the given side. + * + * @param side - side that the voltage will be emitted on + * @return the voltage emitted + */ + public double getVoltageOutput(ForgeDirection side); +} diff --git a/src/main/java/universalelectricity/api/energy/EnergyNetworkLoader.java b/src/main/java/universalelectricity/api/energy/EnergyNetworkLoader.java new file mode 100644 index 0000000..ef067d4 --- /dev/null +++ b/src/main/java/universalelectricity/api/energy/EnergyNetworkLoader.java @@ -0,0 +1,69 @@ +package universalelectricity.api.energy; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import cpw.mods.fml.common.FMLLog; + +/** + * A dynamic network loader for injecting energy networks (NOT for other networks such as fuild + * networks). + * Example usage would be that ElectricityNetwork replaces EnergyNetwork. + * + * @author Calclavia + * + */ +public class EnergyNetworkLoader +{ + /** + * The default IElectricityNetwork used for primary energy networks. + */ + public static Class NETWORK_CLASS; + public static final Set> NETWORK_CLASS_REGISTRY = new HashSet>(); + + static + { + setNetworkClass("universalelectricity.core.net.ElectricalNetwork"); + } + + public static void setNetworkClass(Class networkClass) + { + NETWORK_CLASS_REGISTRY.add(networkClass); + NETWORK_CLASS = networkClass; + } + + public static void setNetworkClass(String className) + { + try + { + setNetworkClass((Class) Class.forName(className)); + } + catch (Exception e) + { + FMLLog.severe("Universal Electricity: Failed to set network class with name " + className); + e.printStackTrace(); + } + } + + public static IEnergyNetwork getNewNetwork(IConductor... conductors) + { + try + { + IEnergyNetwork network = NETWORK_CLASS.newInstance(); + network.getConnectors().addAll(Arrays.asList(conductors)); + return network; + } + catch (InstantiationException e) + { + e.printStackTrace(); + } + catch (IllegalAccessException e) + { + e.printStackTrace(); + } + + return null; + } + +} \ No newline at end of file diff --git a/src/main/java/universalelectricity/api/energy/EnergyStorageHandler.java b/src/main/java/universalelectricity/api/energy/EnergyStorageHandler.java new file mode 100644 index 0000000..f2153cd --- /dev/null +++ b/src/main/java/universalelectricity/api/energy/EnergyStorageHandler.java @@ -0,0 +1,268 @@ +package universalelectricity.api.energy; + +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagDouble; +import net.minecraft.nbt.NBTTagFloat; +import net.minecraft.nbt.NBTTagInt; +import net.minecraft.nbt.NBTTagLong; + +/** + * Can be used internally for IEnergyInterface blocks. This is optional and should be used for + * ease of use purposes. + * + * @author Calclavia, Based on Thermal Expansion + * + */ +public class EnergyStorageHandler +{ + protected long energy; + protected long capacity; + protected long maxReceive; + protected long maxExtract; + + /** + * A cache of the last energy stored through extract and receive. + */ + protected long lastEnergy; + + public EnergyStorageHandler() + { + this(0); + } + + public EnergyStorageHandler(long capacity) + { + this(capacity, capacity, capacity); + } + + public EnergyStorageHandler(long capacity, long maxTransfer) + { + this(capacity, maxTransfer, maxTransfer); + } + + public EnergyStorageHandler(long capacity, long maxReceive, long maxExtract) + { + this.capacity = capacity; + this.maxReceive = maxReceive; + this.maxExtract = maxExtract; + } + + public EnergyStorageHandler readFromNBT(NBTTagCompound nbt) + { + NBTBase energyTag = nbt.getTag("energy"); + if (energyTag instanceof NBTTagDouble) + { + this.energy = (long) ((NBTTagDouble) energyTag).func_150286_g(); + } + else if (energyTag instanceof NBTTagFloat) + { + this.energy = (long) ((NBTTagFloat) energyTag).func_150288_h(); + } + else if (energyTag instanceof NBTTagInt) + { + this.energy = ((NBTTagInt) energyTag).func_150287_d(); + } + else if (energyTag instanceof NBTTagLong) + { + this.energy = ((NBTTagLong) energyTag).func_150291_c(); + } + return this; + } + + public NBTTagCompound writeToNBT(NBTTagCompound nbt) + { + nbt.setLong("energy", this.getEnergy()); + return nbt; + } + + public void setCapacity(long capacity) + { + this.capacity = capacity; + + if (getEnergy() > capacity) + { + energy = capacity; + } + } + + public void setMaxTransfer(long maxTransfer) + { + setMaxReceive(maxTransfer); + setMaxExtract(maxTransfer); + } + + public void setMaxReceive(long maxReceive) + { + this.maxReceive = maxReceive; + } + + public void setMaxExtract(long maxExtract) + { + this.maxExtract = maxExtract; + } + + public long getMaxReceive() + { + return maxReceive; + } + + public long getMaxExtract() + { + return maxExtract; + } + + /** + * This function is included to allow for server -> client sync. Do not call this externally to + * the containing Tile Entity, as not all IEnergyHandlers are + * guaranteed to have it. + * + * @param energy + */ + public void setEnergy(long energy) + { + this.energy = energy; + + if (this.getEnergy() > this.getEnergyCapacity()) + { + this.energy = this.getEnergyCapacity(); + } + else if (this.getEnergy() < 0) + { + this.energy = 0; + } + } + + /** + * This function is included to allow the containing tile to directly and efficiently modify the + * energy contained in the EnergyStorage. Do not rely on this + * externally, as not all IEnergyHandlers are guaranteed to have it. + * + * @param energy + */ + public void modifyEnergyStored(long energy) + { + this.setEnergy(this.getEmptySpace() + energy); + + if (this.getEnergy() > this.getEnergyCapacity()) + { + this.setEnergy(this.getEnergyCapacity()); + } + else if (this.getEnergy() < 0) + { + this.setEnergy(0); + } + } + + public long receiveEnergy(long receive, boolean doReceive) + { + long energyReceived = Math.min(this.getEnergyCapacity() - this.getEnergy(), Math.min(this.getMaxReceive(), receive)); + + if (doReceive) + { + this.lastEnergy = this.getEnergy(); + this.setEnergy(this.getEnergy() + energyReceived); + } + return energyReceived; + } + + public long receiveEnergy(boolean doReceive) + { + return this.receiveEnergy(this.getMaxReceive(), doReceive); + } + + public long receiveEnergy() + { + return this.receiveEnergy(true); + } + + public long extractEnergy(long extract, boolean doExtract) + { + long energyExtracted = Math.min(this.getEnergy(), Math.min(this.getMaxExtract(), extract)); + + if (doExtract) + { + this.lastEnergy = this.getEnergy(); + this.setEnergy(this.getEnergy() - energyExtracted); + } + return energyExtracted; + } + + public long extractEnergy(boolean doExtract) + { + return this.extractEnergy(this.getMaxExtract(), doExtract); + } + + public long extractEnergy() + { + return this.extractEnergy(true); + } + + public boolean checkReceive(long receive) + { + return this.receiveEnergy(receive, false) >= receive; + } + + public boolean checkReceive() + { + return this.checkReceive(this.getMaxReceive()); + } + + public boolean checkExtract(long extract) + { + return this.extractEnergy(extract, false) >= extract; + } + + public boolean checkExtract() + { + return this.checkExtract(this.getMaxExtract()); + } + + public boolean isFull() + { + return this.getEnergy() >= this.getEnergyCapacity(); + } + + public boolean isEmpty() + { + return this.getEnergy() == 0; + } + + public long getLastEnergy() + { + return this.lastEnergy; + } + + /** + * @return True if the last energy state and the current one are either in an + * "empty or not empty" change state. + */ + public boolean didEnergyStateChange() + { + return (this.getLastEnergy() == 0 && this.getEnergy() > 0) || (this.getLastEnergy() > 0 && this.getEnergy() == 0); + } + + /** + * Returns the amount of energy this storage can further store. + */ + public long getEmptySpace() + { + return this.getEnergyCapacity() - this.getEnergy(); + } + + public long getEnergy() + { + return this.energy; + } + + public long getEnergyCapacity() + { + return this.capacity; + } + + @Override + public String toString() + { + return this.getClass().getSimpleName() + "[" + this.getEnergy() + "/" + this.getEnergyCapacity() + "]"; + } +} diff --git a/src/main/java/universalelectricity/api/energy/IConductor.java b/src/main/java/universalelectricity/api/energy/IConductor.java new file mode 100644 index 0000000..b9dfafb --- /dev/null +++ b/src/main/java/universalelectricity/api/energy/IConductor.java @@ -0,0 +1,27 @@ +package universalelectricity.api.energy; + +import universalelectricity.api.net.IConnector; + +/** + * A connector for {EnergyNetwork}. + * + * @author Calclavia + */ +public interface IConductor extends IConnector, IEnergyInterface +{ + /** + * Gets the amount of resistance of energy conducting pass this conductor. + * + * @return The amount of loss in Ohms. + */ + public double getResistance(); + + /** + * The maximum amount of current this conductor can buffer (the transfer rate, essentially). You + * can simply do divide your energy transfer rate by UniversalElectricity.DEFAULT_VOLTAGE if + * your conductor is not voltage sensitive. + * + * @return The amount of current in amperes. + */ + public double getCurrentCapacity(); +} diff --git a/src/main/java/universalelectricity/api/energy/IEnergyContainer.java b/src/main/java/universalelectricity/api/energy/IEnergyContainer.java new file mode 100644 index 0000000..cef443c --- /dev/null +++ b/src/main/java/universalelectricity/api/energy/IEnergyContainer.java @@ -0,0 +1,28 @@ +package universalelectricity.api.energy; + +import net.minecraftforge.common.util.ForgeDirection; + +/** + * This interface is to be applied to all TileEntities which can store energy. + * + * @author Calclavia + */ +public interface IEnergyContainer +{ + /** + * Sets the amount of energy this unit stored. + * + * This function is NOT recommended for calling. + */ + public void setEnergy(ForgeDirection from, long energy); + + /** + * * @return Get the amount of energy currently stored in the block. + */ + public long getEnergy(ForgeDirection from); + + /** + * @return Get the max amount of energy that can be stored in the block. + */ + public long getEnergyCapacity(ForgeDirection from); +} diff --git a/src/main/java/universalelectricity/api/energy/IEnergyInterface.java b/src/main/java/universalelectricity/api/energy/IEnergyInterface.java new file mode 100644 index 0000000..3755be0 --- /dev/null +++ b/src/main/java/universalelectricity/api/energy/IEnergyInterface.java @@ -0,0 +1,37 @@ +package universalelectricity.api.energy; + +import net.minecraftforge.common.util.ForgeDirection; +import universalelectricity.api.net.IConnectable; + +/** + * Applied to all TileEntities that can interact with energy. + * + * @author Calclavia, Inspired by Thermal Expansion + */ +public interface IEnergyInterface extends IConnectable +{ + /** + * Adds energy to a block. Returns the quantity of energy that was accepted. This should always + * return 0 if the block cannot be externally charged. + * + * @param from Orientation the energy is sent in from. + * @param receive Maximum amount of energy (joules) to be sent into the block. + * @param doReceive If false, the charge will only be simulated. + * @return Amount of energy that was accepted by the block. + */ + public double onReceiveEnergy(ForgeDirection from, double receive, boolean doReceive); + + /** + * Removes energy from a block. Returns the quantity of energy that was extracted. This should + * always return 0 if the block cannot be externally discharged. + * + * @param from Orientation the energy is requested from. This direction MAY be passed as + * "Unknown" if it is wrapped from another energy system that has no clear way to find + * direction. (e.g BuildCraft 4) + * @param energy Maximum amount of energy to be sent into the block. + * @param doExtract If false, the charge will only be simulated. + * @return Amount of energy that was given out by the block. + */ + public double onExtractEnergy(ForgeDirection from, double extract, boolean doExtract); + +} diff --git a/src/main/java/universalelectricity/api/energy/IEnergyNetwork.java b/src/main/java/universalelectricity/api/energy/IEnergyNetwork.java new file mode 100644 index 0000000..2cb3e1b --- /dev/null +++ b/src/main/java/universalelectricity/api/energy/IEnergyNetwork.java @@ -0,0 +1,67 @@ +package universalelectricity.api.energy; + +import net.minecraftforge.common.util.ForgeDirection; +import universalelectricity.api.net.INodeNetwork; +import universalelectricity.api.net.IUpdate; + +/** + * The Energy Network for energy items and blocks. + * + * @author Calclavia + */ +public interface IEnergyNetwork extends INodeNetwork, IUpdate +{ + /** + * Produces power to the energy network. + * + * @param conductor - The conductor that is producing into the energy. + * @param side - The direction the source is producing out towards. + * @param receive - The amount that is produced. + * @return The amount that was accepted by the network. + */ + public double produce(IConductor conductor, ForgeDirection from, double amount, boolean doProduce); + + /** + * @return The current buffer in the network that is going sent to all energy handlers. + */ + public double getBuffer(); + + /** + * @return The last buffer in the network that was sent to all energy handlers. + */ + public double getLastBuffer(); + + /** + * Gets an estimated value of what the network wants for energy + */ + public double getRequest(); + + /** + * Gets a value that represents the amount of energy lost in the network + */ + public double getResistance(); + + /** + * Used by conductors to load their internal buffers to the network. This should be called when + * reading NBT data. + * + * @param conductor + */ + public double getBufferOf(IConductor conductor); + + /** + * Used by conductors to load their internal buffers to the network. This should be called when + * writing NBT data. + * + * @param conductor + */ + public void setBufferFor(IConductor conductor, double buffer); + + /** + * Sets the buffer of the network. + * + * @param newBuffer + */ + public void setBuffer(double newBuffer); + +} diff --git a/src/main/java/universalelectricity/api/energy/UnitDisplay.java b/src/main/java/universalelectricity/api/energy/UnitDisplay.java new file mode 100644 index 0000000..218c39d --- /dev/null +++ b/src/main/java/universalelectricity/api/energy/UnitDisplay.java @@ -0,0 +1,217 @@ +package universalelectricity.api.energy; + +/** + * An easy way to display information on electricity for the client. + * + * @author Calclavia + */ +public class UnitDisplay +{ + /** + * Universal Electricity's units are in KILOJOULES, KILOWATTS and KILOVOLTS. Try to make your + * energy ratio as close to real life as possible. + */ + public static enum Unit + { + AMPERE("Amp", "I"), AMP_HOUR("Amp Hour", "Ah"), VOLTAGE("Volt", "V"), WATT("Watt", "W"), + WATT_HOUR("Watt Hour", "Wh"), RESISTANCE("Ohm", "R"), CONDUCTANCE("Siemen", "S"), + JOULES("Joule", "J"), LITER("Liter", "L"), NEWTON_METER("Newton Meter", "Nm"), + REDFLUX("Redstone-Flux", "Rf"), ELECTRICAL_UNITS("Electrical-Units", "Eu"); + + public String name; + public String symbol; + + private Unit(String name, String symbol) + { + this.name = name; + this.symbol = symbol; + } + + public String getPlural() + { + return this.name + "s"; + } + } + + /** Metric system of measurement. */ + public static enum UnitPrefix + { + MICRO("Micro", "u", 0.000001), MILLI("Milli", "m", 0.001), BASE("", "", 1), + KILO("Kilo", "k", 1000), MEGA("Mega", "M", 1000000), GIGA("Giga", "G", 1000000000), + TERA("Tera", "T", 1000000000000d), PETA("Peta", "P", 1000000000000000d), + EXA("Exa", "E", 1000000000000000000d), ZETTA("Zetta", "Z", 1000000000000000000000d), + YOTTA("Yotta", "Y", 1000000000000000000000000d); + + /** long name for the unit */ + public String name; + /** short unit version of the unit */ + public String symbol; + /** Point by which a number is consider to be of this unit */ + public double value; + + private UnitPrefix(String name, String symbol, double value) + { + this.name = name; + this.symbol = symbol; + this.value = value; + } + + public String getName(boolean getShort) + { + if (getShort) + { + return symbol; + } + else + { + return name; + } + } + + /** Divides the value by the unit value start */ + public double process(double value) + { + return value / this.value; + } + + /** Checks if a value is above the unit value start */ + public boolean isAbove(double value) + { + return value > this.value; + } + + /** Checks if a value is lower than the unit value start */ + public boolean isBellow(double value) + { + return value < this.value; + } + } + + public static String getDisplay(double value, Unit unit, int decimalPlaces, boolean isShort) + { + return getDisplay(value, unit, decimalPlaces, isShort, 1); + } + + /** + * Displays the unit as text. Does handle negative numbers, and will place a negative sign in + * front of the output string showing this. Use string.replace to remove the negative sign if + * unwanted + */ + public static String getDisplay(double value, Unit unit, int decimalPlaces, boolean isShort, double multiplier) + { + String unitName = unit.name; + String prefix = ""; + + if (value < 0) + { + value = Math.abs(value); + prefix = "-"; + } + + value *= multiplier; + + if (isShort) + { + unitName = unit.symbol; + } + else if (value > 1) + { + unitName = unit.getPlural(); + } + + if (value == 0) + { + return value + " " + unitName; + } + else + { + for (int i = 0; i < UnitPrefix.values().length; i++) + { + UnitPrefix lowerMeasure = UnitPrefix.values()[i]; + + if (lowerMeasure.isBellow(value) && lowerMeasure.ordinal() == 0) + { + return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(isShort) + unitName; + } + if (lowerMeasure.ordinal() + 1 >= UnitPrefix.values().length) + { + return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(isShort) + unitName; + } + + UnitPrefix upperMeasure = UnitPrefix.values()[i + 1]; + + if ((lowerMeasure.isAbove(value) && upperMeasure.isBellow(value)) || lowerMeasure.value == value) + { + return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(isShort) + unitName; + } + } + } + + return prefix + roundDecimals(value, decimalPlaces) + " " + unitName; + } + + public static String getDisplay(double value, Unit unit) + { + return getDisplay(value, unit, 2, false); + } + + public static String getDisplay(double value, Unit unit, UnitPrefix prefix) + { + return getDisplay(value, unit, 2, false, prefix.value); + } + + public static String getDisplayShort(double value, Unit unit) + { + return getDisplay(value, unit, 2, true); + } + + /** + * Gets a display for the value with a unit that is in the specific prefix. + */ + public static String getDisplayShort(double value, Unit unit, UnitPrefix prefix) + { + return getDisplay(value, unit, 2, true, prefix.value); + } + + public static String getDisplayShort(double value, Unit unit, int decimalPlaces) + { + return getDisplay(value, unit, decimalPlaces, true); + } + + public static String getDisplaySimple(double value, Unit unit, int decimalPlaces) + { + if (value > 1) + { + if (decimalPlaces < 1) + { + return (int) value + " " + unit.getPlural(); + } + + return roundDecimals(value, decimalPlaces) + " " + unit.getPlural(); + } + + if (decimalPlaces < 1) + { + return (int) value + " " + unit.name; + } + + return roundDecimals(value, decimalPlaces) + " " + unit.name; + } + + /** + * Rounds a number to a specific number place places + * + * @param The number + * @return The rounded number + */ + public static double roundDecimals(double d, int decimalPlaces) + { + int j = (int) (d * Math.pow(10, decimalPlaces)); + return j / Math.pow(10, decimalPlaces); + } + + public static double roundDecimals(double d) + { + return roundDecimals(d, 2); + } +} diff --git a/src/main/java/universalelectricity/api/item/IEnergyItem.java b/src/main/java/universalelectricity/api/item/IEnergyItem.java new file mode 100644 index 0000000..af6c07b --- /dev/null +++ b/src/main/java/universalelectricity/api/item/IEnergyItem.java @@ -0,0 +1,47 @@ +package universalelectricity.api.item; + +import net.minecraft.item.ItemStack; + +public interface IEnergyItem +{ + /** + * Adds energy to an item. Returns the quantity of energy that was accepted. This should always + * return 0 if the item cannot be externally charged. + * + * @param itemStack ItemStack to be charged. + * @param energy Maximum amount of energy to be sent into the item. + * @param doRecharge If false, the charge will only be simulated. + * @return Amount of energy that was accepted by the item. + */ + public double recharge(ItemStack itemStack, double energy, boolean doRecharge); + + /** + * Removes energy from an item. Returns the quantity of energy that was removed. This should + * always return 0 if the item cannot be externally discharged. + * + * @param itemStack ItemStack to be discharged. + * @param energy Maximum amount of energy to be removed from the item. + * @param doDischarge If false, the discharge will only be simulated. + * @return Amount of energy that was removed from the item. + */ + public double discharge(ItemStack itemStack, double energy, boolean doDischarge); + + /** + * Get the amount of energy currently stored in the item. + */ + public double getEnergy(ItemStack theItem); + + /** + * Get the max amount of energy that can be stored in the item. + */ + public double getEnergyCapacity(ItemStack theItem); + + /** + * Sets the amount of energy in the ItemStack. Use recharge or discharge instead of calling this + * to be safer! + * + * @param itemStack - the ItemStack. + * @param energy - Amount of electrical energy. + */ + public void setEnergy(ItemStack itemStack, double energy); +} diff --git a/src/main/java/universalelectricity/api/item/IVoltageItem.java b/src/main/java/universalelectricity/api/item/IVoltageItem.java new file mode 100644 index 0000000..3fc2994 --- /dev/null +++ b/src/main/java/universalelectricity/api/item/IVoltageItem.java @@ -0,0 +1,15 @@ +package universalelectricity.api.item; + +import net.minecraft.item.ItemStack; + +/** + * @author Calclavia + * + */ +public interface IVoltageItem +{ + /** + * Get the max amount of voltage of this item. + */ + public double getVoltage(ItemStack theItem); +} diff --git a/src/main/java/universalelectricity/api/item/ItemElectric.java b/src/main/java/universalelectricity/api/item/ItemElectric.java new file mode 100644 index 0000000..94cc028 --- /dev/null +++ b/src/main/java/universalelectricity/api/item/ItemElectric.java @@ -0,0 +1,155 @@ +package universalelectricity.api.item; + +import java.util.List; + +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagFloat; +import net.minecraft.nbt.NBTTagDouble; +import net.minecraft.world.World; +import universalelectricity.api.CompatibilityModule; +import universalelectricity.core.UniversalElectricity; +import universalelectricity.api.energy.UnitDisplay; +import universalelectricity.api.energy.UnitDisplay.Unit; + +/** Extend from this class if your item requires electricity or to be charged. Optionally, you can + * implement IItemElectric instead. + * + * @author Calclavia */ +public abstract class ItemElectric extends Item implements IEnergyItem, IVoltageItem +{ + private static final String ENERGY_NBT = "electricity"; + + public ItemElectric() + { + super(); + setMaxStackSize(1); + setMaxDamage(100); + setNoRepair(); + } + + @Override + public void addInformation(ItemStack itemStack, EntityPlayer entityPlayer, List list, boolean par4) + { + String color = ""; + double joules = getEnergy(itemStack); + + if (joules <= getEnergyCapacity(itemStack) / 3) + { + color = "\u00a74"; + } + else if (joules > getEnergyCapacity(itemStack) * 2 / 3) + { + color = "\u00a72"; + } + else + { + color = "\u00a76"; + } + + list.add(color + UnitDisplay.getDisplayShort(joules, Unit.JOULES) + "/" + UnitDisplay.getDisplayShort(getEnergyCapacity(itemStack), Unit.JOULES)); + } + + /** Makes sure the item is uncharged when it is crafted and not charged. Change this if you do + * not want this to happen! */ + @Override + public void onCreated(ItemStack itemStack, World par2World, EntityPlayer par3EntityPlayer) + { + setEnergy(itemStack, 0); + } + + @Override + public double recharge(ItemStack itemStack, double energy, boolean doReceive) + { + double energyReceived = Math.min(getEnergyCapacity(itemStack) - getEnergy(itemStack), Math.min(getTransferRate(itemStack), energy)); + + if (doReceive) + { + setEnergy(itemStack, getEnergy(itemStack) + energyReceived); + } + + return energyReceived; + } + + public double getTransferRate(ItemStack itemStack) + { + return getEnergyCapacity(itemStack) / 100; + } + + @Override + public double discharge(ItemStack itemStack, double energy, boolean doTransfer) + { + double energyExtracted = Math.min(getEnergy(itemStack), Math.min(getTransferRate(itemStack), energy)); + + if (doTransfer) + { + setEnergy(itemStack, getEnergy(itemStack) - energyExtracted); + } + + return energyExtracted; + } + + @Override + public double getVoltage(ItemStack itemStack) + { + return 120.0; + } + + @Override + public void setEnergy(ItemStack itemStack, double joules) + { + if (itemStack.getTagCompound() == null) + { + itemStack.setTagCompound(new NBTTagCompound()); + } + + double electricityStored = Math.max(Math.min(joules, getEnergyCapacity(itemStack)), 0); + itemStack.getTagCompound().setDouble(ENERGY_NBT, electricityStored); + itemStack.setItemDamage((int) (100 - ((double) electricityStored / (double) getEnergyCapacity(itemStack)) * 100)); + } + + public double getTransfer(ItemStack itemStack) + { + return getEnergyCapacity(itemStack) - getEnergy(itemStack); + } + + @Override + public double getEnergy(ItemStack itemStack) + { + if (itemStack.getTagCompound() == null) + { + itemStack.setTagCompound(new NBTTagCompound()); + } + + double energyStored = 0; + + if (itemStack.getTagCompound().hasKey(ENERGY_NBT)) + { + // Backwards compatibility + NBTBase obj = itemStack.getTagCompound().getTag(ENERGY_NBT); + + if (obj instanceof NBTTagFloat) + { + energyStored = ((NBTTagFloat) obj).func_150286_g(); + } + else if (obj instanceof NBTTagDouble) + { + energyStored = ((NBTTagDouble) obj).func_150286_g(); + } + } + + itemStack.setItemDamage((int) (100 - ((double) energyStored / (double) getEnergyCapacity(itemStack)) * 100)); + return energyStored; + } + + @Override + public void getSubItems(Item par1, CreativeTabs par2CreativeTabs, List par3List) + { + par3List.add(CompatibilityModule.getItemWithCharge(new ItemStack(this), 0)); + par3List.add(CompatibilityModule.getItemWithCharge(new ItemStack(this), getEnergyCapacity(new ItemStack(this)))); + } +} diff --git a/src/main/java/universalelectricity/api/net/IConnectable.java b/src/main/java/universalelectricity/api/net/IConnectable.java new file mode 100644 index 0000000..fe717fb --- /dev/null +++ b/src/main/java/universalelectricity/api/net/IConnectable.java @@ -0,0 +1,19 @@ +package universalelectricity.api.net; + +import net.minecraftforge.common.util.ForgeDirection; + +/** + * @author Calclavia + * + */ +public interface IConnectable +{ + /** + * Can this object connect with another? + * + * @param from - The direction the connection is coming from. + * @param source - The source calling canConnect onto this object. + * @return Return true, if the connection is possible. + */ + public boolean canConnect(ForgeDirection from, Object source); +} diff --git a/src/main/java/universalelectricity/api/net/IConnector.java b/src/main/java/universalelectricity/api/net/IConnector.java new file mode 100644 index 0000000..ac0a41e --- /dev/null +++ b/src/main/java/universalelectricity/api/net/IConnector.java @@ -0,0 +1,29 @@ +package universalelectricity.api.net; + +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.util.ForgeDirection; + +/** + * Applied to TileEntities that has an instance of an electricity network. + * + * @author Calclavia, tilera + * + */ +public interface IConnector extends INetworkProvider, IConnectable +{ + /** + * Gets an array of all the connected IConnecables that this connector is connected to. This + * should correspond to the ForgeDirection index. + * + * @return An array of length "6". + */ + public TileEntity[] getAdjacentConnections(); + + /** + * Gets this connector instance. Used specially for MultiPart connections. + * + * @return The instance, in most cases, just return "this". + */ + public IConnector getInstance(ForgeDirection dir); + +} diff --git a/src/main/java/universalelectricity/api/net/INetwork.java b/src/main/java/universalelectricity/api/net/INetwork.java new file mode 100644 index 0000000..f360be6 --- /dev/null +++ b/src/main/java/universalelectricity/api/net/INetwork.java @@ -0,0 +1,50 @@ +package universalelectricity.api.net; + +import java.util.Set; + +/** + * A network with connectors only. + */ +public interface INetwork +{ + public void addConnector(C connector); + + public void removeConnector(C connector); + + /** + * Gets the set of conductors that make up this network. + * + * @return conductor set + */ + public Set getConnectors(); + + /** + * Reconstructs the network and all objects within it. + */ + public void reconstruct(); + + /** + * Creates a new network that makes up the current network and the network defined in the + * parameters. Be sure to refresh the new network inside this method. + * + * @param network - network to merge + * @return The new network instance. + */ + public N merge(N network); + + /** + * Splits a network by removing a conductor referenced in the parameter. It will then create and + * refresh the new independent networks possibly created by this operation. + * + * @param connection + */ + public void split(C connection); + + /** + * Splits the network between 2 connectors, separating their networks. + * + * @param connectorA + * @param connectorB + */ + public void split(C connectorA, C connectorB); +} diff --git a/src/main/java/universalelectricity/api/net/INetworkProvider.java b/src/main/java/universalelectricity/api/net/INetworkProvider.java new file mode 100644 index 0000000..003b85b --- /dev/null +++ b/src/main/java/universalelectricity/api/net/INetworkProvider.java @@ -0,0 +1,14 @@ +package universalelectricity.api.net; + +/** + * Applied to TileEntities that has an instance of an electricity network. + * + * @author Calclavia + * + */ +public interface INetworkProvider +{ + public N getNetwork(); + + public void setNetwork(N network); +} diff --git a/src/main/java/universalelectricity/api/net/INodeNetwork.java b/src/main/java/universalelectricity/api/net/INodeNetwork.java new file mode 100644 index 0000000..fbe1b23 --- /dev/null +++ b/src/main/java/universalelectricity/api/net/INodeNetwork.java @@ -0,0 +1,22 @@ +package universalelectricity.api.net; + +import java.util.Set; + +/** + * A network of with connectors and individual nodes. + * + * @author Calclavia + * + * @param - the class/interface Type value in which you implement this + * @param - the class/interface Type which makes up the network's connector set + * @param - the class/interface Type which makes up the network's node set + */ +public interface INodeNetwork extends INetwork +{ + /** + * The nodes in a network are the objects that interact with the connectors. + * + * @return The list of nodes in the network. + */ + public Set getNodes(); +} diff --git a/src/main/java/universalelectricity/api/net/IUpdate.java b/src/main/java/universalelectricity/api/net/IUpdate.java new file mode 100644 index 0000000..243a3c0 --- /dev/null +++ b/src/main/java/universalelectricity/api/net/IUpdate.java @@ -0,0 +1,22 @@ +package universalelectricity.api.net; + +public interface IUpdate +{ + /** + * Updates the network. Called by the {NetworkTickHandler}. + */ + public void update(); + + /** + * Can the network update? + * + * @return True if the network can update, otherwise the network tick handler will remove the + * network from the tick list. + */ + public boolean canUpdate(); + + /** + * @return True to leave the network in the ticker. False to remove the network from the ticker. + */ + public boolean continueUpdate(); +} diff --git a/src/main/java/universalelectricity/api/net/NetworkEvent.java b/src/main/java/universalelectricity/api/net/NetworkEvent.java new file mode 100644 index 0000000..7a4714b --- /dev/null +++ b/src/main/java/universalelectricity/api/net/NetworkEvent.java @@ -0,0 +1,52 @@ +package universalelectricity.api.net; + +import cpw.mods.fml.common.eventhandler.Cancelable; +import cpw.mods.fml.common.eventhandler.Event; +import universalelectricity.api.energy.IEnergyNetwork; + +public class NetworkEvent extends Event +{ + public final IEnergyNetwork network; + + public NetworkEvent(IEnergyNetwork network) + { + this.network = network; + } + + /** + * Call this to have your TileEntity produce power into the network. + * + * @author Calclavia + * + */ + @Cancelable + public static class EnergyProduceEvent extends NetworkEvent + { + private Object source; + private long amount; + private boolean doReceive; + + public EnergyProduceEvent(IEnergyNetwork network, Object source, long amount, boolean doReceive) + { + super(network); + this.source = source; + this.amount = amount; + this.doReceive = doReceive; + } + } + + /** + * These events are fired when something happens in the network. + * + * @author Calclavia + * + */ + @Cancelable + public static class EnergyUpdateEvent extends NetworkEvent + { + public EnergyUpdateEvent(IEnergyNetwork network) + { + super(network); + } + } +} diff --git a/src/main/java/universalelectricity/core/UniversalElectricity.java b/src/main/java/universalelectricity/core/UniversalElectricity.java index 103526d..ffb2a9c 100644 --- a/src/main/java/universalelectricity/core/UniversalElectricity.java +++ b/src/main/java/universalelectricity/core/UniversalElectricity.java @@ -5,6 +5,7 @@ import java.io.File; import net.minecraft.block.material.MapColor; import net.minecraft.block.material.Material; import net.minecraftforge.common.config.Configuration; +import universalelectricity.api.CompatibilityType; public class UniversalElectricity { @@ -14,8 +15,8 @@ public class UniversalElectricity { public static final String BUILD_VERSION = "117"; public static final String VERSION = "0.6.2"; public static final Configuration CONFIGURATION = new Configuration(new File(Loader.instance().getConfigDir(), "UniversalElectricity.cfg")); - public static double UE_IC2_RATIO = 10.0D; - public static double UE_RF_RATIO = 2.5D; + public static double UE_IC2_RATIO = CompatibilityType.INDUSTRIALCRAFT.reciprocal_ratio; + public static double UE_RF_RATIO = CompatibilityType.REDSTONE_FLUX.reciprocal_ratio; public static boolean isVoltageSensitive = false; public static boolean isNetworkActive = false; public static final Material machine = new Material(MapColor.ironColor); diff --git a/src/main/java/universalelectricity/core/block/IConductor.java b/src/main/java/universalelectricity/core/block/IConductor.java index 4a182b9..f2ba6bf 100644 --- a/src/main/java/universalelectricity/core/block/IConductor.java +++ b/src/main/java/universalelectricity/core/block/IConductor.java @@ -1,11 +1,17 @@ package universalelectricity.core.block; -import universalelectricity.core.block.IConnectionProvider; -import universalelectricity.core.block.INetworkProvider; +import net.minecraftforge.common.util.ForgeDirection; +import universalelectricity.api.net.IConnector; +import universalelectricity.core.electricity.IElectricityNetwork; -public interface IConductor extends INetworkProvider, IConnectionProvider { +public interface IConductor extends INetworkProvider, IConnectionProvider, IConnector { double getResistance(); double getCurrentCapcity(); + + @Override + default IConnector getInstance(ForgeDirection dir) { + return this; + } } diff --git a/src/main/java/universalelectricity/core/block/IConnector.java b/src/main/java/universalelectricity/core/block/IConnector.java index c903fba..558cb8f 100644 --- a/src/main/java/universalelectricity/core/block/IConnector.java +++ b/src/main/java/universalelectricity/core/block/IConnector.java @@ -1,8 +1,17 @@ package universalelectricity.core.block; import net.minecraftforge.common.util.ForgeDirection; +import universalelectricity.api.net.IConnectable; -public interface IConnector { +public interface IConnector extends IConnectable { - boolean canConnect(ForgeDirection var1); + boolean canConnect(ForgeDirection from); + + @Override + default boolean canConnect(ForgeDirection from, Object source) { + if (source instanceof IConnector) { + return canConnect(from); + } + return false; + } } diff --git a/src/main/java/universalelectricity/core/block/INetworkProvider.java b/src/main/java/universalelectricity/core/block/INetworkProvider.java index 5c190bd..1730ac7 100644 --- a/src/main/java/universalelectricity/core/block/INetworkProvider.java +++ b/src/main/java/universalelectricity/core/block/INetworkProvider.java @@ -2,9 +2,6 @@ package universalelectricity.core.block; import universalelectricity.core.electricity.IElectricityNetwork; -public interface INetworkProvider { +public interface INetworkProvider extends universalelectricity.api.net.INetworkProvider { - IElectricityNetwork getNetwork(); - - void setNetwork(IElectricityNetwork var1); } diff --git a/src/main/java/universalelectricity/core/electricity/ElectricityDisplay.java b/src/main/java/universalelectricity/core/electricity/ElectricityDisplay.java deleted file mode 100644 index 5f14369..0000000 --- a/src/main/java/universalelectricity/core/electricity/ElectricityDisplay.java +++ /dev/null @@ -1,92 +0,0 @@ -package universalelectricity.core.electricity; - - -public class ElectricityDisplay { - - public static String getDisplay(double value, ElectricityDisplay.ElectricUnit unit, int decimalPlaces, boolean isShort) { - String unitName = unit.name; - if(isShort) { - unitName = unit.symbol; - } else if(value > 1.0D) { - unitName = unit.getPlural(); - } - - return value == 0.0D?value + " " + unitName:(value <= ElectricityDisplay.MeasurementUnit.MILLI.value?roundDecimals(ElectricityDisplay.MeasurementUnit.MICRO.process(value), decimalPlaces) + " " + ElectricityDisplay.MeasurementUnit.MICRO.getName(isShort) + unitName:(value < 1.0D?roundDecimals(ElectricityDisplay.MeasurementUnit.MILLI.process(value), decimalPlaces) + " " + ElectricityDisplay.MeasurementUnit.MILLI.getName(isShort) + unitName:(value > ElectricityDisplay.MeasurementUnit.MEGA.value?roundDecimals(ElectricityDisplay.MeasurementUnit.MEGA.process(value), decimalPlaces) + " " + ElectricityDisplay.MeasurementUnit.MEGA.getName(isShort) + unitName:(value > ElectricityDisplay.MeasurementUnit.KILO.value?roundDecimals(ElectricityDisplay.MeasurementUnit.KILO.process(value), decimalPlaces) + " " + ElectricityDisplay.MeasurementUnit.KILO.getName(isShort) + unitName:roundDecimals(value, decimalPlaces) + " " + unitName)))); - } - - public static String getDisplay(double value, ElectricityDisplay.ElectricUnit unit) { - return getDisplay(value, unit, 2, false); - } - - public static String getDisplayShort(double value, ElectricityDisplay.ElectricUnit unit) { - return getDisplay(value, unit, 2, true); - } - - public static String getDisplayShort(double value, ElectricityDisplay.ElectricUnit unit, int decimalPlaces) { - return getDisplay(value, unit, decimalPlaces, true); - } - - public static String getDisplaySimple(double value, ElectricityDisplay.ElectricUnit unit, int decimalPlaces) { - return value > 1.0D?(decimalPlaces < 1?(int)value + " " + unit.getPlural():roundDecimals(value, decimalPlaces) + " " + unit.getPlural()):(decimalPlaces < 1?(int)value + " " + unit.name:roundDecimals(value, decimalPlaces) + " " + unit.name); - } - - public static double roundDecimals(double d, int decimalPlaces) { - int j = (int)(d * Math.pow(10.0D, (double)decimalPlaces)); - return (double)j / Math.pow(10.0D, (double)decimalPlaces); - } - - public static double roundDecimals(double d) { - return roundDecimals(d, 2); - } - - public static enum ElectricUnit { - - AMPERE("Amp", "I"), - AMP_HOUR("Amp Hour", "Ah"), - VOLTAGE("Volt", "V"), - WATT("Watt", "W"), - WATT_HOUR("Watt Hour", "Wh"), - RESISTANCE("Ohm", "R"), - CONDUCTANCE("Siemen", "S"), - JOULES("Joule", "J"); - public String name; - public String symbol; - - private ElectricUnit(String name, String symbol) { - this.name = name; - this.symbol = symbol; - } - - public String getPlural() { - return this.name + "s"; - } - - } - - public static enum MeasurementUnit { - - MICRO("Micro", "mi", 1.0E-6D), - MILLI("Milli", "m", 0.001D), - KILO("Kilo", "k", 1000.0D), - MEGA("Mega", "M", 1000000.0D); - public String name; - public String symbol; - public double value; - - - private MeasurementUnit(String name, String symbol, double value) { - this.name = name; - this.symbol = symbol; - this.value = value; - } - - public String getName(boolean isSymbol) { - return isSymbol?this.symbol:this.name; - } - - public double process(double value) { - return value / this.value; - } - - } -} diff --git a/src/main/java/universalelectricity/core/electricity/ElectricityNetwork.java b/src/main/java/universalelectricity/core/electricity/ElectricityNetwork.java index 800e2a3..f11d6c7 100644 --- a/src/main/java/universalelectricity/core/electricity/ElectricityNetwork.java +++ b/src/main/java/universalelectricity/core/electricity/ElectricityNetwork.java @@ -327,13 +327,39 @@ public class ElectricityNetwork implements IElectricityNetwork { } @Override - public boolean isInactive() { - return conductors.isEmpty(); + public void addConnector(IConductor connector) { + this.conductors.add(connector); } @Override - public void tick() { - // TODO Auto-generated method stub - + public void removeConnector(IConductor connector) { + this.conductors.remove(connector); } + + @Override + public Set getConnectors() { + return this.conductors; + } + + @Override + public void reconstruct() { + refreshConductors(); + } + + @Override + public IElectricityNetwork merge(IElectricityNetwork network) { + this.mergeConnection(network); + return this; + } + + @Override + public void split(IConductor connection) { + splitNetwork(connection); + } + + @Override + public void split(IConductor connectorA, IConductor connectorB) { + // TODO: implement this + } + } diff --git a/src/main/java/universalelectricity/core/electricity/IElectricityNetwork.java b/src/main/java/universalelectricity/core/electricity/IElectricityNetwork.java index fc5c8ed..7d84b5e 100644 --- a/src/main/java/universalelectricity/core/electricity/IElectricityNetwork.java +++ b/src/main/java/universalelectricity/core/electricity/IElectricityNetwork.java @@ -4,10 +4,11 @@ import java.util.HashMap; import java.util.List; import java.util.Set; import net.minecraft.tileentity.TileEntity; +import universalelectricity.api.net.INetwork; import universalelectricity.core.block.IConductor; import universalelectricity.core.block.IConnectionProvider; -public interface IElectricityNetwork { +public interface IElectricityNetwork extends INetwork { void startProducing(TileEntity var1, ElectricityPack var2); @@ -54,9 +55,5 @@ public interface IElectricityNetwork { void mergeConnection(IElectricityNetwork var1); void splitNetwork(IConnectionProvider var1); - - boolean isInactive(); - - void tick(); } diff --git a/src/main/java/universalelectricity/core/item/IItemVoltage.java b/src/main/java/universalelectricity/core/item/IItemVoltage.java index b5b577d..cd15b82 100644 --- a/src/main/java/universalelectricity/core/item/IItemVoltage.java +++ b/src/main/java/universalelectricity/core/item/IItemVoltage.java @@ -1,8 +1,7 @@ package universalelectricity.core.item; -import net.minecraft.item.ItemStack; +import universalelectricity.api.item.IVoltageItem; -public interface IItemVoltage { +public interface IItemVoltage extends IVoltageItem { - double getVoltage(ItemStack var1); } diff --git a/src/main/java/universalelectricity/core/item/ItemElectric.java b/src/main/java/universalelectricity/core/item/ItemElectric.java index b29951f..ebab4bd 100644 --- a/src/main/java/universalelectricity/core/item/ItemElectric.java +++ b/src/main/java/universalelectricity/core/item/ItemElectric.java @@ -7,7 +7,7 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; -import universalelectricity.core.electricity.ElectricityDisplay; +import universalelectricity.api.energy.UnitDisplay; import universalelectricity.core.electricity.ElectricityPack; import universalelectricity.core.item.ElectricItemHelper; import universalelectricity.core.item.IItemElectric; @@ -32,7 +32,7 @@ public abstract class ItemElectric extends Item implements IItemElectric { color = "ยง6"; } - list.add(color + ElectricityDisplay.getDisplay(joules, ElectricityDisplay.ElectricUnit.JOULES) + "/" + ElectricityDisplay.getDisplay(this.getMaxJoules(itemStack), ElectricityDisplay.ElectricUnit.JOULES)); + list.add(color + UnitDisplay.getDisplay(joules, UnitDisplay.Unit.JOULES) + "/" + UnitDisplay.getDisplay(this.getMaxJoules(itemStack), UnitDisplay.Unit.JOULES)); } public void onCreated(ItemStack itemStack, World par2World, EntityPlayer par3EntityPlayer) { diff --git a/src/main/java/universalelectricity/core/path/PathfinderChecker.java b/src/main/java/universalelectricity/core/path/PathfinderChecker.java index 7e7bd08..fdf8d9b 100644 --- a/src/main/java/universalelectricity/core/path/PathfinderChecker.java +++ b/src/main/java/universalelectricity/core/path/PathfinderChecker.java @@ -6,6 +6,7 @@ import java.util.Set; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; +import universalelectricity.api.net.IConnector; import universalelectricity.core.block.IConductor; import universalelectricity.core.block.IConnectionProvider; import universalelectricity.core.path.IPathCallBack; @@ -40,4 +41,31 @@ public class PathfinderChecker extends Pathfinder { } }); } + + public PathfinderChecker(final World world, final IConnector targetConnector, final IConnector[] ignoreConnector) { + super(new IPathCallBack() { + public Set getConnectedNodes(Pathfinder finder, Vector3 currentNode) { + HashSet neighbors = new HashSet(); + + for(int i = 0; i < 6; ++i) { + ForgeDirection direction = ForgeDirection.getOrientation(i); + Vector3 position = currentNode.clone().modifyPositionFromSide(direction); + TileEntity connectedBlock = position.getTileEntity(world); + if(connectedBlock instanceof IConductor && !Arrays.asList(ignoreConnector).contains(connectedBlock) && ((IConductor)connectedBlock).canConnect(direction.getOpposite())) { + neighbors.add(position); + } + } + + return neighbors; + } + public boolean onSearch(Pathfinder finder, Vector3 node) { + if(node.getTileEntity(world) == targetConnector) { + finder.results.add(node); + return true; + } else { + return false; + } + } + }); + } }