aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/xmrvizzy/skyblocker/skyblock/BackpackPreview.java
blob: 53a48a04c22ab8c719dc48001f4b296876676894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package me.xmrvizzy.skyblocker.skyblock;

import com.mojang.blaze3d.systems.RenderSystem;
import me.xmrvizzy.skyblocker.SkyblockerMod;
import me.xmrvizzy.skyblocker.utils.Utils;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
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.render.item.ItemRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.*;
import net.minecraft.util.Identifier;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
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 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;

    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;
            }
        }
    }

    private static File getSaveDir() {
        String uuid = MinecraftClient.getInstance().player.getUuidAsString();
        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();
                        }
                    }
                }
                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());
                    }
                    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) {
        String title = screen.getTitle().getString();
        int index = getStorageIndexFromTitle(title);
        if (index != -1) {
            storage[index] = ((HandledScreen<?>)screen).getScreenHandler().slots.get(0).inventory;
            dirty[index] = true;
        }
    }

    public static boolean renderPreview(MatrixStack matrices, int index, int mouseX, int mouseY) {
        if (index >= 9 && index < 18) index -= 9;
        else if (index >= 27 && index < 45) index -= 18;
        else return false;

        if (storage[index] == null) return false;
        int rows = (storage[index].size() - 9) / 9;

        Screen screen = MinecraftClient.getInstance().currentScreen;
        int x = mouseX + 184 >= screen.width ? mouseX - 188 : mouseX + 8;
        int y = Math.max(0, mouseY - 16);

        RenderSystem.disableDepthTest();
        RenderSystem.setShaderTexture(0, TEXTURE);
        instance.drawTexture(matrices, x, y, 0, 0, 176, 7);
        for (int i = 0; i < rows; ++i)
            instance.drawTexture(matrices, x, y + i * 18 + 7, 0, 7, 176, 18);
        instance.drawTexture(matrices, x, y + rows * 18 + 7, 0, 25, 176, 7);
        RenderSystem.enableDepthTest();

        ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
        TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
        for (int i = 9; i < storage[index].size(); ++i) {
            int itemX = x + (i - 9) % 9 * 18 + 8;
            int itemY = y + (i - 9) / 9 * 18 + 8;
            itemRenderer.zOffset = 200.0F;
            itemRenderer.renderInGui(storage[index].getStack(i), itemX, itemY);
            itemRenderer.renderGuiItemOverlay(textRenderer, storage[index].getStack(i), itemX, itemY);
            itemRenderer.zOffset = 0.0F;
        }

        return true;
    }

    private static int getStorageIndexFromTitle(String title) {
        Matcher echest = ECHEST_PATTERN.matcher(title);
        if (echest.find()) return Integer.parseInt(echest.group(1)) - 1;
        Matcher backpack = BACKPACK_PATTERN.matcher(title);
        if (backpack.find()) return Integer.parseInt(backpack.group(1)) + 8;
        return -1;
    }
}

class DummyInventory implements Inventory {
    private List<ItemStack> stacks;

    public DummyInventory(NbtCompound root) {
        stacks = new ArrayList<>(root.getInt("size") + 9);
        for (int i = 0; i < 9; ++i) stacks.add(ItemStack.EMPTY);
        root.getList("list", NbtCompound.COMPOUND_TYPE).forEach(item ->
                stacks.add(ItemStack.fromNbt((NbtCompound)item))
        );
    }

    @Override
    public int size() { return stacks.size(); }

    @Override
    public boolean isEmpty() { return false; }

    @Override
    public ItemStack getStack(int slot) { return stacks.get(slot); }

    @Override
    public ItemStack removeStack(int slot, int amount) { return null; }

    @Override
    public ItemStack removeStack(int slot) { return null; }

    @Override
    public void setStack(int slot, ItemStack stack) { stacks.set(slot, stack); }

    @Override
    public void markDirty() {}

    @Override
    public boolean canPlayerUse(PlayerEntity player) { return false; }

    @Override
    public void clear() {}
}