aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/FairySouls.java
blob: ac1d2fd9bd7bdcd8c6a4233ad1835f6471610d54 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package io.github.moulberry.notenoughupdates.miscfeatures;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.commands.SimpleCommand;
import io.github.moulberry.notenoughupdates.core.util.render.RenderUtils;
import io.github.moulberry.notenoughupdates.util.Constants;
import io.github.moulberry.notenoughupdates.util.SBInfo;
import io.github.moulberry.notenoughupdates.util.SpecialColour;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.*;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class FairySouls {

    private static HashMap<String, Set<Integer>> foundSouls = new HashMap<>();
    private static List<BlockPos> currentSoulList = null;
    private static List<BlockPos> currentSoulListClose = null;

    private static boolean enabled = false;

    @SubscribeEvent
    public void onWorldUnload(WorldEvent.Unload event) {
        currentSoulList = null;
    }

    public static void load(File file, Gson gson) {
        if(file.exists()) {
            try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
                HashMap<String, List<Number>> foundSoulsList = gson.fromJson(reader, HashMap.class);

                foundSouls = new HashMap<>();
                for(Map.Entry<String, List<Number>> entry : foundSoulsList.entrySet()) {
                    HashSet<Integer> set = new HashSet<>();
                    for(Number n : entry.getValue()) {
                        set.add(n.intValue());
                    }
                    foundSouls.put(entry.getKey(), set);
                }

                return;
            } catch(Exception e) {}
        }
        foundSouls = new HashMap<>();
    }

    public static void save(File file, Gson gson) {
        try {
            file.createNewFile();

            try(BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) {
                writer.write(gson.toJson(foundSouls));
            }
        } catch(IOException ignored) {}
    }

    @SubscribeEvent
    public void onChatReceived(ClientChatReceivedEvent event) {
        if(currentSoulList == null) return;

        if(event.message.getFormattedText().equals("\u00A7r\u00A7dYou have already found that Fairy Soul!\u00A7r") ||
                event.message.getFormattedText().equals("\u00A7d\u00A7lSOUL! \u00A7fYou found a \u00A7r\u00A7dFairy Soul\u00A7r\u00A7f!\u00A7r")) {
            String location = SBInfo.getInstance().getLocation();
            if(location == null) return;

            int closestIndex = -1;
            double closestDistSq = 10*10;
            for(int i=0; i<currentSoulList.size(); i++) {
                BlockPos pos = currentSoulList.get(i);

                double distSq = pos.distanceSq(Minecraft.getMinecraft().thePlayer.getPosition());

                if(distSq < closestDistSq) {
                    closestDistSq = distSq;
                    closestIndex = i;
                }
            }
            if(closestIndex != -1) {
                Set<Integer> found = foundSouls.computeIfAbsent(location, k -> new HashSet<>());
                found.add(closestIndex);
            }
        }
    }

    public static void tick() {
        if(!enabled) return;

        if(Minecraft.getMinecraft().theWorld == null) {
            currentSoulList = null;
            return;
        }

        JsonObject fairySouls = Constants.FAIRYSOULS;
        if(fairySouls == null) return;

        String location = SBInfo.getInstance().getLocation();
        if(location == null) {
            currentSoulList = null;
            return;
        }

        if(currentSoulList == null) {
            if(fairySouls.has(location) && fairySouls.get(location).isJsonArray()) {
                JsonArray locations = fairySouls.get(location).getAsJsonArray();
                currentSoulList = new ArrayList<>();
                for(int i=0; i<locations.size(); i++) {
                    try {
                        String coord = locations.get(i).getAsString();

                        String[] split = coord.split(",");
                        if(split.length == 3) {
                            String xS = split[0];
                            String yS = split[1];
                            String zS = split[2];

                            int x = Integer.parseInt(xS);
                            int y = Integer.parseInt(yS);
                            int z = Integer.parseInt(zS);

                            currentSoulList.add(new BlockPos(x, y , z));
                        }
                    } catch(Exception ignored) {}
                }
            }
        }

        if(currentSoulList != null && !currentSoulList.isEmpty()) {
            TreeMap<Double, BlockPos> distanceSqMap = new TreeMap<>();

            Set<Integer> found = foundSouls.computeIfAbsent(location, k -> new HashSet<>());

            for(int i=0; i<currentSoulList.size(); i++) {
                if(found.contains(i)) continue;

                BlockPos pos = currentSoulList.get(i);
                double distSq = pos.distanceSq(Minecraft.getMinecraft().thePlayer.getPosition());
                distanceSqMap.put(distSq, pos);
            }

            int maxSouls = 15;
            int souls = 0;
            currentSoulListClose = new ArrayList<>();
            for(BlockPos pos : distanceSqMap.values()) {
                currentSoulListClose.add(pos);
                if(++souls >= maxSouls) break;
            }
        }
    }

    @SubscribeEvent
    public void onRenderLast(RenderWorldLastEvent event) {
        if(!enabled) return;

        String location = SBInfo.getInstance().getLocation();
        if(location == null) return;
        if(currentSoulList == null || currentSoulList.isEmpty()) return;

        Set<Integer> found = foundSouls.computeIfAbsent(location, k -> new HashSet<>());

        int rgb = 0xa839ce;
        for(int i=0; i<currentSoulListClose.size(); i++) {
            BlockPos currentSoul = currentSoulListClose.get(i);
            RenderUtils.renderBeaconBeamOrBoundingBox(currentSoul, rgb, 1.0f, event.partialTicks);
        }
    }

    public static class FairySoulsCommandAlt extends SimpleCommand {
        public FairySoulsCommandAlt() {
            super("fairysouls", fairysoulRunnable);
        }
    }

    public static class FairySoulsCommand extends SimpleCommand {

        public FairySoulsCommand() {
            super("neusouls", fairysoulRunnable);
        }
    }

    private static SimpleCommand.ProcessCommandRunnable fairysoulRunnable = new SimpleCommand.ProcessCommandRunnable() {
        @Override
        public void processCommand(ICommandSender sender, String[] args) {
            if(args.length != 1) {
                printHelp();
                return;
            }
            String subcommand = args[0].toLowerCase();

            switch (subcommand) {
                case "help":
                    printHelp();
                    return;
                case "on":
                case "enable":
                    print(EnumChatFormatting.DARK_PURPLE+"Enabled fairy soul waypoints");
                    enabled = true;
                    return;
                case "off":
                case "disable":
                    print(EnumChatFormatting.DARK_PURPLE+"Disabled fairy soul waypoints");
                    enabled = false;
                    return;
                case "clear": {
                    String location = SBInfo.getInstance().getLocation();
                    if(currentSoulList == null || loca