aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/thatgravyboat/skyblockhud/handlers/CurrencyHandler.java
blob: f8c6b86d5bdd2318a80590180e4096eb5ee3c287 (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
package com.thatgravyboat.skyblockhud.handlers;

import com.thatgravyboat.skyblockhud.Utils;
import com.thatgravyboat.skyblockhud.api.events.SidebarLineUpdateEvent;
import com.thatgravyboat.skyblockhud.api.events.SidebarPostEvent;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.Locale;
import java.util.regex.Pattern;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class CurrencyHandler {

  private static int bits = 0;
  private static double coins = 0;

  public static void setBits(int amount) {
    bits = amount;
  }

  public static void setCoins(double amount) {
    coins = amount;
  }

  public static int getBits() {
    return bits;
  }

  public static double getCoins() {
    return coins;
  }

  @SubscribeEvent
  public void onSidebarLineUpdate(SidebarLineUpdateEvent event) {
    if (
      Utils
        .removeColor(event.formattedLine.toLowerCase().trim())
        .contains("purse:") ||
      Utils
        .removeColor(event.formattedLine.toLowerCase().trim())
        .contains("piggy:")
    ) {
      CurrencyHandler.checkCoins(event.formattedLine);
    }
    if (
      Utils
        .removeColor(event.formattedLine.toLowerCase().trim())
        .contains("bits:") &&
      !event.formattedLine.toLowerCase().contains("(")
    ) {
      CurrencyHandler.checkBits(event.formattedLine);
    }
  }

  @SubscribeEvent
  public void onSidebarPost(SidebarPostEvent event) {
    if (!Arrays.toString(event.arrayScores).toLowerCase().contains("bits:")) {
      CurrencyHandler.setBits(0);
    }
  }

  public static String getCoinsFormatted() {
    DecimalFormat formatter = new DecimalFormat(
      "#,###.0",
      DecimalFormatSymbols.getInstance(Locale.CANADA)
    );
    String output = formatter.format(coins);
    if (output.equals(".0")) output = "0.0"; else if (
      output.equals(",0")
    ) output = "0,0";
    return output;
  }

  public static String getBitsFormatted() {
    DecimalFormat formatter = new DecimalFormat(
      "#.#",
      DecimalFormatSymbols.getInstance(Locale.CANADA)
    );
    formatter.setRoundingMode(RoundingMode.FLOOR);
    return bits > 999
      ? formatter.format((double) bits / 1000) + "k"
      : String.valueOf(bits);
  }

  public static void checkCoins(String formatedScoreboardLine) {
    String purse = Utils
      .removeWhiteSpaceAndRemoveWord(
        Utils.removeColor(formatedScoreboardLine.toLowerCase().trim()),
        Utils
            .removeColor(formatedScoreboardLine.toLowerCase().trim())
            .contains("purse:")
          ? "purse:"
          : "piggy:"
      )
      .replace(",", "");
    if (!purse.contains("(") && !purse.contains("+")) {
      try {
        double coins = Double.parseDouble(
          Pattern.compile("[^0-9.]").matcher(purse).replaceAll("")
        );
        CurrencyHandler.setCoins(coins);
      } catch (IllegalArgumentException ex) {
        System.out.println(
          "Failed to parse purse, please report to ThatGravyBoat. Purse Text: " +
          purse
        );
      }
    }
  }

  public static void checkBits(String formatedScoreboardLine) {
    String bits = Utils
      .removeWhiteSpaceAndRemoveWord(
        Utils.removeColor(formatedScoreboardLine.toLowerCase().trim()),
        "bits:"
      )
      .replace(",", "");
    try {
      int bit = Integer.parseInt(
        Pattern.compile("[^0-9]").matcher(bits).replaceAll("")
      );
      CurrencyHandler.setBits(bit);
    } catch (IllegalArgumentException ex) {
      System.out.println(
        "Failed to parse bits, please report to ThatGravyBoat. Bits Text: " +
        bits
      );
    }
  }
}