aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2024-03-08 19:35:56 -0500
committerKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2024-05-24 19:51:46 -0400
commit7675f7569b381c8b9cbf36e2e69e716737455069 (patch)
tree30203444081474886e3741f0fea2fb378dbb3109 /src/main/java
parentf6ba429409ac73ee45992fd80e527c96b39e52e3 (diff)
downloadSkyblocker-7675f7569b381c8b9cbf36e2e69e716737455069.tar.gz
Skyblocker-7675f7569b381c8b9cbf36e2e69e716737455069.tar.bz2
Skyblocker-7675f7569b381c8b9cbf36e2e69e716737455069.zip
Update Skytils format to codecs
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java21
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java16
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java17
3 files changed, 30 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);