mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-30 20:39:10 +00:00
refactor block name
This commit is contained in:
@@ -66,10 +66,23 @@ public abstract class CraftEngine implements Plugin {
|
||||
protected Consumer<Supplier<String>> debugger = (s) -> {};
|
||||
private boolean isReloading;
|
||||
|
||||
private String buildByBit = "%%__BUILTBYBIT__%%";
|
||||
private String polymart = "%%__POLYMART__%%";
|
||||
private String time = "%%__TIMESTAMP__%%";
|
||||
private String user = "%%__USER__%%";
|
||||
private String username = "%%__USERNAME__%%";
|
||||
|
||||
protected CraftEngine() {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public static CraftEngine instance() {
|
||||
if (instance == null) {
|
||||
throw new IllegalStateException("CraftEngine has not been initialized");
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()).addFilter(new LogFilter());
|
||||
@@ -83,6 +96,7 @@ public abstract class CraftEngine implements Plugin {
|
||||
this.configManager = new ConfigManager(this);
|
||||
}
|
||||
|
||||
// TODO Make most things async
|
||||
@Override
|
||||
public void reload() {
|
||||
if (this.isReloading) return;
|
||||
@@ -104,11 +118,14 @@ public abstract class CraftEngine implements Plugin {
|
||||
this.packManager.reload();
|
||||
// load at last
|
||||
this.guiManager.reload();
|
||||
// delayed load
|
||||
this.translationManager.delayedLoad();
|
||||
this.blockManager.delayedLoad();
|
||||
this.furnitureManager.delayedLoad();
|
||||
this.itemBrowserManager.delayedLoad();
|
||||
this.soundManager.delayedLoad();
|
||||
this.imageManager.delayedLoad();
|
||||
// reset debugger
|
||||
if (ConfigManager.debug()) {
|
||||
this.debugger = (s) -> logger.info("[Debug] " + s.get());
|
||||
} else {
|
||||
@@ -166,7 +183,7 @@ public abstract class CraftEngine implements Plugin {
|
||||
|
||||
protected abstract void registerParsers();
|
||||
|
||||
public void delayedEnable() {}
|
||||
protected abstract void delayedEnable();
|
||||
|
||||
protected abstract List<Dependency> platformDependencies();
|
||||
|
||||
@@ -302,12 +319,5 @@ public abstract class CraftEngine implements Plugin {
|
||||
return isReloading;
|
||||
}
|
||||
|
||||
public static CraftEngine instance() {
|
||||
if (instance == null) {
|
||||
throw new IllegalStateException("CraftEngine has not been initialized");
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public abstract boolean hasPlaceholderAPI();
|
||||
}
|
||||
|
||||
@@ -53,4 +53,6 @@ public interface ClientLangManager extends Reloadable, ConfigSectionParser {
|
||||
}
|
||||
|
||||
void addTranslation(String langId, Map<String, String> translations);
|
||||
|
||||
void delayedLoad();
|
||||
}
|
||||
|
||||
@@ -59,4 +59,9 @@ public class ClientLangMangerImpl implements ClientLangManager {
|
||||
.addTranslations(translations);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delayedLoad() {
|
||||
this.i18nData.values().forEach(I18NData::processTranslations);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,20 +2,69 @@ package net.momirealms.craftengine.core.plugin.locale;
|
||||
|
||||
import net.momirealms.craftengine.core.block.BlockStateParser;
|
||||
import net.momirealms.craftengine.core.block.BlockStateVariantProvider;
|
||||
import net.momirealms.craftengine.core.block.CustomBlock;
|
||||
import net.momirealms.craftengine.core.block.ImmutableBlockState;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class I18NData {
|
||||
public final Map<String, String> translations = new HashMap<>();
|
||||
private static final Map<String, Function<String, List<String>>> LANG_KEY_PROCESSORS = new HashMap<>();
|
||||
public Map<String, String> translations = new HashMap<>();
|
||||
|
||||
static {
|
||||
LANG_KEY_PROCESSORS.put("block_name", (id) -> {
|
||||
if (id.contains("[") && id.contains("]")) {
|
||||
ImmutableBlockState parsed = BlockStateParser.deserialize(id);
|
||||
if (parsed == null) return List.of(id);
|
||||
return List.of("block." + stateToRealBlockId(parsed));
|
||||
} else {
|
||||
Key blockId = Key.of(id);
|
||||
Optional<CustomBlock> blockOptional = CraftEngine.instance().blockManager().getBlock(blockId);
|
||||
if (blockOptional.isPresent()) {
|
||||
List<ImmutableBlockState> states = blockOptional.get().variantProvider().states();
|
||||
if (states.size() == 1) {
|
||||
return List.of("block." + stateToRealBlockId(states.get(0)));
|
||||
} else {
|
||||
ArrayList<String> processed = new ArrayList<>();
|
||||
for (ImmutableBlockState state : states) {
|
||||
processed.add("block." + stateToRealBlockId(state));
|
||||
}
|
||||
return processed;
|
||||
}
|
||||
} else {
|
||||
return List.of(id);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void processTranslations() {
|
||||
Map<String, String> temp = new HashMap<>(Math.max(10, this.translations.size()));
|
||||
for (Map.Entry<String, String> entry : this.translations.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
String[] split = key.split(":", 2);
|
||||
if (split.length == 2) {
|
||||
Optional.ofNullable(LANG_KEY_PROCESSORS.get(split[0]))
|
||||
.ifPresentOrElse(processor -> {
|
||||
for (String result : processor.apply(split[1])) {
|
||||
temp.put(result, entry.getValue());
|
||||
}
|
||||
},
|
||||
() -> CraftEngine.instance().logger().warn("Unknown lang type: " + key)
|
||||
);
|
||||
} else {
|
||||
temp.put(key, entry.getValue());
|
||||
}
|
||||
}
|
||||
this.translations = temp;
|
||||
}
|
||||
|
||||
public void addTranslations(Map<String, String> data) {
|
||||
addBlockName(data);
|
||||
translations.putAll(data);
|
||||
this.translations.putAll(data);
|
||||
}
|
||||
|
||||
public void addTranslation(String key, String value) {
|
||||
@@ -43,50 +92,28 @@ public class I18NData {
|
||||
});
|
||||
}
|
||||
|
||||
private static void addBlockName(Map<String, String> data) {
|
||||
String[] keyBuffer = new String[data.size()];
|
||||
int validKeyCount = 0;
|
||||
for (Map.Entry<String, String> entry : data.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
if (key != null && key.length() > 11 && key.startsWith("block_name:")) {
|
||||
keyBuffer[validKeyCount++] = key;
|
||||
private static String stateToRealBlockId(ImmutableBlockState state) {
|
||||
String id = state.customBlockState().handle().toString();
|
||||
int first = -1, last = -1;
|
||||
for (int i = 0; i < id.length(); i++) {
|
||||
char c = id.charAt(i);
|
||||
if (c == '{' && first == -1) {
|
||||
first = i;
|
||||
} else if (c == '}') {
|
||||
last = i;
|
||||
}
|
||||
}
|
||||
|
||||
for (String key : keyBuffer) {
|
||||
String value = data.remove(key);
|
||||
ImmutableBlockState states = BlockStateParser.deserialize(key.substring(11));
|
||||
if (states == null) continue;
|
||||
|
||||
BlockStateVariantProvider variantProvider = states.owner().value().variantProvider();
|
||||
Collection<ImmutableBlockState> stateCollection = variantProvider.states();
|
||||
for (ImmutableBlockState state : stateCollection) {
|
||||
Object blockState = state.customBlockState().handle();
|
||||
String id = blockState.toString();
|
||||
int first = -1, last = -1;
|
||||
for (int i = 0; i < id.length(); i++) {
|
||||
char c = id.charAt(i);
|
||||
if (c == '{' && first == -1) {
|
||||
first = i;
|
||||
} else if (c == '}') {
|
||||
last = i;
|
||||
}
|
||||
}
|
||||
if (first == -1 || last == -1 || last <= first) {
|
||||
CraftEngine.instance().logger().warn("Invalid block ID format: " + id);
|
||||
continue;
|
||||
}
|
||||
int length = last - first - 1;
|
||||
char[] chars = new char[length];
|
||||
id.getChars(first + 1, last, chars, 0);
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (chars[i] == ':') {
|
||||
chars[i] = '.';
|
||||
}
|
||||
}
|
||||
String blockId = new String(chars);
|
||||
data.put("block." + blockId, value);
|
||||
if (first == -1 || last == -1 || last <= first) {
|
||||
throw new IllegalArgumentException("Invalid block state: " + id);
|
||||
}
|
||||
int length = last - first - 1;
|
||||
char[] chars = new char[length];
|
||||
id.getChars(first + 1, last, chars, 0);
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (chars[i] == ':') {
|
||||
chars[i] = '.';
|
||||
}
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ public interface TranslationManager extends Reloadable, ConfigSectionParser {
|
||||
|
||||
void forcedLocale(Locale locale);
|
||||
|
||||
void delayedLoad();
|
||||
|
||||
String miniMessageTranslation(String key, @Nullable Locale locale);
|
||||
|
||||
default Component render(Component component) {
|
||||
|
||||
@@ -62,6 +62,11 @@ public class TranslationManagerImpl implements TranslationManager {
|
||||
this.forcedLocale = locale;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delayedLoad() {
|
||||
this.clientLangManager.delayedLoad();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() {
|
||||
// clear old data
|
||||
|
||||
Reference in New Issue
Block a user