aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core/util/minecraft/LangUtils.java
blob: 28d017d74961355df98f788749fa14d4144aafe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package gtPlusPlus.core.util.minecraft;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Properties;

import cpw.mods.fml.common.registry.LanguageRegistry;
import gtPlusPlus.core.util.reflect.ReflectionUtils;

public class LangUtils {

    @SuppressWarnings("unchecked")
    public static void rewriteEntryForLanguageRegistry(String aLang, String aKey, String aNewValue) {
        LanguageRegistry aInstance = LanguageRegistry.instance();
        Field aModLanguageData = ReflectionUtils.getField(LanguageRegistry.class, "modLanguageData");
        if (aModLanguageData != null) {
            Map<String, Properties> aProps;
            Object aInstanceProps;
            try {
                aInstanceProps = aModLanguageData.get(aInstance);
                if (aInstanceProps != null) {
                    aProps = (Map<String, Properties>) aInstanceProps;
                    Properties aLangProps = aProps.get(aLang);
                    if (aLangProps != null) {
                        if (aLangProps.containsKey(aKey)) {
                            aLangProps.remove(aKey);
                            aLangProps.put(aKey, aNewValue);
                        } else {
                            aLangProps.put(aKey, aNewValue);
                        }
                        aProps.remove(aLang);
                        aProps.put(aLang, aLangProps);
                        ReflectionUtils.setField(aInstance, aModLanguageData, aProps);
                    }
                }
            } catch (IllegalArgumentException | IllegalAccessException ignored) {

            }
        }
    }
}