aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/rosegoldaddons/features/CustomItemMacro.java
blob: 7f6e1795632a79186d8f3d02250eccf051afb7b2 (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
package rosegoldaddons.features;

import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import rosegoldaddons.Main;
import rosegoldaddons.commands.UseCooldown;

public class CustomItemMacro {
    private Thread thread;
    private int milis = 0;
    private boolean working = false;

    @SubscribeEvent
    public void onTick(TickEvent.ClientTickEvent event) {
        if(event.phase == TickEvent.Phase.END || working) return;
        if (!Main.autoUseItems) return;
        if (thread == null || !thread.isAlive()) {
            thread = new Thread(() -> {
                try {
                    working = true;
                    int prevItem = Main.mc.thePlayer.inventory.currentItem;
                    for (String i : UseCooldown.RCitems.keySet()) {
                        if (milis % Math.floor(UseCooldown.RCitems.get(i)/100) == 0) {
                            int slot = findItemInHotbar(i);
                            if (slot != -1) {
                                Main.mc.thePlayer.inventory.currentItem = slot;
                                Main.mc.playerController.sendUseItem(Main.mc.thePlayer, Main.mc.theWorld, Main.mc.thePlayer.inventory.getStackInSlot(slot));
                            }
                        }
                    }
                    for (String i : UseCooldown.LCitems.keySet()) {
                        if (milis % Math.floor(UseCooldown.LCitems.get(i)/100) == 0) {
                            int slot = findItemInHotbar(i);
                            if (slot != -1) {
                                Main.mc.thePlayer.inventory.currentItem = slot;
                                Thread.sleep(100);
                                click();
                            }
                        }
                    }
                    Main.mc.thePlayer.inventory.currentItem = prevItem;
                    milis++;
                    Thread.sleep(100);
                    working = false;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "Custom Item Use");
            thread.start();
        }
    }

    public static void click() {
        /*try {
            Method clickMouse;
            try {
                clickMouse = Minecraft.class.getDeclaredMethod("func_147116_af");
            } catch (NoSuchMethodException e) {
                clickMouse = Minecraft.class.getDeclaredMethod("clickMouse");
            }
            clickMouse.setAccessible(true);
            clickMouse.invoke(Main.mc);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        Main.mc.thePlayer.swingItem();
    }

    private static int findItemInHotbar(String name) {
        InventoryPlayer inv = Main.mc.thePlayer.inventory;
        for (int i = 0; i < 9; i++) {
            ItemStack curStack = inv.getStackInSlot(i);
            if (curStack != null) {
                if (curStack.getDisplayName().contains(name)) {
                    return i;
                }
            }
        }
        return -1;
    }
}