blob: 8eeae2aa37fa26d97339caa550b53c2c16cc6a24 (
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
|
package de.hysky.skyblocker.skyblock.waypoint;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import de.hysky.skyblocker.SkyblockerMod;
import de.hysky.skyblocker.utils.Utils;
import de.hysky.skyblocker.utils.waypoint.WaypointCategory;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Collection;
import java.util.List;
public class Waypoints {
private static final Logger LOGGER = LoggerFactory.getLogger(Waypoints.class);
private static final Codec<List<WaypointCategory>> CODEC = WaypointCategory.CODEC.listOf();
private static final Codec<List<WaypointCategory>> SKYTILS_CODEC = WaypointCategory.SKYTILS_CODEC.listOf();
private static final Path waypointsFile = FabricLoader.getInstance().getConfigDir().resolve(SkyblockerMod.NAMESPACE).resolve("waypoints.json");
static final Multimap<String, WaypointCategory> waypoints = MultimapBuilder.hashKeys().arrayListValues().build();
public static void init() {
loadWaypoints();
ClientLifecycleEvents.CLIENT_STOPPING.register(Waypoints::saveWaypoints);
WorldRenderEvents.AFTER_TRANSLUCENT.register(Waypoints::render);
}
public static void loadWaypoints() {
waypoints.clear();
try (BufferedReader reader = Files.newBufferedReader(waypointsFile)) {
List<WaypointCategory> waypoints = CODEC.parse(JsonOps.INSTANCE, SkyblockerMod.GSON.fromJson(reader, JsonArray.class)).resultOrPartial(LOGGER::error).orElseThrow();
waypoints.forEach(waypointCategory -> Waypoints.waypoints.put(waypointCategory.island(), waypointCategory));
} catch (Exception e) {
LOGGER.error("[Skyblocker Waypoints] Encountered exception while loading waypoints", e);
}
}
public static List<WaypointCategory> fromSkytilsBase64(String base64) {
return fromSkytilsJson(new String(Base64.getDecoder().decode(base64)));
}
public static List<WaypointCategory> fromSkytilsJson(String waypointCategories) {
return SKYTILS_CODEC.parse(JsonOps.INSTANCE, SkyblockerMod.GSON.fromJson(waypointCategories, JsonObject.class).getAsJsonArray("categories")).resultOrPartial(LOGGER::error).orElseThrow();
}
public static String toSkytilsBase64(Collection<WaypointCategory> waypointCategories) {
return Base64.getEncoder().encodeToString(toSkytilsJson(waypointCategories).getBytes());
}
public static String toSkytilsJson(Collection<WaypointCategory> waypointCategories) {
JsonObject waypointCategoriesJson = new JsonObject();
waypointCategoriesJson.add("categories", SKYTILS_CODEC.encodeStart(JsonOps.INSTANCE, List.copyOf(waypointCategories)).resultOrPartial(LOGGER::error).orElseThrow());
return SkyblockerMod.GSON.toJson(waypointCategoriesJson);
}
public static void saveWaypoints(MinecraftClient client) {
try (BufferedWriter writer = Files.newBufferedWriter(waypointsFile)) {
JsonElement waypointsJson = CODEC.encodeStart(JsonOps.INSTANCE, List.copyOf(waypoints.values())).resultOrPartial(LOGGER::error).orElseThrow();
SkyblockerMod.GSON.toJson(waypointsJson, writer);
LOGGER.info("[Skyblocker Waypoints] Saved waypoints");
} catch (Exception e) {
LOGGER.error("[Skyblocker Waypoints] Encountered exception while saving waypoints", e);
}
}
public static void render(WorldRenderContext context) {
Collection<WaypointCategory> categories = waypoints.get(Utils.getLocationRaw());
for (WaypointCategory category : categories) {
if (category != null) {
category.render(context);
}
}
}
}
|