From 027426aacac048b85cd310e9e3d4101e0156a917 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Mon, 22 May 2023 20:30:12 -0400 Subject: Add Fairy Souls Helper --- .../xmrvizzy/skyblocker/skyblock/FairySouls.java | 184 +++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java (limited to 'src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java') diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java new file mode 100644 index 00000000..4480c5e1 --- /dev/null +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java @@ -0,0 +1,184 @@ +package me.xmrvizzy.skyblocker.skyblock; + +import com.google.common.collect.ImmutableSet; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import me.xmrvizzy.skyblocker.SkyblockerMod; +import me.xmrvizzy.skyblocker.config.SkyblockerConfig; +import me.xmrvizzy.skyblocker.utils.NEURepo; +import me.xmrvizzy.skyblocker.utils.RenderHelper; +import me.xmrvizzy.skyblocker.utils.Utils; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; +import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents; +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; +import net.minecraft.client.MinecraftClient; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.text.Text; +import net.minecraft.util.DyeColor; +import net.minecraft.util.math.BlockPos; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.util.*; +import java.util.concurrent.CompletableFuture; + +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + +public class FairySouls { + private static final Logger LOGGER = LoggerFactory.getLogger(FairySouls.class); + private static CompletableFuture fairySoulsLoaded; + private static final Map> fairySouls = new HashMap<>(); + private static final Map>> foundFairies = new HashMap<>(); + + public static void init() { + fairySoulsLoaded = NEURepo.runAsyncAfterLoad(() -> { + try { + BufferedReader reader = new BufferedReader(new FileReader(NEURepo.LOCAL_REPO_DIR.resolve("constants").resolve("fairy_souls.json").toFile())); + for (Map.Entry fairySoulJson : JsonParser.parseReader(reader).getAsJsonObject().asMap().entrySet()) { + if (fairySoulJson.getKey().equals("//") || fairySoulJson.getKey().equals("Max Souls")) { + continue; + } + ImmutableSet.Builder fairySoulsForLocation = ImmutableSet.builder(); + for (JsonElement fairySoul : fairySoulJson.getValue().getAsJsonArray().asList()) { + fairySoulsForLocation.add(parseBlockPos(fairySoul)); + } + fairySouls.put(fairySoulJson.getKey(), fairySoulsForLocation.build()); + } + reader = new BufferedReader(new FileReader(SkyblockerMod.CONFIG_DIR.resolve("found_fairy_souls.json").toFile())); + for (Map.Entry foundFairiesForProfileJson : JsonParser.parseReader(reader).getAsJsonObject().asMap().entrySet()) { + Map> foundFairiesForProfile = new HashMap<>(); + for (Map.Entry foundFairiesForLocationJson : foundFairiesForProfileJson.getValue().getAsJsonObject().asMap().entrySet()) { + Set foundFairiesForLocation = new HashSet<>(); + for (JsonElement foundFairy : foundFairiesForLocationJson.getValue().getAsJsonArray().asList()) { + foundFairiesForLocation.add(parseBlockPos(foundFairy)); + } + foundFairiesForProfile.put(foundFairiesForLocationJson.getKey(), foundFairiesForLocation); + } + foundFairies.put(foundFairiesForProfileJson.getKey(), foundFairiesForProfile); + } + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + LOGGER.error("Failed to load found fairy souls."); + } catch (Exception e) { + e.printStackTrace(); + } + }); + ClientLifecycleEvents.CLIENT_STOPPING.register(FairySouls::saveFoundFairySouls); + WorldRenderEvents.AFTER_TRANSLUCENT.register(FairySouls::render); + ClientReceiveMessageEvents.GAME.register(FairySouls::onChatMessage); + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(literal(SkyblockerMod.NAMESPACE) + .then(literal("fairySouls") + .then(literal("markAllInCurrentIslandFound").executes(context -> { + FairySouls.markAllFairiesFound(); + context.getSource().sendFeedback(Text.translatable("skyblocker.fairySouls.markAllFound")); + return 1; + })) + .then(literal("markAllInCurrentIslandMissing").executes(context -> { + FairySouls.markAllFairiesNotFound(); + context.getSource().sendFeedback(Text.translatable("skyblocker.fairySouls.markAllMissing")); + return 1; + }))))); + } + + private static BlockPos parseBlockPos(JsonElement posJson) { + String[] posArray = posJson.getAsString().split(","); + return new BlockPos(Integer.parseInt(posArray[0]), Integer.parseInt(posArray[1]), Integer.parseInt(posArray[2])); + } + + public static void saveFoundFairySouls(MinecraftClient client) { + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(SkyblockerMod.CONFIG_DIR.resolve("found_fairy_souls.json").toFile())); + JsonObject foundFairiesJson = new JsonObject(); + for (Map.Entry>> foundFairiesForProfile : foundFairies.entrySet()) { + JsonObject foundFairiesForProfileJson = new JsonObject(); + for (Map.Entry> foundFairiesForLocation : foundFairiesForProfile.getValue().entrySet()) { + JsonArray foundFairiesForLocationJson = new JsonArray(); + for (BlockPos foundFairy : foundFairiesForLocation.getValue()) { + foundFairiesForLocationJson.add(foundFairy.getX() + "," + foundFairy.getY() + "," + foundFairy.getZ()); + } + foundFairiesForProfileJson.add(foundFairiesForLocation.getKey(), foundFairiesForLocationJson); + } + foundFairiesJson.add(foundFairiesForProfile.getKey(), foundFairiesForProfileJson); + } + SkyblockerMod.GSON.toJson(foundFairiesJson, writer); + writer.close(); + } catch (IOException e) { + LOGGER.error("Failed to write found fairy souls to file."); + } + } + + public static void render(WorldRenderContext context) { + if (!SkyblockerConfig.get().general.fairySouls.enableFairySouls) { + return; + } + if (!fairySoulsLoaded.isDone()) { + LOGGER.warn("Fairy souls are not loaded yet."); + return; + } + if (!fairySouls.containsKey(Utils.getLocationRaw())) { + return; + } + for (BlockPos fairySoul : fairySouls.get(Utils.getLocationRaw())) { + float[] colorComponents = isFairySoulNotFound(fairySoul) ? DyeColor.GREEN.getColorComponents() : DyeColor.RED.getColorComponents(); + RenderHelper.renderFilledThroughWallsWithBeaconBeam(context, fairySoul, colorComponents, 0.5F); + } + } + + private static boolean isFairySoulNotFound(BlockPos fairySoul) { + Map> foundFairiesForProfile = foundFairies.get(Utils.getProfile()); + if (foundFairiesForProfile == null) { + return true; + } + Set foundFairiesForProfileAndLocation = foundFairiesForProfile.get(Utils.getLocationRaw()); + if (foundFairiesForProfileAndLocation == null) { + return true; + } + return !foundFairiesForProfileAndLocation.contains(fairySoul); + } + + public static void onChatMessage(Text text, boolean overlay) { + String message = text.getString(); + if (message.equals("You have already found that Fairy Soul!") || message.equals("SOUL! You found a Fairy Soul!")) { + markClosestFairyFound(); + } + } + + private static void markClosestFairyFound() { + PlayerEntity player = MinecraftClient.getInstance().player; + if (player == null) { + LOGGER.warn("Failed to mark closest fairy soul as found because player is null."); + return; + } + fairySouls.get(Utils.getLocationRaw()).stream().filter(FairySouls::isFairySoulNotFound).min(Comparator.comparingDouble(fairySoul -> fairySoul.getSquaredDistance(player.getPos()))).ifPresent(fairySoul -> { + initializeFoundFairiesForCurrentProfileAndLocation(); + foundFairies.get(Utils.getProfile()).get(Utils.getLocationRaw()).add(fairySoul); + }); + } + + public static void markAllFairiesFound() { + initializeFoundFairiesForCurrentProfileAndLocation(); + foundFairies.get(Utils.getProfile()).get(Utils.getLocationRaw()).addAll(fairySouls.get(Utils.getLocationRaw())); + } + + public static void markAllFairiesNotFound() { + Map> foundFairiesForProfile = foundFairies.get(Utils.getProfile()); + if (foundFairiesForProfile != null) { + foundFairiesForProfile.remove(Utils.getLocationRaw()); + } + } + + private static void initializeFoundFairiesForCurrentProfileAndLocation() { + initializeFoundFairiesForProfileAndLocation(Utils.getProfile(), Utils.getLocationRaw()); + } + + private static void initializeFoundFairiesForProfileAndLocation(String profile, String location) { + foundFairies.computeIfAbsent(profile, profileKey -> new HashMap<>()); + foundFairies.get(profile).computeIfAbsent(location, locationKey -> new HashSet<>()); + } +} -- cgit From d1bdb78708bfd7f289b59e17dd0b225c1aedd9a2 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Thu, 25 May 2023 20:13:03 -0400 Subject: Fix fairy souls option name --- src/main/java/me/xmrvizzy/skyblocker/config/SkyblockerConfig.java | 2 +- src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java') diff --git a/src/main/java/me/xmrvizzy/skyblocker/config/SkyblockerConfig.java b/src/main/java/me/xmrvizzy/skyblocker/config/SkyblockerConfig.java index f296e487..31887a90 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/config/SkyblockerConfig.java +++ b/src/main/java/me/xmrvizzy/skyblocker/config/SkyblockerConfig.java @@ -224,7 +224,7 @@ public class SkyblockerConfig implements ConfigData { } public static class FairySouls { - public boolean enableFairySouls = true; + public boolean enableFairySoulsHelper = false; } public static class Hitbox { diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java index 4480c5e1..ff3c7fae 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java @@ -114,7 +114,7 @@ public class FairySouls { } public static void render(WorldRenderContext context) { - if (!SkyblockerConfig.get().general.fairySouls.enableFairySouls) { + if (!SkyblockerConfig.get().general.fairySouls.enableFairySoulsHelper) { return; } if (!fairySoulsLoaded.isDone()) { -- cgit From a0d504d9e2ffa95bbd2d9d99e546a44572417de9 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Wed, 31 May 2023 08:31:57 +0800 Subject: Updated NEU repo error handling --- src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java | 5 ++--- .../me/xmrvizzy/skyblocker/skyblock/itemlist/ItemRegistry.java | 4 +++- src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java | 7 +++++-- 3 files changed, 10 insertions(+), 6 deletions(-) (limited to 'src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java') diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java index ff3c7fae..838b2a55 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java @@ -63,10 +63,9 @@ public class FairySouls { } reader.close(); } catch (IOException e) { - e.printStackTrace(); - LOGGER.error("Failed to load found fairy souls."); + LOGGER.error("Failed to load found fairy souls", e); } catch (Exception e) { - e.printStackTrace(); + LOGGER.error("Encountered unknown exception loading fairy souls", e); } }); ClientLifecycleEvents.CLIENT_STOPPING.register(FairySouls::saveFoundFairySouls); diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/itemlist/ItemRegistry.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/itemlist/ItemRegistry.java index 13ca356a..dc63e351 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/itemlist/ItemRegistry.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/itemlist/ItemRegistry.java @@ -36,7 +36,9 @@ public class ItemRegistry { File dir = ITEM_LIST_DIR.toFile(); File[] files = dir.listFiles(); - assert files != null; + if (files == null) { + return; + } for (File file : files) { Path path = ITEM_LIST_DIR.resolve(file.getName()); try { diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java b/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java index 0de6cecd..5254feb1 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/NEURepo.java @@ -7,6 +7,7 @@ import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallba import net.minecraft.client.MinecraftClient; import net.minecraft.text.Text; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.TransportException; import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,11 +53,13 @@ public class NEURepo { Git.cloneRepository().setURI(REMOTE_REPO_URL).setDirectory(NEURepo.LOCAL_REPO_DIR.toFile()).setBranchesToClone(List.of("refs/heads/master")).setBranch("refs/heads/master").call().close(); LOGGER.info("[Skyblocker] NEU Repository Downloaded"); } + } catch (TransportException e){ + LOGGER.error("[Skyblocker] Transport operation failed. Most likely unable to connect to the remote NEU repo on github", e); } catch (RepositoryNotFoundException e) { - LOGGER.warn("Local NEU Repository not found or corrupted, downloading new one", e); + LOGGER.warn("[Skyblocker] Local NEU Repository not found or corrupted, downloading new one", e); deleteAndDownloadRepository(); } catch (Exception e) { - LOGGER.error("Encountered unknown exception while initializing NEU Repository", e); + LOGGER.error("[Skyblocker] Encountered unknown exception while initializing NEU Repository", e); } }); } -- cgit From 8dbfca923398ce1c5c279fcdd07371d4624b2f5e Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sat, 17 Jun 2023 12:55:11 +0800 Subject: Refactor rendering checks --- .../me/xmrvizzy/skyblocker/skyblock/FairySouls.java | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) (limited to 'src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java') diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java index 838b2a55..ccffdcca 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/FairySouls.java @@ -113,19 +113,11 @@ public class FairySouls { } public static void render(WorldRenderContext context) { - if (!SkyblockerConfig.get().general.fairySouls.enableFairySoulsHelper) { - return; - } - if (!fairySoulsLoaded.isDone()) { - LOGGER.warn("Fairy souls are not loaded yet."); - return; - } - if (!fairySouls.containsKey(Utils.getLocationRaw())) { - return; - } - for (BlockPos fairySoul : fairySouls.get(Utils.getLocationRaw())) { - float[] colorComponents = isFairySoulNotFound(fairySoul) ? DyeColor.GREEN.getColorComponents() : DyeColor.RED.getColorComponents(); - RenderHelper.renderFilledThroughWallsWithBeaconBeam(context, fairySoul, colorComponents, 0.5F); + if (SkyblockerConfig.get().general.fairySouls.enableFairySoulsHelper && fairySoulsLoaded.isDone() && fairySouls.containsKey(Utils.getLocationRaw())) { + for (BlockPos fairySoul : fairySouls.get(Utils.getLocationRaw())) { + float[] colorComponents = isFairySoulNotFound(fairySoul) ? DyeColor.GREEN.getColorComponents() : DyeColor.RED.getColorComponents(); + RenderHelper.renderFilledThroughWallsWithBeaconBeam(context, fairySoul, colorComponents, 0.5F); + } } } -- cgit