9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-19 15:09:15 +00:00

新增几种模板参数类型

This commit is contained in:
XiaoMoMi
2025-11-25 23:14:45 +08:00
parent 88b0716c0e
commit 60f79e5cae
7 changed files with 175 additions and 4 deletions

View File

@@ -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__

View File

@@ -172,6 +172,10 @@ warning.config.translation.unknown_locale: "<yellow>Issue found in file <arg:0>
warning.config.template.duplicate: "<yellow>Issue found in file <arg:0> - Duplicated template '<arg:1>'. Please check if there is the same configuration in other files.</yellow>"
warning.config.template.invalid: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is using an invalid template '<arg:2>'.</yellow>"
warning.config.template.argument.self_increase_int.invalid_range: "<yellow>Issue found in file <arg:0> - The template '<arg:1>' is using a 'from' '<arg:2>' larger than 'to' '<arg:3>' in 'self_increase_int' argument.</yellow>"
warning.config.template.argument.to_upper_case.invalid_locale: "<yellow>Issue found in file <arg:0> - The template '<arg:1>' is using an invalid locale '<arg:2>' in 'to_upper_case' argument.</yellow>"
warning.config.template.argument.to_upper_case.missing_value: "<yellow>Issue found in file <arg:0> - The template '<arg:1>' is missing the required 'value' argument for 'to_upper_case' argument.</yellow>"
warning.config.template.argument.to_lower_case.invalid_locale: "<yellow>Issue found in file <arg:0> - The template '<arg:1>' is using an invalid locale '<arg:2>' in 'to_lower_case' argument.</yellow>"
warning.config.template.argument.to_lower_case.missing_value: "<yellow>Issue found in file <arg:0> - The template '<arg:1>' is missing the required 'value' argument for 'to_upper_case' argument.</yellow>"
warning.config.template.argument.list.invalid_type: "<yellow>Issue found in file <arg:0> - The template '<arg:1>' is using a 'list' argument which expects a 'List' as argument while the input argument is a(n) '<arg:2>'.</yellow>"
warning.config.template.argument.missing_value: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the template argument for '<arg:2>'. Please use the arguments option to configure or set a default value for this parameter.</yellow>"
warning.config.vanilla_loot.missing_type: "<yellow>Issue found in file <arg:0> - The vanilla loot '<arg:1>' is missing the required 'type' argument.</yellow>"

View File

@@ -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<String, TemplateArgument> arguments) {
return this.result.get(arguments);
}
public static class Factory implements TemplateArgumentFactory {
@Override
public TemplateArgument create(Map<String, Object> 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);
}
}
}

View File

@@ -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<String, TemplateArgument> 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<String, Object> 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);
}
}
}

View File

@@ -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<TemplateArgumentFactory>) 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<Object>) list);
case Map<?, ?> map -> fromMap((Map<String, Object>) map);
default -> new ObjectTemplateArgument(object);
};
}
public static TemplateArgument fromMap(Map<String, Object> map) {

View File

@@ -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<String, TemplateArgument> arguments) {
return this.result;
}
public static class Factory implements TemplateArgumentFactory {
@Override
public TemplateArgument create(Map<String, Object> 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));
}
}
}

View File

@@ -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<String, TemplateArgument> arguments) {
return this.result;
}
public static class Factory implements TemplateArgumentFactory {
@Override
public TemplateArgument create(Map<String, Object> 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));
}
}
}