Files
FalsePatternLib/src/main/java/com/falsepattern/lib/config/Config.java
T

361 lines
11 KiB
Java
Raw Normal View History

2022-07-31 20:41:56 +02:00
/*
2022-07-30 15:21:11 +02:00
* 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/>.
*/
2022-05-06 18:57:02 +02:00
package com.falsepattern.lib.config;
2022-07-31 20:28:37 +02:00
import com.falsepattern.lib.DeprecationDetails;
2022-05-06 23:53:11 +02:00
import com.falsepattern.lib.StableAPI;
2022-07-31 20:41:56 +02:00
2022-06-08 16:01:06 +02:00
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;
2022-07-31 20:28:37 +02:00
import java.util.function.BiConsumer;
2022-05-06 23:53:11 +02:00
2022-07-25 00:31:10 +02:00
/**
* A modern configuration system to replace the old and obtuse forge config system.
2022-07-31 20:41:56 +02:00
* <p>
2022-07-25 00:31:10 +02:00
* Note that just annotating a configuration class with {@link Config} is not enough, you must also register it using
2022-07-31 20:28:37 +02:00
* {@link ConfigurationManager#initialize(Class[])} or {@link ConfigurationManager#initialize(BiConsumer, Class[])}!
2022-07-25 00:31:10 +02:00
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-05-06 23:53:11 +02:00
@Documented
2022-05-06 18:57:02 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Config {
/**
* The mod id that this configuration is associated with.
*/
String modid();
/**
2022-05-07 16:43:34 +02:00
* Root element category, defaults to "general". You must not specify an empty string.
2022-05-06 18:57:02 +02:00
*/
String category() default "general";
2022-07-25 00:31:10 +02:00
/**
* The lang file key of this configuration. Used in config GUIs.
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-06 18:57:02 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
@interface LangKey {
String value();
}
2022-07-25 00:31:10 +02:00
/**
* The description of the configuration.
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-06 18:57:02 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Comment {
String[] value();
}
2022-07-25 00:31:10 +02:00
/**
* 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.
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-06 18:57:02 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
2022-06-08 16:01:06 +02:00
@interface Ignore {
}
2022-05-06 18:57:02 +02:00
2022-07-25 00:31:10 +02:00
/**
* The default value for a boolean field. Not having a default is deprecated since 0.10, and will be strongly
* enforced in 0.11+!
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-07 01:33:04 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultBoolean {
boolean value();
}
2022-07-25 00:31:10 +02:00
/**
* The range of possible values an int config can have.
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-06 18:57:02 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface RangeInt {
int min() default Integer.MIN_VALUE;
int max() default Integer.MAX_VALUE;
}
2022-07-25 00:31:10 +02:00
/**
* The default value for an int field. Not having a default is deprecated since 0.10, and will be strongly
* enforced in 0.11+!
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-06 23:48:24 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultInt {
2022-05-07 01:33:04 +02:00
int value();
}
2022-07-25 00:31:10 +02:00
/**
* The range of possible values a float config can have.
2022-07-31 20:28:37 +02:00
* Notice: float configs are deprecated! Use double configs instead!
2022-07-25 00:31:10 +02:00
*/
2022-07-31 20:28:37 +02:00
@Deprecated
@DeprecationDetails(stableSince = "0.6.0",
deprecatedSince = "0.10.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-07 01:33:04 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface RangeFloat {
2022-07-31 20:28:37 +02:00
float min() default -Float.MAX_VALUE;
2022-05-07 01:33:04 +02:00
float max() default Float.MAX_VALUE;
2022-05-06 23:48:24 +02:00
}
2022-07-25 00:31:10 +02:00
/**
* The default value for a float field. Not having a default is deprecated since 0.10, and will be strongly
* enforced in 0.11+!
2022-07-31 20:28:37 +02:00
* Notice: float configs are deprecated! Use double configs instead!
2022-07-25 00:31:10 +02:00
*/
2022-07-31 20:28:37 +02:00
@Deprecated
@DeprecationDetails(stableSince = "0.6.0",
deprecatedSince = "0.10.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-06 18:57:02 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
2022-05-07 01:33:04 +02:00
@interface DefaultFloat {
float value();
}
2022-05-06 18:57:02 +02:00
2022-07-31 20:28:37 +02:00
/**
* 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();
}
2022-07-25 00:31:10 +02:00
/**
* The default value for a String field. Not having a default is deprecated since 0.10, and will be strongly
* enforced in 0.11+!
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-07 01:33:04 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultString {
String value();
2022-05-06 18:57:02 +02:00
}
2022-07-31 20:28:37 +02:00
/**
* This annotation limits the maximum number of characters present in a string configuration.
2022-07-31 20:41:56 +02:00
* <p>
2022-07-31 20:28:37 +02:00
* 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!
2022-07-31 20:41:56 +02:00
* <p>
2022-07-31 20:28:37 +02:00
* 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();
}
2022-07-25 00:31:10 +02:00
/**
* A regex pattern for restricting the allowed strings.
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-06 23:48:24 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
2022-05-07 01:33:04 +02:00
@interface Pattern {
String value();
2022-05-06 23:48:24 +02:00
}
2022-07-25 00:31:10 +02:00
/**
* The default value for an Enum field. Not having a default is deprecated since 0.10, and will be strongly
* enforced in 0.11+!
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-07 11:45:57 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultEnum {
String value();
}
2022-07-25 00:31:10 +02:00
/**
* 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+!
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-06-07 21:10:21 -04:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface DefaultStringList {
String[] value();
}
2022-07-31 20:28:37 +02:00
/**
* 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();
}
/**
2022-07-31 20:41:56 +02:00
* The default value for a boolean array field. Not having a default is deprecated since 0.10, and will be strongly
2022-07-31 20:28:37 +02:00
* 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.
2022-07-31 20:41:56 +02:00
* <p>
2022-07-31 20:28:37 +02:00
* 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();
}
2022-07-25 00:31:10 +02:00
/**
* The name of this config property in the config file. If not specified, the field's name will be used instead.
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-06 18:57:02 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Name {
String value();
}
2022-07-25 00:31:10 +02:00
/**
* Whether the specific configuration needs a minecraft restart to be applied.
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-06 18:57:02 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
2022-06-08 16:01:06 +02:00
@interface RequiresMcRestart {
}
2022-05-06 18:57:02 +02:00
2022-07-25 00:31:10 +02:00
/**
* Whether the specific configuration needs a world/server rejoin to be applied.
*/
2022-07-31 20:28:37 +02:00
@StableAPI(since = "0.6.0")
2022-07-25 00:31:10 +02:00
@Documented
2022-05-06 18:57:02 +02:00
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
2022-06-08 16:01:06 +02:00
@interface RequiresWorldRestart {
}
2022-07-25 00:32:10 +02:00
/**
2022-07-31 20:28:37 +02:00
* Signals that this configuration class should be synchronized between the client and the server when
2022-07-25 00:32:10 +02:00
* joining a multiplayer instance.
2022-07-31 20:41:56 +02:00
* <p>
2022-07-25 00:32:10 +02:00
* 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.
*/
@StableAPI(since = "0.10.0")
@Documented
@Retention(RetentionPolicy.RUNTIME)
2022-07-31 20:28:37 +02:00
@Target(ElementType.TYPE)
2022-07-25 00:32:10 +02:00
@interface Synchronize {
2022-07-31 20:28:37 +02:00
}
/**
* 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 {
2022-07-25 00:32:10 +02:00
}
2022-05-06 18:57:02 +02:00
}