diff options
author | Aaron <51387595+AzureAaron@users.noreply.github.com> | 2024-07-10 01:51:38 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-10 01:51:38 -0400 |
commit | 94abbb2b9d23238cd629d065aaf68fadf7fd7796 (patch) | |
tree | 9f024737c6adad2aaa3027152c395edc603bdaeb /src/main/java/de/hysky/skyblocker | |
parent | ef2ed5687455d6cdce2499108f0e65d8a8291273 (diff) | |
parent | 31e15f1b5bd664091461b5c17c2441b84a24d9eb (diff) | |
download | Skyblocker-94abbb2b9d23238cd629d065aaf68fadf7fd7796.tar.gz Skyblocker-94abbb2b9d23238cd629d065aaf68fadf7fd7796.tar.bz2 Skyblocker-94abbb2b9d23238cd629d065aaf68fadf7fd7796.zip |
Merge pull request #822 from kevinthegreat1/waypoints-skytils-fix
Fix waypoints Skytils v1 importing
Diffstat (limited to 'src/main/java/de/hysky/skyblocker')
4 files changed, 30 insertions, 14 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 18096117..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; @@ -21,11 +22,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 +37,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,37 +67,42 @@ public class Waypoints { } } - public static List<WaypointCategory> fromSkytilsBase64(String base64, String defaultIsland) { + public static List<WaypointCategory> fromSkytils(String waypointsString, String defaultIsland) { try { - if (base64.startsWith("<Skytils-Waypoint-Data>(V")) { - int version = Integer.parseInt(base64.substring(26, base64.indexOf(')'))); + if (waypointsString.startsWith("<Skytils-Waypoint-Data>(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(); } - public static List<WaypointCategory> fromSkytilsJson(String waypointCategories, String defaultIsland) { + public static List<WaypointCategory> 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<WaypointCategory> 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<WaypointCategory> waypointCategories) { 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<WaypointsScree GridWidget.Adder adder = gridWidget.createAdder(2); adder.add(ButtonWidget.builder(Text.translatable("skyblocker.waypoints.importWaypointsSkytils"), buttonImport -> { try { - List<WaypointCategory> waypointCategories = Waypoints.fromSkytilsBase64(client.keyboard.getClipboard(), island); + List<WaypointCategory> 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() { 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<NamedWaypoint> 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())); } |