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

Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
jhqwqmc
2025-06-05 22:37:30 +08:00
5 changed files with 131 additions and 58 deletions

View File

@@ -269,21 +269,25 @@ public abstract class AbstractFontManager implements FontManager {
}
private void buildImageTagTrie() {
this.tagMapper = new HashMap<>();
this.tagMapper = new HashMap<>(1024);
for (BitmapImage image : this.images.values()) {
String id = image.id().toString();
this.tagMapper.put(imageTag(id), AdventureHelper.miniMessage().deserialize(image.miniMessage(0, 0)));
this.tagMapper.put("\\" + imageTag(id), Component.text(imageTag(id)));
String simpleImageTag = imageTag(id);
this.tagMapper.put(simpleImageTag, image.componentAt(0, 0));
this.tagMapper.put("\\" + simpleImageTag, Component.text(simpleImageTag));
for (int i = 0; i < image.rows(); i++) {
for (int j = 0; j < image.columns(); j++) {
this.tagMapper.put(imageTag(id + ":" + i + ":" + j), AdventureHelper.miniMessage().deserialize(image.miniMessage(i, j)));
this.tagMapper.put(imageTag("\\" + id + ":" + i + ":" + j), Component.text(imageTag(id + ":" + i + ":" + j)));
String imageArgs = id + ":" + i + ":" + j;
String imageTag = imageTag(imageArgs);
this.tagMapper.put(imageTag, image.componentAt(i, j));
this.tagMapper.put("\\" + imageTag, Component.text(imageTag));
}
}
}
for (int i = -256; i <= 256; i++) {
this.tagMapper.put("<shift:" + i + ">", AdventureHelper.miniMessage().deserialize(this.offsetFont.createOffset(i, FormatUtils::miniMessageFont)));
this.tagMapper.put("\\<shift:" + i + ">", Component.text("<shift:" + i + ">"));
String shiftTag = "<shift:" + i + ">";
this.tagMapper.put(shiftTag, AdventureHelper.miniMessage().deserialize(this.offsetFont.createOffset(i, FormatUtils::miniMessageFont)));
this.tagMapper.put("\\" + shiftTag, Component.text(shiftTag));
}
this.imageTagTrie = Trie.builder()
.ignoreOverlaps()
@@ -377,7 +381,7 @@ public abstract class AbstractFontManager implements FontManager {
Key imageId = new Key(split[0], split[1]);
Optional<BitmapImage> bitmapImage = bitmapImageByImageId(imageId);
if (bitmapImage.isPresent()) {
image = bitmapImage.get().miniMessage(0, 0);
image = bitmapImage.get().miniMessageAt(0, 0);
} else {
throw new LocalizedResourceConfigException("warning.config.emoji.invalid_image", path, id, rawImage);
}
@@ -386,7 +390,7 @@ public abstract class AbstractFontManager implements FontManager {
Optional<BitmapImage> bitmapImage = bitmapImageByImageId(imageId);
if (bitmapImage.isPresent()) {
try {
image = bitmapImage.get().miniMessage(Integer.parseInt(split[2]), Integer.parseInt(split[3]));
image = bitmapImage.get().miniMessageAt(Integer.parseInt(split[2]), Integer.parseInt(split[3]));
} catch (ArrayIndexOutOfBoundsException e) {
throw new LocalizedResourceConfigException("warning.config.emoji.invalid_image", path, id, rawImage);
}

View File

@@ -26,9 +26,9 @@ public class BitmapImage implements Supplier<JsonObject> {
this.codepointGrid = codepointGrid;
}
public String miniMessage(int row, int col) {
public String miniMessageAt(int row, int col) {
int codepoint = codepointGrid[row][col];
return FormatUtils.miniMessageFont(new String(Character.toChars(codepoint)), font.toString());
return FormatUtils.miniMessageFont(new String(Character.toChars(codepoint)), this.font.toString());
}
public int height() {

View File

@@ -3,13 +3,17 @@ package net.momirealms.craftengine.core.font;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import dev.dejvokep.boostedyaml.block.implementation.Section;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.momirealms.craftengine.core.util.CharacterUtils;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
public class OffsetFont {
private final String font;
private final Key fontKey;
private final String NEG_16;
private final String NEG_24;
@@ -37,6 +41,7 @@ public class OffsetFont {
public OffsetFont(Section section) {
font = section.getString("font", "minecraft:default");
fontKey = Key.key(font);
NEG_16 = convertIfUnicode(section.getString("-16", ""));
NEG_24 = convertIfUnicode(section.getString("-24", ""));
@@ -59,16 +64,14 @@ public class OffsetFont {
}
}
public Component createOffset(int offset) {
if (offset == 0) return Component.empty();
return Component.text(Objects.requireNonNull(this.fastLookup.get(offset, k -> k > 0 ? createPos(k) : createNeg(-k)))).font(this.fontKey);
}
public String createOffset(int offset, BiFunction<String, String, String> tagDecorator) {
if (offset == 0) return "";
String raw = fastLookup.get(offset, k -> {
if (k > 0) {
return createPos(k);
} else {
return createNeg(-k);
}
});
return tagDecorator.apply(raw, font);
return tagDecorator.apply(this.fastLookup.get(offset, k -> k > 0 ? createPos(k) : createNeg(-k)), this.font);
}
@SuppressWarnings("DuplicatedCode")

View File

@@ -20,8 +20,8 @@ public class SelfIncreaseIntTemplateArgument implements TemplateArgument {
@Override
public String get() {
String value = String.valueOf(current);
if (current < max) current += 1;
String value = String.valueOf(this.current);
if (this.current < this.max) this.current += 1;
return value;
}
@@ -31,15 +31,15 @@ public class SelfIncreaseIntTemplateArgument implements TemplateArgument {
}
public int current() {
return current;
return this.current;
}
public int min() {
return min;
return this.min;
}
public int max() {
return max;
return this.max;
}
public static class Factory implements TemplateArgumentFactory {

View File

@@ -150,7 +150,8 @@ public class TemplateManagerImpl implements TemplateManager {
// 依次处理map下的每个参数
Map<String, Object> result = new LinkedHashMap<>();
for (Map.Entry<String, Object> inputEntry : input.entrySet()) {
processUnknownTypeMember(inputEntry.getValue(), parentArguments, (processed) -> result.put(inputEntry.getKey(), processed));
String key = applyArgument(inputEntry.getKey(), parentArguments).toString();
processUnknownTypeMember(inputEntry.getValue(), parentArguments, (processed) -> result.put(key, processed));
}
processCallBack.accept(result);
}
@@ -188,18 +189,23 @@ public class TemplateManagerImpl implements TemplateManager {
Map<String, TemplateArgument> parentArguments) {
// 先获取template节点下所有的模板
List<String> templateIds = MiscUtils.getAsStringList(input.get(TEMPLATE));
List<Object> templateList = new ArrayList<>();
List<Object> templateList = new ArrayList<>(templateIds.size());
for (String templateId : templateIds) {
// 如果模板id被用了参数则应先应用参数后再查询模板
Object actualTemplate = applyArgument(templateId, parentArguments);
if (actualTemplate == null) continue; // 忽略被null掉的模板
Object template = Optional.ofNullable(templates.get(Key.of(actualTemplate.toString())))
Object template = Optional.ofNullable(this.templates.get(Key.of(actualTemplate.toString())))
.orElseThrow(() -> new IllegalArgumentException("Template not found: " + actualTemplate));
templateList.add(template);
}
Object argument = input.get(ARGUMENTS);
boolean hasArgument = argument != null;
// 将本节点下的参数与父参数合并
Map<String, TemplateArgument> arguments = mergeArguments(
MiscUtils.castToMap(input.getOrDefault(ARGUMENTS, Collections.emptyMap()), false),
Map<String, TemplateArgument> arguments = !hasArgument ? parentArguments : mergeArguments(
MiscUtils.castToMap(argument, false),
parentArguments
);
@@ -215,6 +221,7 @@ public class TemplateManagerImpl implements TemplateManager {
throw new IllegalArgumentException("Invalid template used. Input: " + GsonHelper.get().toJson(input) + ". Template: " + GsonHelper.get().toJson(obj));
}
});
// overrides是map了merges也只能是map
if (input.get(MERGES) instanceof Map<?, ?> rawMerges) {
Map<String, Object> merges = new LinkedHashMap<>();
processMap(MiscUtils.castToMap(rawMerges, false), arguments, (obj) -> {
@@ -225,6 +232,15 @@ public class TemplateManagerImpl implements TemplateManager {
throw new IllegalArgumentException("Invalid template used. Input: " + GsonHelper.get().toJson(input) + ". Template: " + GsonHelper.get().toJson(obj));
}
});
// 已有templatemergesoverrides 和可选的arguments
if (input.size() > (hasArgument ? 4 : 3)) {
// 会不会有一种可能有笨比用户把模板和普通配置混合在了一起再次遍历input后处理
for (Map.Entry<String, Object> inputEntry : input.entrySet()) {
String inputKey = inputEntry.getKey();
if (NON_TEMPLATE_KEY.contains(inputKey)) continue;
processUnknownTypeMember(inputEntry.getValue(), arguments, (processed) -> merges.put(inputKey, processed));
}
}
// 返回处理结果
return new TemplateProcessingResult(
templateList,
@@ -233,12 +249,29 @@ public class TemplateManagerImpl implements TemplateManager {
arguments
);
} else {
return new TemplateProcessingResult(
templateList,
overrides,
null,
arguments
);
// 已有templateoverrides 和可选的arguments
if (input.size() > (hasArgument ? 3 : 2)) {
Map<String, Object> merges = new LinkedHashMap<>();
// 会不会有一种可能有笨比用户把模板和普通配置混合在了一起再次遍历input后处理
for (Map.Entry<String, Object> inputEntry : input.entrySet()) {
String inputKey = inputEntry.getKey();
if (NON_TEMPLATE_KEY.contains(inputKey)) continue;
processUnknownTypeMember(inputEntry.getValue(), arguments, (processed) -> merges.put(inputKey, processed));
}
return new TemplateProcessingResult(
templateList,
overrides,
merges,
arguments
);
} else {
return new TemplateProcessingResult(
templateList,
overrides,
null,
arguments
);
}
}
} else if (override instanceof List<?> overrides) {
// overrides不为空且不是map
@@ -258,7 +291,6 @@ public class TemplateManagerImpl implements TemplateManager {
arguments
);
} else {
// overrides不为空且不是map
return new TemplateProcessingResult(
templateList,
processedOverrides,
@@ -266,8 +298,15 @@ public class TemplateManagerImpl implements TemplateManager {
arguments
);
}
} else if (override instanceof String rawOverride) {
return new TemplateProcessingResult(
templateList,
applyArgument(rawOverride, arguments),
null,
arguments
);
} else if (override != null) {
// overrides不为空且不是map。此情况不用再考虑merge了
// overrides不为空且不是map,list。此情况不用再考虑merge了
return new TemplateProcessingResult(
templateList,
override,
@@ -287,17 +326,18 @@ public class TemplateManagerImpl implements TemplateManager {
throw new IllegalArgumentException("Invalid template used. Input: " + GsonHelper.get().toJson(input) + ". Template: " + GsonHelper.get().toJson(obj));
}
});
Map<String, Object> overrides = new LinkedHashMap<>();
// 会不会有一种可能有笨比用户不会使用overrides把模板和普通配置混合在了一起再次遍历input后处理
for (Map.Entry<String, Object> inputEntry : input.entrySet()) {
String inputKey = inputEntry.getKey();
if (NON_TEMPLATE_KEY.contains(inputKey)) continue;
// 处理那些overrides
processUnknownTypeMember(inputEntry.getValue(), arguments, (processed) -> overrides.put(inputKey, processed));
// 已有template和merges 和可选的arguments
if (input.size() > (hasArgument ? 3 : 2)) {
// 会不会有一种可能有笨比用户把模板和普通配置混合在了一起再次遍历input后处理
for (Map.Entry<String, Object> inputEntry : input.entrySet()) {
String inputKey = inputEntry.getKey();
if (NON_TEMPLATE_KEY.contains(inputKey)) continue;
processUnknownTypeMember(inputEntry.getValue(), arguments, (processed) -> merges.put(inputKey, processed));
}
}
return new TemplateProcessingResult(
templateList,
overrides.isEmpty() ? null : overrides,
null,
merges,
arguments
);
@@ -312,21 +352,46 @@ public class TemplateManagerImpl implements TemplateManager {
merges,
arguments
);
} else {
Map<String, Object> overrides = new LinkedHashMap<>();
// 会不会有一种可能有笨比用户不会使用overrides把模板和普通配置混合在了一起再次遍历input后处理
for (Map.Entry<String, Object> inputEntry : input.entrySet()) {
String inputKey = inputEntry.getKey();
if (NON_TEMPLATE_KEY.contains(inputKey)) continue;
// 处理那些overrides
processUnknownTypeMember(inputEntry.getValue(), arguments, (processed) -> overrides.put(inputKey, processed));
}
} else if (merge instanceof String rawMerge) {
// merge是个string
return new TemplateProcessingResult(
templateList,
overrides.isEmpty() ? null : overrides,
null,
applyArgument(rawMerge, arguments),
arguments
);
} else if (merge != null) {
// merge是个普通的类型
return new TemplateProcessingResult(
templateList,
null,
merge,
arguments
);
} else {
// 无overrides和merges
// 会不会有一种可能有笨比用户不会使用merges把模板和普通配置混合在了一起再次遍历input后处理
if (input.size() > (hasArgument ? 2 : 1)) {
Map<String, Object> merges = new LinkedHashMap<>();
for (Map.Entry<String, Object> inputEntry : input.entrySet()) {
String inputKey = inputEntry.getKey();
if (NON_TEMPLATE_KEY.contains(inputKey)) continue;
processUnknownTypeMember(inputEntry.getValue(), arguments, (processed) -> merges.put(inputKey, processed));
}
return new TemplateProcessingResult(
templateList,
null,
merges,
arguments
);
} else {
return new TemplateProcessingResult(
templateList,
null,
null,
arguments
);
}
}
}
}
@@ -340,7 +405,7 @@ public class TemplateManagerImpl implements TemplateManager {
// argument_1: "{parent_argument}"
for (Map.Entry<String, Object> argumentEntry : rawChildArguments.entrySet()) {
// 获取最终的string形式参数
String placeholder = argumentEntry.getKey();
String placeholder = applyArgument(argumentEntry.getKey(), parentArguments).toString();
// 父亲参数最大
if (result.containsKey(placeholder)) continue;
Object rawArgument = argumentEntry.getValue();
@@ -385,8 +450,9 @@ public class TemplateManagerImpl implements TemplateManager {
if (input.length() < 3) return input;
if (input.charAt(0) == '{' && input.charAt(input.length() - 1) == '}') {
String key = input.substring(1, input.length() - 1);
if (arguments.containsKey(key)) {
return arguments.get(key).get();
TemplateArgument argument = arguments.get(key);
if (argument != null) {
return argument.get();
}
}
return replacePlaceholders(input, arguments);