Skip to content

Commit 418d53b

Browse files
committed
Fix FloatingLong codec not properly using data results for errors
1 parent 173121d commit 418d53b

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/api/java/mekanism/api/math/FloatingLong.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class FloatingLong extends Number implements Comparable<FloatingLong> {
2525
public static final Codec<FloatingLong> CODEC = new PrimitiveCodec<>() {
2626
@Override
2727
public <T> DataResult<FloatingLong> read(DynamicOps<T> ops, T input) {
28-
return ops.getNumberValue(input).map(number -> fromNumber(number, true));
28+
return ops.getNumberValue(input).flatMap(number -> fromNumber(number, true));
2929
}
3030

3131
@Override
@@ -1140,21 +1140,25 @@ public static FloatingLong parseFloatingLong(String string, boolean isConstant)
11401140
return isConstant ? createConst(value, decimal) : create(value, decimal);
11411141
}
11421142

1143-
private static FloatingLong fromNumber(Number number, boolean isConstant) {
1143+
private static DataResult<FloatingLong> fromNumber(Number number, boolean isConstant) {
11441144
if (number instanceof Integer || number instanceof Long || number instanceof Short || number instanceof Byte || number instanceof BigInteger) {
11451145
long longValue = number.longValue();
11461146
if (longValue < 0) {
1147-
throw new NumberFormatException("Number must be positive");
1147+
return DataResult.error(() -> "Number must be positive");
11481148
}
1149-
return isConstant ? createConst(longValue, (short) 0) : create(longValue, (short) 0);
1149+
return DataResult.success(isConstant ? createConst(longValue, (short) 0) : create(longValue, (short) 0));
11501150
} else if (number instanceof BigDecimal decimal) {
11511151
try {
11521152
long longValue = decimal.longValueExact();
1153-
return isConstant ? createConst(longValue, (short) 0) : create(longValue, (short) 0);
1153+
return DataResult.success(isConstant ? createConst(longValue, (short) 0) : create(longValue, (short) 0));
11541154
} catch (ArithmeticException ignored) {
11551155
}
11561156
}
1157-
return parseFloatingLong(number.toString(), isConstant);
1157+
try {
1158+
return DataResult.success(parseFloatingLong(number.toString(), isConstant));
1159+
} catch (NumberFormatException e) {
1160+
return DataResult.error(e::getMessage);
1161+
}
11581162
}
11591163

11601164
/**

0 commit comments

Comments
 (0)