From 15d0bfb2b98d994329cf118c56be71a4a11e4033 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sun, 7 Jul 2024 23:04:37 +0800 Subject: Fix waypoints Skytils v1 importing --- .../skyblocker/skyblock/waypoint/Waypoints.java | 21 ++++++++++++++------- .../skyblock/waypoint/WaypointsShareScreen.java | 2 +- .../skyblocker/utils/waypoint/NamedWaypoint.java | 6 ++++-- 3 files changed, 19 insertions(+), 10 deletions(-) (limited to 'src/main/java/de') 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 18096117..ce559259 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java @@ -21,11 +21,14 @@ import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; import net.minecraft.client.toast.SystemToast; +import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.BufferedWriter; +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Base64; @@ -33,6 +36,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.function.Function; +import java.util.zip.GZIPInputStream; import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; @@ -62,19 +66,22 @@ public class Waypoints { } } - public static List fromSkytilsBase64(String base64, String defaultIsland) { + public static List fromSkytils(String waypointsString, String defaultIsland) { try { - if (base64.startsWith("(V")) { - int version = Integer.parseInt(base64.substring(26, base64.indexOf(')'))); + if (waypointsString.startsWith("(V")) { + int version = Integer.parseInt(waypointsString.substring(25, waypointsString.indexOf(')'))); + waypointsString = waypointsString.substring(waypointsString.indexOf(':') + 1); if (version == 1) { - return fromSkytilsJson(new String(Base64.getDecoder().decode(base64.substring(base64.indexOf(':') + 1))), defaultIsland); + try (GZIPInputStream reader = new GZIPInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(waypointsString)))) { + return fromSkytilsJson(IOUtils.toString(reader, StandardCharsets.UTF_8), defaultIsland); + } } else { - LOGGER.error("[Skyblocker Waypoints] Unknown Skytils waypoint data version: " + version); + LOGGER.error("[Skyblocker Waypoints] Unknown Skytils waypoint data version: {}", version); } - } else return fromSkytilsJson(new String(Base64.getDecoder().decode(base64)), defaultIsland); + } else return fromSkytilsJson(new String(Base64.getDecoder().decode(waypointsString)), defaultIsland); } catch (NumberFormatException e) { LOGGER.error("[Skyblocker Waypoints] Encountered exception while parsing Skytils waypoint data version", e); - } catch (IllegalArgumentException e) { + } catch (Exception e) { LOGGER.error("[Skyblocker Waypoints] Encountered exception while decoding Skytils waypoint data", e); } return Collections.emptyList(); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsShareScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsShareScreen.java index aee21ec8..eaca845a 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsShareScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsShareScreen.java @@ -31,7 +31,7 @@ public class WaypointsShareScreen extends AbstractWaypointsScreen { try { - List waypointCategories = Waypoints.fromSkytilsBase64(client.keyboard.getClipboard(), island); + List waypointCategories = Waypoints.fromSkytils(client.keyboard.getClipboard(), island); for (WaypointCategory waypointCategory : waypointCategories) { selectedWaypoints.addAll(waypointCategory.waypoints()); waypoints.put(waypointCategory.island(), waypointCategory); diff --git a/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java b/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java index 2f02b51f..5ba920c0 100644 --- a/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java +++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java @@ -6,11 +6,13 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; import com.mojang.serialization.codecs.RecordCodecBuilder; import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.utils.ColorUtils; import de.hysky.skyblocker.utils.render.RenderHelper; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; import net.minecraft.text.Text; import net.minecraft.text.TextCodecs; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.ColorHelper; import net.minecraft.util.math.Vec3d; import java.util.Objects; @@ -32,7 +34,7 @@ public class NamedWaypoint extends Waypoint { Codec.INT.fieldOf("y").forGetter(waypoint -> waypoint.pos.getY()), Codec.INT.fieldOf("z").forGetter(waypoint -> waypoint.pos.getZ()), Codec.either(Codec.STRING, Codec.INT).xmap(either -> either.map(str -> str, Object::toString), Either::left).fieldOf("name").forGetter(waypoint -> waypoint.name.getString()), - Codec.INT.fieldOf("color").forGetter(waypoint -> (int) (waypoint.alpha * 255) << 24 | (int) (waypoint.colorComponents[0] * 255) << 16 | (int) (waypoint.colorComponents[1] * 255) << 8 | (int) (waypoint.colorComponents[2] * 255)), + Codec.INT.optionalFieldOf("color", ColorHelper.Argb.getArgb(128, 0, 255, 0)).forGetter(waypoint -> (int) (waypoint.alpha * 255) << 24 | (int) (waypoint.colorComponents[0] * 255) << 16 | (int) (waypoint.colorComponents[1] * 255) << 8 | (int) (waypoint.colorComponents[2] * 255)), Codec.BOOL.fieldOf("enabled").forGetter(Waypoint::shouldRender) ).apply(instance, NamedWaypoint::fromSkytils)); public final Text name; @@ -69,7 +71,7 @@ public class NamedWaypoint extends Waypoint { if (alpha == 0) { alpha = DEFAULT_HIGHLIGHT_ALPHA; } - return new NamedWaypoint(new BlockPos(x, y, z), name, new float[]{((color & 0x00FF0000) >> 16) / 255f, ((color & 0x0000FF00) >> 8) / 255f, (color & 0x000000FF) / 255f}, alpha, enabled); + return new NamedWaypoint(new BlockPos(x, y, z), name, ColorUtils.getFloatComponents(color), alpha, enabled); } public NamedWaypoint copy() { -- cgit From 31e15f1b5bd664091461b5c17c2441b84a24d9eb Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sun, 7 Jul 2024 23:58:51 +0800 Subject: Fallback to current island when importing waypoints with invalid islands --- .../java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java | 11 +++++++---- .../de/hysky/skyblocker/utils/waypoint/WaypointCategory.java | 4 ++++ .../de/hysky/skyblocker/utils/waypoint/WaypointsTest.java | 6 +++--- 3 files changed, 14 insertions(+), 7 deletions(-) (limited to 'src/main/java/de') 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 ce559259..6866aba3 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java @@ -11,6 +11,7 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.JsonOps; import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.utils.Location; import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.scheduler.Scheduler; import de.hysky.skyblocker.utils.waypoint.WaypointCategory; @@ -87,19 +88,21 @@ public class Waypoints { return Collections.emptyList(); } - public static List fromSkytilsJson(String waypointCategories, String defaultIsland) { + public static List fromSkytilsJson(String waypointCategoriesString, String defaultIsland) { JsonArray waypointCategoriesJson; try { - waypointCategoriesJson = SkyblockerMod.GSON.fromJson(waypointCategories, JsonObject.class).getAsJsonArray("categories"); + waypointCategoriesJson = SkyblockerMod.GSON.fromJson(waypointCategoriesString, JsonObject.class).getAsJsonArray("categories"); } catch (JsonSyntaxException e) { + // Handle the case where there is only a single json list of waypoints and no category data. JsonObject waypointCategoryJson = new JsonObject(); waypointCategoryJson.addProperty("name", "New Category"); waypointCategoryJson.addProperty("island", defaultIsland); - waypointCategoryJson.add("waypoints", SkyblockerMod.GSON.fromJson(waypointCategories, JsonArray.class)); + waypointCategoryJson.add("waypoints", SkyblockerMod.GSON.fromJson(waypointCategoriesString, JsonArray.class)); waypointCategoriesJson = new JsonArray(); waypointCategoriesJson.add(waypointCategoryJson); } - return SKYTILS_CODEC.parse(JsonOps.INSTANCE, waypointCategoriesJson).resultOrPartial(LOGGER::error).orElseThrow(); + List waypointCategories = SKYTILS_CODEC.parse(JsonOps.INSTANCE, waypointCategoriesJson).resultOrPartial(LOGGER::error).orElseThrow(); + return waypointCategories.stream().map(waypointCategory -> Location.from(waypointCategory.island()) == Location.UNKNOWN ? waypointCategory.withIsland(defaultIsland) : waypointCategory).toList(); } public static String toSkytilsBase64(List waypointCategories) { diff --git a/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java b/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java index db2a6d82..8bfef7f4 100644 --- a/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java +++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java @@ -29,6 +29,10 @@ public record WaypointCategory(String name, String island, List w return new WaypointCategory(name, island(), waypoints()); } + public WaypointCategory withIsland(String island) { + return new WaypointCategory(name(), island, waypoints()); + } + public WaypointCategory deepCopy() { return new WaypointCategory(name(), island(), waypoints().stream().map(NamedWaypoint::copy).collect(Collectors.toList())); } diff --git a/src/test/java/de/hysky/skyblocker/utils/waypoint/WaypointsTest.java b/src/test/java/de/hysky/skyblocker/utils/waypoint/WaypointsTest.java index ed7b197e..cf6f56e9 100644 --- a/src/test/java/de/hysky/skyblocker/utils/waypoint/WaypointsTest.java +++ b/src/test/java/de/hysky/skyblocker/utils/waypoint/WaypointsTest.java @@ -133,12 +133,12 @@ public class WaypointsTest { @Test void testFromSkytilsV1Gzip() { String waypointsSkytilsV1Gzip = "(V1):H4sIAAAAAAAC/51STWuEMBD9KyHntJjJl7u3UugfsIdC2UOq6SKk0cZIaxf/e+OC4moL4i2TmXnvzby54FwHc658aRp8RK8X7PSHiS+ctXVtO5Tpd4OyugoNJvhLd3VVurAs/Wy1NzH/HaM7yinBXXwpSfDP8HNICDZOv1lTxDD41hCsi8IUDyHGVCp+4JLGPi57MoMN2gf07EvtznaCl+kCnQJsgGeRgfM5/BJ4UHkFVhNwukm34sD6E8FlY7UbKnHpmqBdHtvmhE+tsejRWLtpqaM8BPfJKDGlS4mMrSXmla38NSu4lDDYsVItEg5ws46XyT8QKxa5j4UBCEhuPN1xKluYIBVUsZt55oujbJxJTObu3BxPmAL51yGhkU2tL4nuJBMCgP57XKf+FzPhnyPAAwAA"; - List waypointCategories = Waypoints.fromSkytils(waypointsSkytilsV1Gzip, "hub"); - List expectedWaypointCategories = List.of(new WaypointCategory("Supply Safe Spots", "instanced", List.of( + List waypointCategories = Waypoints.fromSkytils(waypointsSkytilsV1Gzip, "default_island"); + List expectedWaypointCategories = List.of(new WaypointCategory("Supply Safe Spots", "default_island", List.of( new NamedWaypoint(new BlockPos(-141, 76, -90), "Square", new float[]{0, 1, 0}, 128 / 255f, true), new NamedWaypoint(new BlockPos(-68, 76, -122), "Start Triangle", new float[]{0, 1, 0}, 128 / 255f, true), new NamedWaypoint(new BlockPos(-90, 77, -128), "Triangle", new float[]{0, 1, 0}, 128 / 255f, true) - )), new WaypointCategory("Fuel Cell Safe Spots", "instanced", List.of( + )), new WaypointCategory("Fuel Cell Safe Spots", "default_island", List.of( new NamedWaypoint(new BlockPos(-81, 77, -133), "Triangle 2.0", new float[]{20 / 255f, 0, 1}, 1, true), new NamedWaypoint(new BlockPos(-125, 77, -136), "X", new float[]{20 / 255f, 0, 1}, 1, true), new NamedWaypoint(new BlockPos(-141, 76, -90), "Square", new float[]{20 / 255f, 0, 1}, 1, true), -- cgit