aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/chrisoft/trashyaddon/modules/MatchedItemHighlight.java
blob: 2d5be7e1f9200143c26fcc61e0b65e9d5fd761ef (plain) (blame)
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
package org.chrisoft.trashyaddon.modules;

import meteordevelopment.meteorclient.events.packets.InventoryEvent;
import meteordevelopment.meteorclient.events.packets.PacketEvent;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.utils.render.color.Color;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.c2s.play.CloseHandledScreenC2SPacket;
import net.minecraft.network.packet.s2c.play.CloseScreenS2CPacket;
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.screen.ScreenHandler;

import java.util.ArrayList;

public class MatchedItemHighlight extends Module {
    private ScreenHandler screenHandler;
    private final ArrayList<Color> slotColors;
    private static final Color transparent = new Color(0, 0, 0, 0);
    public MatchedItemHighlight() {
        super(Categories.Render, "Combinable Slots", "Highlight items that can be combined in your inventory and a container.");
        slotColors = new ArrayList<>();
    }

    public void onActivate() { screenHandler = null; }

    public Color getSlotColor(int index) {
        if (!this.isActive() || index >= slotColors.size())
            return transparent;
        return slotColors.get(index);
    }

    private void updateColors() {
        if (screenHandler == null)
            return;
        slotColors.clear();
        for (int i = 0 ; i < screenHandler.slots.size(); ++i)
            slotColors.add(new Color(transparent));
        int firstInventorySlot = screenHandler.slots.size() - 36;
        if (firstInventorySlot <= 0)
            return;
        int colorIndex = 0;
        for (int i = 0; i < firstInventorySlot; ++ i) {
            if (screenHandler.getSlot(i).getStack().isEmpty())
                continue;
            for (int j = firstInventorySlot; j < screenHandler.slots.size(); ++ j) {
                if (screenHandler.getSlot(j).getStack().isEmpty())
                    continue;
                if (ItemStack.areItemsAndComponentsEqual(screenHandler.getSlot(i).getStack(), screenHandler.getSlot(j).getStack()) ||
                    ItemStack.areItemsAndComponentsEqual(screenHandler.getSlot(j).getStack(), screenHandler.getSlot(i).getStack())) {
                    Color c;
                    if (!slotColors.get(i).equals(transparent))
                        c = new Color(slotColors.get(i));
                    else if (!slotColors.get(j).equals(transparent))
                        c = new Color(slotColors.get(j));
                    else {
                        c = Color.fromHsv(colorIndex * 45, 0.6, 1.).a(160);
                        ++ colorIndex;
                        if (colorIndex > 7) colorIndex = 0;
                    }
                    slotColors.set(i, new Color(c));
                    slotColors.set(j, new Color(c));
                }
            }
        }
    }

    @EventHandler
    private void onInventory(InventoryEvent event) {
        if (mc.player.currentScreenHandler instanceof PlayerScreenHandler)
            return;
        screenHandler = mc.player.currentScreenHandler;
        if (screenHandler != null && event.packet.getSyncId() == screenHandler.syncId)
            updateColors();
    }

    @EventHandler
    private void onReceivePacket(PacketEvent.Receive e) {
        if (e.packet instanceof ScreenHandlerSlotUpdateS2CPacket p) {
            if (screenHandler != null && p.getSyncId() == screenHandler.syncId)
                updateColors();
        } else if (e.packet instanceof CloseScreenS2CPacket) {
            screenHandler = null;
            slotColors.clear();
        }
    }

    @EventHandler
    private void onSendPacket(PacketEvent.Send e) {
        if (e.packet instanceof CloseHandledScreenC2SPacket p) {
            screenHandler = null;
            slotColors.clear();
        }
    }
}