diff options
author | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-01-26 21:43:29 -0500 |
---|---|---|
committer | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-05-24 19:51:15 -0400 |
commit | 6ad1335dd95e5df4db38e1d8d022a6c2925d6921 (patch) | |
tree | a8a4ea71c2ee6ad1bd36fed090d71c0f2f55a091 /src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java | |
parent | 92bb441b31cd72d142adee578cc253708cf0101c (diff) | |
download | Skyblocker-6ad1335dd95e5df4db38e1d8d022a6c2925d6921.tar.gz Skyblocker-6ad1335dd95e5df4db38e1d8d022a6c2925d6921.tar.bz2 Skyblocker-6ad1335dd95e5df4db38e1d8d022a6c2925d6921.zip |
Add waypoints loading and saving
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java index 070cacaa..1a9d6836 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java @@ -1,16 +1,49 @@ package de.hysky.skyblocker.skyblock.waypoint; +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.*; public class Waypoints { - Codec<List<WaypointCategory>> CODEC = WaypointCategory.CODEC.listOf(); + private static final Logger LOGGER = LoggerFactory.getLogger(Waypoints.class); + private static final Codec<List<WaypointCategory>> CODEC = WaypointCategory.CODEC.listOf(); + private static final Path waypointsFile = FabricLoader.getInstance().getConfigDir().resolve(SkyblockerMod.NAMESPACE).resolve("waypoints.json"); private static final Map<String, WaypointCategory> waypoints = new HashMap<>(); + 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("Encountered exception while loading waypoints", e); + } + } + public static Collection<WaypointCategory> fromSkytilsBase64(String base64) { return fromSkytilsJson(new String(Base64.getDecoder().decode(base64))); } @@ -22,4 +55,20 @@ public class Waypoints { .map(WaypointCategory::fromSkytilsJson) .toList(); } + + public static void saveWaypoints(MinecraftClient client) { + try (BufferedWriter writer = Files.newBufferedWriter(waypointsFile)) { + JsonElement waypointsJson = CODEC.encodeStart(JsonOps.INSTANCE, new ArrayList<>(waypoints.values())).resultOrPartial(LOGGER::error).orElseThrow(); + SkyblockerMod.GSON.toJson(waypointsJson, writer); + } catch (Exception e) { + LOGGER.error("Encountered exception while saving waypoints", e); + } + } + + public static void render(WorldRenderContext context) { + WaypointCategory category = waypoints.get(Utils.getLocationRaw()); + if (category != null) { + category.render(context); + } + } } |