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

import com.thatgravyboat.skyblockhud.api.events.LocationChangeEvent;
import com.thatgravyboat.skyblockhud.api.events.ProfileSwitchedEvent;
import com.thatgravyboat.skyblockhud.api.events.SidebarLineUpdateEvent;
import com.thatgravyboat.skyblockhud.utils.Utils;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.util.Locale;

public class IslandHandler {

    public static int flightTime;
    public static boolean hadFlightTime;

    public static int redstone;
    public static boolean hadRedstone;

    public static float cleanupPercentage;
    public static boolean hadCleanupPercentage;

    @SubscribeEvent
    public void onSidebarLineUpdate(SidebarLineUpdateEvent event) {
        hadFlightTime = checkFlightDuration(event.formattedLine);
        hadRedstone = checkRestone(event.formattedLine);
        hadCleanupPercentage |= checkPlotClearPercentage(event.formattedLine);
    }

    @SubscribeEvent
    public void onLocationChange(LocationChangeEvent event) {
        hadCleanupPercentage = false;
    }


    @SubscribeEvent
    public void onProfileSwitch(ProfileSwitchedEvent event) {
        flightTime = 0;
    }

    public static boolean checkFlightDuration(String formatedScoreboardLine) {
        if (LocationHandler.getCurrentLocation().getCategory() == LocationCategory.ISLAND
                && Utils.removeColor(formatedScoreboardLine.toLowerCase().trim()).contains("flight duration:")) {
            String timeString = formatedScoreboardLine.toLowerCase().replace("flight duration:", "").replace(" ", "");
            String[] times = timeString.split(":");
            if (times.length == 2) {
                int s = 0;
                try {
                    s += Integer.parseInt(times[0].replaceAll(",", "")) * 60;
                } catch (NumberFormatException ignored) {
                }
                try {
                    s += Integer.parseInt(times[1].replaceAll(",", ""));
                } catch (NumberFormatException ignored) {
                }
                flightTime = s - 1;
            } else if (times.length == 3) {
                int s = 0;
                try {
                    s += Integer.parseInt(times[0].replaceAll(",", "")) * 3600;
                } catch (NumberFormatException ignored) {}
                try {
                    s += Integer.parseInt(times[1].replaceAll(",", "")) * 60;
                } catch (NumberFormatException ignored) {
                }
                try {
                    s += Integer.parseInt(times[2].replaceAll(",", ""));
                } catch (NumberFormatException ignored) {
                }
                flightTime = s - 1;
            }
            return true;
        }
        return false;
    }

    public static boolean checkPlotClearPercentage(String formatedScoreboardLine) {
        if (LocationHandler.getCurrentLocation() == Locations.THEGARDEN) {
            String lowered = formatedScoreboardLine.toLowerCase(Locale.ROOT);
            if (lowered.contains("cleanup:")) {
                try {
                    String s = Utils.removeWhiteSpaceAndRemoveWord(lowered, "cleanup:").replace("%", "");
                    cleanupPercentage = Float.parseFloat(s);
                    return true;
                } catch (Exception ignored) {
                }
            }
        }
        return false;
    }

    public static boolean checkRestone(String formatedScoreboardLine) {
        if (LocationHandler.getCurrentLocation() == Locations.YOURISLAND) {
            try {
                redstone = formatedScoreboardLine.toLowerCase().contains("redstone:") ? Integer.parseInt(Utils.removeWhiteSpaceAndRemoveWord(formatedScoreboardLine, "redstone:").replaceAll(",", "")) : 0;
            } catch (Exception ignored) {
            }
            return formatedScoreboardLine.toLowerCase().contains("redstone:");
        }
        return false;
    }
}