Files
fabric-carpet/src/main/java/carpet/api/settings/Rule.java
T
altrisi 0a972d6c56 Settings rework and encapsulation (#1330)
* Settings rework and encapsulation

* Settings overhaul 3

Move everything in the new system to a new API package

Require rules using the new system to specify descriptions in their translation files

Implement temporary system on top of current translations to allow that to work properly

* Fix getRules fallback

* Final touches to settings refactor

* Mention that set shouldn't throw and remove comment

* Change translation keys to unify with translation PR

* Deprecate printAllRulesToLog to allow using any PrintStream

* Remove restriction of CarpetRules being Comparable

And fix overreaction in a deprecation message

* Bring back user input to rule observers

* Make Validator deprecation warning not spam on use

* Remove unneeded abstract methods in CarpetRule already inherited from CarpetRule

They literally do nothing, don't even force them to be implemented, and may be confusing as to why they're there or as if they are needed to be implemented

* Fix legacy SettingsManager field not being assigned correctly

* Finish fixing conflicts from future merge

* Actually fix remaining conflicts from future merge (missing imports)

* Import the newer rule in SettingsManager

* Refactor Validators

* Don't spam deprecation warnings on custom managers

* Get out the var

* Fix missing warning in old rule observers

* Update src/main/java/carpet/api/settings/Rule.java

* Remove `@ApiStatus.Experimental` annotations

* Fix incorrect `@deprecated` javadoc tags

By removing them given the class is deprecated anyway and else there's too much noise

* Add missing static import

* Remove TypedField and use a regular field

I liked that class tbh, but it's true that it wasn't really needed

* Move `field` assignment out of for removal

* Fix not following contract of `type()` returning boxed value

Previously handled by TypedField

* Fix few incorrect translation keys

* Remove resaving of comments when saving config files

Comments ignoring is still a thing when reading, so `locked` configs for example will keep comments

* Condense rule load messages in one

* Only print loaded settings message when at least one setting was changed

* Fix legacy validator detector

* Tweak and add more context to response to invalid values

* Fix strict validator not actually erroring on invalid input
2022-06-27 13:12:15 +02:00

83 lines
2.7 KiB
Java

package carpet.api.settings;
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;
/**
* Any field in this class annotated with this class is interpreted as a carpet rule.
* The field must be static and have a type of one of:
* - boolean
* - int
* - double
* - String
* - long
* - float
* - a subclass of Enum
* The default value of the rule will be the initial value of the field.
*/
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Rule
{
/**
* <p>An array of categories the rule is in, as {@link String strings}.</p>
*
* <p>Those must have a corresponding translation key. Categories provided in Carpet's {@link RuleCategory} already
* have translation key.</p>
*/
String[] categories();
/**
* Options to select in menu.
* Inferred for booleans and enums. Otherwise, must be present.
*/
String[] options() default {};
/**
* if a rule is not strict - can take any value, otherwise it needs to match
* any of the options
* For enums, its always strict, same for booleans - no need to set that for them.
*/
boolean strict() default true;
/**
* If specified, the rule will automatically enable or disable
* a builtin Scarpet Rule App with this name.
*/
String appSource() default "";
/**
* The class of the validator checked right before the rule is changed, using the returned value as the new value to set, or cancel the change if null is returned.
*/
@SuppressWarnings("rawtypes")
Class<? extends Validator>[] validators() default {};
/**
* <p>A class or list of classes implementing {@link Condition} that will have their {@link Condition#shouldRegister()} method
* executed while the rule is being parsed in {@link SettingsManager#parseSettingsClass(Class)}, that will cause the rule to be skipped
* if it evaluates to false</p>
*
* @see Condition
*/
Class<? extends Condition>[] conditions() default {};
/**
* <p>Represents a condition that must be met for a rule to be registered in a {@link SettingsManager} via
* {@link SettingsManager#parseSettingsClass(Class)}</p>
*
* @see Rule#conditions()
* @see #shouldRegister()
*/
interface Condition {
/**
* <p>Returns whether the rule that has had this {@link Condition} added should register.</p>
* @return {@code true} to register the rule, {@code false} otherwise
*/
boolean shouldRegister();
}
}