From 9aecd81173bf3effaa9a7c04f9f6963c905e5853 Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Thu, 22 Dec 2016 21:38:49 +0100 Subject: [PATCH 01/16] Copy from "calclavia/Electrodynamics". --- build.gradle | 22 +- gradle.properties | 2 +- src/main/java/nova/energy/EnergyItem.java | 40 ++++ src/main/java/nova/energy/UnitDisplay.java | 230 +++++++++++++++++++++ 4 files changed, 288 insertions(+), 6 deletions(-) create mode 100644 src/main/java/nova/energy/EnergyItem.java create mode 100644 src/main/java/nova/energy/UnitDisplay.java diff --git a/build.gradle b/build.gradle index 8658ce0..bbd920c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,16 +1,28 @@ plugins { id "java" - id "nova.gradle" version "0.2.5" + id "nova.gradle" version "0.2.6" id "maven-publish" id "com.jfrog.artifactory" version "3.1.1" } - apply from: "https://raw.githubusercontent.com/NOVA-Team/NOVA-Gradle/master/shared-scripts/java.gradle" dependencies { - compile "nova.core:NovaCore:$novaVersion" - testCompile "nova.core:NovaCore:$novaVersion:wrappertests" + compile nova(nova_version) +} + +nova { + wrappers { + "17" { + wrapper "nova.core:NOVA-Core-Wrapper-MC1.7:$nova_version" + } + "18" { + wrapper "nova.core:NOVA-Core-Wrapper-MC1.8:$nova_version" + } + "1_11" { + wrapper "nova.core:NOVA-Core-Wrapper-MC1.11:$nova_version" + } + } } publishing { @@ -35,4 +47,4 @@ artifactory { publishPom = true } } -} \ No newline at end of file +} diff --git a/gradle.properties b/gradle.properties index 169896f..f54cb5d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ version = 0.0.1-SNAPSHOT group = nova.energy -novaVersion = 0.1.0-SNAPSHOT +nova_version = 0.1.0-SNAPSHOT packaging = jar info.inceptionYear = 2016 diff --git a/src/main/java/nova/energy/EnergyItem.java b/src/main/java/nova/energy/EnergyItem.java new file mode 100644 index 0000000..a0c126c --- /dev/null +++ b/src/main/java/nova/energy/EnergyItem.java @@ -0,0 +1,40 @@ +package nova.energy; + +/** + * An interface for items that store energy in joules. + */ +public interface EnergyItem { + /** + * 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 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(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 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(double energy, boolean doDischarge); + + /** + * Get the amount of energy currently stored in the item. + */ + public double getEnergy(); + + /** + * Sets the amount of energy in the ItemStack. + * @param energy - Amount of electrical energy. + */ + public void setEnergy(double energy); + + /** + * Get the max amount of energy that can be stored in the item. + */ + public double getEnergyCapacity(); +} diff --git a/src/main/java/nova/energy/UnitDisplay.java b/src/main/java/nova/energy/UnitDisplay.java new file mode 100644 index 0000000..143d9c6 --- /dev/null +++ b/src/main/java/nova/energy/UnitDisplay.java @@ -0,0 +1,230 @@ +package nova.energy; + +import java.util.LinkedList; +import java.util.List; + +/** + * An easy way to display information on electricity for the client. + * @author Calclavia + */ +public class UnitDisplay { + public Unit unit; + public double value; + public boolean useSymbol = false; + public int decimalPlaces = 2; + public boolean isSimple = false; + + public UnitDisplay(Unit unit, double value, boolean simple) { + this.unit = unit; + this.value = value; + } + + public UnitDisplay(Unit unit, double value) { + this(unit, value, false); + } + + @Deprecated + public UnitDisplay(double value, Unit unit) { + this(unit, value); + } + + /** + * Rounds a number to a specific number place places + * @param d - 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); + } + + public UnitDisplay multiply(double value) { + this.value *= value; + return this; + } + + public UnitDisplay simple() { + isSimple = true; + return this; + } + + public UnitDisplay symbol() { + return symbol(true); + } + + public UnitDisplay symbol(boolean useSymbol) { + this.useSymbol = useSymbol; + return this; + } + + public UnitDisplay decimal(int decimalPlaces) { + this.decimalPlaces = decimalPlaces; + return this; + } + + @Override + public String toString() { + String unitName = unit.name; + String prefix = ""; + + if (isSimple) { + 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; + } + + if (value < 0) { + value = Math.abs(value); + prefix = "-"; + } + + if (useSymbol) { + unitName = unit.symbol; + } else if (value > 1) { + unitName = unit.getPlural(); + } + + if (value == 0) { + return value + " " + unitName; + } else { + for (int i = 0; i < UnitPrefix.unitPrefixes.size(); i++) { + UnitPrefix lowerMeasure = UnitPrefix.unitPrefixes.get(i); + + if (lowerMeasure.isBellow(value) && i == 0) { + return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(useSymbol) + unitName; + } + if (i + 1 >= UnitPrefix.unitPrefixes.size()) { + return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(useSymbol) + unitName; + } + + UnitPrefix upperMeasure = UnitPrefix.unitPrefixes.get(i + 1); + + if ((lowerMeasure.isAbove(value) && upperMeasure.isBellow(value)) || lowerMeasure.value == value) { + return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(useSymbol) + unitName; + } + } + } + + return prefix + roundDecimals(value, decimalPlaces) + " " + unitName; + } + + /** + * 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 class Unit { + public static final Unit AMPERE = new Unit("Amp", "I"); + public static final Unit AMP_HOUR = new Unit("Amp Hour", "Ah"); + public static final Unit VOLTAGE = new Unit("Volt", "V"); + public static final Unit WATT = new Unit("Watt", "W"); + public static final Unit WATT_HOUR = new Unit("Watt Hour", "Wh"); + public static final Unit RESISTANCE = new Unit("Ohm", "R"); + public static final Unit CONDUCTANCE = new Unit("Siemen", "S"); + public static final Unit JOULES = new Unit("Joule", "J"); + public static final Unit LITER = new Unit("Liter", "L"); + public static final Unit NEWTON_METER = new Unit("Newton Meter", "Nm"); + + public static final Unit REDFLUX = new Unit("Redstone-Flux", "Rf").setPlural("Redstone-Flux"); + public static final Unit MINECRAFT_JOULES = new Unit("Minecraft-Joule", "Mj"); + public static final Unit ELECTRICAL_UNITS = new Unit("Electrical-Unit", "Eu"); + + public final String name; + public final String symbol; + private String plural = null; + + private Unit(String name, String symbol) { + this.name = name; + this.symbol = symbol; + } + + private Unit setPlural(String plural) { + this.plural = plural; + return this; + } + + public String getPlural() { + return this.plural == null ? this.name + "s" : this.plural; + } + } + + /** + * Metric system of measurement. + */ + public static class UnitPrefix { + public static final List unitPrefixes = new LinkedList(); + + public static final UnitPrefix MICRO = new UnitPrefix("Micro", "u", 0.000001); + public static final UnitPrefix MILLI = new UnitPrefix("Milli", "m", 0.001); + public static final UnitPrefix BASE = new UnitPrefix("", "", 1); + public static final UnitPrefix KILO = new UnitPrefix("Kilo", "k", 1000); + public static final UnitPrefix MEGA = new UnitPrefix("Mega", "M", 1000000); + public static final UnitPrefix GIGA = new UnitPrefix("Giga", "G", 1000000000); + public static final UnitPrefix TERA = new UnitPrefix("Tera", "T", 1000000000000d); + public static final UnitPrefix PETA = new UnitPrefix("Peta", "P", 1000000000000000d); + public static final UnitPrefix EXA = new UnitPrefix("Exa", "E", 1000000000000000000d); + public static final UnitPrefix ZETTA = new UnitPrefix("Zetta", "Z", 1000000000000000000000d); + public static final UnitPrefix YOTTA = new UnitPrefix("Yotta", "Y", 1000000000000000000000000d); + /** + * long name for the unit + */ + public final String name; + /** + * short unit version of the unit + */ + public final String symbol; + /** + * Point by which a number is consider to be of this unit + */ + public final double value; + + private UnitPrefix(String name, String symbol, double value) { + this.name = name; + this.symbol = symbol; + this.value = value; + unitPrefixes.add(this); + } + + 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; + } + } +} From aa7c4e346439b315beff74b994b6de173c9c6249 Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Fri, 23 Dec 2016 00:28:56 +0100 Subject: [PATCH 02/16] Begin work on unit conversion + License information --- .gitignore | 1 + Mod Energy Conversion.md | 56 ++++++++ src/main/java/nova/energy/EnergyItem.java | 19 +++ src/main/java/nova/energy/UnitConversion.java | 121 +++++++++++++++++ src/main/java/nova/energy/UnitDisplay.java | 127 +++++++++++++++--- 5 files changed, 307 insertions(+), 17 deletions(-) create mode 100644 Mod Energy Conversion.md create mode 100644 src/main/java/nova/energy/UnitConversion.java diff --git a/.gitignore b/.gitignore index 6bdd20d..ce73626 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ # Except: !LICENSE.txt +!Mod Energy Conversion.md # Sources !/src diff --git a/Mod Energy Conversion.md b/Mod Energy Conversion.md new file mode 100644 index 0000000..483bb70 --- /dev/null +++ b/Mod Energy Conversion.md @@ -0,0 +1,56 @@ +###Author: +[ExE Boss](https://github.com/ExE-Boss) + +### About +This file contains my docs of mod energy conversion. +It was created by observing what ratios other mods used when implementing energy conversion. + +# Recalculated Energy Conversions to remove infinite power generation loops +- Defined by Player: + - 1 EU = 1 FZ-Charge + - 3 EU = 25 RF +- Defined by Eloraam: + - 1 kW (RP2) = 1 McJ +- Defined by Thermal Expansion: + - 1 McJ = 25 RF + +## Derived conversions: +- 3 EU = 1 McJ +- 3 EU = 1 kW (RP2) +- 1 EU = 40 J (Defined by AE) +- 1 McJ = 120 J (Prevents infinite loop) + +--- + +Original file: + +# Mod Energy Conversion by Unit +- 1 McJ = 3 EU (Probably from Forestry) +- 1 EU = 2 Unit +- 1 McJ = 5 Unit (<^ These lead to an infinite power generation loop, should redefine McJ:Unit to 1:6) +- 1 EU = 45 J (Average of AE and MMMPS) +- 1 McJ = 60 J (Leads to infinite loop) +- 1 kW (RP2) = 1 McJ (As defined by Eloraam) +- 1 kW (RP2) = ~3 EU +- 1 kW (RP2) = 60 J +- 1 kW (RP2) = 5-ish Units +- 3 EU = 25 RF +- 1 EU = 1 FZ-Charge +- 1 McJ = 25 RF + +## Math behind some of the conversions +- 1 McJ = 3 EU = 1 kW (RP2) +- ~3 EU = 1 kW (RP2) +- 1 McJ = 5 Units = 2.5 EU +- 1 EU = 2 Units +- 20 J = 1 Unit = 0.5 EU +- 40 J = 1 EU (AE) +- 50 J = 1 EU (MMMPS) +- 60 J = 1 McJ = 1 kW (RP2) + +--- + +# Licence +This work by [ExE Boss](https://github.com/ExE-Boss) is dual-licensed +under a [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/) +and a [GNU Lesser General Public License (LGPL) version 3](https://www.gnu.org/licenses/lgpl-3.0.html). diff --git a/src/main/java/nova/energy/EnergyItem.java b/src/main/java/nova/energy/EnergyItem.java index a0c126c..9ff3657 100644 --- a/src/main/java/nova/energy/EnergyItem.java +++ b/src/main/java/nova/energy/EnergyItem.java @@ -1,3 +1,22 @@ +/* + * Copyright (c) 2015 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ package nova.energy; /** diff --git a/src/main/java/nova/energy/UnitConversion.java b/src/main/java/nova/energy/UnitConversion.java new file mode 100644 index 0000000..a01a15e --- /dev/null +++ b/src/main/java/nova/energy/UnitConversion.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2015 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ +package nova.energy; + +import nova.energy.UnitDisplay.Unit; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +/** + * Convert between different units. (Where supported) + * + * @author ExE Boss + */ +public class UnitConversion { + + private static final Map> CONVERSION = new HashMap<>(); + + private final Unit unit1, unit2; + private final double ratio, reverseRatio; + private final UnitConversion reverse; + + private UnitConversion(Unit unit1, Unit unit2, double ratio) { + this.unit1 = unit1; + this.unit2 = unit2; + this.ratio = ratio; + this.reverseRatio = 1/ratio; + this.reverse = new UnitConversion(unit1, unit2, this.reverseRatio, this); + } + private UnitConversion(Unit unit1, Unit unit2, double ratio, UnitConversion reverse) { + this.unit1 = unit1; + this.unit2 = unit2; + this.ratio = ratio; + this.reverse = reverse; + this.reverseRatio = this.reverse.ratio; + } + public double convert(double value) { + return (this.reverseRatio == 0 && this.ratio != 0 ? value / ratio : value * this.reverseRatio); + } + + public Unit unit1() { + return unit1; + } + public Unit unit2() { + return unit2; + } + public double ratio() { + return ratio; + } + public UnitConversion reverse() { + return reverse; + } + + public static Optional getConvertion(Unit unit1, Unit unit2) { + Map conv = CONVERSION.get(unit1); + if (conv == null) return Optional.empty(); + return Optional.ofNullable(conv.get(unit2)); + } + /** + * + * @param unit1 The unit to convert from + * @param unit2 The unit to convert to + * @param ratio unit1/unit2 + * @return The UnitConversion instance. + * @throws IllegalArgumentException If ratio is 0 + */ + public static UnitConversion registerConversion(Unit unit1, Unit unit2, double ratio) throws IllegalArgumentException { + if (ratio == 0) + throw new IllegalArgumentException("Ratio cannot be 0"); + + Map conv1 = CONVERSION.get(unit1); + if (conv1 == null) conv1 = new HashMap<>(); + if (conv1.containsKey(unit2)) return conv1.get(unit2); + + Map conv2 = CONVERSION.get(unit2); + if (conv2 == null) conv2 = new HashMap<>(); + + UnitConversion uc = new UnitConversion(unit1, unit2, ratio); + + conv1.put(unit2, uc); + conv2.put(unit1, uc.reverse()); + + calculateConversions(); + return uc; + } + + /** + * If UnitA can be converted to UnitB + * and UnitB can be converted to UnitC, + * then UnitA must be convertible to UnitC. + */ + public static void calculateConversions() { + // TODO + } + + static { + registerConversion(Unit.ELECTRICAL_UNITS, Unit.REDFLUX, 3d/25d); + registerConversion(Unit.ELECTRICAL_UNITS, Unit.MINECRAFT_JOULES, 3d/1d); + registerConversion(Unit.REDFLUX, Unit.MINECRAFT_JOULES, 25d/1d); // This should happen automatically + + registerConversion(Unit.ELECTRICAL_UNITS, Unit.JOULE, 40d/1d); + } +} diff --git a/src/main/java/nova/energy/UnitDisplay.java b/src/main/java/nova/energy/UnitDisplay.java index 143d9c6..e14f8e6 100644 --- a/src/main/java/nova/energy/UnitDisplay.java +++ b/src/main/java/nova/energy/UnitDisplay.java @@ -1,7 +1,37 @@ +/* + * Copyright (c) 2015 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ package nova.energy; +import nova.core.loader.Mod; +import nova.core.util.id.Identifiable; +import nova.core.util.id.Identifier; +import nova.core.util.id.StringIdentifier; +import nova.internal.core.launch.ModLoader; + +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; /** * An easy way to display information on electricity for the client. @@ -126,29 +156,35 @@ public String toString() { * 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 class Unit { - public static final Unit AMPERE = new Unit("Amp", "I"); - public static final Unit AMP_HOUR = new Unit("Amp Hour", "Ah"); - public static final Unit VOLTAGE = new Unit("Volt", "V"); - public static final Unit WATT = new Unit("Watt", "W"); - public static final Unit WATT_HOUR = new Unit("Watt Hour", "Wh"); - public static final Unit RESISTANCE = new Unit("Ohm", "R"); - public static final Unit CONDUCTANCE = new Unit("Siemen", "S"); - public static final Unit JOULES = new Unit("Joule", "J"); - public static final Unit LITER = new Unit("Liter", "L"); - public static final Unit NEWTON_METER = new Unit("Newton Meter", "Nm"); - - public static final Unit REDFLUX = new Unit("Redstone-Flux", "Rf").setPlural("Redstone-Flux"); - public static final Unit MINECRAFT_JOULES = new Unit("Minecraft-Joule", "Mj"); - public static final Unit ELECTRICAL_UNITS = new Unit("Electrical-Unit", "Eu"); - + public static class Unit implements Identifiable { + private static final Map UNIT_MAP = new HashMap<>(); + + public static final Unit AMPERE = new Unit("nova:ampere", "Amp", "I"); + public static final Unit AMP_HOUR = new Unit("nova:amp_hour", "Amp Hour", "Ah"); + public static final Unit VOLTAGE = new Unit("nova:voltage", "Volt", "V"); + public static final Unit WATT = new Unit("nova:watt", "Watt", "W"); + public static final Unit WATT_HOUR = new Unit("nova:watt_hour", "Watt Hour", "Wh"); + public static final Unit RESISTANCE = new Unit("nova:resistance", "Ohm", "R"); + public static final Unit CONDUCTANCE = new Unit("nova:conductance", "Siemen", "S"); + public static final Unit JOULE = new Unit("nova:joule", "Joule", "J"); + public static final Unit LITER = new Unit("nova:liter", "Liter", "L"); + public static final Unit NEWTON_METER = new Unit("nova:newton_meter", "Newton Meter", "Nm"); + + public static final Unit REDFLUX = new Unit("forge:redstone_flux", "Redstone-Flux", "RF").setPlural("Redstone-Flux"); + public static final Unit MINECRAFT_JOULES = new Unit("buildcraft:minecraft_joule", "Minecraft-Joule", "McJ"); // MJ is confusing + public static final Unit ELECTRICAL_UNITS = new Unit("ic2:electrical_unit", "Electrical-Unit", "EU"); + + private final String id; public final String name; public final String symbol; private String plural = null; - private Unit(String name, String symbol) { + private Unit(String id, String name, String symbol) { + this.id = id; this.name = name; this.symbol = symbol; + + UNIT_MAP.put(getID(), this); } private Unit setPlural(String plural) { @@ -159,6 +195,63 @@ private Unit setPlural(String plural) { public String getPlural() { return this.plural == null ? this.name + "s" : this.plural; } + + @Override + public Identifier getID() { + return new StringIdentifier(id); + } + + public static Set getUnitsForMod(String modId) { + return UNIT_MAP.values().stream().filter((e) -> { + String id = e.getID().asString(); + if (id.contains(":")) { + return id.substring(0, id.lastIndexOf(':')).startsWith(modId); + } else { + return modId == null || modId.isEmpty(); + } + }).collect(Collectors.toSet()); + } + + public static Optional getUnit(String id) { + return Optional.ofNullable(UNIT_MAP.get(new StringIdentifier(id))); + } + + public static Unit getOrCreateUnit(String id, String name, String unit) { + StringIdentifier idRaw = new StringIdentifier(id); + StringIdentifier idNamespaced = new StringIdentifier(addPrefix(id)); + if (UNIT_MAP.containsKey(idNamespaced)) return UNIT_MAP.get(idNamespaced); + if (UNIT_MAP.containsKey(idRaw)) return UNIT_MAP.get(idRaw); + + Unit unitObj = new Unit(idNamespaced.asString(), name, unit); + return unitObj; + } + + public static Unit getOrCreateUnit(String id, String name, String unit, String plural) { + StringIdentifier idRaw = new StringIdentifier(id); + StringIdentifier idNamespaced = new StringIdentifier(addPrefix(id)); + if (UNIT_MAP.containsKey(idNamespaced)) return UNIT_MAP.get(idNamespaced); + if (UNIT_MAP.containsKey(idRaw)) return UNIT_MAP.get(idRaw); + + Unit unitObj = new Unit(idNamespaced.asString(), name, unit); + return unitObj.setPlural(plural); + } + + private static String addPrefix(String id) { + int prefixEnd = id.lastIndexOf(':'); + String oldPrefix = prefixEnd < 0 ? "" : id.substring(0, prefixEnd); + String newPrefix = null; + Optional mod = ModLoader.instance().activeMod(); + + if (mod.isPresent()) { + newPrefix = mod.get().id(); + } + + if (newPrefix != null && oldPrefix.isEmpty()) { + id = newPrefix + ':' + id; + } + + return id; + } } /** From c2b3b66f609ce06418b3ae61b11cc898e17c4e6d Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Fri, 23 Dec 2016 16:14:37 +0100 Subject: [PATCH 03/16] Update to latest snapshot (Moved Unit.addPrefix to Identifiable.addPrefix) --- src/main/java/nova/energy/EnergyItem.java | 1 + src/main/java/nova/energy/UnitConversion.java | 1 + src/main/java/nova/energy/UnitDisplay.java | 24 ++++--------------- 3 files changed, 6 insertions(+), 20 deletions(-) diff --git a/src/main/java/nova/energy/EnergyItem.java b/src/main/java/nova/energy/EnergyItem.java index 9ff3657..fdbd6ef 100644 --- a/src/main/java/nova/energy/EnergyItem.java +++ b/src/main/java/nova/energy/EnergyItem.java @@ -17,6 +17,7 @@ * You should have received a copy of the GNU General Public License * along with NOVA. If not, see . */ + package nova.energy; /** diff --git a/src/main/java/nova/energy/UnitConversion.java b/src/main/java/nova/energy/UnitConversion.java index a01a15e..2d9a91b 100644 --- a/src/main/java/nova/energy/UnitConversion.java +++ b/src/main/java/nova/energy/UnitConversion.java @@ -17,6 +17,7 @@ * You should have received a copy of the GNU General Public License * along with NOVA. If not, see . */ + package nova.energy; import nova.energy.UnitDisplay.Unit; diff --git a/src/main/java/nova/energy/UnitDisplay.java b/src/main/java/nova/energy/UnitDisplay.java index e14f8e6..a395db7 100644 --- a/src/main/java/nova/energy/UnitDisplay.java +++ b/src/main/java/nova/energy/UnitDisplay.java @@ -17,6 +17,7 @@ * You should have received a copy of the GNU General Public License * along with NOVA. If not, see . */ + package nova.energy; import nova.core.loader.Mod; @@ -180,7 +181,7 @@ public static class Unit implements Identifiable { private String plural = null; private Unit(String id, String name, String symbol) { - this.id = id; + this.id = Identifiable.addPrefix(id, false); this.name = name; this.symbol = symbol; @@ -218,7 +219,7 @@ public static Optional getUnit(String id) { public static Unit getOrCreateUnit(String id, String name, String unit) { StringIdentifier idRaw = new StringIdentifier(id); - StringIdentifier idNamespaced = new StringIdentifier(addPrefix(id)); + StringIdentifier idNamespaced = new StringIdentifier(Identifiable.addPrefix(id, false)); if (UNIT_MAP.containsKey(idNamespaced)) return UNIT_MAP.get(idNamespaced); if (UNIT_MAP.containsKey(idRaw)) return UNIT_MAP.get(idRaw); @@ -228,30 +229,13 @@ public static Unit getOrCreateUnit(String id, String name, String unit) { public static Unit getOrCreateUnit(String id, String name, String unit, String plural) { StringIdentifier idRaw = new StringIdentifier(id); - StringIdentifier idNamespaced = new StringIdentifier(addPrefix(id)); + StringIdentifier idNamespaced = new StringIdentifier(Identifiable.addPrefix(id, false)); if (UNIT_MAP.containsKey(idNamespaced)) return UNIT_MAP.get(idNamespaced); if (UNIT_MAP.containsKey(idRaw)) return UNIT_MAP.get(idRaw); Unit unitObj = new Unit(idNamespaced.asString(), name, unit); return unitObj.setPlural(plural); } - - private static String addPrefix(String id) { - int prefixEnd = id.lastIndexOf(':'); - String oldPrefix = prefixEnd < 0 ? "" : id.substring(0, prefixEnd); - String newPrefix = null; - Optional mod = ModLoader.instance().activeMod(); - - if (mod.isPresent()) { - newPrefix = mod.get().id(); - } - - if (newPrefix != null && oldPrefix.isEmpty()) { - id = newPrefix + ':' + id; - } - - return id; - } } /** From 261fdf575b967964c256fcd59de4cf51a313afd3 Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Sat, 24 Dec 2016 03:23:41 +0100 Subject: [PATCH 04/16] Created EnergyStorage + made UnitDisplay immutable + Begin work on Minecraft 1.11 wrappers Forge 1.11 (and possibly earlier) has a Redstone Flux energy API that NOVA should utilise. --- minecraft/.gitignore | 6 ++ minecraft/1.11/.gitignore | 17 ++++ minecraft/1.11/build.gradle | 67 +++++++++++++ minecraft/1.11/gradle.properties | 10 ++ minecraft/build.gradle | 19 ++++ settings.gradle | 4 +- src/main/java/nova/energy/EnergyItem.java | 23 ++++- src/main/java/nova/energy/EnergyStorage.java | 96 +++++++++++++++++++ src/main/java/nova/energy/UnitConversion.java | 9 +- src/main/java/nova/energy/UnitDisplay.java | 86 +++++++++++------ 10 files changed, 299 insertions(+), 38 deletions(-) create mode 100644 minecraft/.gitignore create mode 100644 minecraft/1.11/.gitignore create mode 100644 minecraft/1.11/build.gradle create mode 100644 minecraft/1.11/gradle.properties create mode 100644 minecraft/build.gradle create mode 100644 src/main/java/nova/energy/EnergyStorage.java diff --git a/minecraft/.gitignore b/minecraft/.gitignore new file mode 100644 index 0000000..e2bf0e7 --- /dev/null +++ b/minecraft/.gitignore @@ -0,0 +1,6 @@ +/* + +!/build.gradle +!/.gitignore + +!/1.11 diff --git a/minecraft/1.11/.gitignore b/minecraft/1.11/.gitignore new file mode 100644 index 0000000..85be317 --- /dev/null +++ b/minecraft/1.11/.gitignore @@ -0,0 +1,17 @@ +# Ignore All +/* + +# Sources +!/src + +# github +!/.gitignore +!/README.md + +# gradle +!/build.gradle +!/build.properties +!/settings.gradle +!/gradle.properties +!/gradlew* +!/gradle diff --git a/minecraft/1.11/build.gradle b/minecraft/1.11/build.gradle new file mode 100644 index 0000000..7c3f596 --- /dev/null +++ b/minecraft/1.11/build.gradle @@ -0,0 +1,67 @@ +apply plugin: "maven-publish" +apply plugin: "com.jfrog.artifactory" +apply from: "https://raw.githubusercontent.com/NOVA-Team/NOVA-Gradle/master/shared-scripts/java.gradle" + +idea.module.name = "Energy-MC-1.11" +archivesBaseName = "NOVA-Energy-Wrapper-MC1.11" + +publishing { + publications { + main(MavenPublication) { + from components.java + + artifactId archivesBaseName + + artifact sourcesJar + artifact javadocJar + + pom.withXml(writePom(project.properties)) + } + } +} + +artifactory { + publish { + defaults { + publications("main") + publishPom = true + } + } +} + +artifacts { + archives jar +} + +apply plugin: 'net.minecraftforge.gradle.forge' + +minecraft { + version = property("minecraft.version") + "-" + property("forge.version") + mappings = 'snapshot_20161111' + runDir = "run" +} + +dependencies { + compile rootProject + compile group: "nova.core", name: "NovaCore", version: property("nova_version"), changing: true + compile "nova.wrapper.mc1_11:NovaWrapper-MC1.11:0.1-SNAPSHOT:deobf" +} + +processResources { + // this will ensure that this task is redone when the versions change. + inputs.property "version", project.version + inputs.property "mcversion", project.minecraft.version + + // replace stuff in mcmod.info, nothing else + from(sourceSets.main.resources.srcDirs) { + include "mcmod.info" + + // replace version and mcversion + expand "version": project.version, "mcversion": project.minecraft.version + } + + // copy everything else, thats not the mcmod.info + from(sourceSets.main.resources.srcDirs) { + exclude "mcmod.info" + } +} diff --git a/minecraft/1.11/gradle.properties b/minecraft/1.11/gradle.properties new file mode 100644 index 0000000..3d511be --- /dev/null +++ b/minecraft/1.11/gradle.properties @@ -0,0 +1,10 @@ +group = nova.core + +minecraft.version = 1.11 +forge.version = 13.19.1.2189 +forgeGradleVersion = 2.2-SNAPSHOT + +packaging = jar +info.inceptionYear = 2016 +info.description = The wrapper of the Nova API to the MinecraftForge 1.11 modding system. +info.organization.name = NOVA diff --git a/minecraft/build.gradle b/minecraft/build.gradle new file mode 100644 index 0000000..905f27d --- /dev/null +++ b/minecraft/build.gradle @@ -0,0 +1,19 @@ +subprojects { + buildscript { + repositories { + mavenCentral() + maven { + name "forge" + url "http://files.minecraftforge.net/maven" + } + maven { + name "sonatype" + url "https://oss.sonatype.org/content/repositories/snapshots/" + } + } + dependencies { + // Minecraft 1.11 requires newer ForgeGradle, while 1.7 and 1.8 require older. + classpath 'net.minecraftforge.gradle:ForgeGradle:' + property('forgeGradleVersion') + } + } +} diff --git a/settings.gradle b/settings.gradle index 33e60a6..7b75a36 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,3 @@ -rootProject.name = 'NOVA-Energy' \ No newline at end of file +rootProject.name = 'NOVA-Energy' + +include "minecraft:1.11" diff --git a/src/main/java/nova/energy/EnergyItem.java b/src/main/java/nova/energy/EnergyItem.java index fdbd6ef..66771a3 100644 --- a/src/main/java/nova/energy/EnergyItem.java +++ b/src/main/java/nova/energy/EnergyItem.java @@ -24,21 +24,24 @@ * An interface for items that store energy in joules. */ public interface EnergyItem { + /** * 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 energy Maximum amount of energy to be sent into the item. + * + * @param energy Maximum amount of energy to be sent into the item (in Joules). * @param doRecharge If false, the charge will only be simulated. - * @return Amount of energy that was accepted by the item. + * @return Amount of energy that was accepted by the item (in Joules). */ public double recharge(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 energy Maximum amount of energy to be removed from the item. + * + * @param energy Maximum amount of energy to be removed from the item (in Joules). * @param doDischarge If false, the discharge will only be simulated. - * @return Amount of energy that was removed from the item. + * @return Amount of energy that was removed from the item (in Joules). */ public double discharge(double energy, boolean doDischarge); @@ -49,7 +52,7 @@ public interface EnergyItem { /** * Sets the amount of energy in the ItemStack. - * @param energy - Amount of electrical energy. + * @param energy - Amount of electrical energy (in Joules). */ public void setEnergy(double energy); @@ -57,4 +60,14 @@ public interface EnergyItem { * Get the max amount of energy that can be stored in the item. */ public double getEnergyCapacity(); + + /** + * @return Whether or not this item can be externally recharged. + */ + public boolean canRecharge(); + + /** + * @return Whether or not this item can be externally discharged. + */ + public boolean canDischarge(); } diff --git a/src/main/java/nova/energy/EnergyStorage.java b/src/main/java/nova/energy/EnergyStorage.java new file mode 100644 index 0000000..31b666e --- /dev/null +++ b/src/main/java/nova/energy/EnergyStorage.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2015 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ +package nova.energy; + +import nova.core.component.Component; +import nova.core.retention.Storable; +import nova.core.retention.Store; + +/** + * + * @author ExE Boss + */ +public class EnergyStorage extends Component implements EnergyItem, Storable { + + @Store + protected double energy; + protected double maxEnergy; + protected double maxRecharge; + protected double maxDischarge; + + public EnergyStorage(double maxEnergy) { + this(maxEnergy, maxEnergy, maxEnergy); + } + + public EnergyStorage(double maxEnergy, double maxTransfer) { + this(maxEnergy, maxTransfer, maxTransfer); + } + + public EnergyStorage(double maxEnergy, double maxRecharge, double maxDischarge) { + this.maxEnergy = maxEnergy; + this.maxRecharge = Math.min(0, Math.max(maxRecharge, maxEnergy)); + this.maxDischarge = Math.min(0, Math.max(maxDischarge, maxEnergy)); + } + + @Override + public double recharge(double energy, boolean doRecharge) { + if (!canRecharge()) return 0; + + energy = Math.min(this.maxEnergy - this.energy, Math.min(this.maxRecharge, energy)); + if (doRecharge) this.energy += energy; + + return energy; + } + + @Override + public double discharge(double energy, boolean doDischarge) { + if (!canRecharge()) return 0; + + energy = Math.min(this.energy, Math.min(this.maxDischarge, energy)); + if (doDischarge) this.energy -= energy; + + return energy; + } + + @Override + public double getEnergy() { + return this.energy; + } + + @Override + public void setEnergy(double energy) { + this.energy = Math.min(0, Math.max(energy, maxEnergy)); + } + + @Override + public double getEnergyCapacity() { + return maxEnergy; + } + + @Override + public boolean canRecharge() { + return maxRecharge > 0; + } + + @Override + public boolean canDischarge() { + return maxDischarge > 0; + } +} diff --git a/src/main/java/nova/energy/UnitConversion.java b/src/main/java/nova/energy/UnitConversion.java index 2d9a91b..fb761d3 100644 --- a/src/main/java/nova/energy/UnitConversion.java +++ b/src/main/java/nova/energy/UnitConversion.java @@ -113,10 +113,13 @@ public static void calculateConversions() { } static { + registerConversion(Unit.ELECTRICAL_UNITS, Unit.JOULE, 40d/1d); + registerConversion(Unit.REDFLUX, Unit.JOULE, 24/5d); + registerConversion(Unit.MINECRAFT_JOULES, Unit.JOULE, 40d/3d/1d); + + // This should also happen automatically registerConversion(Unit.ELECTRICAL_UNITS, Unit.REDFLUX, 3d/25d); registerConversion(Unit.ELECTRICAL_UNITS, Unit.MINECRAFT_JOULES, 3d/1d); - registerConversion(Unit.REDFLUX, Unit.MINECRAFT_JOULES, 25d/1d); // This should happen automatically - - registerConversion(Unit.ELECTRICAL_UNITS, Unit.JOULE, 40d/1d); + registerConversion(Unit.REDFLUX, Unit.MINECRAFT_JOULES, 25d/1d); } } diff --git a/src/main/java/nova/energy/UnitDisplay.java b/src/main/java/nova/energy/UnitDisplay.java index a395db7..9ea3e33 100644 --- a/src/main/java/nova/energy/UnitDisplay.java +++ b/src/main/java/nova/energy/UnitDisplay.java @@ -36,36 +36,56 @@ /** * An easy way to display information on electricity for the client. + * * @author Calclavia */ public class UnitDisplay { - public Unit unit; - public double value; - public boolean useSymbol = false; - public int decimalPlaces = 2; - public boolean isSimple = false; + public final Unit unit; + public final double value; + public final boolean useSymbol; + public final int decimalPlaces; + public final boolean isSimple; - public UnitDisplay(Unit unit, double value, boolean simple) { + public UnitDisplay(Unit unit, double value, boolean useSymbol, int decimalPlaces, boolean simple) { this.unit = unit; this.value = value; + this.useSymbol = useSymbol; + this.decimalPlaces = decimalPlaces; + this.isSimple = simple; } - public UnitDisplay(Unit unit, double value) { - this(unit, value, false); + public UnitDisplay(Unit unit, double value, boolean useSymbol, boolean simple) { + this(unit, value, useSymbol, 2, simple); } - @Deprecated - public UnitDisplay(double value, Unit unit) { - this(unit, value); + public UnitDisplay(Unit unit, double value, int decimalPlaces, boolean simple) { + this(unit, value, false, decimalPlaces, simple); + } + + public UnitDisplay(Unit unit, double value, boolean useSymbol, int decimalPlaces) { + this(unit, value, useSymbol, decimalPlaces, false); + } + + public UnitDisplay(Unit unit, double value, int decimalPlaces) { + this(unit, value, false, decimalPlaces, false); + } + + public UnitDisplay(Unit unit, double value, boolean simple) { + this(unit, value, false, 2, simple); + } + + public UnitDisplay(Unit unit, double value) { + this(unit, value, false, 2, false); } /** * Rounds a number to a specific number place places + * * @param d - the number * @return The rounded number */ public static double roundDecimals(double d, int decimalPlaces) { - int j = (int) (d * Math.pow(10, decimalPlaces)); + long j = Math.round(d * Math.pow(10, decimalPlaces)); return j / Math.pow(10, decimalPlaces); } @@ -74,13 +94,15 @@ public static double roundDecimals(double d) { } public UnitDisplay multiply(double value) { - this.value *= value; - return this; + return new UnitDisplay(unit, value * this.value); } public UnitDisplay simple() { - isSimple = true; - return this; + return (isSimple ? this : new UnitDisplay(unit, value, true)); + } + + public UnitDisplay notSimple() { + return (!isSimple ? this : new UnitDisplay(unit, value, false)); } public UnitDisplay symbol() { @@ -88,19 +110,18 @@ public UnitDisplay symbol() { } public UnitDisplay symbol(boolean useSymbol) { - this.useSymbol = useSymbol; - return this; + return (this.useSymbol ^ useSymbol ? new UnitDisplay(unit, value, isSimple, decimalPlaces, useSymbol) : this); } public UnitDisplay decimal(int decimalPlaces) { - this.decimalPlaces = decimalPlaces; - return this; + return (this.decimalPlaces == decimalPlaces ? this : new UnitDisplay(unit, value, isSimple, decimalPlaces, useSymbol)); } @Override public String toString() { String unitName = unit.name; String prefix = ""; + double value = this.value; if (isSimple) { if (value > 1) { @@ -132,17 +153,17 @@ public String toString() { if (value == 0) { return value + " " + unitName; } else { - for (int i = 0; i < UnitPrefix.unitPrefixes.size(); i++) { - UnitPrefix lowerMeasure = UnitPrefix.unitPrefixes.get(i); + for (int i = 0; i < UnitPrefix.UNIT_PREFIXES.size(); i++) { + UnitPrefix lowerMeasure = UnitPrefix.UNIT_PREFIXES.get(i); if (lowerMeasure.isBellow(value) && i == 0) { return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(useSymbol) + unitName; } - if (i + 1 >= UnitPrefix.unitPrefixes.size()) { + if (i + 1 >= UnitPrefix.UNIT_PREFIXES.size()) { return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(useSymbol) + unitName; } - UnitPrefix upperMeasure = UnitPrefix.unitPrefixes.get(i + 1); + UnitPrefix upperMeasure = UnitPrefix.UNIT_PREFIXES.get(i + 1); if ((lowerMeasure.isAbove(value) && upperMeasure.isBellow(value)) || lowerMeasure.value == value) { return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(useSymbol) + unitName; @@ -171,8 +192,11 @@ public static class Unit implements Identifiable { public static final Unit LITER = new Unit("nova:liter", "Liter", "L"); public static final Unit NEWTON_METER = new Unit("nova:newton_meter", "Newton Meter", "Nm"); + /** + * Redstone Flux, the default energy unit in Minecraft Forge since 1.10-ish. + */ public static final Unit REDFLUX = new Unit("forge:redstone_flux", "Redstone-Flux", "RF").setPlural("Redstone-Flux"); - public static final Unit MINECRAFT_JOULES = new Unit("buildcraft:minecraft_joule", "Minecraft-Joule", "McJ"); // MJ is confusing + public static final Unit MINECRAFT_JOULES = new Unit("buildcraft:minecraft_joule", "Minecraft-Joule", "McJ"); // MJ is confusing with Megajoules public static final Unit ELECTRICAL_UNITS = new Unit("ic2:electrical_unit", "Electrical-Unit", "EU"); private final String id; @@ -202,7 +226,7 @@ public Identifier getID() { return new StringIdentifier(id); } - public static Set getUnitsForMod(String modId) { + public static Set getUnitsFromMod(String modId) { return UNIT_MAP.values().stream().filter((e) -> { String id = e.getID().asString(); if (id.contains(":")) { @@ -242,9 +266,13 @@ public static Unit getOrCreateUnit(String id, String name, String unit, String p * Metric system of measurement. */ public static class UnitPrefix { - public static final List unitPrefixes = new LinkedList(); + public static final List UNIT_PREFIXES = new LinkedList(); - public static final UnitPrefix MICRO = new UnitPrefix("Micro", "u", 0.000001); +// public static final UnitPrefix ATTO = new UnitPrefix("Atto", "a", 0.000000000000001); +// public static final UnitPrefix FEMTO = new UnitPrefix("Femto", "p", 0.000000000000001); +// public static final UnitPrefix PICO = new UnitPrefix("Pico", "p", 0.000000000001); +// public static final UnitPrefix NANO = new UnitPrefix("Nano", "n", 0.000000001); + public static final UnitPrefix MICRO = new UnitPrefix("Micro", "μ", 0.000001); public static final UnitPrefix MILLI = new UnitPrefix("Milli", "m", 0.001); public static final UnitPrefix BASE = new UnitPrefix("", "", 1); public static final UnitPrefix KILO = new UnitPrefix("Kilo", "k", 1000); @@ -272,7 +300,7 @@ private UnitPrefix(String name, String symbol, double value) { this.name = name; this.symbol = symbol; this.value = value; - unitPrefixes.add(this); + UNIT_PREFIXES.add(this); } public String getName(boolean getShort) { From 3aeeb362a1ecd1c8de29897f323f6e4c622fe73c Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Sun, 25 Dec 2016 21:27:18 +0100 Subject: [PATCH 05/16] Work on Minecraft 1.11 wrappers + rename EnergyItem to IEnergyStorage --- minecraft/1.11/build.gradle | 10 ++- minecraft/1.11/gradle.properties | 2 +- .../mc/forge/v1_11/NovaMinecraftEnergy.java | 50 ++++++++++++ .../wrapper/block/backward/BWEnergyBlock.java | 58 +++++++++++++ .../wrapper/block/forward/FWTileEnergy.java | 73 +++++++++++++++++ .../block/forward/FWTileEnergyLoader.java | 61 ++++++++++++++ .../block/forward/FWTileEnergyUpdater.java | 47 +++++++++++ .../energy/backward/BWEnergyStorage.java | 81 +++++++++++++++++++ .../wrapper/item/backward/BWEnergyItem.java | 59 ++++++++++++++ minecraft/1.11/src/main/resources/mcmod.info | 21 +++++ settings.gradle | 2 + src/main/java/nova/energy/EnergyStorage.java | 12 ++- .../{EnergyItem.java => IEnergyStorage.java} | 2 +- src/main/java/nova/energy/UnitConversion.java | 6 ++ 14 files changed, 479 insertions(+), 5 deletions(-) create mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java create mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlock.java create mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergy.java create mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyLoader.java create mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyUpdater.java create mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/backward/BWEnergyStorage.java create mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItem.java create mode 100644 minecraft/1.11/src/main/resources/mcmod.info rename src/main/java/nova/energy/{EnergyItem.java => IEnergyStorage.java} (98%) diff --git a/minecraft/1.11/build.gradle b/minecraft/1.11/build.gradle index 7c3f596..fe85229 100644 --- a/minecraft/1.11/build.gradle +++ b/minecraft/1.11/build.gradle @@ -29,8 +29,14 @@ artifactory { } } +task deobfJar(type: Jar) { + from sourceSets.main.output + classifier = 'deobf' +} + artifacts { archives jar + archives deobfJar } apply plugin: 'net.minecraftforge.gradle.forge' @@ -43,8 +49,8 @@ minecraft { dependencies { compile rootProject - compile group: "nova.core", name: "NovaCore", version: property("nova_version"), changing: true - compile "nova.wrapper.mc1_11:NovaWrapper-MC1.11:0.1-SNAPSHOT:deobf" + compile group: "nova.core", name: "NOVA-Core", version: property("nova_version"), changing: true + compile "nova.core:NOVA-Core-Wrapper-MC1.11:0.1.0-SNAPSHOT:deobf" } processResources { diff --git a/minecraft/1.11/gradle.properties b/minecraft/1.11/gradle.properties index 3d511be..715549e 100644 --- a/minecraft/1.11/gradle.properties +++ b/minecraft/1.11/gradle.properties @@ -6,5 +6,5 @@ forgeGradleVersion = 2.2-SNAPSHOT packaging = jar info.inceptionYear = 2016 -info.description = The wrapper of the Nova API to the MinecraftForge 1.11 modding system. +info.description = The NOVA-Minecraft Minecraft 1.11 wrapper. info.organization.name = NOVA diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java new file mode 100644 index 0000000..32a9973 --- /dev/null +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2015 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ +package nova.energy.wrapper.mc.forge.v1_11; + +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.event.FMLInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import nova.core.loader.Loadable; +import nova.core.wrapper.mc.forge.v1_11.launcher.NovaMinecraft; +import nova.core.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTileLoader; +import nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTileEnergyLoader; + +/** + * + * @author ExE Boss + */ +@Mod(modid = NovaMinecraftEnergy.id, name = NovaMinecraftEnergy.name, version = NovaMinecraftEnergy.version, acceptableRemoteVersions = "*") +public class NovaMinecraftEnergy implements Loadable { + + public static final String version = "0.0.1"; + public static final String id = "novaenergy"; + public static final String name = "NOVA Energy"; + + public NovaMinecraftEnergy() { + NovaMinecraft.registerNovaWrapper(this); + } + + @Override + public void preInit() { + FWTileLoader.registerTileLoader(new FWTileEnergyLoader()); + } +} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlock.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlock.java new file mode 100644 index 0000000..8398a38 --- /dev/null +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlock.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2015 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ +package nova.energy.wrapper.mc.forge.v1_11.wrapper.block.backward; + +import nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.backward.BWEnergyStorage; +import net.minecraft.block.Block; +import net.minecraftforge.energy.IEnergyStorage; +import nova.core.world.World; +import nova.core.wrapper.mc.forge.v1_11.wrapper.block.backward.BWBlock; +import org.apache.commons.math3.geometry.euclidean.threed.Vector3D; + +/** + * + * @author ExE Boss + */ +public class BWEnergyBlock extends BWBlock { + + private IEnergyStorage mcEnergyStorage; + + public BWEnergyBlock(Block block) { + super(block); + } + + public BWEnergyBlock(Block block, World world, Vector3D pos) { + super(block, world, pos); + if (getEnergyStorage() != null) { + components.add(new BWEnergyStorage(mcEnergyStorage)); + } + } + + public IEnergyStorage getEnergyStorage() { + if (mcEnergyStorage == null) { + if (getTileEntity() instanceof IEnergyStorage) { + mcEnergyStorage = (IEnergyStorage) getTileEntity(); + } else if (mcBlock instanceof IEnergyStorage) { + mcEnergyStorage = (IEnergyStorage) mcBlock; + } + } + return mcEnergyStorage; + } +} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergy.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergy.java new file mode 100644 index 0000000..9e9707d --- /dev/null +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergy.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2015 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ +package nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward; + +import net.minecraftforge.energy.IEnergyStorage; +import nova.core.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTile; +import nova.energy.EnergyStorage; +import nova.energy.UnitConversion; +import nova.energy.UnitDisplay; + +/** + * + * @author ExE Boss + */ +public class FWTileEnergy extends FWTile implements IEnergyStorage { + + private static final UnitConversion RF_JOULE = UnitConversion.getConvertion(UnitDisplay.Unit.REDFLUX, UnitDisplay.Unit.JOULE).get(); + private static final UnitConversion JOULE_RF = RF_JOULE.reverse(); + + public FWTileEnergy() { + } + + public FWTileEnergy(String blockID) { + this.blockID = blockID; + } + + @Override + public int receiveEnergy(int maxReceive, boolean simulate) { + return (int) Math.round(JOULE_RF.convert(getBlock().components.get(EnergyStorage.class).recharge(RF_JOULE.convert(maxReceive), !simulate))); + } + + @Override + public int extractEnergy(int maxExtract, boolean simulate) { + return (int) Math.round(JOULE_RF.convert(getBlock().components.get(EnergyStorage.class).discharge(RF_JOULE.convert(maxExtract), !simulate))); + } + + @Override + public int getEnergyStored() { + return (int) Math.round(JOULE_RF.convert(getBlock().components.get(EnergyStorage.class).getEnergy())); + } + + @Override + public int getMaxEnergyStored() { + return (int) Math.round(JOULE_RF.convert(getBlock().components.get(EnergyStorage.class).getEnergyCapacity())); + } + + @Override + public boolean canExtract() { + return getBlock().components.get(EnergyStorage.class).canDischarge(); + } + + @Override + public boolean canReceive() { + return getBlock().components.get(EnergyStorage.class).canRecharge(); + } +} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyLoader.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyLoader.java new file mode 100644 index 0000000..b3f6ca5 --- /dev/null +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyLoader.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2015 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ +package nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward; + +import net.minecraft.nbt.NBTTagCompound; +import nova.core.block.Block; +import nova.core.component.Updater; +import nova.core.wrapper.mc.forge.v1_11.asm.lib.ComponentInjector; +import nova.core.wrapper.mc.forge.v1_11.wrapper.block.forward.FWCustomTileLoader; +import nova.core.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTile; +import nova.energy.EnergyStorage; + +/** + * + * @author ExE Boss + */ +public class FWTileEnergyLoader implements FWCustomTileLoader { + + private static ComponentInjector injector = new ComponentInjector<>(FWTileEnergy.class); + private static ComponentInjector updaterInjector = new ComponentInjector<>(FWTileEnergyUpdater.class); + + @Override + public boolean isBlockSupported(Block block) { + return (block.components.has(EnergyStorage.class)); + } + + @Override + public FWTile loadTile(Block block, NBTTagCompound data) { + if (block.components.has(EnergyStorage.class)) { + FWTile tile = (block instanceof Updater) ? updaterInjector.inject(block, new Class[0], new Object[0]) : injector.inject(block, new Class[0], new Object[0]); + tile.setBlock(block); + return tile; + } + return null; + } + + @Override + public FWTile loadTile(Block block, String blockID) { + if (block.components.has(EnergyStorage.class)) { + + } + return null; + } +} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyUpdater.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyUpdater.java new file mode 100644 index 0000000..86dece6 --- /dev/null +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyUpdater.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2015 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ +package nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward; + +import net.minecraft.util.ITickable; +import nova.core.component.Updater; + +/** + * + * @author ExE Boss + */ +public class FWTileEnergyUpdater extends FWTileEnergy implements ITickable { + + public FWTileEnergyUpdater() { + } + + public FWTileEnergyUpdater(String blockID) { + this.blockID = blockID; + } + + /** + * Updates the block. + */ + @Override + public void update() { + if (block != null) { + ((Updater) block).update(0.05); + } + } +} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/backward/BWEnergyStorage.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/backward/BWEnergyStorage.java new file mode 100644 index 0000000..02ff9e3 --- /dev/null +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/backward/BWEnergyStorage.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2015 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ +package nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.backward; + +import net.minecraftforge.energy.IEnergyStorage; +import nova.energy.EnergyStorage; +import nova.energy.UnitConversion; +import nova.energy.UnitDisplay; + +/** + * + * @author ExE Boss + */ +public class BWEnergyStorage extends EnergyStorage { + + private static final UnitConversion RF_JOULE = UnitConversion.getConvertion(UnitDisplay.Unit.REDFLUX, UnitDisplay.Unit.JOULE).get(); + private static final UnitConversion JOULE_RF = RF_JOULE.reverse(); + + private final IEnergyStorage mcEnergyStorage; + + public BWEnergyStorage(IEnergyStorage mcEnergyStorage) { + this.mcEnergyStorage = mcEnergyStorage; + } + + @Override + public double recharge(double energy, boolean doRecharge) { + return RF_JOULE.convert(mcEnergyStorage.receiveEnergy((int) Math.round(JOULE_RF.convert(energy)), !doRecharge)); + } + + @Override + public double discharge(double energy, boolean doDischarge) { + return RF_JOULE.convert(mcEnergyStorage.extractEnergy((int) Math.round(JOULE_RF.convert(energy)), !doDischarge)); + } + + @Override + public double getEnergy() { + return RF_JOULE.convert(mcEnergyStorage.getEnergyStored()); + } + + @Override + public void setEnergy(double energy) { + int rf = (int) Math.round(JOULE_RF.convert(energy)) - mcEnergyStorage.getEnergyStored(); + if (rf > 0) { + mcEnergyStorage.receiveEnergy(rf, false); + } else if (rf < 0) { + mcEnergyStorage.extractEnergy(-rf, false); + } + } + + @Override + public double getEnergyCapacity() { + return RF_JOULE.convert(mcEnergyStorage.getMaxEnergyStored()); + } + + @Override + public boolean canDischarge() { + return mcEnergyStorage.canExtract(); + } + + @Override + public boolean canRecharge() { + return mcEnergyStorage.canReceive(); + } +} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItem.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItem.java new file mode 100644 index 0000000..a08af45 --- /dev/null +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItem.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2015 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NOVA. If not, see . + */ +package nova.energy.wrapper.mc.forge.v1_11.wrapper.item.backward; + +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.energy.IEnergyStorage; +import nova.core.wrapper.mc.forge.v1_11.wrapper.item.backward.BWItem; +import nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.backward.BWEnergyStorage; + +/** + * + * @author ExE Boss + */ +public class BWEnergyItem extends BWItem { + + private IEnergyStorage mcEnergyStorage; + + public BWEnergyItem(ItemStack itemStack) { + super(itemStack); + if (getEnergyStorage() != null) { + components.add(new BWEnergyStorage(mcEnergyStorage)); + } + } + + public BWEnergyItem(Item item, int meta, NBTTagCompound tag) { + super(item, meta, tag); + if (getEnergyStorage() != null) { + components.add(new BWEnergyStorage(mcEnergyStorage)); + } + } + + public IEnergyStorage getEnergyStorage() { + if (mcEnergyStorage == null) { + if (getItem() instanceof IEnergyStorage) { + mcEnergyStorage = (IEnergyStorage) getItem(); + } + } + return mcEnergyStorage; + } +} diff --git a/minecraft/1.11/src/main/resources/mcmod.info b/minecraft/1.11/src/main/resources/mcmod.info new file mode 100644 index 0000000..ec9763e --- /dev/null +++ b/minecraft/1.11/src/main/resources/mcmod.info @@ -0,0 +1,21 @@ +{ + "modListVersion": 2, + "modList": [{ + "modid": "novaenergy", + "name": "NOVA Energy", + "description": "The Energy component of NOVA.", + "version": "0.0.1", + "mcversion": "1.11", + "url": "https://novaapi.net/", + "updateUrl": "", + "authorList": [ "calclavia", "RX14", "AEnterprise", "magik6k", "Shadowfacts" ], + "credits": "Created by the NOVA Team.\nPorted to Minecraft 1.11 by: ExE Boss.", + "logoFile": "", + "screenshots": [], + "parent": "", + "requiredMods": [], + "dependencies": [], + "dependants": [], + "useDependencyInformation": true + }] +} diff --git a/settings.gradle b/settings.gradle index 7b75a36..3a7c2c0 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,5 @@ rootProject.name = 'NOVA-Energy' include "minecraft:1.11" + +project(":minecraft:1.11").name = "NOVA-Energy-Wrapper-MC1.11" diff --git a/src/main/java/nova/energy/EnergyStorage.java b/src/main/java/nova/energy/EnergyStorage.java index 31b666e..a5174b0 100644 --- a/src/main/java/nova/energy/EnergyStorage.java +++ b/src/main/java/nova/energy/EnergyStorage.java @@ -27,7 +27,7 @@ * * @author ExE Boss */ -public class EnergyStorage extends Component implements EnergyItem, Storable { +public class EnergyStorage extends Component implements IEnergyStorage, Storable { @Store protected double energy; @@ -35,6 +35,9 @@ public class EnergyStorage extends Component implements EnergyItem, Storable { protected double maxRecharge; protected double maxDischarge; + protected EnergyStorage() { + } + public EnergyStorage(double maxEnergy) { this(maxEnergy, maxEnergy, maxEnergy); } @@ -49,6 +52,13 @@ public EnergyStorage(double maxEnergy, double maxRecharge, double maxDischarge) this.maxDischarge = Math.min(0, Math.max(maxDischarge, maxEnergy)); } + public EnergyStorage(double energy, double maxEnergy, double maxRecharge, double maxDischarge) { + this.energy = energy; + this.maxEnergy = maxEnergy; + this.maxRecharge = maxRecharge; + this.maxDischarge = maxDischarge; + } + @Override public double recharge(double energy, boolean doRecharge) { if (!canRecharge()) return 0; diff --git a/src/main/java/nova/energy/EnergyItem.java b/src/main/java/nova/energy/IEnergyStorage.java similarity index 98% rename from src/main/java/nova/energy/EnergyItem.java rename to src/main/java/nova/energy/IEnergyStorage.java index 66771a3..5f716f5 100644 --- a/src/main/java/nova/energy/EnergyItem.java +++ b/src/main/java/nova/energy/IEnergyStorage.java @@ -23,7 +23,7 @@ /** * An interface for items that store energy in joules. */ -public interface EnergyItem { +public interface IEnergyStorage { /** * Adds energy to an item. Returns the quantity of energy that was accepted. This should always diff --git a/src/main/java/nova/energy/UnitConversion.java b/src/main/java/nova/energy/UnitConversion.java index fb761d3..80fcdde 100644 --- a/src/main/java/nova/energy/UnitConversion.java +++ b/src/main/java/nova/energy/UnitConversion.java @@ -66,6 +66,12 @@ public Unit unit2() { public double ratio() { return ratio; } + + /** + * Get the {@link UnitConversion} for the opposite direction. + * + * @return The {@link UnitConversion} for the opposite direction. + */ public UnitConversion reverse() { return reverse; } From e93b98b70ab7c56628fc27e26efb3b44a8e17886 Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Sun, 25 Dec 2016 22:00:21 +0100 Subject: [PATCH 06/16] Fix gradle.properties --- minecraft/1.11/gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minecraft/1.11/gradle.properties b/minecraft/1.11/gradle.properties index 715549e..4b8ef0a 100644 --- a/minecraft/1.11/gradle.properties +++ b/minecraft/1.11/gradle.properties @@ -1,4 +1,4 @@ -group = nova.core +group = nova.energy minecraft.version = 1.11 forge.version = 13.19.1.2189 From eaef75c282e897d2ea2215d00ca5f82345e30c8c Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Mon, 2 Jan 2017 20:49:30 +0100 Subject: [PATCH 07/16] Remove IEnergyStorage + Finished BWEnergyBlock and BWEnergyItem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TODO: Minecraft Forge energy items most likely don’t work by implementing IEnergyStorage, as that seems to be intended for tile entities. Additional changes: Revert subproject display name change. --- build.gradle | 1 + minecraft/1.11/build.gradle | 2 +- .../mc/forge/v1_11/NovaMinecraftEnergy.java | 9 ++- .../wrapper/block/backward/BWEnergyBlock.java | 58 --------------- .../BWEnergyBlockComponentHandler.java | 25 +++++++ .../wrapper/item/backward/BWEnergyItem.java | 59 --------------- .../BWEnergyItemComponentHandler.java | 25 +++++++ settings.gradle | 2 - src/main/java/nova/energy/EnergyStorage.java | 42 +++++++++-- src/main/java/nova/energy/IEnergyStorage.java | 73 ------------------- 10 files changed, 92 insertions(+), 204 deletions(-) delete mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlock.java create mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlockComponentHandler.java delete mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItem.java create mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItemComponentHandler.java delete mode 100644 src/main/java/nova/energy/IEnergyStorage.java diff --git a/build.gradle b/build.gradle index bbd920c..1365b75 100644 --- a/build.gradle +++ b/build.gradle @@ -21,6 +21,7 @@ nova { } "1_11" { wrapper "nova.core:NOVA-Core-Wrapper-MC1.11:$nova_version" + runtime project(":minecraft:1.11") } } } diff --git a/minecraft/1.11/build.gradle b/minecraft/1.11/build.gradle index fe85229..d528675 100644 --- a/minecraft/1.11/build.gradle +++ b/minecraft/1.11/build.gradle @@ -50,7 +50,7 @@ minecraft { dependencies { compile rootProject compile group: "nova.core", name: "NOVA-Core", version: property("nova_version"), changing: true - compile "nova.core:NOVA-Core-Wrapper-MC1.11:0.1.0-SNAPSHOT:deobf" + compile group: "nova.core", name: "NOVA-Core-Wrapper-MC1.11", version: property("nova_version"), classifier: 'deobf', changing: true } processResources { diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java index 32a9973..f0d0d42 100644 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java @@ -20,13 +20,14 @@ package nova.energy.wrapper.mc.forge.v1_11; import net.minecraftforge.fml.common.Mod; -import net.minecraftforge.fml.common.event.FMLInitializationEvent; -import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; -import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import nova.core.loader.Loadable; import nova.core.wrapper.mc.forge.v1_11.launcher.NovaMinecraft; +import nova.core.wrapper.mc.forge.v1_11.wrapper.block.backward.BWBlockComponentHandler; import nova.core.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTileLoader; +import nova.core.wrapper.mc.forge.v1_11.wrapper.item.backward.BWItemComponentHandler; +import nova.energy.wrapper.mc.forge.v1_11.wrapper.block.backward.BWEnergyBlockComponentHandler; import nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTileEnergyLoader; +import nova.energy.wrapper.mc.forge.v1_11.wrapper.item.backward.BWEnergyItemComponentHandler; /** * @@ -46,5 +47,7 @@ public NovaMinecraftEnergy() { @Override public void preInit() { FWTileLoader.registerTileLoader(new FWTileEnergyLoader()); + BWBlockComponentHandler.register(new BWEnergyBlockComponentHandler()); + BWItemComponentHandler.register(new BWEnergyItemComponentHandler()); } } diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlock.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlock.java deleted file mode 100644 index 8398a38..0000000 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlock.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2015 NOVA, All rights reserved. - * This library is free software, licensed under GNU Lesser General Public License version 3 - * - * This file is part of NOVA. - * - * NOVA is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * NOVA is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with NOVA. If not, see . - */ -package nova.energy.wrapper.mc.forge.v1_11.wrapper.block.backward; - -import nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.backward.BWEnergyStorage; -import net.minecraft.block.Block; -import net.minecraftforge.energy.IEnergyStorage; -import nova.core.world.World; -import nova.core.wrapper.mc.forge.v1_11.wrapper.block.backward.BWBlock; -import org.apache.commons.math3.geometry.euclidean.threed.Vector3D; - -/** - * - * @author ExE Boss - */ -public class BWEnergyBlock extends BWBlock { - - private IEnergyStorage mcEnergyStorage; - - public BWEnergyBlock(Block block) { - super(block); - } - - public BWEnergyBlock(Block block, World world, Vector3D pos) { - super(block, world, pos); - if (getEnergyStorage() != null) { - components.add(new BWEnergyStorage(mcEnergyStorage)); - } - } - - public IEnergyStorage getEnergyStorage() { - if (mcEnergyStorage == null) { - if (getTileEntity() instanceof IEnergyStorage) { - mcEnergyStorage = (IEnergyStorage) getTileEntity(); - } else if (mcBlock instanceof IEnergyStorage) { - mcEnergyStorage = (IEnergyStorage) mcBlock; - } - } - return mcEnergyStorage; - } -} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlockComponentHandler.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlockComponentHandler.java new file mode 100644 index 0000000..fe1d79f --- /dev/null +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlockComponentHandler.java @@ -0,0 +1,25 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package nova.energy.wrapper.mc.forge.v1_11.wrapper.block.backward; + +import net.minecraft.block.Block; +import net.minecraftforge.energy.IEnergyStorage; +import nova.core.wrapper.mc.forge.v1_11.wrapper.block.backward.BWBlock; +import nova.core.wrapper.mc.forge.v1_11.wrapper.block.backward.BWBlockComponentHandler; +import nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.backward.BWEnergyStorage; + +/** + * + * @author ExE Boss + */ +public class BWEnergyBlockComponentHandler implements BWBlockComponentHandler { + + @Override + public void addComponents(BWBlock novaBlock, Block mcBlock) { + if (novaBlock.getTileEntity() instanceof IEnergyStorage) + novaBlock.components.add(new BWEnergyStorage((IEnergyStorage) novaBlock.getTileEntity())); + } +} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItem.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItem.java deleted file mode 100644 index a08af45..0000000 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItem.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2015 NOVA, All rights reserved. - * This library is free software, licensed under GNU Lesser General Public License version 3 - * - * This file is part of NOVA. - * - * NOVA is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * NOVA is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with NOVA. If not, see . - */ -package nova.energy.wrapper.mc.forge.v1_11.wrapper.item.backward; - -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraftforge.energy.IEnergyStorage; -import nova.core.wrapper.mc.forge.v1_11.wrapper.item.backward.BWItem; -import nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.backward.BWEnergyStorage; - -/** - * - * @author ExE Boss - */ -public class BWEnergyItem extends BWItem { - - private IEnergyStorage mcEnergyStorage; - - public BWEnergyItem(ItemStack itemStack) { - super(itemStack); - if (getEnergyStorage() != null) { - components.add(new BWEnergyStorage(mcEnergyStorage)); - } - } - - public BWEnergyItem(Item item, int meta, NBTTagCompound tag) { - super(item, meta, tag); - if (getEnergyStorage() != null) { - components.add(new BWEnergyStorage(mcEnergyStorage)); - } - } - - public IEnergyStorage getEnergyStorage() { - if (mcEnergyStorage == null) { - if (getItem() instanceof IEnergyStorage) { - mcEnergyStorage = (IEnergyStorage) getItem(); - } - } - return mcEnergyStorage; - } -} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItemComponentHandler.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItemComponentHandler.java new file mode 100644 index 0000000..deeb5f4 --- /dev/null +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItemComponentHandler.java @@ -0,0 +1,25 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package nova.energy.wrapper.mc.forge.v1_11.wrapper.item.backward; + +import net.minecraft.item.Item; +import net.minecraftforge.energy.IEnergyStorage; +import nova.core.wrapper.mc.forge.v1_11.wrapper.item.backward.BWItem; +import nova.core.wrapper.mc.forge.v1_11.wrapper.item.backward.BWItemComponentHandler; +import nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.backward.BWEnergyStorage; + +/** + * + * @author ExE Boss + */ +public class BWEnergyItemComponentHandler implements BWItemComponentHandler { + + @Override + public void addComponents(BWItem novaItem, Item mcItem) { + if (mcItem instanceof IEnergyStorage) + novaItem.components.add(new BWEnergyStorage((IEnergyStorage) mcItem)); + } +} diff --git a/settings.gradle b/settings.gradle index 3a7c2c0..7b75a36 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,5 +1,3 @@ rootProject.name = 'NOVA-Energy' include "minecraft:1.11" - -project(":minecraft:1.11").name = "NOVA-Energy-Wrapper-MC1.11" diff --git a/src/main/java/nova/energy/EnergyStorage.java b/src/main/java/nova/energy/EnergyStorage.java index a5174b0..4277d26 100644 --- a/src/main/java/nova/energy/EnergyStorage.java +++ b/src/main/java/nova/energy/EnergyStorage.java @@ -24,10 +24,11 @@ import nova.core.retention.Store; /** + * A component for items and blocks that store energy in joules. * * @author ExE Boss */ -public class EnergyStorage extends Component implements IEnergyStorage, Storable { +public class EnergyStorage extends Component implements Storable { @Store protected double energy; @@ -59,7 +60,14 @@ public EnergyStorage(double energy, double maxEnergy, double maxRecharge, double this.maxDischarge = maxDischarge; } - @Override + /** + * 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 energy Maximum amount of energy to be sent into the item (in Joules). + * @param doRecharge If false, the charge will only be simulated. + * @return Amount of energy that was accepted by the item (in Joules). + */ public double recharge(double energy, boolean doRecharge) { if (!canRecharge()) return 0; @@ -69,7 +77,14 @@ public double recharge(double energy, boolean doRecharge) { return energy; } - @Override + /** + * 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 energy Maximum amount of energy to be removed from the item (in Joules). + * @param doDischarge If false, the discharge will only be simulated. + * @return Amount of energy that was removed from the item (in Joules). + */ public double discharge(double energy, boolean doDischarge) { if (!canRecharge()) return 0; @@ -79,27 +94,38 @@ public double discharge(double energy, boolean doDischarge) { return energy; } - @Override + /** + * Get the amount of energy currently stored in the item. + */ public double getEnergy() { return this.energy; } - @Override + /** + * Sets the amount of energy in the ItemStack. + * @param energy - Amount of electrical energy (in Joules). + */ public void setEnergy(double energy) { this.energy = Math.min(0, Math.max(energy, maxEnergy)); } - @Override + /** + * Get the max amount of energy that can be stored in the item. + */ public double getEnergyCapacity() { return maxEnergy; } - @Override + /** + * @return Whether or not this item can be externally recharged. + */ public boolean canRecharge() { return maxRecharge > 0; } - @Override + /** + * @return Whether or not this item can be externally discharged. + */ public boolean canDischarge() { return maxDischarge > 0; } diff --git a/src/main/java/nova/energy/IEnergyStorage.java b/src/main/java/nova/energy/IEnergyStorage.java deleted file mode 100644 index 5f716f5..0000000 --- a/src/main/java/nova/energy/IEnergyStorage.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2015 NOVA, All rights reserved. - * This library is free software, licensed under GNU Lesser General Public License version 3 - * - * This file is part of NOVA. - * - * NOVA is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * NOVA is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with NOVA. If not, see . - */ - -package nova.energy; - -/** - * An interface for items that store energy in joules. - */ -public interface IEnergyStorage { - - /** - * 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 energy Maximum amount of energy to be sent into the item (in Joules). - * @param doRecharge If false, the charge will only be simulated. - * @return Amount of energy that was accepted by the item (in Joules). - */ - public double recharge(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 energy Maximum amount of energy to be removed from the item (in Joules). - * @param doDischarge If false, the discharge will only be simulated. - * @return Amount of energy that was removed from the item (in Joules). - */ - public double discharge(double energy, boolean doDischarge); - - /** - * Get the amount of energy currently stored in the item. - */ - public double getEnergy(); - - /** - * Sets the amount of energy in the ItemStack. - * @param energy - Amount of electrical energy (in Joules). - */ - public void setEnergy(double energy); - - /** - * Get the max amount of energy that can be stored in the item. - */ - public double getEnergyCapacity(); - - /** - * @return Whether or not this item can be externally recharged. - */ - public boolean canRecharge(); - - /** - * @return Whether or not this item can be externally discharged. - */ - public boolean canDischarge(); -} From bd8f5ba3e39271f3b708bafcf5e97711efda0884 Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Mon, 2 Jan 2017 21:02:09 +0100 Subject: [PATCH 08/16] Prevent initializing of EnergyStorage with more energy than maximum. --- src/main/java/nova/energy/EnergyStorage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/nova/energy/EnergyStorage.java b/src/main/java/nova/energy/EnergyStorage.java index 4277d26..8e45a14 100644 --- a/src/main/java/nova/energy/EnergyStorage.java +++ b/src/main/java/nova/energy/EnergyStorage.java @@ -54,8 +54,8 @@ public EnergyStorage(double maxEnergy, double maxRecharge, double maxDischarge) } public EnergyStorage(double energy, double maxEnergy, double maxRecharge, double maxDischarge) { - this.energy = energy; this.maxEnergy = maxEnergy; + this.setEnergy(energy); this.maxRecharge = maxRecharge; this.maxDischarge = maxDischarge; } From 1a493b9395b8168f308cfedbc1a3c59d46550134 Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Tue, 3 Jan 2017 20:42:06 +0100 Subject: [PATCH 09/16] Changed component handlers and custom tile loaders to events + Changed wrapper to NOVA Mod Reason: Consistency with the rest of the NOVA wrappers. --- .../mc/forge/v1_11/NovaMinecraftEnergy.java | 58 +++++++++++++----- .../BWEnergyBlockComponentHandler.java | 25 -------- .../block/forward/FWTileEnergyLoader.java | 61 ------------------- .../BWEnergyItemComponentHandler.java | 25 -------- 4 files changed, 44 insertions(+), 125 deletions(-) delete mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlockComponentHandler.java delete mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyLoader.java delete mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItemComponentHandler.java diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java index f0d0d42..18b2eeb 100644 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java @@ -19,35 +19,65 @@ */ package nova.energy.wrapper.mc.forge.v1_11; -import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.energy.IEnergyStorage; +import nova.core.component.Updater; +import nova.core.event.bus.GlobalEvents; import nova.core.loader.Loadable; -import nova.core.wrapper.mc.forge.v1_11.launcher.NovaMinecraft; -import nova.core.wrapper.mc.forge.v1_11.wrapper.block.backward.BWBlockComponentHandler; -import nova.core.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTileLoader; -import nova.core.wrapper.mc.forge.v1_11.wrapper.item.backward.BWItemComponentHandler; -import nova.energy.wrapper.mc.forge.v1_11.wrapper.block.backward.BWEnergyBlockComponentHandler; -import nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTileEnergyLoader; -import nova.energy.wrapper.mc.forge.v1_11.wrapper.item.backward.BWEnergyItemComponentHandler; +import nova.core.loader.Mod; +import nova.core.wrapper.mc.forge.v1_11.asm.lib.ComponentInjector; +import nova.core.wrapper.mc.forge.v1_11.util.WrapperEvent; +import nova.energy.EnergyStorage; +import nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTileEnergy; +import nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTileEnergyUpdater; +import nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.backward.BWEnergyStorage; /** * * @author ExE Boss */ -@Mod(modid = NovaMinecraftEnergy.id, name = NovaMinecraftEnergy.name, version = NovaMinecraftEnergy.version, acceptableRemoteVersions = "*") +@Mod(id = NovaMinecraftEnergy.id, name = NovaMinecraftEnergy.name, version = NovaMinecraftEnergy.version, novaVersion = "0.1.0") public class NovaMinecraftEnergy implements Loadable { public static final String version = "0.0.1"; public static final String id = "novaenergy"; public static final String name = "NOVA Energy"; - public NovaMinecraftEnergy() { - NovaMinecraft.registerNovaWrapper(this); + private static ComponentInjector injector = new ComponentInjector<>(FWTileEnergy.class); + private static ComponentInjector updaterInjector = new ComponentInjector<>(FWTileEnergyUpdater.class); + + private final GlobalEvents events; + + public NovaMinecraftEnergy(GlobalEvents events) { + this.events = events; } @Override public void preInit() { - FWTileLoader.registerTileLoader(new FWTileEnergyLoader()); - BWBlockComponentHandler.register(new BWEnergyBlockComponentHandler()); - BWItemComponentHandler.register(new BWEnergyItemComponentHandler()); + events.on(WrapperEvent.BWBlockCreate.class).bind(evt -> { + if (evt.novaBlock.getTileEntity() instanceof IEnergyStorage) + evt.novaBlock.components.add(new BWEnergyStorage((IEnergyStorage) evt.novaBlock.getTileEntity())); + }); + events.on(WrapperEvent.BWItemCreate.class).bind(evt -> { + if (evt.mcItem instanceof IEnergyStorage) + evt.novaItem.components.add(new BWEnergyStorage((IEnergyStorage) evt.mcItem)); + }); + events.on(WrapperEvent.FWTileLoad.class).bind(evt -> { + if (!evt.block.components.has(EnergyStorage.class)) return; + if (evt.hasResult()) return; + + FWTileEnergy tile; + if (evt.data.isPresent()) { + tile = (evt.block instanceof Updater) ? + updaterInjector.inject(evt.block, new Class[0], new Object[0]) : + injector.inject(evt.block, new Class[0], new Object[0]); + tile.setBlock(evt.block); + } else { + tile = (evt.block instanceof Updater) ? + updaterInjector.inject(evt.block, new Class[] { String.class }, new Object[] { evt.block.getID().asString() }) : + injector.inject(evt.block, new Class[] { String.class }, new Object[] { evt.block.getID().asString() }); + tile.setBlock(evt.block); + } + evt.setResult(tile); + }); } } diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlockComponentHandler.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlockComponentHandler.java deleted file mode 100644 index fe1d79f..0000000 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/backward/BWEnergyBlockComponentHandler.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package nova.energy.wrapper.mc.forge.v1_11.wrapper.block.backward; - -import net.minecraft.block.Block; -import net.minecraftforge.energy.IEnergyStorage; -import nova.core.wrapper.mc.forge.v1_11.wrapper.block.backward.BWBlock; -import nova.core.wrapper.mc.forge.v1_11.wrapper.block.backward.BWBlockComponentHandler; -import nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.backward.BWEnergyStorage; - -/** - * - * @author ExE Boss - */ -public class BWEnergyBlockComponentHandler implements BWBlockComponentHandler { - - @Override - public void addComponents(BWBlock novaBlock, Block mcBlock) { - if (novaBlock.getTileEntity() instanceof IEnergyStorage) - novaBlock.components.add(new BWEnergyStorage((IEnergyStorage) novaBlock.getTileEntity())); - } -} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyLoader.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyLoader.java deleted file mode 100644 index b3f6ca5..0000000 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyLoader.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2015 NOVA, All rights reserved. - * This library is free software, licensed under GNU Lesser General Public License version 3 - * - * This file is part of NOVA. - * - * NOVA is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * NOVA is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with NOVA. If not, see . - */ -package nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward; - -import net.minecraft.nbt.NBTTagCompound; -import nova.core.block.Block; -import nova.core.component.Updater; -import nova.core.wrapper.mc.forge.v1_11.asm.lib.ComponentInjector; -import nova.core.wrapper.mc.forge.v1_11.wrapper.block.forward.FWCustomTileLoader; -import nova.core.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTile; -import nova.energy.EnergyStorage; - -/** - * - * @author ExE Boss - */ -public class FWTileEnergyLoader implements FWCustomTileLoader { - - private static ComponentInjector injector = new ComponentInjector<>(FWTileEnergy.class); - private static ComponentInjector updaterInjector = new ComponentInjector<>(FWTileEnergyUpdater.class); - - @Override - public boolean isBlockSupported(Block block) { - return (block.components.has(EnergyStorage.class)); - } - - @Override - public FWTile loadTile(Block block, NBTTagCompound data) { - if (block.components.has(EnergyStorage.class)) { - FWTile tile = (block instanceof Updater) ? updaterInjector.inject(block, new Class[0], new Object[0]) : injector.inject(block, new Class[0], new Object[0]); - tile.setBlock(block); - return tile; - } - return null; - } - - @Override - public FWTile loadTile(Block block, String blockID) { - if (block.components.has(EnergyStorage.class)) { - - } - return null; - } -} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItemComponentHandler.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItemComponentHandler.java deleted file mode 100644 index deeb5f4..0000000 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/item/backward/BWEnergyItemComponentHandler.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package nova.energy.wrapper.mc.forge.v1_11.wrapper.item.backward; - -import net.minecraft.item.Item; -import net.minecraftforge.energy.IEnergyStorage; -import nova.core.wrapper.mc.forge.v1_11.wrapper.item.backward.BWItem; -import nova.core.wrapper.mc.forge.v1_11.wrapper.item.backward.BWItemComponentHandler; -import nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.backward.BWEnergyStorage; - -/** - * - * @author ExE Boss - */ -public class BWEnergyItemComponentHandler implements BWItemComponentHandler { - - @Override - public void addComponents(BWItem novaItem, Item mcItem) { - if (mcItem instanceof IEnergyStorage) - novaItem.components.add(new BWEnergyStorage((IEnergyStorage) mcItem)); - } -} From 71d65d19724064f561e7d8f6624be7550d431fb7 Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Tue, 3 Jan 2017 20:49:03 +0100 Subject: [PATCH 10/16] Update javadoc + indentation in UnitDisplay class --- .../mc/forge/v1_11/NovaMinecraftEnergy.java | 1 + src/main/java/nova/energy/UnitDisplay.java | 22 ++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java index 18b2eeb..e9f504f 100644 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java @@ -32,6 +32,7 @@ import nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.backward.BWEnergyStorage; /** + * Compatibility with Forge Energy, which has been in Minecraft Forge since Minecraft 1.10.2. * * @author ExE Boss */ diff --git a/src/main/java/nova/energy/UnitDisplay.java b/src/main/java/nova/energy/UnitDisplay.java index 9ea3e33..06799b5 100644 --- a/src/main/java/nova/energy/UnitDisplay.java +++ b/src/main/java/nova/energy/UnitDisplay.java @@ -268,19 +268,21 @@ public static Unit getOrCreateUnit(String id, String name, String unit, String p public static class UnitPrefix { public static final List UNIT_PREFIXES = new LinkedList(); -// public static final UnitPrefix ATTO = new UnitPrefix("Atto", "a", 0.000000000000001); +// public static final UnitPrefix YOCTO = new UnitPrefix("Yocto", "y", 0.000000000000000000000001); +// public static final UnitPrefix ZEPTO = new UnitPrefix("Zepto", "z", 0.000000000000000000001); +// public static final UnitPrefix ATTO = new UnitPrefix("Atto", "a", 0.000000000000000001); // public static final UnitPrefix FEMTO = new UnitPrefix("Femto", "p", 0.000000000000001); -// public static final UnitPrefix PICO = new UnitPrefix("Pico", "p", 0.000000000001); -// public static final UnitPrefix NANO = new UnitPrefix("Nano", "n", 0.000000001); +// public static final UnitPrefix PICO = new UnitPrefix("Pico", "p", 0.000000000001); +// public static final UnitPrefix NANO = new UnitPrefix("Nano", "n", 0.000000001); public static final UnitPrefix MICRO = new UnitPrefix("Micro", "μ", 0.000001); public static final UnitPrefix MILLI = new UnitPrefix("Milli", "m", 0.001); - public static final UnitPrefix BASE = new UnitPrefix("", "", 1); - public static final UnitPrefix KILO = new UnitPrefix("Kilo", "k", 1000); - public static final UnitPrefix MEGA = new UnitPrefix("Mega", "M", 1000000); - public static final UnitPrefix GIGA = new UnitPrefix("Giga", "G", 1000000000); - public static final UnitPrefix TERA = new UnitPrefix("Tera", "T", 1000000000000d); - public static final UnitPrefix PETA = new UnitPrefix("Peta", "P", 1000000000000000d); - public static final UnitPrefix EXA = new UnitPrefix("Exa", "E", 1000000000000000000d); + public static final UnitPrefix BASE = new UnitPrefix("", "", 1); + public static final UnitPrefix KILO = new UnitPrefix("Kilo", "k", 1000); + public static final UnitPrefix MEGA = new UnitPrefix("Mega", "M", 1000000); + public static final UnitPrefix GIGA = new UnitPrefix("Giga", "G", 1000000000); + public static final UnitPrefix TERA = new UnitPrefix("Tera", "T", 1000000000000d); + public static final UnitPrefix PETA = new UnitPrefix("Peta", "P", 1000000000000000d); + public static final UnitPrefix EXA = new UnitPrefix("Exa", "E", 1000000000000000000d); public static final UnitPrefix ZETTA = new UnitPrefix("Zetta", "Z", 1000000000000000000000d); public static final UnitPrefix YOTTA = new UnitPrefix("Yotta", "Y", 1000000000000000000000000d); /** From 958c18ee2870fdfe84c9b4b3d0b57db8f143c148 Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Wed, 4 Jan 2017 19:40:28 +0100 Subject: [PATCH 11/16] Switch to capabilities Thanks @shadowfacts for pointing me in the right direction at MinecraftForge/MinecraftForge#3599. --- .../mc/forge/v1_11/NovaMinecraftEnergy.java | 66 ++++++++++------- .../wrapper/block/forward/FWTileEnergy.java | 73 ------------------- .../block/forward/FWTileEnergyUpdater.java | 47 ------------ .../energy/forward/FWEnergyStorage.java | 70 ++++++++++++++++++ 4 files changed, 109 insertions(+), 147 deletions(-) delete mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergy.java delete mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyUpdater.java create mode 100644 minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/forward/FWEnergyStorage.java diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java index e9f504f..bd94861 100644 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java @@ -19,17 +19,16 @@ */ package nova.energy.wrapper.mc.forge.v1_11; +import net.minecraft.util.EnumFacing; +import net.minecraftforge.energy.CapabilityEnergy; import net.minecraftforge.energy.IEnergyStorage; -import nova.core.component.Updater; import nova.core.event.bus.GlobalEvents; import nova.core.loader.Loadable; import nova.core.loader.Mod; -import nova.core.wrapper.mc.forge.v1_11.asm.lib.ComponentInjector; import nova.core.wrapper.mc.forge.v1_11.util.WrapperEvent; import nova.energy.EnergyStorage; -import nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTileEnergy; -import nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTileEnergyUpdater; import nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.backward.BWEnergyStorage; +import nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.forward.FWEnergyStorage; /** * Compatibility with Forge Energy, which has been in Minecraft Forge since Minecraft 1.10.2. @@ -43,9 +42,6 @@ public class NovaMinecraftEnergy implements Loadable { public static final String id = "novaenergy"; public static final String name = "NOVA Energy"; - private static ComponentInjector injector = new ComponentInjector<>(FWTileEnergy.class); - private static ComponentInjector updaterInjector = new ComponentInjector<>(FWTileEnergyUpdater.class); - private final GlobalEvents events; public NovaMinecraftEnergy(GlobalEvents events) { @@ -55,30 +51,46 @@ public NovaMinecraftEnergy(GlobalEvents events) { @Override public void preInit() { events.on(WrapperEvent.BWBlockCreate.class).bind(evt -> { - if (evt.novaBlock.getTileEntity() instanceof IEnergyStorage) - evt.novaBlock.components.add(new BWEnergyStorage((IEnergyStorage) evt.novaBlock.getTileEntity())); + IEnergyStorage energyCapability = null; + for (EnumFacing facing : EnumFacing.values()) { + if (!evt.novaBlock.getTileEntity().hasCapability(CapabilityEnergy.ENERGY, facing)) + continue; + + energyCapability = evt.novaBlock.getTileEntity().getCapability(CapabilityEnergy.ENERGY, facing); + if (energyCapability != null) + break; // NOVA-Energy is Unsided + } + + if (energyCapability == null && evt.novaBlock.getTileEntity().hasCapability(CapabilityEnergy.ENERGY, null)) + energyCapability = evt.novaBlock.getTileEntity().getCapability(CapabilityEnergy.ENERGY, null); + + if (energyCapability != null) + evt.novaBlock.components.add(new BWEnergyStorage(energyCapability)); }); + events.on(WrapperEvent.BWItemCreate.class).bind(evt -> { - if (evt.mcItem instanceof IEnergyStorage) - evt.novaItem.components.add(new BWEnergyStorage((IEnergyStorage) evt.mcItem)); + if (evt.itemStack.isPresent() && evt.itemStack.get().hasCapability(CapabilityEnergy.ENERGY, null)) + evt.novaItem.components.add(new BWEnergyStorage(evt.itemStack.get().getCapability(CapabilityEnergy.ENERGY, null))); }); - events.on(WrapperEvent.FWTileLoad.class).bind(evt -> { - if (!evt.block.components.has(EnergyStorage.class)) return; - if (evt.hasResult()) return; - FWTileEnergy tile; - if (evt.data.isPresent()) { - tile = (evt.block instanceof Updater) ? - updaterInjector.inject(evt.block, new Class[0], new Object[0]) : - injector.inject(evt.block, new Class[0], new Object[0]); - tile.setBlock(evt.block); - } else { - tile = (evt.block instanceof Updater) ? - updaterInjector.inject(evt.block, new Class[] { String.class }, new Object[] { evt.block.getID().asString() }) : - injector.inject(evt.block, new Class[] { String.class }, new Object[] { evt.block.getID().asString() }); - tile.setBlock(evt.block); - } - evt.setResult(tile); + events.on(WrapperEvent.BWEntityCreate.class).bind(evt -> { + if (evt.mcEntity.hasCapability(CapabilityEnergy.ENERGY, null)) + evt.novaEntity.components.add(new BWEnergyStorage(evt.mcEntity.getCapability(CapabilityEnergy.ENERGY, null))); + }); + + events.on(WrapperEvent.FWTileCreate.class).bind(evt -> { + if (evt.novaBlock.components.has(EnergyStorage.class)) // Components are unsided + evt.tileEntity.addCapability(CapabilityEnergy.ENERGY, new FWEnergyStorage(evt.novaBlock.components.get(EnergyStorage.class)), null); + }); + + events.on(WrapperEvent.FWItemInitCapabilities.class).bind(evt -> { + if (evt.novaItem.components.has(EnergyStorage.class)) // Components are unsided + evt.capabilityProvider.addCapability(CapabilityEnergy.ENERGY, new FWEnergyStorage(evt.novaItem.components.get(EnergyStorage.class)), null); + }); + + events.on(WrapperEvent.FWEntityCreate.class).bind(evt -> { + if (evt.novaBlock.components.has(EnergyStorage.class)) // Components are unsided + evt.mcEntity.addCapability(CapabilityEnergy.ENERGY, new FWEnergyStorage(evt.novaBlock.components.get(EnergyStorage.class)), null); }); } } diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergy.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergy.java deleted file mode 100644 index 9e9707d..0000000 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergy.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2015 NOVA, All rights reserved. - * This library is free software, licensed under GNU Lesser General Public License version 3 - * - * This file is part of NOVA. - * - * NOVA is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * NOVA is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with NOVA. If not, see . - */ -package nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward; - -import net.minecraftforge.energy.IEnergyStorage; -import nova.core.wrapper.mc.forge.v1_11.wrapper.block.forward.FWTile; -import nova.energy.EnergyStorage; -import nova.energy.UnitConversion; -import nova.energy.UnitDisplay; - -/** - * - * @author ExE Boss - */ -public class FWTileEnergy extends FWTile implements IEnergyStorage { - - private static final UnitConversion RF_JOULE = UnitConversion.getConvertion(UnitDisplay.Unit.REDFLUX, UnitDisplay.Unit.JOULE).get(); - private static final UnitConversion JOULE_RF = RF_JOULE.reverse(); - - public FWTileEnergy() { - } - - public FWTileEnergy(String blockID) { - this.blockID = blockID; - } - - @Override - public int receiveEnergy(int maxReceive, boolean simulate) { - return (int) Math.round(JOULE_RF.convert(getBlock().components.get(EnergyStorage.class).recharge(RF_JOULE.convert(maxReceive), !simulate))); - } - - @Override - public int extractEnergy(int maxExtract, boolean simulate) { - return (int) Math.round(JOULE_RF.convert(getBlock().components.get(EnergyStorage.class).discharge(RF_JOULE.convert(maxExtract), !simulate))); - } - - @Override - public int getEnergyStored() { - return (int) Math.round(JOULE_RF.convert(getBlock().components.get(EnergyStorage.class).getEnergy())); - } - - @Override - public int getMaxEnergyStored() { - return (int) Math.round(JOULE_RF.convert(getBlock().components.get(EnergyStorage.class).getEnergyCapacity())); - } - - @Override - public boolean canExtract() { - return getBlock().components.get(EnergyStorage.class).canDischarge(); - } - - @Override - public boolean canReceive() { - return getBlock().components.get(EnergyStorage.class).canRecharge(); - } -} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyUpdater.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyUpdater.java deleted file mode 100644 index 86dece6..0000000 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/block/forward/FWTileEnergyUpdater.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2015 NOVA, All rights reserved. - * This library is free software, licensed under GNU Lesser General Public License version 3 - * - * This file is part of NOVA. - * - * NOVA is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * NOVA is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with NOVA. If not, see . - */ -package nova.energy.wrapper.mc.forge.v1_11.wrapper.block.forward; - -import net.minecraft.util.ITickable; -import nova.core.component.Updater; - -/** - * - * @author ExE Boss - */ -public class FWTileEnergyUpdater extends FWTileEnergy implements ITickable { - - public FWTileEnergyUpdater() { - } - - public FWTileEnergyUpdater(String blockID) { - this.blockID = blockID; - } - - /** - * Updates the block. - */ - @Override - public void update() { - if (block != null) { - ((Updater) block).update(0.05); - } - } -} diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/forward/FWEnergyStorage.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/forward/FWEnergyStorage.java new file mode 100644 index 0000000..cd2d38c --- /dev/null +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/forward/FWEnergyStorage.java @@ -0,0 +1,70 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package nova.energy.wrapper.mc.forge.v1_11.wrapper.energy.forward; + +import net.minecraftforge.energy.IEnergyStorage; +import nova.energy.EnergyStorage; +import nova.energy.UnitConversion; +import nova.energy.UnitDisplay; + +/** + * A FWEnergyStorage is a NOVA energy capability. + * + * @author ExE Boss + */ +public class FWEnergyStorage implements IEnergyStorage { + + private static final UnitConversion RF_JOULE = UnitConversion.getConvertion(UnitDisplay.Unit.REDFLUX, UnitDisplay.Unit.JOULE).get(); + private static final UnitConversion JOULE_RF = RF_JOULE.reverse(); + + private final EnergyStorage energyStorage; + + public FWEnergyStorage(EnergyStorage energyStorage) { + this.energyStorage = energyStorage; + } + + @Override + public int receiveEnergy(int maxReceive, boolean simulate) { + if (energyStorage != null) + return (int) Math.round(JOULE_RF.convert(energyStorage.recharge(RF_JOULE.convert(maxReceive), !simulate))); + return 0; + } + + @Override + public int extractEnergy(int maxExtract, boolean simulate) { + if (energyStorage != null) + return (int) Math.round(JOULE_RF.convert(energyStorage.discharge(RF_JOULE.convert(maxExtract), !simulate))); + return 0; + } + + @Override + public int getEnergyStored() { + if (energyStorage != null) + return (int) Math.round(JOULE_RF.convert(energyStorage.getEnergy())); + return 0; + } + + @Override + public int getMaxEnergyStored() { + if (energyStorage != null) + return (int) Math.round(JOULE_RF.convert(energyStorage.getEnergyCapacity())); + return 0; + } + + @Override + public boolean canExtract() { + if (energyStorage != null) + return energyStorage.canDischarge(); + return false; + } + + @Override + public boolean canReceive() { + if (energyStorage != null) + return energyStorage.canRecharge(); + return false; + } +} From 29674d20404363b4a403ad73ba351ee24c88601c Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Wed, 4 Jan 2017 22:39:55 +0100 Subject: [PATCH 12/16] =?UTF-8?q?Fix=20NPE=20when=20block=20doesn=E2=80=99?= =?UTF-8?q?t=20have=20a=20tile=20entity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mc/forge/v1_11/NovaMinecraftEnergy.java | 6 ++++++ minecraft/1.11/src/main/resources/mcmod.info | 21 ------------------- 2 files changed, 6 insertions(+), 21 deletions(-) delete mode 100644 minecraft/1.11/src/main/resources/mcmod.info diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java index bd94861..e4a0a12 100644 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java @@ -51,6 +51,9 @@ public NovaMinecraftEnergy(GlobalEvents events) { @Override public void preInit() { events.on(WrapperEvent.BWBlockCreate.class).bind(evt -> { + if (evt.novaBlock.getTileEntity() == null) + return; + IEnergyStorage energyCapability = null; for (EnumFacing facing : EnumFacing.values()) { if (!evt.novaBlock.getTileEntity().hasCapability(CapabilityEnergy.ENERGY, facing)) @@ -79,6 +82,9 @@ public void preInit() { }); events.on(WrapperEvent.FWTileCreate.class).bind(evt -> { + if (evt.tileEntity == null) + return; + if (evt.novaBlock.components.has(EnergyStorage.class)) // Components are unsided evt.tileEntity.addCapability(CapabilityEnergy.ENERGY, new FWEnergyStorage(evt.novaBlock.components.get(EnergyStorage.class)), null); }); diff --git a/minecraft/1.11/src/main/resources/mcmod.info b/minecraft/1.11/src/main/resources/mcmod.info deleted file mode 100644 index ec9763e..0000000 --- a/minecraft/1.11/src/main/resources/mcmod.info +++ /dev/null @@ -1,21 +0,0 @@ -{ - "modListVersion": 2, - "modList": [{ - "modid": "novaenergy", - "name": "NOVA Energy", - "description": "The Energy component of NOVA.", - "version": "0.0.1", - "mcversion": "1.11", - "url": "https://novaapi.net/", - "updateUrl": "", - "authorList": [ "calclavia", "RX14", "AEnterprise", "magik6k", "Shadowfacts" ], - "credits": "Created by the NOVA Team.\nPorted to Minecraft 1.11 by: ExE Boss.", - "logoFile": "", - "screenshots": [], - "parent": "", - "requiredMods": [], - "dependencies": [], - "dependants": [], - "useDependencyInformation": true - }] -} From 908cf907c9e362c4ce329bd6b87fc300c6d5dcf6 Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Wed, 4 Jan 2017 23:05:51 +0100 Subject: [PATCH 13/16] Move wrapper main class to proper package Reason: Consistency with already implemented wrappers --- .../NovaEnergyWrapper.java} | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) rename minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/{NovaMinecraftEnergy.java => launch/NovaEnergyWrapper.java} (92%) diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/launch/NovaEnergyWrapper.java similarity index 92% rename from minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java rename to minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/launch/NovaEnergyWrapper.java index e4a0a12..04c5db3 100644 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/NovaMinecraftEnergy.java +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/launch/NovaEnergyWrapper.java @@ -17,7 +17,7 @@ * You should have received a copy of the GNU General Public License * along with NOVA. If not, see . */ -package nova.energy.wrapper.mc.forge.v1_11; +package nova.energy.wrapper.mc.forge.v1_11.launch; import net.minecraft.util.EnumFacing; import net.minecraftforge.energy.CapabilityEnergy; @@ -35,16 +35,16 @@ * * @author ExE Boss */ -@Mod(id = NovaMinecraftEnergy.id, name = NovaMinecraftEnergy.name, version = NovaMinecraftEnergy.version, novaVersion = "0.1.0") -public class NovaMinecraftEnergy implements Loadable { +@Mod(id = NovaEnergyWrapper.id, name = NovaEnergyWrapper.name, version = NovaEnergyWrapper.version, novaVersion = "0.1.0") +public class NovaEnergyWrapper implements Loadable { public static final String version = "0.0.1"; - public static final String id = "novaenergy"; + public static final String id = "nova-energy-wrapper"; public static final String name = "NOVA Energy"; private final GlobalEvents events; - public NovaMinecraftEnergy(GlobalEvents events) { + public NovaEnergyWrapper(GlobalEvents events) { this.events = events; } From 1511b985d3e849e49161992f3ecc5bd2b9953f90 Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Thu, 5 Jan 2017 22:33:07 +0100 Subject: [PATCH 14/16] Get rid of Identifiable.addPrefix --- src/main/java/nova/energy/UnitDisplay.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main/java/nova/energy/UnitDisplay.java b/src/main/java/nova/energy/UnitDisplay.java index 06799b5..921d61d 100644 --- a/src/main/java/nova/energy/UnitDisplay.java +++ b/src/main/java/nova/energy/UnitDisplay.java @@ -205,7 +205,7 @@ public static class Unit implements Identifiable { private String plural = null; private Unit(String id, String name, String symbol) { - this.id = Identifiable.addPrefix(id, false); + this.id = id; this.name = name; this.symbol = symbol; @@ -243,21 +243,17 @@ public static Optional getUnit(String id) { public static Unit getOrCreateUnit(String id, String name, String unit) { StringIdentifier idRaw = new StringIdentifier(id); - StringIdentifier idNamespaced = new StringIdentifier(Identifiable.addPrefix(id, false)); - if (UNIT_MAP.containsKey(idNamespaced)) return UNIT_MAP.get(idNamespaced); if (UNIT_MAP.containsKey(idRaw)) return UNIT_MAP.get(idRaw); - Unit unitObj = new Unit(idNamespaced.asString(), name, unit); + Unit unitObj = new Unit(idRaw.asString(), name, unit); return unitObj; } public static Unit getOrCreateUnit(String id, String name, String unit, String plural) { StringIdentifier idRaw = new StringIdentifier(id); - StringIdentifier idNamespaced = new StringIdentifier(Identifiable.addPrefix(id, false)); - if (UNIT_MAP.containsKey(idNamespaced)) return UNIT_MAP.get(idNamespaced); if (UNIT_MAP.containsKey(idRaw)) return UNIT_MAP.get(idRaw); - Unit unitObj = new Unit(idNamespaced.asString(), name, unit); + Unit unitObj = new Unit(idRaw.asString(), name, unit); return unitObj.setPlural(plural); } } From 755b0c245886ed8a7086876e7d6d5548739e6875 Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Thu, 5 Jan 2017 22:35:01 +0100 Subject: [PATCH 15/16] Update MCP mappings --- minecraft/1.11/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minecraft/1.11/build.gradle b/minecraft/1.11/build.gradle index d528675..d5d4961 100644 --- a/minecraft/1.11/build.gradle +++ b/minecraft/1.11/build.gradle @@ -43,7 +43,7 @@ apply plugin: 'net.minecraftforge.gradle.forge' minecraft { version = property("minecraft.version") + "-" + property("forge.version") - mappings = 'snapshot_20161111' + mappings = 'snapshot_20161220' runDir = "run" } From 2e3a1fc7431f002bc0702064fb9a6869b8e4cc28 Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Wed, 11 Jan 2017 01:31:56 +0100 Subject: [PATCH 16/16] =?UTF-8?q?Move=20Unit=20to=20it=E2=80=99s=20own=20c?= =?UTF-8?q?lass=20+=20Write=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../forge/v1_11/launch/NovaEnergyWrapper.java | 4 +- .../energy/backward/BWEnergyStorage.java | 4 +- .../energy/forward/FWEnergyStorage.java | 4 +- src/main/java/nova/energy/Unit.java | 179 ++++++++++++++++++ src/main/java/nova/energy/UnitConversion.java | 13 +- src/main/java/nova/energy/UnitDisplay.java | 179 +----------------- .../java/nova/energy/UnitConversionTest.java | 30 +++ 7 files changed, 228 insertions(+), 185 deletions(-) create mode 100644 src/main/java/nova/energy/Unit.java create mode 100644 src/test/java/nova/energy/UnitConversionTest.java diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/launch/NovaEnergyWrapper.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/launch/NovaEnergyWrapper.java index 04c5db3..b8f6385 100644 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/launch/NovaEnergyWrapper.java +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/launch/NovaEnergyWrapper.java @@ -95,8 +95,8 @@ public void preInit() { }); events.on(WrapperEvent.FWEntityCreate.class).bind(evt -> { - if (evt.novaBlock.components.has(EnergyStorage.class)) // Components are unsided - evt.mcEntity.addCapability(CapabilityEnergy.ENERGY, new FWEnergyStorage(evt.novaBlock.components.get(EnergyStorage.class)), null); + if (evt.novaEntity.components.has(EnergyStorage.class)) // Components are unsided + evt.mcEntity.addCapability(CapabilityEnergy.ENERGY, new FWEnergyStorage(evt.novaEntity.components.get(EnergyStorage.class)), null); }); } } diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/backward/BWEnergyStorage.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/backward/BWEnergyStorage.java index 02ff9e3..a3b7f4f 100644 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/backward/BWEnergyStorage.java +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/backward/BWEnergyStorage.java @@ -21,8 +21,8 @@ import net.minecraftforge.energy.IEnergyStorage; import nova.energy.EnergyStorage; +import nova.energy.Unit; import nova.energy.UnitConversion; -import nova.energy.UnitDisplay; /** * @@ -30,7 +30,7 @@ */ public class BWEnergyStorage extends EnergyStorage { - private static final UnitConversion RF_JOULE = UnitConversion.getConvertion(UnitDisplay.Unit.REDFLUX, UnitDisplay.Unit.JOULE).get(); + private static final UnitConversion RF_JOULE = UnitConversion.getConvertion(Unit.REDFLUX, Unit.JOULE).get(); private static final UnitConversion JOULE_RF = RF_JOULE.reverse(); private final IEnergyStorage mcEnergyStorage; diff --git a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/forward/FWEnergyStorage.java b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/forward/FWEnergyStorage.java index cd2d38c..4a55d71 100644 --- a/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/forward/FWEnergyStorage.java +++ b/minecraft/1.11/src/main/java/nova/energy/wrapper/mc/forge/v1_11/wrapper/energy/forward/FWEnergyStorage.java @@ -7,8 +7,8 @@ import net.minecraftforge.energy.IEnergyStorage; import nova.energy.EnergyStorage; +import nova.energy.Unit; import nova.energy.UnitConversion; -import nova.energy.UnitDisplay; /** * A FWEnergyStorage is a NOVA energy capability. @@ -17,7 +17,7 @@ */ public class FWEnergyStorage implements IEnergyStorage { - private static final UnitConversion RF_JOULE = UnitConversion.getConvertion(UnitDisplay.Unit.REDFLUX, UnitDisplay.Unit.JOULE).get(); + private static final UnitConversion RF_JOULE = UnitConversion.getConvertion(Unit.REDFLUX, Unit.JOULE).get(); private static final UnitConversion JOULE_RF = RF_JOULE.reverse(); private final EnergyStorage energyStorage; diff --git a/src/main/java/nova/energy/Unit.java b/src/main/java/nova/energy/Unit.java new file mode 100644 index 0000000..3183bd6 --- /dev/null +++ b/src/main/java/nova/energy/Unit.java @@ -0,0 +1,179 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package nova.energy; + +import nova.core.util.Identifiable; +import nova.core.util.registry.Registry; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * @author ExE Boss + */ +public final class Unit implements Identifiable { + private static final Registry REGISTRY = new Registry<>(); + + public static final Unit METRE = getOrCreateUnit("nova:metre", "Metre", "m"); + public static final Unit GRAM = getOrCreateUnit("nova:gram", "Gram", "g").setBasePrefix(Prefix.KILO); + + public static final Unit AMPERE = getOrCreateUnit("nova:ampere", "Amp", "I"); + public static final Unit AMP_HOUR = getOrCreateUnit("nova:amp_hour", "Amp Hour", "Ah"); + public static final Unit VOLTAGE = getOrCreateUnit("nova:voltage", "Volt", "V"); + public static final Unit WATT = getOrCreateUnit("nova:watt", "Watt", "W"); + public static final Unit WATT_HOUR = getOrCreateUnit("nova:watt_hour", "Watt Hour", "Wh"); + public static final Unit RESISTANCE = getOrCreateUnit("nova:resistance", "Ohm", "R"); + public static final Unit CONDUCTANCE = getOrCreateUnit("nova:conductance", "Siemen", "S"); + public static final Unit JOULE = getOrCreateUnit("nova:joule", "Joule", "J"); + public static final Unit LITER = getOrCreateUnit("nova:liter", "Liter", "L"); + public static final Unit NEWTON_METER = getOrCreateUnit("nova:newton_meter", "Newton Meter", "Nm"); + + /** + * Redstone Flux, the default energy unit in Minecraft Forge since 1.10-ish. + */ + public static final Unit REDFLUX = getOrCreateUnit("forge:redstone_flux", "Redstone-Flux", "RF").setPlural("Redstone-Flux"); + public static final Unit MINECRAFT_JOULES = getOrCreateUnit("buildcraft:minecraft_joule", "Minecraft-Joule", "McJ"); // MJ is confusing with Megajoules + public static final Unit ELECTRICAL_UNITS = getOrCreateUnit("ic2:electrical_unit", "Electrical-Unit", "EU"); + + private final String id; + public final String name; + public final String symbol; + private String plural = null; + private Prefix basePrefix = null; + + private Unit(String id, String name, String symbol) { + this.id = id; + this.name = name; + this.symbol = symbol; + } + + private Unit setPlural(String plural) { + if (this.plural == null) + this.plural = plural; + return this; + } + + private Unit setBasePrefix(Prefix basePrefix) { + if (this.basePrefix == null) + this.basePrefix = basePrefix; + return this; + } + + public String getPlural() { + return this.plural == null ? this.name + "s" : this.plural; + } + + @Override + public String getID() { + return id; + } + + public Set getUnitsFromMod(String modId) { + return REGISTRY.stream().filter((e) -> { + String id = e.getID(); + if (id.contains(":")) { + return id.substring(0, id.lastIndexOf(':')).startsWith(modId); + } else { + return modId == null || modId.isEmpty(); + } + }).collect(Collectors.toSet()); + } + + public Optional getUnit(String id) { + return REGISTRY.get(id); + } + + public static Unit getOrCreateUnit(String id, String name, String unit) { + if (REGISTRY.contains(id)) return REGISTRY.get(id).get(); + return REGISTRY.register(new Unit(id, name, unit)); + } + + public static Unit getOrCreateUnit(String id, String name, String unit, String plural) { + if (REGISTRY.contains(id)) return REGISTRY.get(id).get(); + return REGISTRY.register(new Unit(id, name, unit)).setPlural(plural); + } + + /** + * Metric system of measurement. + */ + public static class Prefix { + private static final List UNIT_PREFIXES = new LinkedList<>(); + +// public static final Prefix YOCTO = new Prefix("Yocto", "y", 0.000000000000000000000001); +// public static final Prefix ZEPTO = new Prefix("Zepto", "z", 0.000000000000000000001); +// public static final Prefix ATTO = new Prefix("Atto", "a", 0.000000000000000001); +// public static final Prefix FEMTO = new Prefix("Femto", "p", 0.000000000000001); +// public static final Prefix PICO = new Prefix("Pico", "p", 0.000000000001); +// public static final Prefix NANO = new Prefix("Nano", "n", 0.000000001); + public static final Prefix MICRO = new Prefix("Micro", "μ", 0.000001); + public static final Prefix MILLI = new Prefix("Milli", "m", 0.001); + public static final Prefix BASE = new Prefix("", "", 1); + public static final Prefix KILO = new Prefix("Kilo", "k", 1000); + public static final Prefix MEGA = new Prefix("Mega", "M", 1000000); + public static final Prefix GIGA = new Prefix("Giga", "G", 1000000000); + public static final Prefix TERA = new Prefix("Tera", "T", 1000000000000d); + public static final Prefix PETA = new Prefix("Peta", "P", 1000000000000000d); + public static final Prefix EXA = new Prefix("Exa", "E", 1000000000000000000d); + public static final Prefix ZETTA = new Prefix("Zetta", "Z", 1000000000000000000000d); + public static final Prefix YOTTA = new Prefix("Yotta", "Y", 1000000000000000000000000d); + /** + * long name for the unit + */ + public final String name; + /** + * short unit version of the unit + */ + public final String symbol; + /** + * Point by which a number is consider to be of this unit + */ + public final double value; + + private Prefix(String name, String symbol, double value) { + this.name = name; + this.symbol = symbol; + this.value = value; + UNIT_PREFIXES.add(this); + } + + 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 List getPrefixes() { + return Collections.unmodifiableList(UNIT_PREFIXES); + } + } +} diff --git a/src/main/java/nova/energy/UnitConversion.java b/src/main/java/nova/energy/UnitConversion.java index 80fcdde..2c1610b 100644 --- a/src/main/java/nova/energy/UnitConversion.java +++ b/src/main/java/nova/energy/UnitConversion.java @@ -20,8 +20,6 @@ package nova.energy; -import nova.energy.UnitDisplay.Unit; - import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -81,6 +79,7 @@ public static Optional getConvertion(Unit unit1, Unit unit2) { if (conv == null) return Optional.empty(); return Optional.ofNullable(conv.get(unit2)); } + /** * * @param unit1 The unit to convert from @@ -94,11 +93,17 @@ public static UnitConversion registerConversion(Unit unit1, Unit unit2, double r throw new IllegalArgumentException("Ratio cannot be 0"); Map conv1 = CONVERSION.get(unit1); - if (conv1 == null) conv1 = new HashMap<>(); + if (conv1 == null) { + conv1 = new HashMap<>(); + CONVERSION.put(unit1, conv1); + } if (conv1.containsKey(unit2)) return conv1.get(unit2); Map conv2 = CONVERSION.get(unit2); - if (conv2 == null) conv2 = new HashMap<>(); + if (conv2 == null) { + conv2 = new HashMap<>(); + CONVERSION.put(unit2, conv2); + } UnitConversion uc = new UnitConversion(unit1, unit2, ratio); diff --git a/src/main/java/nova/energy/UnitDisplay.java b/src/main/java/nova/energy/UnitDisplay.java index 921d61d..f5e87b2 100644 --- a/src/main/java/nova/energy/UnitDisplay.java +++ b/src/main/java/nova/energy/UnitDisplay.java @@ -20,20 +20,6 @@ package nova.energy; -import nova.core.loader.Mod; -import nova.core.util.id.Identifiable; -import nova.core.util.id.Identifier; -import nova.core.util.id.StringIdentifier; -import nova.internal.core.launch.ModLoader; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - /** * An easy way to display information on electricity for the client. * @@ -153,17 +139,17 @@ public String toString() { if (value == 0) { return value + " " + unitName; } else { - for (int i = 0; i < UnitPrefix.UNIT_PREFIXES.size(); i++) { - UnitPrefix lowerMeasure = UnitPrefix.UNIT_PREFIXES.get(i); + for (int i = 0; i < Unit.Prefix.getPrefixes().size(); i++) { + Unit.Prefix lowerMeasure = Unit.Prefix.getPrefixes().get(i); if (lowerMeasure.isBellow(value) && i == 0) { return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(useSymbol) + unitName; } - if (i + 1 >= UnitPrefix.UNIT_PREFIXES.size()) { + if (i + 1 >= Unit.Prefix.getPrefixes().size()) { return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(useSymbol) + unitName; } - UnitPrefix upperMeasure = UnitPrefix.UNIT_PREFIXES.get(i + 1); + Unit.Prefix upperMeasure = Unit.Prefix.getPrefixes().get(i + 1); if ((lowerMeasure.isAbove(value) && upperMeasure.isBellow(value)) || lowerMeasure.value == value) { return prefix + roundDecimals(lowerMeasure.process(value), decimalPlaces) + " " + lowerMeasure.getName(useSymbol) + unitName; @@ -173,161 +159,4 @@ public String toString() { return prefix + roundDecimals(value, decimalPlaces) + " " + unitName; } - - /** - * 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 class Unit implements Identifiable { - private static final Map UNIT_MAP = new HashMap<>(); - - public static final Unit AMPERE = new Unit("nova:ampere", "Amp", "I"); - public static final Unit AMP_HOUR = new Unit("nova:amp_hour", "Amp Hour", "Ah"); - public static final Unit VOLTAGE = new Unit("nova:voltage", "Volt", "V"); - public static final Unit WATT = new Unit("nova:watt", "Watt", "W"); - public static final Unit WATT_HOUR = new Unit("nova:watt_hour", "Watt Hour", "Wh"); - public static final Unit RESISTANCE = new Unit("nova:resistance", "Ohm", "R"); - public static final Unit CONDUCTANCE = new Unit("nova:conductance", "Siemen", "S"); - public static final Unit JOULE = new Unit("nova:joule", "Joule", "J"); - public static final Unit LITER = new Unit("nova:liter", "Liter", "L"); - public static final Unit NEWTON_METER = new Unit("nova:newton_meter", "Newton Meter", "Nm"); - - /** - * Redstone Flux, the default energy unit in Minecraft Forge since 1.10-ish. - */ - public static final Unit REDFLUX = new Unit("forge:redstone_flux", "Redstone-Flux", "RF").setPlural("Redstone-Flux"); - public static final Unit MINECRAFT_JOULES = new Unit("buildcraft:minecraft_joule", "Minecraft-Joule", "McJ"); // MJ is confusing with Megajoules - public static final Unit ELECTRICAL_UNITS = new Unit("ic2:electrical_unit", "Electrical-Unit", "EU"); - - private final String id; - public final String name; - public final String symbol; - private String plural = null; - - private Unit(String id, String name, String symbol) { - this.id = id; - this.name = name; - this.symbol = symbol; - - UNIT_MAP.put(getID(), this); - } - - private Unit setPlural(String plural) { - this.plural = plural; - return this; - } - - public String getPlural() { - return this.plural == null ? this.name + "s" : this.plural; - } - - @Override - public Identifier getID() { - return new StringIdentifier(id); - } - - public static Set getUnitsFromMod(String modId) { - return UNIT_MAP.values().stream().filter((e) -> { - String id = e.getID().asString(); - if (id.contains(":")) { - return id.substring(0, id.lastIndexOf(':')).startsWith(modId); - } else { - return modId == null || modId.isEmpty(); - } - }).collect(Collectors.toSet()); - } - - public static Optional getUnit(String id) { - return Optional.ofNullable(UNIT_MAP.get(new StringIdentifier(id))); - } - - public static Unit getOrCreateUnit(String id, String name, String unit) { - StringIdentifier idRaw = new StringIdentifier(id); - if (UNIT_MAP.containsKey(idRaw)) return UNIT_MAP.get(idRaw); - - Unit unitObj = new Unit(idRaw.asString(), name, unit); - return unitObj; - } - - public static Unit getOrCreateUnit(String id, String name, String unit, String plural) { - StringIdentifier idRaw = new StringIdentifier(id); - if (UNIT_MAP.containsKey(idRaw)) return UNIT_MAP.get(idRaw); - - Unit unitObj = new Unit(idRaw.asString(), name, unit); - return unitObj.setPlural(plural); - } - } - - /** - * Metric system of measurement. - */ - public static class UnitPrefix { - public static final List UNIT_PREFIXES = new LinkedList(); - -// public static final UnitPrefix YOCTO = new UnitPrefix("Yocto", "y", 0.000000000000000000000001); -// public static final UnitPrefix ZEPTO = new UnitPrefix("Zepto", "z", 0.000000000000000000001); -// public static final UnitPrefix ATTO = new UnitPrefix("Atto", "a", 0.000000000000000001); -// public static final UnitPrefix FEMTO = new UnitPrefix("Femto", "p", 0.000000000000001); -// public static final UnitPrefix PICO = new UnitPrefix("Pico", "p", 0.000000000001); -// public static final UnitPrefix NANO = new UnitPrefix("Nano", "n", 0.000000001); - public static final UnitPrefix MICRO = new UnitPrefix("Micro", "μ", 0.000001); - public static final UnitPrefix MILLI = new UnitPrefix("Milli", "m", 0.001); - public static final UnitPrefix BASE = new UnitPrefix("", "", 1); - public static final UnitPrefix KILO = new UnitPrefix("Kilo", "k", 1000); - public static final UnitPrefix MEGA = new UnitPrefix("Mega", "M", 1000000); - public static final UnitPrefix GIGA = new UnitPrefix("Giga", "G", 1000000000); - public static final UnitPrefix TERA = new UnitPrefix("Tera", "T", 1000000000000d); - public static final UnitPrefix PETA = new UnitPrefix("Peta", "P", 1000000000000000d); - public static final UnitPrefix EXA = new UnitPrefix("Exa", "E", 1000000000000000000d); - public static final UnitPrefix ZETTA = new UnitPrefix("Zetta", "Z", 1000000000000000000000d); - public static final UnitPrefix YOTTA = new UnitPrefix("Yotta", "Y", 1000000000000000000000000d); - /** - * long name for the unit - */ - public final String name; - /** - * short unit version of the unit - */ - public final String symbol; - /** - * Point by which a number is consider to be of this unit - */ - public final double value; - - private UnitPrefix(String name, String symbol, double value) { - this.name = name; - this.symbol = symbol; - this.value = value; - UNIT_PREFIXES.add(this); - } - - 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; - } - } } diff --git a/src/test/java/nova/energy/UnitConversionTest.java b/src/test/java/nova/energy/UnitConversionTest.java new file mode 100644 index 0000000..bad163c --- /dev/null +++ b/src/test/java/nova/energy/UnitConversionTest.java @@ -0,0 +1,30 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package nova.energy; + +import org.assertj.core.data.Offset; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * + * @author ExE Boss + */ +public class UnitConversionTest { + + public UnitConversionTest() { + } + + @Test + public void testConvert() { + UnitConversion RF_JOULE = UnitConversion.getConvertion(Unit.REDFLUX, Unit.JOULE).get(); + UnitConversion JOULE_RF = UnitConversion.getConvertion(Unit.JOULE, Unit.REDFLUX).get(); + + assertThat(RF_JOULE.convert(24)).isEqualTo(5, Offset.offset(1 / 10_000_000D)); + assertThat(JOULE_RF.convert(5)).isEqualTo(24, Offset.offset(1 / 10_000_000D)); + } +}