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

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.StringUtils;
import net.minecraft.world.World;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import rosegoldaddons.Main;

import java.lang.reflect.Method;

public class SwordSwapping {
    public static int tickCount = 0;
    private Thread thread;

    public static void rightClick() {
        try {
            Method rightClickMouse = null;
            try {
                rightClickMouse = Minecraft.class.getDeclaredMethod("rightClickMouse");
            } catch (NoSuchMethodException e) {
                rightClickMouse = Minecraft.class.getDeclaredMethod("func_147121_ag");
            }
            rightClickMouse.setAccessible(true);
            rightClickMouse.invoke(Minecraft.getMinecraft());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static int findItemInHotbar(String name) {
        InventoryPlayer inv = Minecraft.getMinecraft().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;
    }

    @SubscribeEvent
    public void onTick(TickEvent.PlayerTickEvent event) {
        if (Minecraft.getMinecraft().currentScreen != null) return;
        if (!Main.AOTSMacro && !Main.SoulWhipMacro) {
            tickCount = 0;
            return;
        }
        if (thread == null || !thread.isAlive()) {
            thread = new Thread(() -> {
                try {
                    Thread.sleep(Main.configFile.swapFrequency);
                    int prevItem = Minecraft.getMinecraft().thePlayer.inventory.currentItem;
                    int orbSlot = findItemInHotbar("Power Orb");
                    int tubaSlot = findItemInHotbar("Tuba");
                    int wandSlot = findItemInHotbar("Atonement");
                    int whipSlot = -1;
                    int aotsSlot = -1;
                    if(Main.AOTSMacro) {
                        aotsSlot = findItemInHotbar("Shredded");
                    }
                    if(Main.SoulWhipMacro) {
                        whipSlot = findItemInHotbar("Whip");
                    }
                    if(whipSlot != -1) {
                        Minecraft.getMinecraft().thePlayer.inventory.currentItem = whipSlot;
                        rightClick();
                    }
                    if(aotsSlot != -1) {
                        Minecraft.getMinecraft().thePlayer.inventory.currentItem = aotsSlot;
                        rightClick();
                    }
                    if(Main.configFile.UseUtility) {
                        if(tickCount % Math.round((1000/Main.configFile.swapFrequency*20)) == 1 && tubaSlot != -1) {
                            Thread.sleep(1);
                            Minecraft.getMinecraft().thePlayer.inventory.currentItem = tubaSlot;
                            rightClick();
                        }
                        if(tickCount % Math.round((1000/Main.configFile.swapFrequency*59)) == 1 && orbSlot != -1) {
                            Thread.sleep(1);
                            Minecraft.getMinecraft().thePlayer.inventory.currentItem = orbSlot;
                            rightClick();
                        }
                        if(tickCount % Math.round((1000/Main.configFile.swapFrequency*7)) == 1 && wandSlot != -1) {
                            Thread.sleep(1);
                            Minecraft.getMinecraft().thePlayer.inventory.currentItem = wandSlot;
                            rightClick();
                        }
                    }
                    Minecraft.getMinecraft().thePlayer.inventory.currentItem = prevItem;
                    tickCount++;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "Sword Swap");
            thread.start();
        }
    }
}