-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyBindUtils.java
166 lines (144 loc) · 5.42 KB
/
KeyBindUtils.java
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
package net.taunahi_v3.ezskyblock.util;
import com.google.common.collect.ImmutableMap;
import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Vec3;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class KeyBindUtils {
private static final Minecraft mc = Minecraft.getMinecraft();
public static final KeyBinding[] allKeys = {
mc.gameSettings.keyBindAttack,
mc.gameSettings.keyBindUseItem,
mc.gameSettings.keyBindBack,
mc.gameSettings.keyBindForward,
mc.gameSettings.keyBindLeft,
mc.gameSettings.keyBindRight,
mc.gameSettings.keyBindJump,
mc.gameSettings.keyBindSneak,
mc.gameSettings.keyBindSprint,
};
public static final KeyBinding[] allKeys2 = {
mc.gameSettings.keyBindBack,
mc.gameSettings.keyBindForward,
mc.gameSettings.keyBindLeft,
mc.gameSettings.keyBindRight,
mc.gameSettings.keyBindJump,
};
public static void rightClick() {
if (!ReflectionUtils.invoke(mc, "func_147121_ag")) {
ReflectionUtils.invoke(mc, "rightClickMouse");
}
}
public static void leftClick() {
if (!ReflectionUtils.invoke(mc, "func_147116_af")) {
ReflectionUtils.invoke(mc, "clickMouse");
}
}
public static void middleClick() {
if (!ReflectionUtils.invoke(mc, "func_147112_ai")) {
ReflectionUtils.invoke(mc, "middleClickMouse");
}
}
public static void onTick(KeyBinding key) {
if (mc.currentScreen == null) {
KeyBinding.onTick(key.getKeyCode());
}
}
public static void setKeyBindState(KeyBinding key, boolean pressed) {
if (pressed) {
if (mc.currentScreen != null) {
realSetKeyBindState(key, false);
return;
}
}
realSetKeyBindState(key, pressed);
}
private static void realSetKeyBindState(KeyBinding key, boolean pressed) {
if (pressed) {
if (!key.isKeyDown()) {
KeyBinding.onTick(key.getKeyCode());
KeyBinding.setKeyBindState(key.getKeyCode(), true);
}
} else {
if (key.isKeyDown()) {
KeyBinding.setKeyBindState(key.getKeyCode(), false);
}
}
}
public static void stopMovement() {
stopMovement(false);
}
public static void stopMovement(boolean ignoreAttack) {
realSetKeyBindState(mc.gameSettings.keyBindForward, false);
realSetKeyBindState(mc.gameSettings.keyBindBack, false);
realSetKeyBindState(mc.gameSettings.keyBindRight, false);
realSetKeyBindState(mc.gameSettings.keyBindLeft, false);
if (!ignoreAttack) {
realSetKeyBindState(mc.gameSettings.keyBindAttack, false);
}
realSetKeyBindState(mc.gameSettings.keyBindUseItem, false);
realSetKeyBindState(mc.gameSettings.keyBindSneak, false);
realSetKeyBindState(mc.gameSettings.keyBindJump, false);
realSetKeyBindState(mc.gameSettings.keyBindSprint, false);
}
public static void holdThese(KeyBinding... keyBinding) {
releaseAllExcept(keyBinding);
for (KeyBinding key : keyBinding) {
if (key != null)
realSetKeyBindState(key, true);
}
}
public static void releaseAllExcept(KeyBinding... keyBinding) {
for (KeyBinding key : allKeys) {
if (key != null && !contains(keyBinding, key) && key.isKeyDown()) {
realSetKeyBindState(key, false);
}
}
}
public static boolean contains(KeyBinding[] keyBinding, KeyBinding key) {
for (KeyBinding keyBind : keyBinding) {
if (keyBind != null && keyBind.getKeyCode() == key.getKeyCode())
return true;
}
return false;
}
public static boolean areAllKeybindsReleased() {
for (KeyBinding key : allKeys2) {
if (key != null && key.isKeyDown())
return false;
}
return true;
}
public static KeyBinding[] getHoldingKeybinds() {
KeyBinding[] keybinds = new KeyBinding[allKeys.length];
int i = 0;
for (KeyBinding key : allKeys) {
if (key != null && key.isKeyDown()) {
keybinds[i] = key;
i++;
}
}
return keybinds;
}
private static final Map<Integer, KeyBinding> keyBindMap = ImmutableMap.of(
0, mc.gameSettings.keyBindForward,
90, mc.gameSettings.keyBindLeft,
180, mc.gameSettings.keyBindBack,
-90, mc.gameSettings.keyBindRight
);
public static List<KeyBinding> getNeededKeyPresses(Vec3 orig, Vec3 dest) {
List<KeyBinding> keys = new ArrayList<>();
double[] delta = {orig.xCoord - dest.xCoord, orig.zCoord - dest.zCoord};
float requiredAngle = (float) (MathHelper.atan2(delta[0], -delta[1]) * (180.0 / Math.PI));
float angleDifference = AngleUtils.normalizeYaw(requiredAngle - mc.thePlayer.rotationYaw) * -1;
keyBindMap.forEach((yaw, key) -> {
if (Math.abs(yaw - angleDifference) < 67.5 || Math.abs(yaw - (angleDifference + 360.0)) < 67.5) {
keys.add(key);
}
});
return keys;
}
}