aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/tectech/util/TTUtility.java
blob: f552fde984874dc063cda6c8111a70a00d2896cc (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package tectech.util;

import java.lang.reflect.Field;
import java.math.BigInteger;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import net.minecraft.item.ItemStack;

import cpw.mods.fml.common.registry.GameRegistry;
import gregtech.GTMod;
import gregtech.api.metatileentity.implementations.MTETieredMachineBlock;

/**
 * Created by Tec on 21.03.2017.
 */
public final class TTUtility {

    private TTUtility() {}

    private static final StringBuilder STRING_BUILDER = new StringBuilder();
    private static final Map<Locale, Formatter> FORMATTER_MAP = new HashMap<>();

    private static Formatter getFormatter() {
        STRING_BUILDER.setLength(0);
        return FORMATTER_MAP.computeIfAbsent(
            Locale.getDefault(Locale.Category.FORMAT),
            locale -> new Formatter(STRING_BUILDER, locale));
    }

    public static String formatNumberExp(double value) {
        return getFormatter().format("%+.5E", value)
            .toString();
    }

    public static String toExponentForm(BigInteger number) {
        BigInteger abs = number.abs();
        String strNum = abs.toString();
        int exponent = strNum.length() - 1;
        return (number.signum() == -1 ? "-" : "") + strNum.charAt(0) + "." + strNum.substring(1, 3) + "e" + exponent;

    }

    public static int bitStringToInt(String bits) {
        if (bits == null) {
            return 0;
        }
        if (bits.length() > 32) {
            throw new NumberFormatException("Too long!");
        }
        return Integer.parseInt(bits, 2);
    }

    public static int hexStringToInt(String hex) {
        if (hex == null) {
            return 0;
        }
        if (hex.length() > 8) {
            throw new NumberFormatException("Too long!");
        }
        return Integer.parseInt(hex, 16);
    }

    public static double stringToDouble(String str) {
        if (str == null) {
            return 0;
        }
        return Double.parseDouble(str);
    }

    public static String longBitsToShortString(long number) {
        StringBuilder result = new StringBuilder(71);

        for (int i = 63; i >= 0; i--) {
            long mask = 1L << i;
            result.append((number & mask) != 0 ? ":" : ".");

            if (i % 8 == 0) {
                result.append('|');
            }
        }
        result.replace(result.length() - 1, result.length(), "");

        return result.toString();
    }

    public static float map(float x, float in_min, float in_max, float out_min, float out_max) {
        return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
    }

    public static String getUniqueIdentifier(ItemStack is) {
        return GameRegistry.findUniqueIdentifierFor(is.getItem()).modId + ':' + is.getUnlocalizedName();
    }

    public static void setTier(int tier, Object o) {
        // TODO why is it using reflection to change a final field from GREGTECH ?
        if (!(o instanceof MTETieredMachineBlock)) {
            GTMod.GT_FML_LOGGER.error(
                "Could not set tier as object " + o.getClass()
                    .getName() + " isn't instance of MTETieredMachineBlock");
            return;
        }
        try {
            Field field = MTETieredMachineBlock.class.getField("mTier");
            field.setAccessible(true);
            field.set(o, (byte) tier);
        } catch (Exception e) {
            GTMod.GT_FML_LOGGER.error(
                "Could not set tier of " + o.getClass()
                    .getName(),
                e);
        }
    }

    @Deprecated
    public static double receiveDouble(double previousValue, int startIndex, int index, int value) {
        return Double.longBitsToDouble(receiveLong(Double.doubleToLongBits(previousValue), startIndex, index, value));
    }

    public static long receiveLong(long previousValue, int startIndex, int index, int value) {
        value &= 0xFFFF;
        switch (index - startIndex) {
            case 0 -> {
                previousValue &= 0xFFFF_FFFF_FFFF_0000L;
                previousValue |= value;
            }
            case 1 -> {
                previousValue &= 0xFFFF_FFFF_0000_FFFFL;
                previousValue |= (long) value << 16;
            }
            case 2 -> {
                previousValue &= 0xFFFF_0000_FFFF_FFFFL;
                previousValue |= (long) value << 32;
            }
            case 3 -> {
                previousValue &= 0x0000_FFFF_FFFF_FFFFL;
                previousValue |= (long) value << 48;
            }
        }
        return previousValue;
    }

    @Deprecated
    public static float receiveFloat(float previousValue, int startIndex, int index, int value) {
        return Float.intBitsToFloat(receiveInteger(Float.floatToIntBits(previousValue), startIndex, index, value));
    }

    public static int receiveInteger(int previousValue, int startIndex, int index, int value) {
        value &= 0xFFFF;
        switch (index - startIndex) {
            case 0 -> {
                previousValue &= 0xFFFF_0000;
                previousValue |= value;
            }
            case 1 -> {
                previousValue &= 0x0000_FFFF;
                previousValue |= value << 16;
            }
        }
        return previousValue;
    }

    public static String[][] appendStringArrays(String[][] firstArray, String[][] secondArray) {
        int totalLength = firstArray.length + secondArray.length;
        String[][] resultArray = new String[totalLength][];

        System.arraycopy(firstArray, 0, resultArray, 0, firstArray.length);
        System.arraycopy(secondArray, 0, resultArray, firstArray.length, secondArray.length);
        return resultArray;
    }

    public static String[][] replaceLetters(String[][] array, String replacement) {
        String[][] outputArray = new String[array.length][];
        for (int i = 0; i < array.length; i++) {
            outputArray[i] = new String[array[i].length];
            for (int j = 0; j < array[i].length; j++) {
                outputArray[i][j] = array[i][j].replaceAll("[A-Z]", replacement);
            }
        }
        return outputArray;
    }
}