Skip to content

Commit 9c21d94

Browse files
thiakilpupnewfster
andauthored
Switch energy system to using long primitives instead of a custom object class (#8159)
--------- Co-authored-by: Sara Freimer <sara@freimer.com>
1 parent 93dcef6 commit 9c21d94

File tree

270 files changed

+3503
-5534
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

270 files changed

+3503
-5534
lines changed

src/api/java/mekanism/api/MekanismAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private MekanismAPI() {
3434
/**
3535
* The version of the api classes - may not always match the mod's version
3636
*/
37-
public static final String API_VERSION = "10.6.4";
37+
public static final String API_VERSION = "10.6.6";
3838
/**
3939
* Mekanism's Mod ID
4040
*/

src/api/java/mekanism/api/SerializerHelper.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.mojang.serialization.MapCodec;
99
import com.mojang.serialization.MapDecoder.Implementation;
1010
import com.mojang.serialization.MapLike;
11+
import com.mojang.serialization.codecs.PrimitiveCodec;
1112
import com.mojang.serialization.codecs.RecordCodecBuilder;
1213
import java.util.Optional;
1314
import java.util.function.Function;
@@ -48,6 +49,69 @@ private SerializerHelper() {
4849
return Codec.LONG.flatXmap(checker, checker);
4950
});
5051

52+
@Deprecated(since = "10.6.6", forRemoval = true)//TODO - 1.22: Remove
53+
private static final Codec<Long> LEGACY_CODEC_FLOATING_LONG = new PrimitiveCodec<>() {
54+
@Override
55+
public <T> DataResult<Long> read(DynamicOps<T> ops, T input) {
56+
return ops.getStringValue(input).flatMap(number -> {
57+
try {
58+
long value;
59+
int index = number.indexOf('.');
60+
if (index == -1) {
61+
value = Long.parseUnsignedLong(number);
62+
} else {
63+
value = Long.parseUnsignedLong(number, 0, index, 10);
64+
}
65+
if (value < 0) {
66+
//Clamp unsigned to positive.
67+
value = Long.MAX_VALUE;
68+
}
69+
if (value == 0 && index != -1) {
70+
//If we are at zero, see if we should ceil the decimal
71+
if (Long.parseLong(number, index + 1, number.length(), 10) > 0) {
72+
return DataResult.success(1L);
73+
}
74+
}
75+
return DataResult.success(value);
76+
} catch (NumberFormatException e) {
77+
return DataResult.error(e::getMessage);
78+
}
79+
});
80+
}
81+
82+
@Override
83+
public <T> T write(DynamicOps<T> ops, Long value) {
84+
return ops.createLong(value);
85+
}
86+
87+
@Override
88+
public String toString() {
89+
return "LegacyFloatingLong";
90+
}
91+
};
92+
93+
/**
94+
* Long Codec which accepts a number >= 0
95+
*
96+
* @since 10.6.6
97+
* @deprecated Prefer {@link #POSITIVE_LONG_CODEC}. This field just exists for people who want to be able to load legacy data that was stored as a FloatingLong/
98+
*/
99+
@Deprecated(since = "10.6.6", forRemoval = true)//TODO - 1.22: Remove
100+
//Note: We use vanilla's withAlternative instead of Neo's as we always want to encode with the non legacy codec
101+
public static final Codec<Long> POSITIVE_LONG_CODEC_LEGACY = Codec.withAlternative(POSITIVE_LONG_CODEC, LEGACY_CODEC_FLOATING_LONG);
102+
103+
/**
104+
* Long Codec which accepts a number > 0
105+
*
106+
* @since 10.6.6
107+
* @deprecated Prefer {@link #POSITIVE_LONG_CODEC}. This field just exists for people who want to be able to load legacy data that was stored as a FloatingLong/
108+
*/
109+
@Deprecated(since = "10.6.6", forRemoval = true)//TODO - 1.22: Remove
110+
//Note: We use vanilla's withAlternative instead of Neo's as we always want to encode with the non legacy codec
111+
public static final Codec<Long> POSITIVE_NONZERO_LONG_CODEC_LEGACY = Codec.withAlternative(POSITIVE_NONZERO_LONG_CODEC,
112+
LEGACY_CODEC_FLOATING_LONG.validate(val -> val == 0 ? DataResult.error(() -> "Value must be greater than zero") : DataResult.success(val))
113+
);
114+
51115
/**
52116
* Custom codec to allow serializing an item stack without the upper bounds.
53117
*

src/api/java/mekanism/api/chemical/gas/attribute/GasAttributes.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import java.util.List;
44
import java.util.function.IntSupplier;
5+
import java.util.function.LongSupplier;
56
import mekanism.api.MekanismAPI;
67
import mekanism.api.chemical.attribute.ChemicalAttribute;
78
import mekanism.api.chemical.gas.Gas;
8-
import mekanism.api.math.FloatingLong;
9-
import mekanism.api.math.FloatingLongSupplier;
9+
import mekanism.api.math.MathUtils;
1010
import mekanism.api.providers.IGasProvider;
1111
import mekanism.api.radiation.IRadiationManager;
1212
import mekanism.api.text.APILang;
@@ -116,7 +116,7 @@ public List<Component> addTooltipText(List<Component> list) {
116116
ITooltipHelper tooltipHelper = ITooltipHelper.INSTANCE;
117117
list.add(APILang.CHEMICAL_ATTRIBUTE_COOLANT_EFFICIENCY.translateColored(EnumColor.GRAY, EnumColor.INDIGO, tooltipHelper.getPercent(conductivity)));
118118
list.add(APILang.CHEMICAL_ATTRIBUTE_COOLANT_ENTHALPY.translateColored(EnumColor.GRAY, EnumColor.INDIGO,
119-
tooltipHelper.getEnergyPerMBDisplayShort(FloatingLong.createConst(thermalEnthalpy))));
119+
tooltipHelper.getEnergyPerMBDisplayShort(MathUtils.clampToLong(thermalEnthalpy))));
120120
return list;
121121
}
122122
}
@@ -188,19 +188,21 @@ public Gas getCooledGas() {
188188
public static class Fuel extends ChemicalAttribute {
189189

190190
private final IntSupplier burnTicks;
191-
private final FloatingLongSupplier energyDensity;
191+
private final LongSupplier energyDensity;
192192

193193
/**
194194
* @param burnTicks The number of ticks one mB of fuel can be burned for before being depleted; must be greater than zero.
195195
* @param energyDensity The energy density in one mB of fuel; must be greater than zero.
196196
*
197197
* @since 10.4.0
198198
*/
199-
public Fuel(int burnTicks, FloatingLong energyDensity) {
199+
public Fuel(int burnTicks, long energyDensity) {
200200
if (burnTicks <= 0) {
201201
throw new IllegalArgumentException("Fuel attributes must burn for at least one tick! Burn Ticks: " + burnTicks);
202-
} else if (energyDensity.isZero()) {
202+
} else if (energyDensity <= 0) {
203203
throw new IllegalArgumentException("Fuel attributes must have an energy density greater than zero!");
204+
} else if (energyDensity / burnTicks == 0L) {
205+
throw new IllegalArgumentException("Energy density per tick must be greater than zero! (integer division)");
204206
}
205207
this.burnTicks = () -> burnTicks;
206208
this.energyDensity = () -> energyDensity;
@@ -211,7 +213,7 @@ public Fuel(int burnTicks, FloatingLong energyDensity) {
211213
* zero.
212214
* @param energyDensity Supplier for the energy density of one mB of fuel. The supplier should return values be greater than zero.
213215
*/
214-
public Fuel(IntSupplier burnTicks, FloatingLongSupplier energyDensity) {
216+
public Fuel(IntSupplier burnTicks, LongSupplier energyDensity) {
215217
this.burnTicks = burnTicks;
216218
this.energyDensity = energyDensity;
217219
}
@@ -226,17 +228,17 @@ public int getBurnTicks() {
226228
/**
227229
* Gets the amount of energy produced per tick of this fuel.
228230
*/
229-
public FloatingLong getEnergyPerTick() {
231+
public long getEnergyPerTick() {
230232
int ticks = getBurnTicks();
231233
//If we have less than one tick, the density is invalid
232234
if (ticks < 1) {
233235
MekanismAPI.logger.warn("Invalid tick count ({}) for Fuel attribute, this number should be at least 1.", ticks);
234-
return FloatingLong.ZERO;
236+
return 0;
235237
} else if (ticks == 1) {
236238
//Single tick, no division necessary
237-
return energyDensity.get();
239+
return energyDensity.getAsLong();
238240
}
239-
return energyDensity.get().divide(ticks);
241+
return energyDensity.getAsLong() / ticks;
240242
}
241243

242244
@Override
@@ -245,7 +247,7 @@ public List<Component> addTooltipText(List<Component> list) {
245247
ITooltipHelper tooltipHelper = ITooltipHelper.INSTANCE;
246248
list.add(APILang.CHEMICAL_ATTRIBUTE_FUEL_BURN_TICKS.translateColored(EnumColor.GRAY, EnumColor.INDIGO, tooltipHelper.getFormattedNumber(getBurnTicks())));
247249
list.add(APILang.CHEMICAL_ATTRIBUTE_FUEL_ENERGY_DENSITY.translateColored(EnumColor.GRAY, EnumColor.INDIGO,
248-
tooltipHelper.getEnergyPerMBDisplayShort(energyDensity.get())));
250+
tooltipHelper.getEnergyPerMBDisplayShort(energyDensity.getAsLong())));
249251
return list;
250252
}
251253
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package mekanism.api.container;
2+
3+
import net.minecraft.core.Direction;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
/**
7+
* Helper to define a generalized way to get the contents of a container.
8+
*
9+
* @since 10.6.6
10+
*/
11+
@FunctionalInterface
12+
public interface InContainerGetterLong {
13+
14+
/**
15+
* @param container Container index of the container to query.
16+
* @param side The side we are interacting with the handler from (null for internal).
17+
*
18+
* @return The object stored in the given container.
19+
*/
20+
long getStored(int container, @Nullable Direction side);
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package mekanism.api.container;
2+
3+
import mekanism.api.Action;
4+
import net.minecraft.core.Direction;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
/**
8+
* Helper to define generalized container interactions for use in our batch container utils. Mainly used for chemicals.
9+
*
10+
* @since 10.6.6
11+
*/
12+
@FunctionalInterface
13+
public interface LongToLongContainerInteraction {
14+
15+
/**
16+
* @param container Container index to interact with
17+
* @param amount Amount being adjusted by the interaction.
18+
* @param side The side we are interacting with the handler from (null for internal).
19+
* @param action The action to perform, either {@link Action#EXECUTE} or {@link Action#SIMULATE}
20+
*
21+
* @return Result of the interaction (for example the result of an insert or extraction)
22+
*/
23+
long interact(int container, long amount, @Nullable Direction side, Action action);
24+
}

src/api/java/mekanism/api/datagen/recipe/builder/ElectrolysisRecipeBuilder.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import mekanism.api.annotations.NothingNullByDefault;
44
import mekanism.api.chemical.gas.GasStack;
55
import mekanism.api.datagen.recipe.MekanismRecipeBuilder;
6-
import mekanism.api.math.FloatingLong;
76
import mekanism.api.recipes.ElectrolysisRecipe;
87
import mekanism.api.recipes.basic.BasicElectrolysisRecipe;
98
import mekanism.api.recipes.ingredients.FluidStackIngredient;
@@ -14,7 +13,7 @@ public class ElectrolysisRecipeBuilder extends MekanismRecipeBuilder<Electrolysi
1413
private final FluidStackIngredient input;
1514
private final GasStack leftGasOutput;
1615
private final GasStack rightGasOutput;
17-
private FloatingLong energyMultiplier = FloatingLong.ONE;
16+
private long energyMultiplier = 1;
1817

1918
protected ElectrolysisRecipeBuilder(FluidStackIngredient input, GasStack leftGasOutput, GasStack rightGasOutput) {
2019
this.input = input;
@@ -41,8 +40,8 @@ public static ElectrolysisRecipeBuilder separating(FluidStackIngredient input, G
4140
*
4241
* @param multiplier Multiplier to the energy cost in relation to the configured hydrogen separating energy cost. This value must be greater than or equal to one.
4342
*/
44-
public ElectrolysisRecipeBuilder energyMultiplier(FloatingLong multiplier) {
45-
if (multiplier.smallerThan(FloatingLong.ONE)) {
43+
public ElectrolysisRecipeBuilder energyMultiplier(long multiplier) {
44+
if (multiplier < 1) {
4645
throw new IllegalArgumentException("Energy multiplier must be greater than or equal to one");
4746
}
4847
this.energyMultiplier = multiplier;

src/api/java/mekanism/api/datagen/recipe/builder/ItemStackToEnergyRecipeBuilder.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import mekanism.api.annotations.NothingNullByDefault;
44
import mekanism.api.datagen.recipe.MekanismRecipeBuilder;
5-
import mekanism.api.math.FloatingLong;
65
import mekanism.api.recipes.ItemStackToEnergyRecipe;
76
import mekanism.api.recipes.basic.BasicItemStackToEnergyRecipe;
87
import mekanism.api.recipes.ingredients.ItemStackIngredient;
@@ -11,9 +10,9 @@
1110
public class ItemStackToEnergyRecipeBuilder extends MekanismRecipeBuilder<ItemStackToEnergyRecipeBuilder> {
1211

1312
private final ItemStackIngredient input;
14-
private final FloatingLong output;
13+
private final long output;
1514

16-
protected ItemStackToEnergyRecipeBuilder(ItemStackIngredient input, FloatingLong output) {
15+
protected ItemStackToEnergyRecipeBuilder(ItemStackIngredient input, long output) {
1716
this.input = input;
1817
this.output = output;
1918
}
@@ -24,8 +23,8 @@ protected ItemStackToEnergyRecipeBuilder(ItemStackIngredient input, FloatingLong
2423
* @param input Input.
2524
* @param output Output.
2625
*/
27-
public static ItemStackToEnergyRecipeBuilder energyConversion(ItemStackIngredient input, FloatingLong output) {
28-
if (output.isZero()) {
26+
public static ItemStackToEnergyRecipeBuilder energyConversion(ItemStackIngredient input, long output) {
27+
if (output <= 0L) {
2928
throw new IllegalArgumentException("This energy conversion recipe requires an energy output greater than zero");
3029
}
3130
return new ItemStackToEnergyRecipeBuilder(input, output);

src/api/java/mekanism/api/datagen/recipe/builder/PressurizedReactionRecipeBuilder.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import mekanism.api.annotations.NothingNullByDefault;
44
import mekanism.api.chemical.gas.GasStack;
55
import mekanism.api.datagen.recipe.MekanismRecipeBuilder;
6-
import mekanism.api.math.FloatingLong;
76
import mekanism.api.recipes.PressurizedReactionRecipe;
87
import mekanism.api.recipes.basic.BasicPressurizedReactionRecipe;
98
import mekanism.api.recipes.ingredients.GasStackIngredient;
@@ -17,7 +16,7 @@ public class PressurizedReactionRecipeBuilder extends MekanismRecipeBuilder<Pres
1716
private final ItemStackIngredient inputSolid;
1817
private final FluidStackIngredient inputFluid;
1918
private final GasStackIngredient inputGas;
20-
private FloatingLong energyRequired = FloatingLong.ZERO;
19+
private long energyRequired = 0;
2120
private final int duration;
2221
private final ItemStack outputItem;
2322
private final GasStack outputGas;
@@ -98,7 +97,10 @@ private static void validateDuration(int duration) {
9897
*
9998
* @param energyRequired How much "extra" energy this recipe requires, compared to the base energy requirements of the machine performing the recipe.
10099
*/
101-
public PressurizedReactionRecipeBuilder energyRequired(FloatingLong energyRequired) {
100+
public PressurizedReactionRecipeBuilder energyRequired(long energyRequired) {
101+
if (energyRequired < 0) {
102+
throw new IllegalArgumentException("This reaction recipe must have a positive or zero energy requirement.");
103+
}
102104
this.energyRequired = energyRequired;
103105
return this;
104106
}

0 commit comments

Comments
 (0)