Optimise ForgeConfigSpec and make Range public (#9828)

Backport of #9810 to 1.18.2
This commit is contained in:
Paint_Ninja authored and GitHub committed 2023-12-30 21:39:41 +00:00
1 parent b2c52b37a7
commit 892e236a74
2 files changed
+21 -23

No files matched your search

+1 -1
View File
@@ -34,7 +34,7 @@ import org.gradle.plugins.ide.eclipse.model.SourceFolder
plugins {
id 'org.cadixdev.licenser' version '0.6.1'
id 'com.github.ben-manes.versions' version '0.36.0'
id 'net.minecraftforge.gradleutils' version '2.+'
id 'net.minecraftforge.gradleutils' version '[2.2,2.3)'
id 'eclipse'
id 'de.undercouch.download' version '5.1.3'
}
@@ -24,6 +24,7 @@ import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
@@ -55,14 +56,16 @@ import com.google.common.collect.ObjectArrays;
*/
public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfig> implements IConfigSpec<ForgeConfigSpec>//TODO: Remove extends and pipe everything through getSpec/getValues?
{
private Map<List<String>, String> levelComments;
private Map<List<String>, String> levelTranslationKeys;
private final Map<List<String>, String> levelComments;
private final Map<List<String>, String> levelTranslationKeys;
private UnmodifiableConfig values;
private final UnmodifiableConfig values;
private Config childConfig;
private boolean isCorrecting = false;
private static final Pattern WINDOWS_NEWLINE = Pattern.compile("\r\n");
private ForgeConfigSpec(UnmodifiableConfig storage, UnmodifiableConfig values, Map<List<String>, String> levelComments, Map<List<String>, String> levelTranslationKeys) {
super(storage);
this.values = values;
@@ -123,11 +126,9 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
private void resetCaches(final Iterable<Object> configValues) {
configValues.forEach(value -> {
if (value instanceof ConfigValue) {
final ConfigValue<?> configValue = (ConfigValue<?>) value;
if (value instanceof ConfigValue<?> configValue) {
configValue.clearCache();
} else if (value instanceof Config) {
final Config innerConfig = (Config) value;
} else if (value instanceof Config innerConfig) {
this.resetCaches(innerConfig.valueMap().values());
}
});
@@ -266,15 +267,12 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
private boolean stringsMatchIgnoringNewlines(@Nullable Object obj1, @Nullable Object obj2)
{
if(obj1 instanceof String && obj2 instanceof String)
if(obj1 instanceof String string1 && obj2 instanceof String string2)
{
String string1 = (String) obj1;
String string2 = (String) obj2;
if(string1.length() > 0 && string2.length() > 0)
if (!string1.isEmpty() && !string2.isEmpty())
{
return string1.replaceAll("\r\n", "\n")
.equals(string2.replaceAll("\r\n", "\n"));
return WINDOWS_NEWLINE.matcher(string1).replaceAll("\n")
.equals(WINDOWS_NEWLINE.matcher(string2).replaceAll("\n"));
}
}
@@ -286,10 +284,10 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
{
private final Config storage = Config.of(LinkedHashMap::new, InMemoryFormat.withUniversalSupport()); // Use LinkedHashMap for consistent ordering
private BuilderContext context = new BuilderContext();
private Map<List<String>, String> levelComments = new HashMap<>();
private Map<List<String>, String> levelTranslationKeys = new HashMap<>();
private List<String> currentPath = new ArrayList<>();
private List<ConfigValue<?>> values = new ArrayList<>();
private final Map<List<String>, String> levelComments = new HashMap<>();
private final Map<List<String>, String> levelTranslationKeys = new HashMap<>();
private final List<String> currentPath = new ArrayList<>();
private final List<ConfigValue<?>> values = new ArrayList<>();
private boolean hasInvalidComment = false;
//Object
@@ -375,7 +373,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
LogManager.getLogger().debug(CORE, "List on key {} is deemed to need correction. It is null, not a list, or an empty list. Modders, consider defineListAllowEmpty?", path.get(path.size() - 1));
return getDefault();
}
List<?> list = Lists.newArrayList((List<?>) value);
List<?> list = new ArrayList<>((List<?>) value);
list.removeIf(elementValidator.negate());
if (list.isEmpty()) {
LogManager.getLogger().debug(CORE, "List on key {} is deemed to need correction. It failed validation.", path.get(path.size() - 1));
@@ -395,7 +393,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
LogManager.getLogger().debug(CORE, "List on key {} is deemed to need correction, as it is null or not a list.", path.get(path.size() - 1));
return getDefault();
}
List<?> list = Lists.newArrayList((List<?>) value);
List<?> list = new ArrayList<>((List<?>) value);
list.removeIf(elementValidator.negate());
if (list.isEmpty()) {
LogManager.getLogger().debug(CORE, "List on key {} is deemed to need correction. It failed validation.", path.get(path.size() - 1));
@@ -696,7 +694,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
}
@SuppressWarnings("unused")
private static class Range<V extends Comparable<? super V>> implements Predicate<Object>
public static class Range<V extends Comparable<? super V>> implements Predicate<Object>
{
private final Class<? extends V> clazz;
private final V min;
@@ -832,7 +830,7 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
public List<String> getPath()
{
return Lists.newArrayList(path);
return new ArrayList<>(path);
}
@Override