mirror of
https://github.com/FalsePattern/FalsePatternLib.git
synced 2026-11-16 22:15:11 +00:00
the grand config refactor
This commit is contained in:
32 files changed
+3038
-190
No files matched your search
@@ -20,23 +20,25 @@
|
||||
*/
|
||||
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;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
/**
|
||||
* A modern configuration system to replace the old and obtuse forge config system.
|
||||
*
|
||||
* Note that just annotating a configuration class with {@link Config} is not enough, you must also register it using
|
||||
* {@link com.falsepattern.lib.config.ConfigurationManager#registerConfig}!
|
||||
* {@link ConfigurationManager#initialize(Class[])} or {@link ConfigurationManager#initialize(BiConsumer, Class[])}!
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@StableAPI(since = "0.6.0")
|
||||
public @interface Config {
|
||||
/**
|
||||
* The mod id that this configuration is associated with.
|
||||
@@ -51,6 +53,7 @@ public @interface Config {
|
||||
/**
|
||||
* The lang file key of this configuration. Used in config GUIs.
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.TYPE})
|
||||
@@ -61,6 +64,7 @@ public @interface Config {
|
||||
/**
|
||||
* The description of the configuration.
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@@ -72,6 +76,7 @@ public @interface Config {
|
||||
* If you have extra fields in the config class used for anything else other than configuring, you must annotate
|
||||
* the using this so that the config engine doesn't pick them up.
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@@ -82,6 +87,7 @@ public @interface Config {
|
||||
* The default value for a boolean field. Not having a default is deprecated since 0.10, and will be strongly
|
||||
* enforced in 0.11+!
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@@ -92,6 +98,7 @@ public @interface Config {
|
||||
/**
|
||||
* The range of possible values an int config can have.
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@@ -105,6 +112,7 @@ public @interface Config {
|
||||
* The default value for an int field. Not having a default is deprecated since 0.10, and will be strongly
|
||||
* enforced in 0.11+!
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@@ -114,12 +122,16 @@ public @interface Config {
|
||||
|
||||
/**
|
||||
* The range of possible values a float config can have.
|
||||
* Notice: float configs are deprecated! Use double configs instead!
|
||||
*/
|
||||
@Deprecated
|
||||
@DeprecationDetails(stableSince = "0.6.0",
|
||||
deprecatedSince = "0.10.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface RangeFloat {
|
||||
float min() default Float.MIN_VALUE;
|
||||
float min() default -Float.MAX_VALUE;
|
||||
|
||||
float max() default Float.MAX_VALUE;
|
||||
}
|
||||
@@ -127,7 +139,11 @@ public @interface Config {
|
||||
/**
|
||||
* The default value for a float field. Not having a default is deprecated since 0.10, and will be strongly
|
||||
* enforced in 0.11+!
|
||||
* Notice: float configs are deprecated! Use double configs instead!
|
||||
*/
|
||||
@Deprecated
|
||||
@DeprecationDetails(stableSince = "0.6.0",
|
||||
deprecatedSince = "0.10.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@@ -135,10 +151,36 @@ public @interface Config {
|
||||
float value();
|
||||
}
|
||||
|
||||
/**
|
||||
* The range of possible values a double config can have.
|
||||
*/
|
||||
@StableAPI(since = "0.10.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface RangeDouble {
|
||||
double min() default -Double.MAX_VALUE;
|
||||
|
||||
double max() default Double.MAX_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* The default value for a double field. Not having a default is deprecated since 0.10, and will be strongly
|
||||
* enforced in 0.11+!
|
||||
*/
|
||||
@StableAPI(since = "0.10.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface DefaultDouble {
|
||||
double value();
|
||||
}
|
||||
|
||||
/**
|
||||
* The default value for a String field. Not having a default is deprecated since 0.10, and will be strongly
|
||||
* enforced in 0.11+!
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@@ -146,9 +188,27 @@ public @interface Config {
|
||||
String value();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This annotation limits the maximum number of characters present in a string configuration.
|
||||
*
|
||||
* 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!
|
||||
*
|
||||
* 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")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface StringMaxLength {
|
||||
int value();
|
||||
}
|
||||
|
||||
/**
|
||||
* A regex pattern for restricting the allowed strings.
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@@ -160,6 +220,7 @@ public @interface Config {
|
||||
* The default value for an Enum field. Not having a default is deprecated since 0.10, and will be strongly
|
||||
* enforced in 0.11+!
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@@ -171,6 +232,7 @@ public @interface Config {
|
||||
* The default value for a string array field. Not having a default is deprecated since 0.10, and will be strongly
|
||||
* enforced in 0.11+!
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@@ -178,9 +240,74 @@ public @interface Config {
|
||||
String[] value();
|
||||
}
|
||||
|
||||
/**
|
||||
* The default value for a double array field. Not having a default is deprecated since 0.10, and will be strongly
|
||||
* enforced in 0.11+!
|
||||
*/
|
||||
@StableAPI(since = "0.10.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface DefaultDoubleList {
|
||||
double[] value();
|
||||
}
|
||||
|
||||
/**
|
||||
* The default value for an int array field. Not having a default is deprecated since 0.10, and will be strongly
|
||||
* enforced in 0.11+!
|
||||
*/
|
||||
@StableAPI(since = "0.10.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface DefaultIntList {
|
||||
int[] value();
|
||||
}
|
||||
|
||||
/**
|
||||
* The default value for an 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")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface DefaultBooleanList {
|
||||
boolean[] value();
|
||||
}
|
||||
|
||||
/**
|
||||
* If this annotation is present, the list in the config will be forced to have exactly the amount of elements as
|
||||
* the default value.
|
||||
*/
|
||||
@StableAPI(since = "0.10.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface ListFixedLength {
|
||||
}
|
||||
|
||||
/**
|
||||
* This annotation limits the maximum number of elements present in an array configuration. Only effective if
|
||||
* {@link ListFixedLength} is NOT present.
|
||||
*
|
||||
* 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!
|
||||
*/
|
||||
@StableAPI(since = "0.10.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface ListMaxLength {
|
||||
int value();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The name of this config property in the config file. If not specified, the field's name will be used instead.
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@@ -191,6 +318,7 @@ public @interface Config {
|
||||
/**
|
||||
* Whether the specific configuration needs a minecraft restart to be applied.
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.TYPE})
|
||||
@@ -200,6 +328,7 @@ public @interface Config {
|
||||
/**
|
||||
* Whether the specific configuration needs a world/server rejoin to be applied.
|
||||
*/
|
||||
@StableAPI(since = "0.6.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.TYPE})
|
||||
@@ -207,7 +336,7 @@ public @interface Config {
|
||||
}
|
||||
|
||||
/**
|
||||
* Signals that this configuration entry/class should be synchronized between the client and the server when
|
||||
* Signals that this configuration class should be synchronized between the client and the server when
|
||||
* joining a multiplayer instance.
|
||||
*
|
||||
* Note that synchronization ALWAYS happens FROM the server TO the client. The server should NEVER attempt to get
|
||||
@@ -216,20 +345,17 @@ public @interface Config {
|
||||
@StableAPI(since = "0.10.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.TYPE})
|
||||
@Target(ElementType.TYPE)
|
||||
@interface Synchronize {
|
||||
/**
|
||||
* This is a limit on how many bytes the client will accept from the server. If this limit is exceeded,
|
||||
* the client shall refuse to connect to the server. For String[]-s, the amount of bytes is the sum of all
|
||||
* strings inside the array.<br><br>
|
||||
*
|
||||
* This limitation option exists to avoid malicious servers flooding clients with data.<br><br>
|
||||
*
|
||||
* By default, the client will accept any amount of bytes.<br><br>
|
||||
*
|
||||
* This only applies for String and String[] configurations, and is ignored on the other config types.
|
||||
* For enums, this value is computed automatically.
|
||||
*/
|
||||
int maxLength();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this to mark config fields you don't want to synchronize in a class marked with {@link Synchronize}.
|
||||
*/
|
||||
@StableAPI(since = "0.10.0")
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface NoSync {
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,9 @@
|
||||
*/
|
||||
package com.falsepattern.lib.config;
|
||||
|
||||
import com.falsepattern.lib.DeprecationDetails;
|
||||
import com.falsepattern.lib.StableAPI;
|
||||
import com.falsepattern.lib.internal.FalsePatternLib;
|
||||
import com.falsepattern.lib.internal.impl.config.ConfigurationManagerImpl;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -28,9 +30,11 @@ import lombok.val;
|
||||
|
||||
import cpw.mods.fml.client.config.IConfigElement;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
/**
|
||||
* Class for controlling the loading of configuration files.
|
||||
@@ -39,13 +43,64 @@ import java.util.List;
|
||||
@StableAPI(since = "0.6.0")
|
||||
public class ConfigurationManager {
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static void initialize(Class<?>... configClasses) throws ConfigException {
|
||||
initialize((a, b) -> {}, configClasses);
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
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 {
|
||||
return validate((x, y) -> {}, resetInvalid, configClasses);
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static boolean validate(BiConsumer<Class<?>, Field> validatorErrorCallback, boolean resetInvalid, Class<?>... configClasses)
|
||||
throws ConfigException {
|
||||
boolean valid = true;
|
||||
for (val clazz: configClasses) {
|
||||
valid &= ConfigurationManagerImpl.validateFields(validatorErrorCallback, clazz, resetInvalid);
|
||||
}
|
||||
return valid;
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static void loadFromFile(Class<?>... configClasses) throws ConfigException {
|
||||
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) {
|
||||
if (validateAndResetInvalid) {
|
||||
ConfigurationManagerImpl.validateFields((a, b) -> {}, clazz, true);
|
||||
}
|
||||
ConfigurationManagerImpl.save(clazz);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a configuration class to be loaded. This should be done in preInit.
|
||||
* Registers a configuration class to be loaded. This should be done early on.
|
||||
*
|
||||
* @param configClass The class to register.
|
||||
*/
|
||||
@Deprecated
|
||||
@DeprecationDetails(stableSince = "0.6.0",
|
||||
deprecatedSince = "0.10.0")
|
||||
public static void registerConfig(Class<?> configClass) throws ConfigException {
|
||||
ConfigurationManagerImpl.registerConfig(configClass);
|
||||
FalsePatternLib.getLog().warn("A mod is using the deprecated config registration method! The following exception contains the stacktrace.", new Exception());
|
||||
ConfigurationManagerImpl.registerLoadSaveConfig(configClass);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,6 +111,7 @@ public class ConfigurationManager {
|
||||
* @return The configuration elements.
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
@StableAPI(since = "0.6.0")
|
||||
public static List<IConfigElement> getConfigElements(Class<?> configClass) throws ConfigException {
|
||||
return ConfigurationManagerImpl.getConfigElements(configClass);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
*
|
||||
* The above copyright notice, this permission notice and the word "SNEED"
|
||||
* shall be included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.falsepattern.lib.config.event;
|
||||
|
||||
import com.falsepattern.lib.StableAPI;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.val;
|
||||
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
/**
|
||||
* This event is pushed on the FMLCommonHandler event bus once all the config synchronizations have finished.
|
||||
*
|
||||
* This is a client-only event, and is never triggered serverside!
|
||||
*/
|
||||
@SideOnly(Side.CLIENT)
|
||||
@StableAPI(since = "0.10.0")
|
||||
@NoArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class AllConfigSyncEvent extends Event {
|
||||
|
||||
public static boolean postStart() {
|
||||
val event = new Start();
|
||||
FMLCommonHandler.instance().bus().post(event);
|
||||
return !event.isCanceled();
|
||||
}
|
||||
|
||||
public static void postEnd() {
|
||||
FMLCommonHandler.instance().bus().post(new End());
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@StableAPI(since = "0.10.0")
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public static class Start extends AllConfigSyncEvent {
|
||||
@Override
|
||||
public boolean isCancelable() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@StableAPI(since = "0.10.0")
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public static class End extends AllConfigSyncEvent {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
*
|
||||
* The above copyright notice, this permission notice and the word "SNEED"
|
||||
* shall be included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.falsepattern.lib.config.event;
|
||||
|
||||
import com.falsepattern.lib.StableAPI;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.val;
|
||||
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
/**
|
||||
* This event is pushed on the FMLCommonHandler event bus once each time a single config synchronization has finished.
|
||||
*
|
||||
* This is a client-only event, and is never triggered serverside!
|
||||
*/
|
||||
@SideOnly(Side.CLIENT)
|
||||
@StableAPI(since = "0.10.0")
|
||||
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class ConfigSyncEvent extends Event {
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final Class<?> configClass;
|
||||
|
||||
public static boolean postStart(Class<?> configClass) {
|
||||
val event = new Start(configClass);
|
||||
FMLCommonHandler.instance().bus().post(event);
|
||||
return !event.isCanceled();
|
||||
}
|
||||
|
||||
public static void postEndSuccess(Class<?> configClass) {
|
||||
FMLCommonHandler.instance().bus().post(new End(configClass, true, null));
|
||||
}
|
||||
|
||||
public static void postEndFailure(Class<?> configClass, Throwable error) {
|
||||
FMLCommonHandler.instance().bus().post(new End(configClass, false, error));
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static class Start extends ConfigSyncEvent {
|
||||
private Start(Class<?> configClass) {
|
||||
super(configClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelable() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static class End extends ConfigSyncEvent {
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final boolean successful;
|
||||
/**
|
||||
* null if successful == true.
|
||||
*/
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final Throwable error;
|
||||
|
||||
private End(Class<?> configClass, boolean successful, Throwable error) {
|
||||
super(configClass);
|
||||
this.successful = successful;
|
||||
this.error = error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
*
|
||||
* The above copyright notice, this permission notice and the word "SNEED"
|
||||
* shall be included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.falsepattern.lib.config.event;
|
||||
|
||||
import com.falsepattern.lib.StableAPI;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
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 class Client extends ConfigSyncRequestEvent {
|
||||
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static class Server extends ConfigSyncRequestEvent {
|
||||
private final List<EntityPlayerMP> players;
|
||||
|
||||
public List<EntityPlayerMP> getPlayers() {
|
||||
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)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
/**
|
||||
* Copyright (C) 2022 FalsePattern
|
||||
* All Rights Reserved
|
||||
* <p>
|
||||
* The above copyright notice, this permission notice and the word "SNEED"
|
||||
* shall be included in all copies or substantial portions of the Software.
|
||||
* <p>
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
* <p>
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
* <p>
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.falsepattern.lib.config.event;
|
||||
|
||||
import com.falsepattern.lib.StableAPI;
|
||||
import com.falsepattern.lib.config.Config;
|
||||
import com.falsepattern.lib.internal.FalsePatternLib;
|
||||
import com.falsepattern.lib.text.FormattedText;
|
||||
import com.falsepattern.lib.toasts.GuiToast;
|
||||
import com.falsepattern.lib.toasts.SimpleToast;
|
||||
import com.falsepattern.lib.toasts.icon.ToastBG;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.val;
|
||||
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public abstract class ConfigValidationFailureEvent extends Event {
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final String reason;
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final Class<?> configClass;
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final String fieldName;
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final boolean listElement;
|
||||
@StableAPI(since = "0.10.0")
|
||||
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));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
public static void fieldIsNull(Field field, int listIndex) {
|
||||
FMLCommonHandler.instance().bus().post(new FieldIsNull(field.getDeclaringClass(), field.getName(), listIndex));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static class NumericRangeOutOfBounds extends ConfigValidationFailureEvent {
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final String value;
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final String min;
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final String max;
|
||||
|
||||
public NumericRangeOutOfBounds(Class<?> configClass, String fieldName, int listIndex, String value, String min, String max) {
|
||||
super("Number range out of bounds", configClass, fieldName, listIndex >= 0, listIndex);
|
||||
this.value = value;
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customText(StringBuilder b) {
|
||||
b.append("\nValue: ").append(value)
|
||||
.append("\nMin: ").append(min)
|
||||
.append("\nMax: ").append(max);
|
||||
}
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static class ListSizeOutOfBounds extends ConfigValidationFailureEvent {
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final int size;
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final boolean fixedSize;
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final int maxSize;
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final int defaultSize;
|
||||
|
||||
private ListSizeOutOfBounds(Class<?> configClass, String fieldName, int size, boolean fixedSize, int maxSize, int defaultSize) {
|
||||
super("Array size out of bounds", configClass, fieldName, false, -1);
|
||||
this.size = size;
|
||||
this.fixedSize = fixedSize;
|
||||
this.maxSize = maxSize;
|
||||
this.defaultSize = defaultSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customText(StringBuilder b) {
|
||||
b.append("\nSize: ").append(size);
|
||||
if (fixedSize) {
|
||||
b.append("\nRequired size: ").append(defaultSize);
|
||||
}
|
||||
b.append("\nMaximum size: ").append(maxSize);
|
||||
}
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static class StringSizeOutOfBounds extends ConfigValidationFailureEvent {
|
||||
@StableAPI(since = "0.10.0")
|
||||
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;
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customText(StringBuilder b) {
|
||||
b.append("\nText: ").append(text)
|
||||
.append("\nSize: ").append(text.length())
|
||||
.append("\nMax size:").append(maxSize);
|
||||
}
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static class FieldIsNull extends ConfigValidationFailureEvent {
|
||||
private FieldIsNull(Class<?> configClass, String fieldName, int listIndex) {
|
||||
super("Unexpected null", configClass, fieldName, listIndex >= 0, listIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customText(StringBuilder b) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@StableAPI(since = "0.10.0")
|
||||
public static class StringPatternMismatch extends ConfigValidationFailureEvent {
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final String text;
|
||||
@StableAPI(since = "0.10.0")
|
||||
public final String pattern;
|
||||
|
||||
private StringPatternMismatch(Class<?> configClass, String fieldName, int listIndex, String text, String pattern) {
|
||||
super("String pattern mismatch", configClass, fieldName, listIndex >= 0, listIndex);
|
||||
this.text = text;
|
||||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customText(StringBuilder b) {
|
||||
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