diff options
Diffstat (limited to 'src/main/java/me/xmrvizzy/skyblocker/skyblock/BackpackPreview.java')
-rw-r--r-- | src/main/java/me/xmrvizzy/skyblocker/skyblock/BackpackPreview.java | 132 |
1 files changed, 80 insertions, 52 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/BackpackPreview.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/BackpackPreview.java index 2638b0a6..123a4dab 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/BackpackPreview.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/BackpackPreview.java @@ -9,6 +9,7 @@ import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.ingame.HandledScreen; +import net.minecraft.client.network.PlayerListEntry; import net.minecraft.client.render.item.ItemRenderer; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.entity.player.PlayerEntity; @@ -18,7 +19,9 @@ import net.minecraft.nbt.*; import net.minecraft.util.Identifier; import java.io.File; +import java.nio.file.Path; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -26,82 +29,94 @@ import java.util.regex.Pattern; public class BackpackPreview extends DrawableHelper { private static final Identifier TEXTURE = new Identifier(SkyblockerMod.NAMESPACE, "textures/gui/inventory_background.png"); private static final BackpackPreview instance = new BackpackPreview(); + private static final Pattern PROFILE_PATTERN = Pattern.compile("Profile: ([a-zA-Z]+)"); private static final Pattern ECHEST_PATTERN = Pattern.compile("Ender Chest.*\\((\\d+)/\\d+\\)"); private static final Pattern BACKPACK_PATTERN = Pattern.compile("Backpack.*\\((\\d+)/\\d+\\)"); private static final int STORAGE_SIZE = 27; private static final Inventory[] storage = new Inventory[STORAGE_SIZE]; private static final boolean[] dirty = new boolean[STORAGE_SIZE]; - private static boolean loaded = false; + + private static String loaded = ""; // uuid + sb profile currently loaded + private static Path save_dir = null; public static void tick() { if (Utils.isOnSkyblock) { - for (int index = 0; index < STORAGE_SIZE; ++index) - if (dirty[index]) saveStorage(index); - if (MinecraftClient.getInstance().currentScreen != null) { - String title = MinecraftClient.getInstance().currentScreen.getTitle().getString(); - int index = getStorageIndexFromTitle(title); - if (index != -1) dirty[index] = true; + // save all dirty storages + saveStorage(); + // update save dir based on uuid and sb profile + String uuid = MinecraftClient.getInstance().getSession().getUuid().replaceAll("-", ""); + String profile = getSkyblockProfile(); + if (uuid != null && profile != null) { + save_dir = FabricLoader.getInstance().getConfigDir().resolve("skyblocker/backpack-preview/" + uuid + "/" + profile); + save_dir.toFile().mkdirs(); + if (loaded.equals(uuid + "/" + profile)) { + // mark currently opened storage as dirty + if (MinecraftClient.getInstance().currentScreen != null) { + String title = MinecraftClient.getInstance().currentScreen.getTitle().getString(); + int index = getStorageIndexFromTitle(title); + if (index != -1) dirty[index] = true; + } + } else { + // load storage again because uuid/profile changed + loaded = uuid + "/" + profile; + loadStorage(); + } } } } - private static File getSaveDir() { - String uuid = MinecraftClient.getInstance().getSession().getUuid(); - File dir = FabricLoader.getInstance().getConfigDir().resolve("skyblocker/backpack-preview/" + uuid).toFile(); - dir.mkdirs(); - return dir; - } - - public static void loadStorage(HandledScreen screen) { - if (!loaded) { - String title = screen.getTitle().getString(); - if (title.equals("Storage")) { - for (int index = 0; index < STORAGE_SIZE; ++index) { - File file = new File(getSaveDir().getPath(), index + ".nbt"); - if (file.isFile()) { - try { - NbtCompound root = NbtIo.read(file); - storage[index] = new DummyInventory(root); - } catch (Exception e) { - e.printStackTrace(); - } - } + public static void loadStorage() { + assert(save_dir != null); + for (int index = 0; index < STORAGE_SIZE; ++index) { + storage[index] = null; + dirty[index] = false; + File file = save_dir.resolve(index + ".nbt").toFile(); + if (file.isFile()) { + try { + NbtCompound root = NbtIo.read(file); + storage[index] = new DummyInventory(root); + } catch (Exception e) { + e.printStackTrace(); } - loaded = true; } } } - private static void saveStorage(int index) { - File file = new File(getSaveDir().getPath(), index + ".nbt"); - if (storage[index] != null) { - try { - NbtCompound root = new NbtCompound(); - NbtList list = new NbtList(); - for (int i = 9; i < storage[index].size(); ++i) { - ItemStack stack = storage[index].getStack(i); - NbtCompound item = new NbtCompound(); - if (stack.isEmpty()) { - item.put("id", NbtString.of("minecraft:air")); - item.put("Count", NbtInt.of(1)); - } else { - item.put("id", NbtString.of(stack.getItem().toString())); - item.put("Count", NbtInt.of(stack.getCount())); - item.put("tag", stack.getNbt()); + private static void saveStorage() { + assert(save_dir != null); + for (int index = 0; index < STORAGE_SIZE; ++index) { + if (dirty[index]) { + if (storage[index] != null) { + try { + NbtCompound root = new NbtCompound(); + NbtList list = new NbtList(); + for (int i = 9; i < storage[index].size(); ++i) { + ItemStack stack = storage[index].getStack(i); + NbtCompound item = new NbtCompound(); + if (stack.isEmpty()) { + item.put("id", NbtString.of("minecraft:air")); + item.put("Count", NbtInt.of(1)); + } else { + item.put("id", NbtString.of(stack.getItem().toString())); + item.put("Count", NbtInt.of(stack.getCount())); + item.put("tag", stack.getNbt()); + } + list.add(item); + } + root.put("list", list); + root.put("size", NbtInt.of(storage[index].size() - 9)); + NbtIo.write(root, save_dir.resolve(index + ".nbt").toFile()); + dirty[index] = false; + } catch (Exception e) { + e.printStackTrace(); } - list.add(item); } - root.put("list", list); - root.put("size", NbtInt.of(storage[index].size() - 9)); - NbtIo.write(root, file); - } catch (Exception e) { - e.printStackTrace(); } } } - public static void updateStorage(HandledScreen screen) { + public static void updateStorage(HandledScreen<?> screen) { String title = screen.getTitle().getString(); int index = getStorageIndexFromTitle(title); if (index != -1) { @@ -151,6 +166,19 @@ public class BackpackPreview extends DrawableHelper { if (backpack.find()) return Integer.parseInt(backpack.group(1)) + 8; return -1; } + + private static String getSkyblockProfile() { + Collection<PlayerListEntry> list = MinecraftClient.getInstance().getNetworkHandler().getPlayerList(); + for (PlayerListEntry entry : list) { + if (entry.getDisplayName() != null) { + Matcher matcher = PROFILE_PATTERN.matcher(entry.getDisplayName().getString()); + if (matcher.find()) { + return matcher.group(1); + } + } + } + return null; + } } class DummyInventory implements Inventory { |