Skip to content

Commit

Permalink
Support waypoints list without category
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed Mar 30, 2024
1 parent 01b8c80 commit 5771a41
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.kevinthegreat.skyblockmod.SkyblockMod;
import com.mojang.brigadier.Command;
import com.mojang.serialization.Codec;
Expand Down Expand Up @@ -59,16 +60,16 @@ public static void loadWaypoints() {
}
}

public static List<WaypointCategory> fromSkytilsBase64(String base64) {
public static List<WaypointCategory> fromSkytilsBase64(String base64, String defaultIsland) {
try {
if (base64.startsWith("<Skytils-Waypoint-Data>(V")) {
int version = Integer.parseInt(base64.substring(26, base64.indexOf(')')));
if (version == 1) {
return fromSkytilsJson(new String(Base64.getDecoder().decode(base64.substring(base64.indexOf(':') + 1))));
return fromSkytilsJson(new String(Base64.getDecoder().decode(base64.substring(base64.indexOf(':') + 1))), defaultIsland);
} else {
LOGGER.error("[Skyblocker Waypoints] Unknown Skytils waypoint data version: " + version);
}
} else return fromSkytilsJson(new String(Base64.getDecoder().decode(base64)));
} else return fromSkytilsJson(new String(Base64.getDecoder().decode(base64)), defaultIsland);
} catch (NumberFormatException e) {
LOGGER.error("[Skyblocker Waypoints] Encountered exception while parsing Skytils waypoint data version", e);
} catch (IllegalArgumentException e) {
Expand All @@ -77,8 +78,19 @@ public static List<WaypointCategory> fromSkytilsBase64(String base64) {
return Collections.emptyList();
}

public static List<WaypointCategory> fromSkytilsJson(String waypointCategories) {
return SKYTILS_CODEC.parse(JsonOps.INSTANCE, SkyblockMod.GSON.fromJson(waypointCategories, JsonObject.class).getAsJsonArray("categories")).resultOrPartial(LOGGER::error).orElseThrow();
public static List<WaypointCategory> fromSkytilsJson(String waypointCategories, String defaultIsland) {
JsonArray waypointCategoriesJson;
try {
waypointCategoriesJson = SkyblockMod.GSON.fromJson(waypointCategories, JsonObject.class).getAsJsonArray("categories");
} catch (JsonSyntaxException e) {
JsonObject waypointCategoryJson = new JsonObject();
waypointCategoryJson.addProperty("name", "New Category");
waypointCategoryJson.addProperty("island", defaultIsland);
waypointCategoryJson.add("waypoints", SkyblockMod.GSON.fromJson(waypointCategories, JsonArray.class));
waypointCategoriesJson = new JsonArray();
waypointCategoriesJson.add(waypointCategoryJson);
}
return SKYTILS_CODEC.parse(JsonOps.INSTANCE, waypointCategoriesJson).resultOrPartial(LOGGER::error).orElseThrow();
}

public static String toSkytilsBase64(List<WaypointCategory> waypointCategories) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected void init() {
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());
List<WaypointCategory> waypointCategories = Waypoints.fromSkytilsBase64(client.keyboard.getClipboard(), island);
for (WaypointCategory waypointCategory : waypointCategories) {
selectedWaypoints.addAll(waypointCategory.waypoints());
waypoints.put(waypointCategory.island(), waypointCategory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class WaypointsTest {
@Test
void testFromSkytilsBase64() {
String waypointCategoriesSkytilsBase64 = "eyJjYXRlZ29yaWVzIjpbeyJuYW1lIjoiY2F0ZWdvcnkiLCJ3YXlwb2ludHMiOlt7Im5hbWUiOiJ3YXlwb2ludCIsIngiOjAsInkiOjAsInoiOjAsImVuYWJsZWQiOmZhbHNlLCJjb2xvciI6LTg3MjM4MjIwOSwiYWRkZWRBdCI6MX0seyJuYW1lIjoxLCJ4IjotMSwieSI6MCwieiI6MSwiZW5hYmxlZCI6dHJ1ZSwiY29sb3IiOjAsImFkZGVkQXQiOjF9XSwiaXNsYW5kIjoiaHViIn1dfQ==";
List<WaypointCategory> waypointCategories = Waypoints.fromSkytilsBase64(waypointCategoriesSkytilsBase64);
List<WaypointCategory> waypointCategories = Waypoints.fromSkytilsBase64(waypointCategoriesSkytilsBase64, "");
List<WaypointCategory> expectedWaypointCategories = List.of(new WaypointCategory("category", "hub", List.of(new NamedWaypoint(BlockPos.ORIGIN, "waypoint", new float[]{0f, 0.5019608f, 1f}, 0.8f, false), new NamedWaypoint(new BlockPos(-1, 0, 1), "1", new float[]{0f, 0f, 0f}, true))));

Assertions.assertEquals(expectedWaypointCategories, waypointCategories);
Expand Down

0 comments on commit 5771a41

Please sign in to comment.