aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/data/BestiaryEntry.java
blob: a6c78e7843e96cdfaccd05ce7584fb3fd86fd3de (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
package de.cowtipper.cowlection.data;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import org.apache.commons.lang3.StringUtils;

import java.text.NumberFormat;
import java.util.Locale;

public class BestiaryEntry {
    public static ItemStack triggerItem;

    public static int highestKillsToGo;
    public static int highestKillGoal;
    public static int highestPercentageToGo;
    public static int widestMobNameWidth;

    private static int widestKillsToGoWidth;
    private static int widestKillGoalToGoWidth;
    private static int widestPercentageToGoWidth;

    private static final NumberFormat numberFormatter;
    private static final char dashSpacerChar;
    private static final char spacerChar;
    private static int dashSpacerWidth;
    private static int spacerWidth;

    private final String mobName;
    private final int mobNameWidth;
    private final int killsToGo;
    private final int killsGoal;
    private final int percentageToGo;

    static {
        numberFormatter = NumberFormat.getNumberInstance(Locale.US);
        numberFormatter.setMaximumFractionDigits(0);
        dashSpacerChar = '·';
        spacerChar = ' ';
    }

    public BestiaryEntry(String mobName, int currentKills, int killsGoal) {
        this.mobName = mobName;
        this.killsToGo = killsGoal - currentKills;
        this.killsGoal = killsGoal;
        this.percentageToGo = 100 - (currentKills * 100 / killsGoal);
        if (killsToGo > highestKillsToGo) {
            highestKillsToGo = killsToGo;
        }
        if (killsGoal > highestKillGoal) {
            highestKillGoal = killsGoal;
        }
        if (percentageToGo > highestPercentageToGo) {
            highestPercentageToGo = percentageToGo;
        }
        this.mobNameWidth = Minecraft.getMinecraft().fontRendererObj.getStringWidth(mobName);
        if (mobNameWidth > widestMobNameWidth) {
            widestMobNameWidth = mobNameWidth;
        }
    }

    /**
     * Bestiary not unlocked yet
     */
    public BestiaryEntry(String mobName) {
        this.mobName = mobName;
        mobNameWidth = -1;
        killsToGo = Integer.MAX_VALUE;
        killsGoal = -1;
        percentageToGo = Integer.MAX_VALUE;
    }

    public static void reinitialize(ItemStack triggerItem) {
        BestiaryEntry.triggerItem = triggerItem;

        FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
        dashSpacerWidth = fontRenderer.getCharWidth(dashSpacerChar);
        spacerWidth = fontRenderer.getCharWidth(spacerChar);

        highestKillsToGo = 0;
        highestKillGoal = 0;
        highestPercentageToGo = 0;
        widestMobNameWidth = 0;
    }

    public static void calculateWidestEntries() {
        FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
        widestKillsToGoWidth = fontRenderer.getStringWidth(numberFormatter.format(highestKillsToGo));
        widestKillGoalToGoWidth = fontRenderer.getStringWidth(numberFormatter.format(highestKillGoal));
        widestPercentageToGoWidth = fontRenderer.getStringWidth(numberFormatter.format(highestPercentageToGo));
    }

    public static boolean isDifferentTriggerItem(ItemStack triggerItem) {
        return BestiaryEntry.triggerItem == null || !BestiaryEntry.triggerItem.getIsItemStackEqual(triggerItem);
    }

    public String getFormattedOutput(boolean sortBestiaryOverviewByKills) {
        if (percentageToGo == Integer.MAX_VALUE) {
            return mobName + EnumChatFormatting.GRAY + ": not unlocked yet";
        }

        FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
        StringBuilder currentEntry = new StringBuilder();
        String formattedKillsToGo = numberFormatter.format(killsToGo);
        String formattedKillsGoal = numberFormatter.format(killsGoal);
        String formattedPercentageToGo = numberFormatter.format(percentageToGo);

        int gapSize = (widestMobNameWidth - mobNameWidth) + (sortBestiaryOverviewByKills
                ? widestKillsToGoWidth - fontRenderer.getStringWidth(formattedKillsToGo)
                : widestPercentageToGoWidth - fontRenderer.getStringWidth(formattedPercentageToGo));
        int amountOfSpacerChars = Math.max(gapSize, 0) / dashSpacerWidth;

        currentEntry.append(mobName).append(EnumChatFormatting.RESET).append(EnumChatFormatting.DARK_GRAY).append(StringUtils.repeat(dashSpacerChar, amountOfSpacerChars)).append(' ');
        int remainingGap = gapSize % dashSpacerWidth;
        if (remainingGap >= 3) {
            // quite a large gap left = add another smaller spacer
            currentEntry.append(spacerChar);
            remainingGap -= spacerWidth;
        }

        StringBuilder killsInfo = new StringBuilder().append(sortBestiaryOverviewByKills ? EnumChatFormatting.AQUA : EnumChatFormatting.DARK_AQUA)
                .append(formattedKillsToGo).append(EnumChatFormatting.DARK_GRAY).append('/').append(formattedKillsGoal).append(EnumChatFormatting.GRAY).append(" kills");
        StringBuilder percentageInfo = new StringBuilder().append(sortBestiaryOverviewByKills ? EnumChatFormatting.DARK_AQUA : EnumChatFormatting.AQUA)
                .append(formattedPercentageToGo).append(EnumChatFormatting.DARK_GRAY).append("%");
        String spacer = EnumChatFormatting.DARK_GRAY + " ⬌ ";

        currentEntry.append(sortBestiaryOverviewByKills ? killsInfo : percentageInfo).append(spacer);
        int gapSize2 = ((sortBestiaryOverviewByKills
                ? (widestPercentageToGoWidth - fontRenderer.getStringWidth(formattedPercentageToGo) + widestKillGoalToGoWidth - fontRenderer.getStringWidth(formattedKillsGoal))
                : (widestKillsToGoWidth - fontRenderer.getStringWidth(formattedKillsToGo)))
                + remainingGap);
        int amountOf2ndSpacerChars = Math.max(gapSize2, 0) / spacerWidth;
        currentEntry.append(StringUtils.repeat(spacerChar, amountOf2ndSpacerChars));

        currentEntry.append(sortBestiaryOverviewByKills ? percentageInfo : killsInfo).append(EnumChatFormatting.GRAY).append(" to go");
        return currentEntry.toString();
    }

    public int getKillsToGo() {
        return killsToGo;
    }

    public int getPercentageToGo() {
        return percentageToGo;
    }

    public String getMobName() {
        return mobName;
    }
}