diff --git a/common-files/src/main/resources/resources/default/configuration/templates/block_states.yml b/common-files/src/main/resources/resources/default/configuration/templates/block_states.yml index ed493907e..50b0f8de5 100644 --- a/common-files/src/main/resources/resources/default/configuration/templates/block_states.yml +++ b/common-files/src/main/resources/resources/default/configuration/templates/block_states.yml @@ -85,7 +85,11 @@ templates: template: default:block_state/__leaves__ arguments: auto_state: leaves - leaves_base_model: leaves + leaves_base_model: + type: condition + condition: ${tintable:-false} + on-true: leaves + on-false: cube_all # tintable leaves block default:block_state/tintable_leaves: template: default:block_state/__leaves__ diff --git a/common-files/src/main/resources/translations/en.yml b/common-files/src/main/resources/translations/en.yml index 8b92bd343..b3ce311aa 100644 --- a/common-files/src/main/resources/translations/en.yml +++ b/common-files/src/main/resources/translations/en.yml @@ -172,6 +172,10 @@ warning.config.translation.unknown_locale: "Issue found in file warning.config.template.duplicate: "Issue found in file - Duplicated template ''. Please check if there is the same configuration in other files." warning.config.template.invalid: "Issue found in file - The config '' is using an invalid template ''." warning.config.template.argument.self_increase_int.invalid_range: "Issue found in file - The template '' is using a 'from' '' larger than 'to' '' in 'self_increase_int' argument." +warning.config.template.argument.to_upper_case.invalid_locale: "Issue found in file - The template '' is using an invalid locale '' in 'to_upper_case' argument." +warning.config.template.argument.to_upper_case.missing_value: "Issue found in file - The template '' is missing the required 'value' argument for 'to_upper_case' argument." +warning.config.template.argument.to_lower_case.invalid_locale: "Issue found in file - The template '' is using an invalid locale '' in 'to_lower_case' argument." +warning.config.template.argument.to_lower_case.missing_value: "Issue found in file - The template '' is missing the required 'value' argument for 'to_upper_case' argument." warning.config.template.argument.list.invalid_type: "Issue found in file - The template '' is using a 'list' argument which expects a 'List' as argument while the input argument is a(n) ''." warning.config.template.argument.missing_value: "Issue found in file - The config '' is missing the template argument for ''. Please use the arguments option to configure or set a default value for this parameter." warning.config.vanilla_loot.missing_type: "Issue found in file - The vanilla loot '' is missing the required 'type' argument." diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/ConditionTemplateArgument.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/ConditionTemplateArgument.java new file mode 100644 index 000000000..ebd5df786 --- /dev/null +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/ConditionTemplateArgument.java @@ -0,0 +1,35 @@ +package net.momirealms.craftengine.core.plugin.config.template; + +import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; + +import java.util.Map; + +public class ConditionTemplateArgument implements TemplateArgument { + public static final Factory FACTORY = new Factory(); + private final TemplateArgument result; + + private ConditionTemplateArgument(TemplateArgument result) { + this.result = result; + } + + @Override + public Key type() { + return TemplateArguments.CONDITION; + } + + @Override + public Object get(Map arguments) { + return this.result.get(arguments); + } + + public static class Factory implements TemplateArgumentFactory { + + @Override + public TemplateArgument create(Map arguments) { + TemplateArgument onTrue = TemplateArguments.fromObject(ResourceConfigUtils.get(arguments, "on-true", "on_true")); + TemplateArgument onFalse = TemplateArguments.fromObject(ResourceConfigUtils.get(arguments, "on-false", "on_false")); + return new ConditionTemplateArgument(ResourceConfigUtils.getAsBoolean(arguments.get("condition"), "condition") ? onTrue : onFalse); + } + } +} diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/SelfIncreaseIntTemplateArgument.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/SelfIncreaseIntTemplateArgument.java index d01b87124..d607f4358 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/SelfIncreaseIntTemplateArgument.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/SelfIncreaseIntTemplateArgument.java @@ -11,17 +11,30 @@ public class SelfIncreaseIntTemplateArgument implements TemplateArgument { private final int min; private final int max; private int current; + private final int step; + private final int stepInterval; + private int callCount; - public SelfIncreaseIntTemplateArgument(int min, int max) { + public SelfIncreaseIntTemplateArgument(int min, int max, int step, int stepInterval) { this.min = min; this.max = max; this.current = min; + this.step = step; + this.stepInterval = stepInterval; + this.callCount = 0; } @Override public String get(Map arguments) { String value = String.valueOf(this.current); - if (this.current < this.max) this.current += 1; + this.callCount++; + if (this.stepInterval <= 0 || this.callCount % this.stepInterval == 0) { + if (this.current + this.step <= this.max) { + this.current += this.step; + } else { + this.current = this.max; + } + } return value; } @@ -42,13 +55,27 @@ public class SelfIncreaseIntTemplateArgument implements TemplateArgument { return this.max; } + public int step() { + return this.step; + } + + public int stepInterval() { + return this.stepInterval; + } + + public int callCount() { + return this.callCount; + } + public static class Factory implements TemplateArgumentFactory { @Override public TemplateArgument create(Map arguments) { int from = ResourceConfigUtils.getAsInt(arguments.get("from"), "from"); int to = ResourceConfigUtils.getAsInt(arguments.get("to"), "to"); + int step = ResourceConfigUtils.getAsInt(arguments.getOrDefault("step", 1), "step"); + int stepInterval = ResourceConfigUtils.getAsInt(arguments.getOrDefault("step-interval", 1), "step-interval"); if (from > to) throw new LocalizedResourceConfigException("warning.config.template.argument.self_increase_int.invalid_range", String.valueOf(from), String.valueOf(to)); - return new SelfIncreaseIntTemplateArgument(from, to); + return new SelfIncreaseIntTemplateArgument(from, to, step, stepInterval); } } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/TemplateArguments.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/TemplateArguments.java index e45c45cb6..b49caec1c 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/TemplateArguments.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/TemplateArguments.java @@ -6,6 +6,7 @@ import net.momirealms.craftengine.core.registry.WritableRegistry; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.ResourceKey; +import java.util.List; import java.util.Map; public class TemplateArguments { @@ -14,8 +15,11 @@ public class TemplateArguments { public static final Key MAP = Key.of("craftengine:map"); public static final Key LIST = Key.of("craftengine:list"); public static final Key NULL = Key.of("craftengine:null"); + public static final Key CONDITION = Key.of("craftengine:condition"); public static final Key EXPRESSION = Key.of("craftengine:expression"); public static final Key OBJECT = Key.of("craftengine:object"); // No Factory, internal use + public static final Key TO_UPPER_CASE = Key.of("craftengine:to_upper_case"); + public static final Key TO_LOWER_CASE = Key.of("craftengine:to_lower_case"); public static void register(Key key, TemplateArgumentFactory factory) { ((WritableRegistry) BuiltInRegistries.TEMPLATE_ARGUMENT_FACTORY) @@ -29,6 +33,19 @@ public class TemplateArguments { register(LIST, ListTemplateArgument.FACTORY); register(NULL, NullTemplateArgument.FACTORY); register(EXPRESSION, ExpressionTemplateArgument.FACTORY); + register(CONDITION, ConditionTemplateArgument.FACTORY); + register(TO_UPPER_CASE, ToUpperCaseTemplateArgument.FACTORY); + register(TO_LOWER_CASE, ToLowerCaseTemplateArgument.FACTORY); + } + + @SuppressWarnings("unchecked") + public static TemplateArgument fromObject(Object object) { + return switch (object) { + case null -> NullTemplateArgument.INSTANCE; + case List list -> new ListTemplateArgument((List) list); + case Map map -> fromMap((Map) map); + default -> new ObjectTemplateArgument(object); + }; } public static TemplateArgument fromMap(Map map) { diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/ToLowerCaseTemplateArgument.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/ToLowerCaseTemplateArgument.java new file mode 100644 index 000000000..e00dc0f97 --- /dev/null +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/ToLowerCaseTemplateArgument.java @@ -0,0 +1,42 @@ +package net.momirealms.craftengine.core.plugin.config.template; + +import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException; +import net.momirealms.craftengine.core.plugin.locale.TranslationManager; +import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; + +import java.util.Locale; +import java.util.Map; + +public class ToLowerCaseTemplateArgument implements TemplateArgument { + public static final Factory FACTORY = new Factory(); + private final String result; + + private ToLowerCaseTemplateArgument(String result) { + this.result = result; + } + + @Override + public Key type() { + return TemplateArguments.TO_LOWER_CASE; + } + + @Override + public Object get(Map arguments) { + return this.result; + } + + public static class Factory implements TemplateArgumentFactory { + + @Override + public TemplateArgument create(Map arguments) { + String text = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("value"), "warning.config.template.argument.to_lower_case.missing_value"); + String localeName = arguments.containsKey("locale") ? arguments.get("locale").toString() : null; + Locale locale = localeName != null ? TranslationManager.parseLocale(localeName) : Locale.ROOT; + if (locale == null) { + throw new LocalizedResourceConfigException("warning.config.template.argument.to_lower_case.invalid_locale", localeName); + } + return new ToLowerCaseTemplateArgument(text.toLowerCase(locale)); + } + } +} diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/ToUpperCaseTemplateArgument.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/ToUpperCaseTemplateArgument.java new file mode 100644 index 000000000..33f67f6f3 --- /dev/null +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/config/template/ToUpperCaseTemplateArgument.java @@ -0,0 +1,42 @@ +package net.momirealms.craftengine.core.plugin.config.template; + +import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException; +import net.momirealms.craftengine.core.plugin.locale.TranslationManager; +import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; + +import java.util.Locale; +import java.util.Map; + +public class ToUpperCaseTemplateArgument implements TemplateArgument { + public static final Factory FACTORY = new Factory(); + private final String result; + + private ToUpperCaseTemplateArgument(String result) { + this.result = result; + } + + @Override + public Key type() { + return TemplateArguments.TO_UPPER_CASE; + } + + @Override + public Object get(Map arguments) { + return this.result; + } + + public static class Factory implements TemplateArgumentFactory { + + @Override + public TemplateArgument create(Map arguments) { + String text = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("value"), "warning.config.template.argument.to_upper_case.missing_value"); + String localeName = arguments.containsKey("locale") ? arguments.get("locale").toString() : null; + Locale locale = localeName != null ? TranslationManager.parseLocale(localeName) : Locale.ROOT; + if (locale == null) { + throw new LocalizedResourceConfigException("warning.config.template.argument.to_upper_case.invalid_locale", localeName); + } + return new ToUpperCaseTemplateArgument(text.toUpperCase(locale)); + } + } +}