Skip to content

Commit

Permalink
Store FloatingLongs as a string in NBT to be able to easily switch to…
Browse files Browse the repository at this point in the history
… unsigned longs if we decide to without voiding saved energy again
  • Loading branch information
pupnewfster committed Mar 25, 2020
1 parent 2711127 commit a2bc3a0
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 50 deletions.
2 changes: 0 additions & 2 deletions src/api/java/mekanism/api/NBTConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public final class NBTConstants {
public static final String CONTROL_TYPE = "controlType";
public static final String DATA = "data";
public static final String DATA_TYPE = "dataType";
public static final String DECIMAL = "decimal";
public static final String DELAY = "delay";
public static final String DIMENSION = "dimension";
public static final String DUMP_LEFT = "dumpLeft";
Expand Down Expand Up @@ -154,6 +153,5 @@ public final class NBTConstants {
public static final String UPDATE_DELAY = "updateDelay";
public static final String UPGRADES = "upgrades";
public static final String USED_NODES = "usedNodes";
public static final String VALUE = "value";
public static final String WORLD_GEN_VERSION = "worldGenVersion";
}
2 changes: 1 addition & 1 deletion src/api/java/mekanism/api/energy/IEnergyContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ default FloatingLong getNeeded() {
default CompoundNBT serializeNBT() {
CompoundNBT nbt = new CompoundNBT();
if (!isEmpty()) {
nbt.put(NBTConstants.STORED, getEnergy().serializeNBT());
nbt.putString(NBTConstants.STORED, getEnergy().toString());
}
return nbt;
}
Expand Down
37 changes: 1 addition & 36 deletions src/api/java/mekanism/api/math/FloatingLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import mekanism.api.NBTConstants;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.common.util.INBTSerializable;

/**
* A class representing a positive number with an internal value defined by a long, and a floating point number stored in a short
*/
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class FloatingLong extends Number implements Comparable<FloatingLong>, INBTSerializable<CompoundNBT> {
public class FloatingLong extends Number implements Comparable<FloatingLong> {

//TODO: Eventually we should define a way of doing a set of operations all at once, and outputting a new value
// given that way we can internally do all the calculations using primitives rather than spamming a lot of objects
Expand Down Expand Up @@ -131,20 +127,6 @@ public static FloatingLong createConst(long value, short decimal) {
return new FloatingLong(value, decimal, true);
}

/**
* Reads a mutable {@link FloatingLong} from NBT
*
* @param nbt The {@link CompoundNBT} to read from
*
* @return A mutable {@link FloatingLong}, or {@link #ZERO} if the given nbt is null or empty.
*/
public static FloatingLong readFromNBT(@Nullable CompoundNBT nbt) {
if (nbt == null || nbt.isEmpty()) {
return ZERO;
}
return create(nbt.getLong(NBTConstants.VALUE), nbt.getShort(NBTConstants.DECIMAL));
}

/**
* Reads a mutable {@link FloatingLong} from a buffer
*
Expand Down Expand Up @@ -655,23 +637,6 @@ public double doubleValue() {
return longValue() + decimal / (double) SINGLE_UNIT;
}

@Override
public CompoundNBT serializeNBT() {
//TODO: Do we want to do this in a different form to make sure that it will support unsigned longs
CompoundNBT nbt = new CompoundNBT();
nbt.putLong(NBTConstants.VALUE, value);
nbt.putShort(NBTConstants.DECIMAL, decimal);
return nbt;
}

@Override
public void deserializeNBT(CompoundNBT nbt) {
if (isConstant) {
throw new IllegalStateException("Tried to modify a floating constant long");
}
setAndClampValues(nbt.getLong(NBTConstants.VALUE), nbt.getShort(NBTConstants.DECIMAL));
}

/**
* Writes this {@link FloatingLong} to the given buffer
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public FloatingLong getMaxEnergy() {
public CompoundNBT serializeNBT() {
CompoundNBT nbt = new CompoundNBT();
if (!isEmpty()) {
nbt.put(NBTConstants.STORED, stored.serializeNBT());
nbt.putString(NBTConstants.STORED, stored.toString());
}
return nbt;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void updateEnergyUsage(FloatingLong energyUsage) {
@Override
public CompoundNBT serializeNBT() {
CompoundNBT nbt = super.serializeNBT();
nbt.put(NBTConstants.ENERGY_USAGE, getEnergyPerTick().serializeNBT());
nbt.putString(NBTConstants.ENERGY_USAGE, getEnergyPerTick().toString());
return nbt;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ public void read(CompoundNBT nbtTags) {
@Override
public CompoundNBT write(CompoundNBT nbtTags) {
super.write(nbtTags);
nbtTags.put(NBTConstants.LAST_FIRED, lastFired.serializeNBT());
nbtTags.putString(NBTConstants.LAST_FIRED, lastFired.toString());
return nbtTags;
}

@Nonnull
@Override
public CompoundNBT getUpdateTag() {
CompoundNBT updateTag = super.getUpdateTag();
updateTag.put(NBTConstants.LAST_FIRED, lastFired.serializeNBT());
updateTag.putString(NBTConstants.LAST_FIRED, lastFired.toString());
return updateTag;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public void read(CompoundNBT nbtTags) {
@Override
public CompoundNBT write(CompoundNBT nbtTags) {
super.write(nbtTags);
nbtTags.put(NBTConstants.MIN, minThreshold.serializeNBT());
nbtTags.put(NBTConstants.MAX, maxThreshold.serializeNBT());
nbtTags.putString(NBTConstants.MIN, minThreshold.toString());
nbtTags.putString(NBTConstants.MAX, maxThreshold.toString());
nbtTags.putInt(NBTConstants.TIME, time);
nbtTags.putInt(NBTConstants.OUTPUT_MODE, outputMode.ordinal());
return nbtTags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ public TransmitterType getTransmitterType() {
@Override
public void read(CompoundNBT nbtTags) {
super.read(nbtTags);
if (nbtTags.contains(NBTConstants.ENERGY_STORED, NBT.TAG_COMPOUND)) {
lastWrite = FloatingLong.readFromNBT(nbtTags.getCompound(NBTConstants.ENERGY_STORED));
if (nbtTags.contains(NBTConstants.ENERGY_STORED, NBT.TAG_STRING)) {
try {
lastWrite = FloatingLong.parseFloatingLong(nbtTags.getString(NBTConstants.ENERGY_STORED));
} catch (NumberFormatException e) {
lastWrite = FloatingLong.ZERO;
}
} else {
lastWrite = FloatingLong.ZERO;
}
Expand All @@ -157,7 +161,7 @@ public CompoundNBT write(CompoundNBT nbtTags) {
if (lastWrite.isZero()) {
nbtTags.remove(NBTConstants.ENERGY_STORED);
} else {
nbtTags.put(NBTConstants.ENERGY_STORED, lastWrite.serializeNBT());
nbtTags.putString(NBTConstants.ENERGY_STORED, lastWrite.toString());
}
return nbtTags;
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/mekanism/common/util/NBTUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ public static void setInfusionStackIfPresent(CompoundNBT nbt, String key, Consum
}

public static void setFloatingLongIfPresent(CompoundNBT nbt, String key, FloatingLongConsumer setter) {
if (nbt.contains(key, NBT.TAG_COMPOUND)) {
setter.accept(FloatingLong.readFromNBT(nbt.getCompound(key)));
if (nbt.contains(key, NBT.TAG_STRING)) {
try {
setter.accept(FloatingLong.parseFloatingLong(nbt.getString(key)));
} catch (NumberFormatException e) {
setter.accept(FloatingLong.ZERO);
}
}
}

Expand Down

0 comments on commit a2bc3a0

Please sign in to comment.