mirror of
https://github.com/FalsePattern/FalsePatternLib.git
synced 2026-11-17 05:15:02 +00:00
code cleanup
This commit is contained in:
76 files changed
+691
-622
No files matched your search
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
*
|
||||
@@ -22,6 +22,7 @@ package com.falsepattern.lib.config;
|
||||
|
||||
import com.falsepattern.lib.DeprecationDetails;
|
||||
import com.falsepattern.lib.StableAPI;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -31,7 +32,7 @@ import java.util.function.BiConsumer;
|
||||
|
||||
/**
|
||||
* A modern configuration system to replace the old and obtuse forge config system.
|
||||
*
|
||||
* <p>
|
||||
* Note that just annotating a configuration class with {@link Config} is not enough, you must also register it using
|
||||
* {@link ConfigurationManager#initialize(Class[])} or {@link ConfigurationManager#initialize(BiConsumer, Class[])}!
|
||||
*/
|
||||
@@ -191,10 +192,10 @@ public @interface Config {
|
||||
|
||||
/**
|
||||
* This annotation limits the maximum number of characters present in a string configuration.
|
||||
*
|
||||
* <p>
|
||||
* Note: If this annotation is not present, the maximum length will be implicitly set to 256 to avoid malicious
|
||||
* synchronizations that would make clients run out of memory!
|
||||
*
|
||||
* <p>
|
||||
* When used with a string list, this limit will apply to each element individually, not to the size of the list as a whole.
|
||||
*/
|
||||
@StableAPI(since = "0.10.0")
|
||||
@@ -265,7 +266,7 @@ public @interface Config {
|
||||
}
|
||||
|
||||
/**
|
||||
* The default value for an boolean array field. Not having a default is deprecated since 0.10, and will be strongly
|
||||
* The default value for a boolean array field. Not having a default is deprecated since 0.10, and will be strongly
|
||||
* enforced in 0.11+!
|
||||
*/
|
||||
@StableAPI(since = "0.10.0")
|
||||
@@ -290,7 +291,7 @@ public @interface Config {
|
||||
/**
|
||||
* This annotation limits the maximum number of elements present in an array configuration. Only effective if
|
||||
* {@link ListFixedLength} is NOT present.
|
||||
*
|
||||
* <p>
|
||||
* Note: If this annotation is not present, the maximum length will be implicitly set to 256 to avoid malicious
|
||||
* synchronizations that would make clients run out of memory!
|
||||
*/
|
||||
@@ -303,7 +304,6 @@ public @interface Config {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The name of this config property in the config file. If not specified, the field's name will be used instead.
|
||||
*/
|
||||
@@ -338,7 +338,7 @@ public @interface Config {
|
||||
/**
|
||||
* Signals that this configuration class should be synchronized between the client and the server when
|
||||
* joining a multiplayer instance.
|
||||
*
|
||||
* <p>
|
||||
* Note that synchronization ALWAYS happens FROM the server TO the client. The server should NEVER attempt to get
|
||||
* configuration values from a client. This is to avoid malicious clients manipulating the server configs.
|
||||
*/
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
*
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
*
|
||||
@@ -49,17 +49,18 @@ public class ConfigurationManager {
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static void initialize(BiConsumer<Class<?>, Field> validatorErrorCallback, Class<?>... configClasses) throws ConfigException {
|
||||
for (val clazz: configClasses) {
|
||||
public static void initialize(BiConsumer<Class<?>, Field> validatorErrorCallback, Class<?>... configClasses)
|
||||
throws ConfigException {
|
||||
for (val clazz : configClasses) {
|
||||
ConfigurationManagerImpl.register(clazz);
|
||||
ConfigurationManagerImpl.load(clazz);
|
||||
ConfigurationManagerImpl.validateFields(validatorErrorCallback, clazz, true);
|
||||
ConfigurationManagerImpl.save(clazz);
|
||||
}
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static boolean validate(boolean resetInvalid, Class<?>... configClasses)
|
||||
throws ConfigException {
|
||||
public static boolean validate(boolean resetInvalid, Class<?>... configClasses) throws ConfigException {
|
||||
return validate((x, y) -> {}, resetInvalid, configClasses);
|
||||
}
|
||||
|
||||
@@ -67,7 +68,7 @@ public class ConfigurationManager {
|
||||
public static boolean validate(BiConsumer<Class<?>, Field> validatorErrorCallback, boolean resetInvalid, Class<?>... configClasses)
|
||||
throws ConfigException {
|
||||
boolean valid = true;
|
||||
for (val clazz: configClasses) {
|
||||
for (val clazz : configClasses) {
|
||||
valid &= ConfigurationManagerImpl.validateFields(validatorErrorCallback, clazz, resetInvalid);
|
||||
}
|
||||
return valid;
|
||||
@@ -75,14 +76,14 @@ public class ConfigurationManager {
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static void loadFromFile(Class<?>... configClasses) throws ConfigException {
|
||||
for (val clazz: configClasses) {
|
||||
for (val clazz : configClasses) {
|
||||
ConfigurationManagerImpl.load(clazz);
|
||||
}
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static void saveToFile(boolean validateAndResetInvalid, Class<?>... configClasses) throws ConfigException {
|
||||
for (val clazz: configClasses) {
|
||||
for (val clazz : configClasses) {
|
||||
if (validateAndResetInvalid) {
|
||||
ConfigurationManagerImpl.validateFields((a, b) -> {}, clazz, true);
|
||||
}
|
||||
@@ -99,7 +100,9 @@ public class ConfigurationManager {
|
||||
@DeprecationDetails(stableSince = "0.6.0",
|
||||
deprecatedSince = "0.10.0")
|
||||
public static void registerConfig(Class<?> configClass) throws ConfigException {
|
||||
FalsePatternLib.getLog().warn("A mod is using the deprecated config registration method! The following exception contains the stacktrace.", new Exception());
|
||||
FalsePatternLib.getLog()
|
||||
.warn("A mod is using the deprecated config registration method! The following exception contains the stacktrace.",
|
||||
new Exception());
|
||||
ConfigurationManagerImpl.registerLoadSaveConfig(configClass);
|
||||
}
|
||||
|
||||
@@ -127,11 +130,13 @@ public class ConfigurationManager {
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static List<IConfigElement> getConfigElementsMulti(Class<?>... configClasses) throws ConfigException {
|
||||
switch (configClasses.length) {
|
||||
case 0: return Collections.emptyList();
|
||||
case 1: return getConfigElements(configClasses[0]);
|
||||
case 0:
|
||||
return Collections.emptyList();
|
||||
case 1:
|
||||
return getConfigElements(configClasses[0]);
|
||||
default:
|
||||
val result = new ArrayList<IConfigElement>();
|
||||
for (val configClass: configClasses) {
|
||||
for (val configClass : configClasses) {
|
||||
result.addAll(getConfigElements(configClass));
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
*
|
||||
@@ -24,6 +24,7 @@ import cpw.mods.fml.client.config.ConfigGuiType;
|
||||
import cpw.mods.fml.client.config.GuiConfigEntries;
|
||||
import cpw.mods.fml.client.config.GuiEditArrayEntries;
|
||||
import cpw.mods.fml.client.config.IConfigElement;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
*
|
||||
@@ -21,28 +21,25 @@
|
||||
package com.falsepattern.lib.config;
|
||||
|
||||
import com.falsepattern.lib.StableAPI;
|
||||
import lombok.val;
|
||||
|
||||
import cpw.mods.fml.client.config.GuiConfig;
|
||||
import cpw.mods.fml.client.config.IConfigElement;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import cpw.mods.fml.client.config.GuiConfig;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@StableAPI(since = "0.9.0")
|
||||
public class SimpleGuiConfig extends GuiConfig {
|
||||
public SimpleGuiConfig(GuiScreen parent, Class<?> configClass, String modID, String modName) throws ConfigException {
|
||||
public SimpleGuiConfig(GuiScreen parent, Class<?> configClass, String modID, String modName)
|
||||
throws ConfigException {
|
||||
this(parent, new Class<?>[]{configClass}, modID, modName);
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public SimpleGuiConfig(GuiScreen parent, Class<?>[] configClasses, String modID, String modName) throws ConfigException {
|
||||
super(parent, ConfigurationManager.getConfigElementsMulti(configClasses), modID, false, false, modName + " Configuration", I18n.format("falsepatternlib.gui.config.description"));
|
||||
public SimpleGuiConfig(GuiScreen parent, Class<?>[] configClasses, String modID, String modName)
|
||||
throws ConfigException {
|
||||
super(parent, ConfigurationManager.getConfigElementsMulti(configClasses), modID, false, false,
|
||||
modName + " Configuration", I18n.format("falsepatternlib.gui.config.description"));
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
*
|
||||
@@ -21,10 +21,11 @@
|
||||
package com.falsepattern.lib.config;
|
||||
|
||||
import com.falsepattern.lib.StableAPI;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import cpw.mods.fml.client.IModGuiFactory;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
*
|
||||
@@ -32,7 +32,7 @@ import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
/**
|
||||
* This event is pushed on the FMLCommonHandler event bus once all the config synchronizations have finished.
|
||||
*
|
||||
* <p>
|
||||
* This is a client-only event, and is never triggered serverside!
|
||||
*/
|
||||
@SideOnly(Side.CLIENT)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
*
|
||||
@@ -32,7 +32,7 @@ import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
/**
|
||||
* This event is pushed on the FMLCommonHandler event bus once each time a single config synchronization has finished.
|
||||
*
|
||||
* <p>
|
||||
* This is a client-only event, and is never triggered serverside!
|
||||
*/
|
||||
@SideOnly(Side.CLIENT)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
*
|
||||
@@ -36,11 +36,20 @@ import java.util.List;
|
||||
* You can use this event to manually trigger a synchronization request. The two different flavors are used on the two
|
||||
* different game sides. If you want to trigger this on the server, use {@link #postServer}, and if on the client, use
|
||||
* {@link #postClient}
|
||||
*
|
||||
*/
|
||||
@StableAPI(since = "0.10.0")
|
||||
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class ConfigSyncRequestEvent extends Event {
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static void postClient() {
|
||||
FMLCommonHandler.instance().bus().post(new Client());
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static void postServer(List<EntityPlayerMP> players) {
|
||||
FMLCommonHandler.instance().bus().post(new Server(new ArrayList<>(players)));
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static class Client extends ConfigSyncRequestEvent {
|
||||
|
||||
@@ -55,14 +64,4 @@ public class ConfigSyncRequestEvent extends Event {
|
||||
return Collections.unmodifiableList(players);
|
||||
}
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static void postClient() {
|
||||
FMLCommonHandler.instance().bus().post(new Client());
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static void postServer(List<EntityPlayerMP> players) {
|
||||
FMLCommonHandler.instance().bus().post(new Server(new ArrayList<>(players)));
|
||||
}
|
||||
}
|
||||
@@ -54,15 +54,24 @@ public abstract class ConfigValidationFailureEvent extends Event {
|
||||
public final int listIndex;
|
||||
|
||||
public static void postNumericRangeOutOfBounds(Field field, int listIndex, String value, String min, String max) {
|
||||
FMLCommonHandler.instance().bus().post(new NumericRangeOutOfBounds(field.getDeclaringClass(), field.getName(), listIndex, value, min, max));
|
||||
FMLCommonHandler.instance()
|
||||
.bus()
|
||||
.post(new NumericRangeOutOfBounds(field.getDeclaringClass(), field.getName(), listIndex, value,
|
||||
min, max));
|
||||
}
|
||||
|
||||
public static void postSize(Field field, int requestedSize, boolean fixedSize, int maxSize, int defaultSize) {
|
||||
FMLCommonHandler.instance().bus().post(new ListSizeOutOfBounds(field.getDeclaringClass(), field.getName(), requestedSize, fixedSize, maxSize, defaultSize));
|
||||
FMLCommonHandler.instance()
|
||||
.bus()
|
||||
.post(new ListSizeOutOfBounds(field.getDeclaringClass(), field.getName(), requestedSize,
|
||||
fixedSize, maxSize, defaultSize));
|
||||
}
|
||||
|
||||
public static void postStringSizeOutOfBounds(Field field, int listIndex, String text, int maxSize) {
|
||||
FMLCommonHandler.instance().bus().post(new StringSizeOutOfBounds(field.getDeclaringClass(), field.getName(), listIndex, text, maxSize));
|
||||
FMLCommonHandler.instance()
|
||||
.bus()
|
||||
.post(new StringSizeOutOfBounds(field.getDeclaringClass(), field.getName(), listIndex, text,
|
||||
maxSize));
|
||||
}
|
||||
|
||||
public static void fieldIsNull(Field field, int listIndex) {
|
||||
@@ -70,7 +79,35 @@ public abstract class ConfigValidationFailureEvent extends Event {
|
||||
}
|
||||
|
||||
public static void postStringPatternMismatch(Field field, int listIndex, String text, String pattern) {
|
||||
FMLCommonHandler.instance().bus().post(new StringPatternMismatch(field.getDeclaringClass(), field.getName(), listIndex, text, pattern));
|
||||
FMLCommonHandler.instance()
|
||||
.bus()
|
||||
.post(new StringPatternMismatch(field.getDeclaringClass(), field.getName(), listIndex, text,
|
||||
pattern));
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public void toast() {
|
||||
val ann = configClass.getAnnotation(Config.class);
|
||||
val toast = new SimpleToast(ToastBG.TOAST_DARK, null,
|
||||
FormattedText.parse(EnumChatFormatting.RED + "Config validation failed")
|
||||
.toChatText()
|
||||
.get(0),
|
||||
FormattedText.parse(ann.modid() + ":" + ann.category()).toChatText().get(0), false,
|
||||
2000);
|
||||
GuiToast.add(toast);
|
||||
}
|
||||
|
||||
protected abstract void customText(StringBuilder b);
|
||||
|
||||
public void logWarn() {
|
||||
val errorString = new StringBuilder("Error validating configuration field!");
|
||||
errorString.append("\nReason: ").append(reason);
|
||||
errorString.append("\nClass: ").append(configClass.getName()).append("\nField: ").append(fieldName);
|
||||
if (listElement) {
|
||||
errorString.append("\nArray index: ").append(listIndex);
|
||||
}
|
||||
customText(errorString);
|
||||
FalsePatternLib.getLog().error(errorString.toString());
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
@@ -91,9 +128,7 @@ public abstract class ConfigValidationFailureEvent extends Event {
|
||||
|
||||
@Override
|
||||
protected void customText(StringBuilder b) {
|
||||
b.append("\nValue: ").append(value)
|
||||
.append("\nMin: ").append(min)
|
||||
.append("\nMax: ").append(max);
|
||||
b.append("\nValue: ").append(value).append("\nMin: ").append(min).append("\nMax: ").append(max);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,6 +167,7 @@ public abstract class ConfigValidationFailureEvent extends Event {
|
||||
public final String text;
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final int maxSize;
|
||||
|
||||
private StringSizeOutOfBounds(Class<?> configClass, String fieldName, int listIndex, String text, int maxSize) {
|
||||
super("String size out of bounds", configClass, fieldName, listIndex >= 0, listIndex);
|
||||
this.text = text;
|
||||
@@ -140,9 +176,12 @@ public abstract class ConfigValidationFailureEvent extends Event {
|
||||
|
||||
@Override
|
||||
protected void customText(StringBuilder b) {
|
||||
b.append("\nText: ").append(text)
|
||||
.append("\nSize: ").append(text.length())
|
||||
.append("\nMax size:").append(maxSize);
|
||||
b.append("\nText: ")
|
||||
.append(text)
|
||||
.append("\nSize: ")
|
||||
.append(text.length())
|
||||
.append("\nMax size:")
|
||||
.append(maxSize);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,32 +212,7 @@ public abstract class ConfigValidationFailureEvent extends Event {
|
||||
|
||||
@Override
|
||||
protected void customText(StringBuilder b) {
|
||||
b.append("\nText: ").append(text)
|
||||
.append("\nPattern: ").append(pattern);
|
||||
b.append("\nText: ").append(text).append("\nPattern: ").append(pattern);
|
||||
}
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public void toast() {
|
||||
val ann = configClass.getAnnotation(Config.class);
|
||||
val toast = new SimpleToast(ToastBG.TOAST_DARK, null,
|
||||
FormattedText.parse(EnumChatFormatting.RED + "Config validation failed").toChatText().get(0),
|
||||
FormattedText.parse(ann.modid() + ":" + ann.category()).toChatText().get(0),
|
||||
false, 2000);
|
||||
GuiToast.add(toast);
|
||||
}
|
||||
|
||||
protected abstract void customText(StringBuilder b);
|
||||
|
||||
public void logWarn() {
|
||||
val errorString = new StringBuilder("Error validating configuration field!");
|
||||
errorString.append("\nReason: ").append(reason);
|
||||
errorString.append("\nClass: ").append(configClass.getName())
|
||||
.append("\nField: ").append(fieldName);
|
||||
if (listElement) {
|
||||
errorString.append("\nArray index: ").append(listIndex);
|
||||
}
|
||||
customText(errorString);
|
||||
FalsePatternLib.getLog().error(errorString.toString());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user