Skip to content

Commit

Permalink
Automatically add range to config comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tterrag1098 committed Jun 23, 2019
1 parent 448d996 commit 252b94a
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/main/java/net/minecraftforge/common/ForgeConfigSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.ObjectArrays;

/*
* Like {@link com.electronwill.nightconfig.core.ConfigSpec} except in builder format, and extended to acept comments, language keys,
Expand Down Expand Up @@ -247,6 +248,7 @@ public <V extends Comparable<? super V>> ConfigValue<V> defineInRange(String pat
public <V extends Comparable<? super V>> ConfigValue<V> defineInRange(List<String> path, Supplier<V> defaultSupplier, V min, V max, Class<V> clazz) {
Range<V> range = new Range<>(clazz, min, max);
context.setRange(range);
context.setComment(ObjectArrays.concat(context.getComment(), "Range: " + range.toString()));
if (min.compareTo(max) > 0)
throw new IllegalArgumentException("Range min most be less then max.");
return define(path, defaultSupplier, range);
Expand Down Expand Up @@ -539,7 +541,7 @@ private void validate(boolean value, String message)
@SuppressWarnings("unused")
private static class Range<V extends Comparable<? super V>> implements Predicate<Object>
{
private final Class<V> clazz;
private final Class<? extends V> clazz;
private final V min;
private final V max;

Expand All @@ -550,7 +552,7 @@ private Range(Class<V> clazz, V min, V max)
this.max = max;
}

public Class<V> getClazz() { return clazz; }
public Class<? extends V> getClazz() { return clazz; }
public V getMin() { return min; }
public V getMax() { return max; }

Expand All @@ -561,6 +563,19 @@ public boolean test(Object t)
V c = clazz.cast(t);
return c.compareTo(min) >= 0 && c.compareTo(max) <= 0;
}

@Override
public String toString()
{
if (clazz == Integer.class) {
if (max.equals(Integer.MAX_VALUE)) {
return "> " + min;
} else if (min.equals(Integer.MIN_VALUE)) {
return "< " + max;
}
} // TODO add more special cases?
return min + " ~ " + max;
}
}

public static class ValueSpec
Expand Down

0 comments on commit 252b94a

Please sign in to comment.