aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/Danker/features/loot/TrophyFishTracker.java
blob: 1098e4b14897cab2f0ff4c71552c2edc6b756868 (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
package me.Danker.features.loot;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import me.Danker.events.ModInitEvent;
import me.Danker.events.PostConfigInitEvent;
import me.Danker.utils.RenderUtils;
import me.Danker.utils.Utils;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StringUtils;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TrophyFishTracker {

    public static JsonObject fish = new JsonObject();
    public static JsonObject fishSession = new JsonObject();
    public static Pattern fishPattern = Pattern.compile("TROPHY FISH! You caught a (?<fish>.*) (?<tier>.*).");
    public static String configFile;

    public static JsonObject createEmpty() {
        JsonObject fish = new JsonObject();

        JsonObject tiers = new JsonObject();
        tiers.addProperty("BRONZE", 0);
        tiers.addProperty("SILVER", 0);
        tiers.addProperty("GOLD", 0);
        tiers.addProperty("DIAMOND", 0);

        fish.add("Sulpher Skitter", Utils.deepCopy(tiers));
        fish.add("Obfuscated 1", Utils.deepCopy(tiers));
        fish.add("Steaming-Hot Flounder", Utils.deepCopy(tiers));
        fish.add("Obfuscated 2", Utils.deepCopy(tiers));
        fish.add("Gusher", Utils.deepCopy(tiers));
        fish.add("Blobfish", Utils.deepCopy(tiers));
        fish.add("Slugfish", Utils.deepCopy(tiers));
        fish.add("Obfuscated 3", Utils.deepCopy(tiers));
        fish.add("Flyfish", Utils.deepCopy(tiers));
        fish.add("Lavahorse", Utils.deepCopy(tiers));
        fish.add("Mana Ray", Utils.deepCopy(tiers));
        fish.add("Volcanic Stonefish", Utils.deepCopy(tiers));
        fish.add("Vanille", Utils.deepCopy(tiers));
        fish.add("Skeleton Fish", Utils.deepCopy(tiers));
        fish.add("Moldfin", Utils.deepCopy(tiers));
        fish.add("Soul Fish", Utils.deepCopy(tiers));
        fish.add("Karate Fish", Utils.deepCopy(tiers));
        fish.add("Golden Fish", Utils.deepCopy(tiers));

        return fish;
    }

    @SubscribeEvent
    public void init(ModInitEvent event) {
        configFile = event.configDirectory + "/dsmtrophyfish.json";
    }

    @SubscribeEvent
    public void postConfigInit(PostConfigInitEvent event) {
        if (fish.entrySet().isEmpty()) fish = createEmpty();
        fishSession = createEmpty();
        save();
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onChat(ClientChatReceivedEvent event) {
        String message = StringUtils.stripControlCodes(event.message.getUnformattedText());

        if (!Utils.inSkyblock) return;
        if (!Utils.tabLocation.equals("Crimson Isle")) return;
        if (event.type == 2) return;
        if (message.contains(":")) return;

        Matcher matcher = fishPattern.matcher(message);

        if (matcher.matches()) {
            String fishName = matcher.group("fish");
            String tier = matcher.group("tier");

            JsonObject fishObj = fish.get(fishName).getAsJsonObject();
            int amount = fishObj.get(tier).getAsInt();
            fishObj.addProperty(tier, amount + 1);

            JsonObject fishSessionObj = fishSession.get(fishName).getAsJsonObject();
            int amountSession = fishSessionObj.get(tier).getAsInt();
            fishSessionObj.addProperty(tier, amountSession + 1);

            save();
        }
    }

    public static void save() {
        try (FileWriter writer = new FileWriter(configFile)) {
            new GsonBuilder().create().toJson(fish, writer);
            writer.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static String getTierCount(JsonObject obj, String name) {
        JsonObject type = obj.get(name).getAsJsonObject();

        int bronze = type.get("BRONZE").getAsInt();
        int silver = type.get("SILVER").getAsInt();
        int gold = type.get("GOLD").getAsInt();
        int diamond = type.get("DIAMOND").getAsInt();

        return EnumChatFormatting.DARK_GRAY + "" + bronze + EnumChatFormatting.WHITE + "-" +
               EnumChatFormatting.GRAY + silver + EnumChatFormatting.WHITE + "-" +
               EnumChatFormatting.GOLD + gold + EnumChatFormatting.WHITE + "-" +
               EnumChatFormatting.AQUA + diamond;
    }

    public static int getSum(JsonObject obj, String name) {
        JsonObject type = obj.get(name).getAsJsonObject();
        return type.get("BRONZE").getAsInt() +
               type.get("SILVER").getAsInt() +
               type.get("GOLD").getAsInt() +
               type.get("DIAMOND").getAsInt();
    }

    public static void drawCompletion(JsonObject obj, String name, int x, int y, double scale) {
        JsonObject type = obj.get(name).getAsJsonObject();

        boolean bronze = type.get("BRONZE").getAsInt() > 0;
        boolean silver = type.get("SILVER").getAsInt() > 0;
        boolean gold = type.get("GOLD").getAsInt() > 0;
        boolean diamond = type.get("DIAMOND").getAsInt() > 0;

        ItemStack incomplete = new ItemStack(Items.dye, 1, 8);
        ItemStack bronzeComplete = new ItemStack(Items.brick);
        ItemStack silverComplete = new ItemStack(Items.iron_ingot);
        ItemStack goldComplete = new ItemStack(Items.gold_ingot);
        ItemStack diamondComplete = new ItemStack(Items.diamond);

        // -116 hides behind chat + tab
        RenderUtils.renderItem(bronze ? bronzeComplete : incomplete, x, y - 2, -116, scale / 1.3D);
        RenderUtils.renderItem(silver ? silverComplete : incomplete, x + 15, y - 2, -116, scale / 1.3D);
        RenderUtils.renderItem(gold ? goldComplete : incomplete, x + 30, y - 2, -116, scale / 1.3D);
        RenderUtils.renderItem(diamond ? diamondComplete : incomplete, x + 45, y - 2, -116, scale / 1.3D);
    }

}