Skip to content

Commit

Permalink
Add number specific ranges.
Browse files Browse the repository at this point in the history
<log>Added number specific range codecs</log>
  • Loading branch information
ThatGravyBoat committed Dec 21, 2023
1 parent 326a05d commit a377013
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.teamresourceful.resourcefullib.common.collections.WeightedCollection;
import com.teamresourceful.resourcefullib.common.exceptions.UtilityClassException;
import net.minecraft.core.Registry;
import net.minecraft.util.ExtraCodecs;
import org.jetbrains.annotations.Nullable;

import java.util.*;
Expand All @@ -19,6 +20,24 @@

public final class CodecExtras {

public static final Codec<Double> NON_NEGATIVE_DOUBLE = doubleRangeWithMessage(0, Double.MAX_VALUE, aDouble -> "Value must be greater than or equal to 0. Found: " + aDouble);
public static final Codec<Double> POSITIVE_DOUBLE = doubleRangeWithMessage(1, Double.MAX_VALUE, aDouble -> "Value must be greater than 0. Found: " + aDouble);
public static final Codec<Double> NON_POSITIVE_DOUBLE = doubleRangeWithMessage(Double.MIN_VALUE, 0, aDouble -> "Value must be less than or equal to 0. Found: " + aDouble);
public static final Codec<Double> NEGATIVE_DOUBLE = doubleRangeWithMessage(Double.MIN_VALUE, -1, aDouble -> "Value must be less than 0. Found: " + aDouble);
public static final Codec<Double> DOUBLE_UNIT_INTERVAL = doubleRangeWithMessage(0, 1, aDouble -> "Value must be between 0 and 1. Found: " + aDouble);

public static final Codec<Float> NON_NEGATIVE_FLOAT = floatRangeWithMessage(0f, Float.MAX_VALUE, aFloat -> "Value must be greater than or equal to 0. Found: " + aFloat);
public static final Codec<Float> POSITIVE_FLOAT = floatRangeWithMessage(1f, Float.MAX_VALUE, aFloat -> "Value must be greater than 0. Found: " + aFloat);
public static final Codec<Float> NON_POSITIVE_FLOAT = floatRangeWithMessage(Float.MIN_VALUE, 0f, aFloat -> "Value must be less than or equal to 0. Found: " + aFloat);
public static final Codec<Float> NEGATIVE_FLOAT = floatRangeWithMessage(Float.MIN_VALUE, -1f, aFloat -> "Value must be less than 0. Found: " + aFloat);
public static final Codec<Float> FLOAT_UNIT_INTERVAL = floatRangeWithMessage(0f, 1f, aFloat -> "Value must be between 0 and 1. Found: " + aFloat);

public static final Codec<Integer> NON_NEGATIVE_INT = ExtraCodecs.NON_NEGATIVE_INT;
public static final Codec<Integer> POSITIVE_INT = ExtraCodecs.POSITIVE_INT;
public static final Codec<Integer> NON_POSITIVE_INT = intRangeWithMessage(Integer.MIN_VALUE, 0, integer -> "Value must be less than or equal to 0. Found: " + integer);
public static final Codec<Integer> NEGATIVE_INT = intRangeWithMessage(Integer.MIN_VALUE, -1, integer -> "Value must be less than 0. Found: " + integer);
public static final Codec<Integer> INT_UNIT_INTERVAL = intRangeWithMessage(0, 1, integer -> "Value must be between 0 and 1. Found: " + integer);

private CodecExtras() throws UtilityClassException {
throw new UtilityClassException();
}
Expand Down Expand Up @@ -91,4 +110,26 @@ private static JsonElement clearNulls(JsonElement json) {
}
return json.isJsonNull() ? null : json;
}

private static Codec<Integer> intRangeWithMessage(int i, int j, Function<Integer, String> function) {
return ExtraCodecs.validate(
Codec.INT,
integer -> integer.compareTo(i) >= 0 && integer.compareTo(j) <= 0 ? DataResult.success(integer) : DataResult.error(() -> function.apply(integer))
);
}


private static Codec<Double> doubleRangeWithMessage(double i, double j, Function<Double, String> function) {
return ExtraCodecs.validate(
Codec.DOUBLE,
aDouble -> aDouble.compareTo(i) >= 0 && aDouble.compareTo(j) <= 0 ? DataResult.success(aDouble) : DataResult.error(() -> function.apply(aDouble))
);
}

private static Codec<Float> floatRangeWithMessage(float i, float j, Function<Float, String> function) {
return ExtraCodecs.validate(
Codec.FLOAT,
aFloat -> aFloat.compareTo(i) >= 0 && aFloat.compareTo(j) <= 0 ? DataResult.success(aFloat) : DataResult.error(() -> function.apply(aFloat))
);
}
}

0 comments on commit a377013

Please sign in to comment.