mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-19 15:09:15 +00:00
允许语言文件解析shift和image
This commit is contained in:
@@ -30,6 +30,8 @@ dependencies {
|
||||
compileOnly("net.kyori:adventure-text-serializer-gson:${rootProject.properties["adventure_bundle_version"]}") {
|
||||
exclude("com.google.code.gson", "gson")
|
||||
}
|
||||
compileOnly("net.kyori:adventure-text-serializer-legacy:${rootProject.properties["adventure_bundle_version"]}")
|
||||
|
||||
compileOnly("net.kyori:adventure-text-serializer-json-legacy-impl:${rootProject.properties["adventure_bundle_version"]}")
|
||||
// Command
|
||||
compileOnly("org.incendo:cloud-core:${rootProject.properties["cloud_core_version"]}")
|
||||
|
||||
@@ -2,7 +2,6 @@ package net.momirealms.craftengine.core.font;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
import net.momirealms.craftengine.core.item.AbstractItemManager;
|
||||
import net.momirealms.craftengine.core.pack.LoadingSequence;
|
||||
import net.momirealms.craftengine.core.pack.Pack;
|
||||
import net.momirealms.craftengine.core.pack.ResourceLocation;
|
||||
@@ -76,6 +75,11 @@ public abstract class AbstractFontManager implements FontManager {
|
||||
this.networkTagMapper = new HashMap<>(1024);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OffsetFont offsetFont() {
|
||||
return offsetFont;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unload() {
|
||||
this.fonts.clear();
|
||||
|
||||
@@ -46,6 +46,8 @@ public interface FontManager extends Manageable {
|
||||
|
||||
IllegalCharacterProcessResult processIllegalCharacters(String raw, char replacement);
|
||||
|
||||
OffsetFont offsetFont();
|
||||
|
||||
ConfigParser[] parsers();
|
||||
|
||||
default EmojiTextProcessResult replaceMiniMessageEmoji(@NotNull String miniMessage, @Nullable Player player) {
|
||||
|
||||
@@ -12,7 +12,8 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class OffsetFont {
|
||||
private final String font;
|
||||
private static OffsetFont instance;
|
||||
private final net.momirealms.craftengine.core.util.Key font;
|
||||
private final Key fontKey;
|
||||
|
||||
private final String NEG_16;
|
||||
@@ -39,10 +40,14 @@ public class OffsetFont {
|
||||
.maximumSize(256)
|
||||
.build();
|
||||
|
||||
public OffsetFont(Section section) {
|
||||
font = section.getString("font", "minecraft:default");
|
||||
fontKey = Key.key(font);
|
||||
public net.momirealms.craftengine.core.util.Key font() {
|
||||
return font;
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public OffsetFont(Section section) {
|
||||
font = net.momirealms.craftengine.core.util.Key.of(section.getString("font", "minecraft:default"));
|
||||
fontKey = Key.key(font.namespace(), font.value());
|
||||
NEG_16 = convertIfUnicode(section.getString("-16", ""));
|
||||
NEG_24 = convertIfUnicode(section.getString("-24", ""));
|
||||
NEG_32 = convertIfUnicode(section.getString("-32", ""));
|
||||
@@ -71,7 +76,7 @@ public class OffsetFont {
|
||||
|
||||
public String createOffset(int offset, BiFunction<String, String, String> tagDecorator) {
|
||||
if (offset == 0) return "";
|
||||
return tagDecorator.apply(this.fastLookup.get(offset, k -> k > 0 ? createPos(k) : createNeg(-k)), this.font);
|
||||
return tagDecorator.apply(this.fastLookup.get(offset, k -> k > 0 ? createPos(k) : createNeg(-k)), this.font.asString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
@@ -148,7 +153,7 @@ public class OffsetFont {
|
||||
|
||||
private String convertIfUnicode(String s) {
|
||||
if (s.startsWith("\\u")) {
|
||||
return new String(CharacterUtils.decodeUnicodeToChars(font));
|
||||
return new String(CharacterUtils.decodeUnicodeToChars(font.asString()));
|
||||
} else {
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -4,9 +4,8 @@ public final class LoadingSequence {
|
||||
private LoadingSequence() {}
|
||||
|
||||
public static final int TEMPLATE = 0;
|
||||
public static final int BLOCK_STATE_MAPPING = 5;
|
||||
public static final int GLOBAL_VAR = 10;
|
||||
public static final int LANG = 20;
|
||||
public static final int BLOCK_STATE_MAPPING = 10;
|
||||
public static final int GLOBAL_VAR = 20;
|
||||
public static final int TRANSLATION = 30;
|
||||
public static final int EQUIPMENT = 40;
|
||||
public static final int ITEM = 50;
|
||||
@@ -20,4 +19,5 @@ public final class LoadingSequence {
|
||||
public static final int VANILLA_LOOTS = 130;
|
||||
public static final int EMOJI = 140;
|
||||
public static final int ADVANCEMENT = 150;
|
||||
public static final int LANG = 160;
|
||||
}
|
||||
|
||||
@@ -2,13 +2,19 @@ package net.momirealms.craftengine.core.plugin.locale;
|
||||
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.core.block.AbstractBlockManager;
|
||||
import net.momirealms.craftengine.core.font.FontManager;
|
||||
import net.momirealms.craftengine.core.font.OffsetFont;
|
||||
import net.momirealms.craftengine.core.pack.LoadingSequence;
|
||||
import net.momirealms.craftengine.core.pack.Pack;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.Plugin;
|
||||
import net.momirealms.craftengine.core.plugin.PluginProperties;
|
||||
import net.momirealms.craftengine.core.plugin.config.*;
|
||||
import net.momirealms.craftengine.core.plugin.text.component.ComponentProvider;
|
||||
import net.momirealms.craftengine.core.plugin.text.minimessage.ImageTag;
|
||||
import net.momirealms.craftengine.core.plugin.text.minimessage.IndexedArgumentTag;
|
||||
import net.momirealms.craftengine.core.plugin.text.minimessage.ShiftTag;
|
||||
import net.momirealms.craftengine.core.util.AdventureHelper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -25,6 +31,7 @@ import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TranslationManagerImpl implements TranslationManager {
|
||||
@@ -276,6 +283,10 @@ public class TranslationManagerImpl implements TranslationManager {
|
||||
|
||||
public class LangParser implements IdSectionConfigParser {
|
||||
public static final String[] CONFIG_SECTION_NAME = new String[] {"lang", "language", "languages"};
|
||||
private final Function<String, String> langProcessor = s -> {
|
||||
Component deserialize = AdventureHelper.miniMessage().deserialize(AdventureHelper.legacyToMiniMessage(s), ShiftTag.INSTANCE, ImageTag.INSTANCE);
|
||||
return AdventureHelper.getLegacy().serialize(deserialize);
|
||||
};
|
||||
|
||||
@Override
|
||||
public int loadingSequence() {
|
||||
@@ -293,7 +304,7 @@ public class TranslationManagerImpl implements TranslationManager {
|
||||
Map<String, String> sectionData = section.entrySet().stream()
|
||||
.collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
entry -> String.valueOf(entry.getValue())
|
||||
entry -> this.langProcessor.apply(String.valueOf(entry.getValue()))
|
||||
));
|
||||
TranslationManagerImpl.this.addClientTranslation(langId, sectionData);
|
||||
}
|
||||
|
||||
@@ -9,10 +9,12 @@ import net.kyori.adventure.text.ComponentIteratorType;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.TextReplacementConfig;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.internal.parser.TokenParser;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.kyori.adventure.text.serializer.json.JSONOptions;
|
||||
import net.kyori.adventure.text.serializer.json.legacyimpl.NBTLegacyHoverEventSerializer;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import net.momirealms.craftengine.core.plugin.context.Context;
|
||||
import net.momirealms.craftengine.core.plugin.text.component.ComponentProvider;
|
||||
import net.momirealms.sparrow.nbt.Tag;
|
||||
@@ -33,6 +35,7 @@ public class AdventureHelper {
|
||||
private final MiniMessage miniMessageCustom;
|
||||
private final GsonComponentSerializer gsonComponentSerializer;
|
||||
private final NBTComponentSerializer nbtComponentSerializer;
|
||||
private final LegacyComponentSerializer legacyComponentSerializer;
|
||||
private static final TextReplacementConfig REPLACE_LF = TextReplacementConfig.builder().matchLiteral("\n").replacement(Component.newline()).build();
|
||||
/**
|
||||
* This iterator slices a component into individual parts that
|
||||
@@ -65,6 +68,7 @@ public class AdventureHelper {
|
||||
b.value(JSONOptions.EMIT_HOVER_SHOW_ENTITY_KEY_AS_TYPE_AND_UUID_AS_ID, true);
|
||||
});
|
||||
}
|
||||
this.legacyComponentSerializer = LegacyComponentSerializer.builder().build();
|
||||
this.gsonComponentSerializer = builder.build();
|
||||
this.nbtComponentSerializer = NBTComponentSerializer.builder()
|
||||
.editItem(item -> {
|
||||
@@ -84,20 +88,10 @@ public class AdventureHelper {
|
||||
private static final AdventureHelper INSTANCE = new AdventureHelper();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the singleton instance of AdventureHelper.
|
||||
*
|
||||
* @return the singleton instance
|
||||
*/
|
||||
public static AdventureHelper getInstance() {
|
||||
return SingletonHolder.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the MiniMessage instance.
|
||||
*
|
||||
* @return the MiniMessage instance
|
||||
*/
|
||||
public static MiniMessage miniMessage() {
|
||||
return getInstance().miniMessage;
|
||||
}
|
||||
@@ -106,70 +100,22 @@ public class AdventureHelper {
|
||||
return getInstance().miniMessageCustom;
|
||||
}
|
||||
|
||||
public static LegacyComponentSerializer getLegacy() {
|
||||
return getInstance().legacyComponentSerializer;
|
||||
}
|
||||
|
||||
public static MiniMessage strictMiniMessage() {
|
||||
return getInstance().miniMessageStrict;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the GsonComponentSerializer instance.
|
||||
*
|
||||
* @return the GsonComponentSerializer instance
|
||||
*/
|
||||
public static GsonComponentSerializer getGson() {
|
||||
return getInstance().gsonComponentSerializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the NBTComponentSerializer instance.
|
||||
*
|
||||
* @return the NBTComponentSerializer instance
|
||||
*/
|
||||
public static NBTComponentSerializer getNBT() {
|
||||
return getInstance().nbtComponentSerializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to an audience.
|
||||
*
|
||||
* @param audience the audience to send the message to
|
||||
* @param message the message component
|
||||
*/
|
||||
public static void sendMessage(Audience audience, Component message) {
|
||||
audience.sendMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays a sound for an audience.
|
||||
*
|
||||
* @param audience the audience to play the sound for
|
||||
* @param sound the sound to play
|
||||
*/
|
||||
public static void playSound(Audience audience, Sound sound) {
|
||||
audience.playSound(sound);
|
||||
}
|
||||
|
||||
/**
|
||||
* Surrounds text with a MiniMessage font tag.
|
||||
*
|
||||
* @param text the text to surround
|
||||
* @param font the font as a {@link Key}
|
||||
* @return the text surrounded by the MiniMessage font tag
|
||||
*/
|
||||
public static String surroundWithMiniMessageFont(String text, Key font) {
|
||||
return "<font:" + font.asString() + ">" + text + "</font>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Surrounds text with a MiniMessage font tag.
|
||||
*
|
||||
* @param text the text to surround
|
||||
* @param font the font as a {@link String}
|
||||
* @return the text surrounded by the MiniMessage font tag
|
||||
*/
|
||||
public static String surroundWithMiniMessageFont(String text, String font) {
|
||||
return "<font:" + font + ">" + text + "</font>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a JSON string to a MiniMessage string.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user