mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-28 19:39:11 +00:00
refactor lang system
This commit is contained in:
@@ -453,17 +453,6 @@ public class BukkitBlockManager extends AbstractBlockManager {
|
||||
Map<String, Object> behaviorSection = MiscUtils.castToMap(section.getOrDefault("behavior", Map.of()), false);
|
||||
|
||||
BukkitCustomBlock block = new BukkitCustomBlock(id, holder, properties, appearances, variants, settings, behaviorSection, lootTable);
|
||||
// read block name
|
||||
String blockName = (String) section.getOrDefault("name", null);
|
||||
if (blockName != null && blockName.startsWith("i18n:")) {
|
||||
plugin.translationManager().i18nData().forEach((locale, i18nData) -> {
|
||||
plugin.debug(() -> "locale: " + toMinecraftLocale(locale) + ": " + i18nData.translate(blockName.substring(5)));
|
||||
block.addBlockName(toMinecraftLocale(locale), i18nData.translate(blockName.substring(5)));
|
||||
}
|
||||
);
|
||||
} else {
|
||||
block.addBlockName(blockName);
|
||||
}
|
||||
|
||||
// bind appearance
|
||||
bindAppearance(block);
|
||||
@@ -478,18 +467,6 @@ public class BukkitBlockManager extends AbstractBlockManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static String toMinecraftLocale(Locale locale) {
|
||||
String language = locale.getLanguage().toLowerCase();
|
||||
String country = locale.getCountry().toLowerCase();
|
||||
if ("en".equals(language) && country.isEmpty()) {
|
||||
return "en_us";
|
||||
}
|
||||
if (country.isEmpty()) {
|
||||
return language;
|
||||
}
|
||||
return language + "_" + country;
|
||||
}
|
||||
|
||||
private void bindAppearance(CustomBlock block) {
|
||||
for (ImmutableBlockState state : block.variantProvider().states()) {
|
||||
ImmutableBlockState previous = this.stateId2ImmutableBlockStates[state.customBlockState().registryId() - BlockStateUtils.vanillaStateSize()];
|
||||
|
||||
@@ -8,7 +8,6 @@ import net.momirealms.craftengine.core.block.*;
|
||||
import net.momirealms.craftengine.core.block.properties.Property;
|
||||
import net.momirealms.craftengine.core.loot.LootTable;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.locale.I18NData;
|
||||
import net.momirealms.craftengine.core.registry.Holder;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.Tristate;
|
||||
@@ -138,24 +137,4 @@ public class BukkitCustomBlock extends CustomBlock {
|
||||
CraftEngine.instance().logger().warn("Failed to init block settings", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void addBlockName(String lang, String blockName) {
|
||||
I18NData i18nData = new I18NData();
|
||||
for (ImmutableBlockState state : this.variantProvider().states()) {
|
||||
try {
|
||||
Object blockState = state.customBlockState().handle();
|
||||
Object block = Reflections.method$BlockStateBase$getBlock.invoke(blockState);
|
||||
String translationKey = (String) Reflections.method$BlockBehaviour$getDescriptionId.invoke(block);
|
||||
i18nData.addTranslation(translationKey, blockName);
|
||||
} catch (Exception e) {
|
||||
CraftEngine.instance().logger().warn("Failed to get the " + state.owner().value().id() + " translationKey");
|
||||
}
|
||||
}
|
||||
this.addBlockName(lang, i18nData);
|
||||
}
|
||||
|
||||
public void addBlockName(String blockName) {
|
||||
if (blockName == null) return;
|
||||
addBlockName("en_us", blockName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
|
||||
|
||||
@Override
|
||||
public Collection<BukkitServerPlayer> onlineUsers() {
|
||||
return onlineUsers.values();
|
||||
return new ArrayList<>(this.onlineUsers.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -173,22 +173,22 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
|
||||
public void init() {
|
||||
if (init) return;
|
||||
try {
|
||||
plugin.bootstrap().getServer().getMessenger().registerIncomingPluginChannel(plugin.bootstrap(), MOD_CHANNEL, this);
|
||||
plugin.bootstrap().getServer().getMessenger().registerOutgoingPluginChannel(plugin.bootstrap(), MOD_CHANNEL);
|
||||
this.plugin.bootstrap().getServer().getMessenger().registerIncomingPluginChannel(this.plugin.bootstrap(), MOD_CHANNEL, this);
|
||||
this.plugin.bootstrap().getServer().getMessenger().registerOutgoingPluginChannel(this.plugin.bootstrap(), MOD_CHANNEL);
|
||||
Object server = Reflections.method$MinecraftServer$getServer.invoke(null);
|
||||
Object serverConnection = Reflections.field$MinecraftServer$connection.get(server);
|
||||
@SuppressWarnings("unchecked")
|
||||
List<ChannelFuture> channels = (List<ChannelFuture>) Reflections.field$ServerConnectionListener$channels.get(serverConnection);
|
||||
ListMonitor<ChannelFuture> monitor = new ListMonitor<>(channels, (future) -> {
|
||||
if (!active) return;
|
||||
if (!this.active) return;
|
||||
Channel channel = future.channel();
|
||||
injectServerChannel(channel);
|
||||
injectedChannels.add(channel);
|
||||
this.injectedChannels.add(channel);
|
||||
}, (object) -> {});
|
||||
Reflections.field$ServerConnectionListener$channels.set(serverConnection, monitor);
|
||||
init = true;
|
||||
this.init = true;
|
||||
} catch (ReflectiveOperationException e) {
|
||||
plugin.logger().warn("Failed to init server connection", e);
|
||||
this.plugin.logger().warn("Failed to init server connection", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -516,7 +516,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
|
||||
}
|
||||
}
|
||||
|
||||
public class PluginChannelDecoder extends MessageToMessageDecoder<ByteBuf> {
|
||||
public static class PluginChannelDecoder extends MessageToMessageDecoder<ByteBuf> {
|
||||
private final NetWorkUser player;
|
||||
|
||||
public PluginChannelDecoder(NetWorkUser player) {
|
||||
|
||||
Reference in New Issue
Block a user