Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.util.ObfuscationReflectionHelper;
import org.apache.commons.lang3.tuple.Pair;

import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -158,27 +157,22 @@ public static <V> V lastValue(List<V> list, V defaultValue)
}

/**
* Reflection to get Forge's range of a value
* Gets Forge's range of a value
*/
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings("unchecked")
public void loadRange()
{
if(this.range == null)
{
try
ForgeConfigSpec.Range<?> range = this.valueSpec.getRange();
if(range != null)
{
Object range = ObfuscationReflectionHelper.getPrivateValue(ForgeConfigSpec.ValueSpec.class, this.valueSpec, "range");
if(range != null)
{
Class rangeClass = Class.forName("net.minecraftforge.common.ForgeConfigSpec$Range");
Object min = ObfuscationReflectionHelper.getPrivateValue(rangeClass, range, "min");
Object max = ObfuscationReflectionHelper.getPrivateValue(rangeClass, range, "max");
this.range = Pair.of((T) min, (T) max);
return;
}
this.range = Pair.of((T) range.getMin(), (T) range.getMax());
}
else
{
this.range = Pair.of(null, null);
}
catch(ClassNotFoundException ignored) {}
this.range = Pair.of(null, null);
}
}
}
2 changes: 2 additions & 0 deletions forge/src/test/java/test/config/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static class Test
public final ForgeConfigSpec.ConfigValue<List<? extends Long>> longList;
public final ForgeConfigSpec.ConfigValue<List<? extends Double>> doubleList;
public final ForgeConfigSpec.EnumValue<ChatFormatting> restrictedEnums;
public final ForgeConfigSpec.ConfigValue<String> stringWithPattern;

public Test(ForgeConfigSpec.Builder builder)
{
Expand All @@ -59,6 +60,7 @@ public Test(ForgeConfigSpec.Builder builder)
this.longValue = builder.comment("This is a Long value").defineInRange("longValue", 0L, 0L, 10L);
this.enumValue = builder.comment("This is an Enum value").defineEnum("enumValue", ChatFormatting.BLACK);
this.restrictedEnums = builder.comment("An enum value but with restricted values").defineEnum("restrictedEnums", ChatFormatting.RED, ChatFormatting.RED, ChatFormatting.GREEN, ChatFormatting.BLUE);
this.stringWithPattern = builder.comment("A string value with pattern \\d+").define("stringWithPattern", "0", o -> o instanceof String s && s.matches("\\d+"));
builder.pop();
builder.translation("forge_config.config_test.client.lists").push("lists");
this.intList = builder.comment("This is an Integer list").defineList("intList", Arrays.asList(5, 10), o -> o instanceof Integer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,22 @@ public static <V> V lastValue(List<V> list, V defaultValue)
}

/**
* Reflection to get Forge's range of a value
* Gets Forge's range of a value
*/
@SuppressWarnings({"unchecked"})
@SuppressWarnings("unchecked")
public void loadRange()
{
if(this.range == null)
{
this.range = Pair.of((T) this.valueSpec.getRange().getMin(), (T) this.valueSpec.getRange().getMax());
ModConfigSpec.Range<?> range = this.valueSpec.getRange();
if(range != null)
{
this.range = Pair.of((T) range.getMin(), (T) range.getMax());
}
else
{
this.range = Pair.of(null, null);
}
}
}
}
3 changes: 3 additions & 0 deletions neoforge/src/test/java/test/config/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

/**
* Author: MrCrayfish
Expand Down Expand Up @@ -48,6 +49,7 @@ public static class Test
public final ModConfigSpec.ConfigValue<List<? extends Long>> longList;
public final ModConfigSpec.ConfigValue<List<? extends Double>> doubleList;
public final ModConfigSpec.EnumValue<ChatFormatting> restrictedEnums;
public final ModConfigSpec.ConfigValue<String> stringWithPattern;

public Test(ModConfigSpec.Builder builder)
{
Expand All @@ -59,6 +61,7 @@ public Test(ModConfigSpec.Builder builder)
this.longValue = builder.comment("This is a Long value").defineInRange("longValue", 0L, 0L, 10L);
this.enumValue = builder.comment("This is an Enum value").defineEnum("enumValue", ChatFormatting.BLACK);
this.restrictedEnums = builder.comment("An enum value but with restricted values").defineEnum("restrictedEnums", ChatFormatting.RED, ChatFormatting.RED, ChatFormatting.GREEN, ChatFormatting.BLUE);
this.stringWithPattern = builder.comment("A string value with pattern \\d+").define("stringWithPattern", "0", o -> o instanceof String s && s.matches("\\d+"));
builder.pop();
builder.translation("forge_config.config_test.client.lists").push("lists");
this.intList = builder.comment("This is an Integer list").defineList("intList", Arrays.asList(5, 10), o -> o instanceof Integer);
Expand Down