mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-29 03:49:15 +00:00
feat(block): 添加自定义方块名称支持
This commit is contained in:
@@ -7,8 +7,8 @@ import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import dev.dejvokep.boostedyaml.YamlDocument;
|
||||
import net.momirealms.craftengine.bukkit.block.worldedit.WorldEditCommandHelper;
|
||||
import net.momirealms.craftengine.bukkit.block.worldedit.WorldEditBlockRegister;
|
||||
import net.momirealms.craftengine.bukkit.block.worldedit.WorldEditCommandHelper;
|
||||
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
|
||||
import net.momirealms.craftengine.bukkit.plugin.injector.BukkitInjector;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.PacketConsumers;
|
||||
@@ -452,7 +452,18 @@ public class BukkitBlockManager extends AbstractBlockManager {
|
||||
// create block
|
||||
Map<String, Object> behaviorSection = MiscUtils.castToMap(section.getOrDefault("behavior", Map.of()), false);
|
||||
|
||||
CustomBlock block = new BukkitCustomBlock(id, holder, properties, appearances, variants, settings, behaviorSection, lootTable);
|
||||
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);
|
||||
@@ -467,6 +478,18 @@ 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,6 +8,7 @@ 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;
|
||||
@@ -137,4 +138,24 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5528,4 +5528,16 @@ public class Reflections {
|
||||
ReflectionUtils.getMethod(
|
||||
clazz$ItemStack,new String[]{"b"}, clazz$Registry, clazz$BlockInWorld
|
||||
);
|
||||
|
||||
public static final Method method$BlockStateBase$getBlock = requireNonNull(
|
||||
ReflectionUtils.getMethod(
|
||||
clazz$BlockStateBase, clazz$Block
|
||||
)
|
||||
);
|
||||
|
||||
public static final Method method$BlockBehaviour$getDescriptionId = requireNonNull(
|
||||
VersionHelper.isVersionNewerThan1_21_2()
|
||||
? ReflectionUtils.getMethod(clazz$BlockBehaviour, String.class)
|
||||
: ReflectionUtils.getMethod(clazz$Block, String.class)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user