diff options
author | viciscat <51047087+viciscat@users.noreply.github.com> | 2024-07-04 01:03:45 +0200 |
---|---|---|
committer | viciscat <51047087+viciscat@users.noreply.github.com> | 2024-07-04 01:03:45 +0200 |
commit | ac61fe9c8166dc1fdc0dbd0f3bca3a8a0f5fb02a (patch) | |
tree | 5b13457849286f1dde37505cbe6b2806832735b4 /src/main | |
parent | 19e0fb096e427da5921009e8315aa0ac093b70ae (diff) | |
download | Skyblocker-ac61fe9c8166dc1fdc0dbd0f3bca3a8a0f5fb02a.tar.gz Skyblocker-ac61fe9c8166dc1fdc0dbd0f3bca3a8a0f5fb02a.tar.bz2 Skyblocker-ac61fe9c8166dc1fdc0dbd0f3bca3a8a0f5fb02a.zip |
mhm
Diffstat (limited to 'src/main')
3 files changed, 67 insertions, 11 deletions
diff --git a/src/main/java/de/hysky/skyblocker/events/SkyblockEvents.java b/src/main/java/de/hysky/skyblocker/events/SkyblockEvents.java index c268103d..93426143 100644 --- a/src/main/java/de/hysky/skyblocker/events/SkyblockEvents.java +++ b/src/main/java/de/hysky/skyblocker/events/SkyblockEvents.java @@ -26,6 +26,16 @@ public final class SkyblockEvents { } }); + /** + * Called when the player's Skyblock profile changes. + * @implNote This is called upon receiving the chat message for the profile change rather than the exact moment of profile change, so it may be delayed by a few seconds. + */ + public static final Event<ProfileChange> PROFILE_CHANGE = EventFactory.createArrayBacked(ProfileChange.class, callbacks -> (prev, profile) -> { + for (ProfileChange callback : callbacks) { + callback.onSkyblockProfileChange(prev, profile); + } + }); + @Environment(EnvType.CLIENT) @FunctionalInterface public interface SkyblockJoin { @@ -43,4 +53,10 @@ public final class SkyblockEvents { public interface SkyblockLocationChange { void onSkyblockLocationChange(Location location); } + + @Environment(EnvType.CLIENT) + @FunctionalInterface + public interface ProfileChange { + void onSkyblockProfileChange(String prevProfileId, String profileId); + } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/SkyblockInventoryScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/item/SkyblockInventoryScreen.java index dcadd366..8c800783 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/SkyblockInventoryScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/SkyblockInventoryScreen.java @@ -2,10 +2,13 @@ package de.hysky.skyblocker.skyblock.item; import com.mojang.serialization.Codec; import de.hysky.skyblocker.SkyblockerMod; +import de.hysky.skyblocker.events.SkyblockEvents; import de.hysky.skyblocker.mixins.accessors.SlotAccessor; import de.hysky.skyblocker.utils.ItemUtils; +import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.scheduler.MessageScheduler; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.ingame.InventoryScreen; import net.minecraft.client.util.math.MatrixStack; @@ -24,10 +27,12 @@ import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.BufferedWriter; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; /** @@ -44,29 +49,54 @@ public class SkyblockInventoryScreen extends InventoryScreen { private static final Identifier SLOT_TEXTURE = Identifier.ofVanilla("container/slot"); private static final Identifier EMPTY_SLOT = Identifier.of(SkyblockerMod.NAMESPACE, "equipment/empty_icon"); + private static final Path FOLDER = SkyblockerMod.CONFIG_DIR.resolve("equipment"); private final Slot[] equipmentSlots = new Slot[4]; - public static void initEquipment() { - ClientLifecycleEvents.CLIENT_STARTED.register(client1 -> { - Path resolve = SkyblockerMod.CONFIG_DIR.resolve("equipment.nbt"); + private static void save(String profileId) { + try { + Files.createDirectories(FOLDER); + } catch (IOException e) { + LOGGER.error("[Skyblocker] Failed to create folder for equipment!", e); + } + Path resolve = FOLDER.resolve(profileId + ".nbt"); + + try (BufferedWriter writer = Files.newBufferedWriter(resolve)) { + writer.write(new StringNbtWriter().apply(CODEC.encodeStart(NbtOps.INSTANCE, equipment).getOrThrow())); + } catch (Exception e) { + LOGGER.error("[Skyblocker] Failed to save Equipment data", e); + } + } + + private static void load(String profileId) { + Path resolve = FOLDER.resolve(profileId + ".nbt"); + CompletableFuture.supplyAsync(() -> { try (BufferedReader reader = Files.newBufferedReader(resolve)) { - ItemStack[] array = CODEC.parse( + return CODEC.parse( NbtOps.INSTANCE, StringNbtReader.parse(reader.lines().collect(Collectors.joining()))) .getOrThrow(); - System.arraycopy(array, 0, equipment, 0, Math.min(array.length, 4)); } catch (NoSuchFileException ignored) { } catch (Exception e) { LOGGER.error("[Skyblocker] Failed to load Equipment data", e); + } - }); + return null; + // Schedule on main thread to avoid any async weirdness + }).thenAccept(itemStacks -> MinecraftClient.getInstance().execute(() -> System.arraycopy(itemStacks, 0, equipment, 0, Math.min(itemStacks.length, 4)))); + + } + + public static void initEquipment() { + + SkyblockEvents.PROFILE_CHANGE.register(((prevProfileId, profileId) -> { + if (!prevProfileId.isEmpty()) save(prevProfileId); + load(profileId); + })); ClientLifecycleEvents.CLIENT_STOPPING.register(client1 -> { - Path resolve = SkyblockerMod.CONFIG_DIR.resolve("equipment.nbt"); - try (BufferedWriter writer = Files.newBufferedWriter(resolve)) { - writer.write(new StringNbtWriter().apply(CODEC.encodeStart(NbtOps.INSTANCE, equipment).getOrThrow())); - } catch (Exception e) { - LOGGER.error("[Skyblocker] Failed to save Equipment data", e); + String profileId = Utils.getProfileId(); + if (!profileId.isBlank()) { + save(profileId); } }); } @@ -93,6 +123,8 @@ public class SkyblockInventoryScreen extends InventoryScreen { return super.mouseClicked(mouseX, mouseY, button); } + private boolean canDrawTooltips = false; + @Override public void render(DrawContext context, int mouseX, int mouseY, float delta) { super.render(context, mouseX, mouseY, delta); @@ -104,10 +136,14 @@ public class SkyblockInventoryScreen extends InventoryScreen { if (isPointWithinBounds(equipmentSlot.x, equipmentSlot.y, 16, 16, mouseX, mouseY)) drawSlotHighlight(context, equipmentSlot.x, equipmentSlot.y, 0); } matrices.pop(); + canDrawTooltips = true; + drawMouseoverTooltip(context, mouseX, mouseY); + canDrawTooltips = false; } @Override protected void drawMouseoverTooltip(DrawContext context, int x, int y) { + if (!canDrawTooltips) return; super.drawMouseoverTooltip(context, x, y); if (!handler.getCursorStack().isEmpty()) return; for (Slot equipmentSlot : equipmentSlots) { diff --git a/src/main/java/de/hysky/skyblocker/utils/Utils.java b/src/main/java/de/hysky/skyblocker/utils/Utils.java index 7ee5ec38..19b07879 100644 --- a/src/main/java/de/hysky/skyblocker/utils/Utils.java +++ b/src/main/java/de/hysky/skyblocker/utils/Utils.java @@ -460,7 +460,11 @@ public class Utils { if (message.startsWith(PROFILE_MESSAGE_PREFIX)) { profile = message.substring(PROFILE_MESSAGE_PREFIX.length()).split("§b")[0]; } else if (message.startsWith(PROFILE_ID_PREFIX)) { + String prevProfileId = profileId; profileId = message.substring(PROFILE_ID_PREFIX.length()); + if (!prevProfileId.equals(profileId)) { + SkyblockEvents.PROFILE_CHANGE.invoker().onSkyblockProfileChange(prevProfileId, profileId); + } MuseumItemCache.tick(profileId); } |