aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/rosegoldaddons/features/ForagingIslandMacro.java
blob: c284e924252e61e228ace82f50767f6a7d5b7c19 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package rosegoldaddons.features;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.*;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import rosegoldaddons.Main;
import rosegoldaddons.utils.ChatUtils;
import rosegoldaddons.utils.RotationUtils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Random;

public class ForagingIslandMacro {
    private Thread thread;
    private final String[] responses = {"wtf??", "hello?", "hi?", "uhhhhhh", "what the", "??????"};

    @SubscribeEvent
    public void renderWorld(RenderWorldLastEvent event) {
        if (Main.forageOnIsland) {
            if (thread == null || !thread.isAlive()) {
                thread = new Thread(() -> {
                    try {
                        BlockPos furthestDirt = furthestEmptyDirt();
                        //Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("furthest dirt "+furthestDirt));
                        int sapling = findItemInHotbar("Jungle Sapling");
                        if(sapling == -1) {
                            sapling = findItemInHotbar("Oak Sapling");
                        }
                        int bonemeal = findItemInHotbar("Bone Meal");
                        int treecap = findItemInHotbar("Treecapitator");
                        int rod = findItemInHotbar("Rod");
                        if(sapling == -1) {
                            ChatUtils.sendMessage("§cNo jungle saplings in hotbar");
                        }
                        if(bonemeal == -1) {
                            ChatUtils.sendMessage("§cNo bonemeal in hotbar");
                        }
                        if(treecap == -1) {
                            ChatUtils.sendMessage("§cNo Treecapitator in hotbar");
                        }
                        if(rod == -1) {
                            ChatUtils.sendMessage("§cNo Fishing Rod in hotbar");
                        }
                        if(sapling == -1 || bonemeal == -1 || treecap == -1 || rod == -1) {
                            playAnnoyingAlert();
                            Main.forageOnIsland = false;
                            ChatUtils.sendMessage("§cForaging Macro Deactivated");
                            return;
                        }
                        if (furthestDirt != null) {
                            RotationUtils.facePos(new Vec3(furthestDirt.getX() + 0.5, furthestDirt.getY() - 0.5, furthestDirt.getZ() + 0.5));
                            Thread.sleep(Main.configFile.smoothLookVelocity * 2L);
                            if (sapling != -1) {
                                if (Minecraft.getMinecraft().objectMouseOver.typeOfHit.toString().equals("BLOCK")) {
                                    BlockPos pos = Minecraft.getMinecraft().objectMouseOver.getBlockPos();
                                    Block block = Minecraft.getMinecraft().theWorld.getBlockState(pos).getBlock();
                                    if (block == Blocks.sapling) {
                                        click();
                                        Thread.sleep(20);
                                    }
                                }
                                Minecraft.getMinecraft().thePlayer.inventory.currentItem = sapling;
                                //Minecraft.getMinecraft().playerController.onPlayerRightClick(Minecraft.getMinecraft().thePlayer, Minecraft.getMinecraft().theWorld, Minecraft.getMinecraft().thePlayer.inventory.getCurrentItem(), furthestDirt, EnumFacing.NORTH, Minecraft.getMinecraft().objectMouseOver.hitVec);
                                rightClick();
                            }
                        } else {
                            BlockPos dirt = closestDirt();
                            if (dirt != null) {
                                RotationUtils.facePos(new Vec3(dirt.getX() + 0.5, dirt.getY(), dirt.getZ() + 0.5));
                                if (bonemeal != -1 && treecap != -1) {
                                    Minecraft.getMinecraft().thePlayer.inventory.currentItem = bonemeal;
                                    Random rand = new Random();
                                    int toAdd = 0;
                                    if(Main.configFile.randomizeForaging) {
                                        toAdd = rand.nextInt(20);
                                    }
                                    //ChatUtils.sendMessage("extra delay: "+toAdd+"%");
                                    Thread.sleep(Math.round(150*(1+(toAdd/100))));
                                    rightClick();
                                    rightClick();
                                    Minecraft.getMinecraft().thePlayer.inventory.currentItem = treecap;
                                    Thread.sleep(Math.round(Main.configFile.treecapDelay*(1+(toAdd/100))));
                                    KeyBinding.setKeyBindState(Minecraft.getMinecraft().gameSettings.keyBindAttack.getKeyCode(), true);
                                    Thread.sleep(Math.round(150*(1+(toAdd/100))));
                                    KeyBinding.setKeyBindState(Minecraft.getMinecraft().gameSettings.keyBindAttack.getKeyCode(), false);
                                    Thread.sleep(Math.round(25*(1+(toAdd/100))));
                                    Minecraft.getMinecraft().thePlayer.inventory.currentItem = rod;
                                    Thread.sleep(Math.round(Main.configFile.prerodDelay*(1+(toAdd/100))));
                                    rightClick();
                                    Thread.sleep(Math.round(Main.configFile.postrodDelay*(1+(toAdd/100))));
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }, "Island Foraging Macro");
                thread.start();
            }
        }
    }

    @SubscribeEvent
    public void renderWorld2(RenderWorldLastEvent event) {
        if (!Main.forageOnIsland) return;
        BlockPos e = closestDirt();
        if(e == null) {
            Main.forageOnIsland = false;
            ChatUtils.sendMessage("§cNo dirt in range of player");
            ChatUtils.sendMessage("§cForaging Macro Deactivated");
            playAnnoyingAlert();
            if(Main.configFile.forageantisus) {
                int rand = new Random().nextInt(responses.length);
                int rand2 = new Random().nextInt(2000);
                new Thread(() -> {
                    try {
                        Thread.sleep(rand2);
                        Minecraft.getMinecraft().thePlayer.sendChatMessage("/ac "+responses[rand]);
                        Thread.sleep(rand2*2);
                        Minecraft.getMinecraft().getNetHandler().getNetworkManager().closeChannel(new ChatComponentText("Antisus activated lets hope you didnt get banned"));
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                }).start();
            }
        }
    }

    private BlockPos furthestEmptyDirt() {
        int r = 5;
        BlockPos playerPos = Minecraft.getMinecraft().thePlayer.getPosition();
        playerPos.add(0, 1, 0);
        Vec3 playerVec = Minecraft.getMinecraft().thePlayer.getPositionVector();
        Vec3i vec3i = new Vec3i(r, r, r);
        ArrayList<Vec3> dirts = new ArrayList<Vec3>();
        if (playerPos != null) {
            for (BlockPos blockPos : BlockPos.getAllInBox(playerPos.add(vec3i), playerPos.subtract(vec3i))) {
                IBlockState blockState = Minecraft.getMinecraft().theWorld.getBlockState(blockPos);
                IBlockState blockState2 = Minecraft.getMinecraft().theWorld.getBlockState(blockPos.add(0, 1, 0));
                //Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(blockState.getBlock().toString()));
                if (blockState.getBlock() == Blocks.dirt && blockState2.getBlock() == Blocks.air) {
                    dirts.add(new Vec3(blockPos.getX() + 0.5, blockPos.getY(), blockPos.getZ() + 0.5));
                }
            }
        }
        double biggest = -1;
        Vec3 furthest = null;
        for (int i = 0; i < dirts.size(); i++) {
            double dist = dirts.get(i).distanceTo(playerVec);
            if (dist > biggest) {
                biggest = dist;
                furthest = dirts.get(i);
            }
        }
        if (furthest != null && biggest < 4) {
            return new BlockPos(furthest.xCoord, furthest.yCoord, furthest.zCoord);
        }
        return null;
    }

    private BlockPos closestDirt() {
        int r = 5;
        BlockPos playerPos = Minecraft.getMinecraft().thePlayer.getPosition();
        playerPos.add(0, 1, 0);
        Vec3 playerVec = Minecraft.getMinecraft().thePlayer.getPositionVector();
        Vec3i vec3i = new Vec3i(r, r, r);
        ArrayList<Vec3> dirts = new ArrayList<Vec3>();
        if (playerPos != null) {
            for (BlockPos blockPos : BlockPos.getAllInBox(playerPos.add(vec3i), playerPos.subtract(vec3i))) {
                IBlockState blockState = Minecraft.getMinecraft().theWorld.getBlockState(blockPos);
                //Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(blockState.getBlock().toString()));
                if (blockState.getBlock() == Blocks.dirt) {
                    dirts.add(new Vec3(blockPos.getX() + 0.5, blockPos.getY(), blockPos.getZ() + 0.5));
                }
            }
        }
        double smallest = 9999;
        Vec3 closest = null;
        for (int i = 0; i < dirts.size(); i++) {
            double dist = dirts.get(i).distanceTo(playerVec);
            if (dist < smallest) {
                smallest = dist;
                closest = dirts.get(i);
            }
        }
        if (closest != null && smallest < 4) {
            return new BlockPos(closest.xCoord, closest.yCoord, closest.zCoord);
        }
        return null;
    }

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

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

    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(Minecraft.getMinecraft());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void playAlert() {
        Minecraft.getMinecraft().thePlayer.playSound("random.orb", 1, 0.5F);
    }

    private static void playAnnoyingAlert() {
        new Thread(() -> {
            try {
                Minecraft.getMinecraft().thePlayer.playSound("random.orb", 1, 0.5F);
                Thread.sleep(100);
                Minecraft.getMinecraft().thePlayer.playSound("random.orb", 1, 0.5F);
                Thread.sleep(100);
                Minecraft.getMinecraft().thePlayer.playSound("random.orb", 1, 0.5F);
                Thread.sleep(100);
                Minecraft.getMinecraft().thePlayer.playSound("random.orb", 1, 0.5F);
                Thread.sleep(100);
                Minecraft.getMinecraft().thePlayer.playSound("random.orb", 1, 0.5F);
                Thread.sleep(100);
                Minecraft.getMinecraft().thePlayer.playSound("random.orb", 1, 0.5F);
                Thread.sleep(100);
                Minecraft.getMinecraft().thePlayer.playSound("random.orb", 1, 0.5F);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }).start();
    }
}