diff options
4 files changed, 39 insertions, 24 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 89c3f051..8eeae2aa 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java @@ -29,6 +29,7 @@ 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(); @@ -48,16 +49,22 @@ public class Waypoints { } } - public static Collection<WaypointCategory> fromSkytilsBase64(String base64) { + public static List<WaypointCategory> fromSkytilsBase64(String base64) { return fromSkytilsJson(new String(Base64.getDecoder().decode(base64))); } - public static Collection<WaypointCategory> fromSkytilsJson(String waypointCategories) { - JsonObject waypointCategoriesJson = SkyblockerMod.GSON.fromJson(waypointCategories, JsonObject.class); - return waypointCategoriesJson.getAsJsonArray("categories").asList().stream() - .map(JsonObject.class::cast) - .map(WaypointCategory::fromSkytilsJson) - .toList(); + 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) { 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 db95634b..f959de78 100644 --- a/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java +++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java @@ -1,12 +1,11 @@ package de.hysky.skyblocker.utils.waypoint; import com.google.common.primitives.Floats; -import com.google.gson.JsonObject; +import com.mojang.datafixers.util.Either; 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.PosUtils; import de.hysky.skyblocker.utils.render.RenderHelper; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; import net.minecraft.text.Text; @@ -26,6 +25,14 @@ public class NamedWaypoint extends Waypoint { ).fieldOf("colorComponents").forGetter(secretWaypoint -> secretWaypoint.colorComponents), Codec.BOOL.fieldOf("shouldRender").forGetter(Waypoint::shouldRender) ).apply(instance, NamedWaypoint::new)); + public static final Codec<NamedWaypoint> SKYTILS_CODEC = RecordCodecBuilder.create(instance -> instance.group( + Codec.INT.fieldOf("x").forGetter(waypoint -> waypoint.pos.getX()), + 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.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)); protected final Text name; protected final Vec3d centerPos; @@ -55,9 +62,8 @@ public class NamedWaypoint extends Waypoint { this.centerPos = pos.toCenterPos(); } - public static NamedWaypoint fromSkytilsJson(JsonObject waypointJson) { - int color = waypointJson.get("color").getAsInt(); - return new NamedWaypoint(PosUtils.parsePosJson(waypointJson), waypointJson.get("name").getAsString(), () -> SkyblockerConfigManager.get().general.waypoints.waypointType, new float[]{((color & 0x00FF0000) >> 16) / 255f, ((color & 0x0000FF00) >> 8) / 255f, (color & 0x000000FF) / 255f}, waypointJson.get("enabled").getAsBoolean()); + public static NamedWaypoint fromSkytils(int x, int y, int z, String name, int color, boolean enabled) { + return new NamedWaypoint(new BlockPos(x, y, z), name, new float[]{((color & 0x00FF0000) >> 16) / 255f, ((color & 0x0000FF00) >> 8) / 255f, (color & 0x000000FF) / 255f}, enabled); } public Text getName() { 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 d31455f6..b1ac6135 100644 --- a/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java +++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java @@ -1,6 +1,5 @@ package de.hysky.skyblocker.utils.waypoint; -import com.google.gson.JsonObject; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; @@ -14,22 +13,16 @@ public record WaypointCategory(String name, String island, List<NamedWaypoint> w Codec.STRING.fieldOf("island").forGetter(WaypointCategory::island), NamedWaypoint.CODEC.listOf().fieldOf("waypoints").forGetter(WaypointCategory::waypoints) ).apply(instance, WaypointCategory::new)); + public static final Codec<WaypointCategory> SKYTILS_CODEC = RecordCodecBuilder.create(instance -> instance.group( + Codec.STRING.fieldOf("name").forGetter(WaypointCategory::name), + Codec.STRING.fieldOf("island").forGetter(WaypointCategory::island), + NamedWaypoint.SKYTILS_CODEC.listOf().fieldOf("waypoints").forGetter(WaypointCategory::waypoints) + ).apply(instance, WaypointCategory::new)); public WaypointCategory(WaypointCategory waypointCategory) { this(waypointCategory.name(), waypointCategory.island(), new ArrayList<>(waypointCategory.waypoints())); } - public static WaypointCategory fromSkytilsJson(JsonObject waypointCategory) { - return new WaypointCategory( - waypointCategory.get("name").getAsString(), - waypointCategory.get("island").getAsString(), - waypointCategory.getAsJsonArray("waypoints").asList().stream() - .map(JsonObject.class::cast) - .map(NamedWaypoint::fromSkytilsJson) - .toList() - ); - } - public void render(WorldRenderContext context) { for (NamedWaypoint waypoint : waypoints) { waypoint.render(context); 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 91470224..ad237718 100644 --- a/src/test/java/de/hysky/skyblocker/utils/waypoint/WaypointsTest.java +++ b/src/test/java/de/hysky/skyblocker/utils/waypoint/WaypointsTest.java @@ -17,4 +17,13 @@ public class WaypointsTest { Assertions.assertEquals(expectedWaypointCategories, waypointCategories); } + + @Test + void testToSkytilsBase64() { + Collection<WaypointCategory> waypointCategories = List.of(new WaypointCategory("category", "hub", List.of(new NamedWaypoint(BlockPos.ORIGIN, "waypoint", new float[]{0f, 0.5f, 1f}, false), new NamedWaypoint(new BlockPos(-1, 0, 1), "1", new float[]{0f, 0f, 0f}, true)))); + String waypointCategoriesSkytilsBase64 = Waypoints.toSkytilsBase64(waypointCategories); + String expectedWaypointCategoriesSkytilsBase64 = "ewogICJjYXRlZ29yaWVzIjogWwogICAgewogICAgICAibmFtZSI6ICJjYXRlZ29yeSIsCiAgICAgICJpc2xhbmQiOiAiaHViIiwKICAgICAgIndheXBvaW50cyI6IFsKICAgICAgICB7CiAgICAgICAgICAibmFtZSI6ICJ3YXlwb2ludCIsCiAgICAgICAgICAiY29sb3IiOiAzMjc2NywKICAgICAgICAgICJlbmFibGVkIjogZmFsc2UsCiAgICAgICAgICAieCI6IDAsCiAgICAgICAgICAieSI6IDAsCiAgICAgICAgICAieiI6IDAKICAgICAgICB9LAogICAgICAgIHsKICAgICAgICAgICJuYW1lIjogIjEiLAogICAgICAgICAgImNvbG9yIjogMCwKICAgICAgICAgICJlbmFibGVkIjogdHJ1ZSwKICAgICAgICAgICJ4IjogLTEsCiAgICAgICAgICAieSI6IDAsCiAgICAgICAgICAieiI6IDEKICAgICAgICB9CiAgICAgIF0KICAgIH0KICBdCn0="; + + Assertions.assertEquals(expectedWaypointCategoriesSkytilsBase64, waypointCategoriesSkytilsBase64); + } } |