|
| 1 | +package io.github.cpearl0.ctnhcore.client.ponder; |
| 2 | + |
| 3 | +import io.github.cpearl0.ctnhcore.CTNHCore; |
| 4 | +import io.github.cpearl0.ctnhcore.client.ponder.Electric.CarbonBrushes; |
| 5 | +import io.github.cpearl0.ctnhcore.client.ponder.Kinetic.BigDam; |
| 6 | +import io.github.cpearl0.ctnhcore.client.ponder.Kinetic.Meadow; |
| 7 | +import io.github.cpearl0.ctnhcore.client.ponder.Kinetic.MechanicalExporter; |
| 8 | +import io.github.cpearl0.ctnhcore.client.ponder.Kinetic.SmashingFactory; |
| 9 | +import io.github.cpearl0.ctnhcore.client.ponder.Mana.MysticSpire; |
| 10 | + |
| 11 | +import com.ctnhlang.CN; |
| 12 | +import com.ctnhlang.EN; |
| 13 | +import com.ctnhlang.langprovider.LangKeyBuilder; |
| 14 | +import net.createmod.ponder.foundation.PonderIndex; |
| 15 | +import net.minecraft.network.chat.Component; |
| 16 | +import net.minecraft.network.chat.ComponentContents; |
| 17 | +import net.minecraft.network.chat.contents.TranslatableContents; |
| 18 | + |
| 19 | +import tech.vixhentx.mcmod.ctnhlib.langprovider.Lang; |
| 20 | +import tech.vixhentx.mcmod.ctnhlib.registrate.lang.RegistrateCNLangProvider; |
| 21 | + |
| 22 | +import com.tterrag.registrate.providers.RegistrateLangProvider; |
| 23 | + |
| 24 | +import java.lang.reflect.Field; |
| 25 | +import java.util.List; |
| 26 | +import java.util.LinkedHashMap; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Optional; |
| 29 | + |
| 30 | +public final class CTNHPonderLang { |
| 31 | + |
| 32 | + private static final List<Class<?>> PONDER_LANG_CLASSES = List.of( |
| 33 | + BigDam.class, |
| 34 | + CarbonBrushes.class, |
| 35 | + Meadow.class, |
| 36 | + MechanicalExporter.class, |
| 37 | + SmashingFactory.class, |
| 38 | + MysticSpire.class); |
| 39 | + |
| 40 | + private static final Map<String, Entry> LANG_ENTRIES = new LinkedHashMap<>(); |
| 41 | + private static final Map<String, String> CN_ENTRIES = new LinkedHashMap<>(); |
| 42 | + private static boolean collectedLangEntries; |
| 43 | + |
| 44 | + private CTNHPonderLang() {} |
| 45 | + |
| 46 | + static Optional<Entry> find(Component component) { |
| 47 | + ComponentContents contents = component.getContents(); |
| 48 | + if (contents instanceof TranslatableContents translatable) { |
| 49 | + return find(translatable.getKey()); |
| 50 | + } |
| 51 | + return Optional.empty(); |
| 52 | + } |
| 53 | + |
| 54 | + private static Optional<Entry> find(String key) { |
| 55 | + collectLangEntries(); |
| 56 | + return Optional.ofNullable(LANG_ENTRIES.get(key)); |
| 57 | + } |
| 58 | + |
| 59 | + private static void collectLangEntries() { |
| 60 | + if (collectedLangEntries) { |
| 61 | + return; |
| 62 | + } |
| 63 | + collectedLangEntries = true; |
| 64 | + for (Class<?> ownerClass : PONDER_LANG_CLASSES) { |
| 65 | + collectLangEntries(ownerClass); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static void collectLangEntries(Class<?> ownerClass) { |
| 70 | + for (Field field : ownerClass.getDeclaredFields()) { |
| 71 | + if (!Lang.class.isAssignableFrom(field.getType())) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + EN en = field.getAnnotation(EN.class); |
| 75 | + CN cn = field.getAnnotation(CN.class); |
| 76 | + if (en == null && cn == null) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + LANG_ENTRIES.put( |
| 80 | + LangKeyBuilder.buildKey(ownerClass, field, CTNHCore.MODID), |
| 81 | + new Entry(firstValue(en), firstValue(cn))); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static String firstValue(EN en) { |
| 86 | + return en == null ? "" : firstValue(en.value()); |
| 87 | + } |
| 88 | + |
| 89 | + private static String firstValue(CN cn) { |
| 90 | + return cn == null ? "" : firstValue(cn.value()); |
| 91 | + } |
| 92 | + |
| 93 | + private static String firstValue(String[] values) { |
| 94 | + return values.length == 0 ? "" : values[0]; |
| 95 | + } |
| 96 | + |
| 97 | + static void addCN(String key, String value) { |
| 98 | + CN_ENTRIES.put(key, value); |
| 99 | + } |
| 100 | + |
| 101 | + public static void init(RegistrateLangProvider provider) { |
| 102 | + PonderIndex.getLangAccess().provideLang(CTNHCore.MODID, provider::add); |
| 103 | + } |
| 104 | + |
| 105 | + public static void init(RegistrateCNLangProvider provider) { |
| 106 | + CN_ENTRIES.clear(); |
| 107 | + PonderIndex.getLangAccess().provideLang(CTNHCore.MODID, (key, value) -> {}); |
| 108 | + CN_ENTRIES.forEach(provider::add); |
| 109 | + } |
| 110 | + |
| 111 | + record Entry(String en, String cn) {} |
| 112 | +} |
0 commit comments