aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/util/Util.java
blob: a1a9e4c0baa1ae06daf0b358b4df9deb95d45590 (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
package util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.oredict.OreDictionary;

import common.items.ErrorItem;

public class Util {

    protected static final DecimalFormat percentFormatRound_6 = new DecimalFormat("0.000000%");
    protected static final DecimalFormat percentFormatRound_2 = new DecimalFormat("0.00%");
    protected static final BigDecimal Threshold_1 = BigDecimal.valueOf(0.01);
    protected static DecimalFormat standardFormat;

    static {
        DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
        dfs.setExponentSeparator("x10^");
        standardFormat = new DecimalFormat("0.00E0", dfs);
    }

    public static ItemStack getStackofAmountFromOreDict(String oredictName, final int amount) {
        final ArrayList<ItemStack> list = OreDictionary.getOres(oredictName);
        if (!list.isEmpty()) {
            final ItemStack ret = list.get(0)
                .copy();
            ret.stackSize = amount;
            return ret;
        }
        System.err.println("Failed to find " + oredictName + " in OreDict");
        return new ItemStack(ErrorItem.getInstance(), amount);
    }

    public static ItemStack[] toItemStackArray(List<ItemStack> stacksList) {
        if (stacksList.size() == 0) {
            return null;
        }

        ItemStack[] ret = new ItemStack[stacksList.size()];
        Iterator<ItemStack> iterator = stacksList.iterator();
        for (int i = 0; i < ret.length; i++) {
            ret[i] = iterator.next();
        }
        return ret;
    }

    public static FluidStack[] toFluidStackArray(List<FluidStack> stacksList) {
        if (stacksList.size() == 0) {
            return null;
        }

        FluidStack[] ret = new FluidStack[stacksList.size()];
        Iterator<FluidStack> iterator = stacksList.iterator();
        for (int i = 0; i < ret.length; i++) {
            ret[i] = iterator.next();
        }
        return ret;
    }

    /* If the number is less than 1, we round by the 6, otherwise to 2 */
    public static String toPercentageFrom(BigInteger value, BigInteger maxValue) {
        if (BigInteger.ZERO.equals(maxValue)) {
            return "0.00%";
        }
        BigDecimal result = new BigDecimal(value).setScale(6, RoundingMode.HALF_UP)
            .divide(new BigDecimal(maxValue), RoundingMode.HALF_UP);
        if (result.compareTo(Threshold_1) < 0) {
            return percentFormatRound_6.format(result);
        } else {
            return percentFormatRound_2.format(result);
        }
    }

    /* Get a string like this: 4.56*10^25 */
    public static String toStandardForm(BigInteger number) {
        if (BigInteger.ZERO.equals(number)) {
            return "0";
        }

        return standardFormat.format(number);
    }

}