/
InputHandler.java
254 lines (218 loc) · 6.88 KB
/
InputHandler.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
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
package mezz.jei.input;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import mezz.jei.config.Config;
import mezz.jei.config.KeyBindings;
import mezz.jei.gui.Focus;
import mezz.jei.gui.ItemListOverlay;
import mezz.jei.gui.RecipesGui;
import mezz.jei.util.Commands;
import mezz.jei.util.MouseHelper;
public class InputHandler {
private final RecipesGui recipesGui;
private final ItemListOverlay itemListOverlay;
private final MouseHelper mouseHelper;
private final List<IMouseHandler> mouseHandlers = new ArrayList<>();
private final List<IKeyable> keyables = new ArrayList<>();
private final List<IShowsRecipeFocuses> showsRecipeFocuses = new ArrayList<>();
private boolean clickHandled = false;
public InputHandler(RecipesGui recipesGui, ItemListOverlay itemListOverlay, GuiContainer guiContainer) {
this.recipesGui = recipesGui;
this.itemListOverlay = itemListOverlay;
this.mouseHelper = new MouseHelper();
List<ICloseable> objects = new ArrayList<>();
objects.add(recipesGui);
objects.add(itemListOverlay);
objects.add(new GuiContainerWrapper(guiContainer, recipesGui));
for (Object gui : objects) {
if (gui instanceof IMouseHandler) {
mouseHandlers.add((IMouseHandler) gui);
}
if (gui instanceof IKeyable) {
keyables.add((IKeyable) gui);
}
if (gui instanceof IShowsRecipeFocuses) {
showsRecipeFocuses.add((IShowsRecipeFocuses) gui);
}
}
}
public boolean handleMouseEvent(int mouseX, int mouseY) {
boolean cancelEvent = false;
if (Mouse.getEventButton() > -1) {
if (Mouse.getEventButtonState()) {
if (!clickHandled) {
cancelEvent = handleMouseClick(Mouse.getEventButton(), mouseX, mouseY);
clickHandled = cancelEvent;
}
} else if (clickHandled) {
clickHandled = false;
cancelEvent = true;
}
} else if (Mouse.getEventDWheel() != 0) {
cancelEvent = handleMouseScroll(Mouse.getEventDWheel(), mouseX, mouseY);
}
return cancelEvent;
}
private boolean handleMouseScroll(int dWheel, int mouseX, int mouseY) {
for (IMouseHandler scrollable : mouseHandlers) {
if (scrollable.handleMouseScrolled(mouseX, mouseY, dWheel)) {
return true;
}
}
return false;
}
private boolean handleMouseClick(int mouseButton, int mouseX, int mouseY) {
for (IMouseHandler clickable : mouseHandlers) {
if (clickable.handleMouseClicked(mouseX, mouseY, mouseButton)) {
return true;
}
}
Focus focus = getFocusUnderMouseForClick(mouseX, mouseY);
if (focus != null && handleMouseClickedFocus(mouseButton, focus)) {
return true;
}
return recipesGui.isOpen();
}
@Nullable
private Focus getFocusUnderMouseForClick(int mouseX, int mouseY) {
for (IShowsRecipeFocuses gui : showsRecipeFocuses) {
if (!(gui instanceof IMouseHandler)) {
continue;
}
Focus focus = gui.getFocusUnderMouse(mouseX, mouseY);
if (focus != null) {
return focus;
}
}
return null;
}
@Nullable
private Focus getFocusUnderMouseForKey(int mouseX, int mouseY) {
for (IShowsRecipeFocuses gui : showsRecipeFocuses) {
Focus focus = gui.getFocusUnderMouse(mouseX, mouseY);
if (focus != null) {
return focus;
}
}
return null;
}
private boolean handleMouseClickedFocus(int mouseButton, @Nonnull Focus focus) {
if (Config.isEditModeEnabled() && GuiScreen.isCtrlKeyDown()) {
if (handleClickEditStack(mouseButton, focus)) {
return true;
}
}
if (Config.isCheatItemsEnabled() && focus.getStack() != null) {
if (mouseButton == 0) {
Commands.giveFullStack(focus.getStack());
return true;
} else if (mouseButton == 1) {
Commands.giveOneFromStack(focus.getStack());
return true;
}
}
if (mouseButton == 0) {
recipesGui.showRecipes(focus);
return true;
} else if (mouseButton == 1) {
recipesGui.showUses(focus);
return true;
}
return false;
}
private boolean handleClickEditStack(int mouseButton, @Nonnull Focus focus) {
ItemStack itemStack = focus.getStack();
if (itemStack == null) {
return false;
}
boolean wildcard;
if (mouseButton == 0) {
wildcard = false;
} else if (mouseButton == 1) {
wildcard = true;
} else {
return false;
}
if (Config.isItemOnConfigBlacklist(focus.getStack(), wildcard)) {
Config.removeItemFromConfigBlacklist(focus.getStack(), wildcard);
} else {
Item item = itemStack.getItem();
if (wildcard && !item.getHasSubtypes()) {
return false;
}
Config.addItemToConfigBlacklist(focus.getStack(), wildcard);
}
return true;
}
public boolean handleKeyEvent() {
boolean cancelEvent = false;
if (Keyboard.getEventKeyState()) {
int eventKey = Keyboard.getEventKey();
cancelEvent = handleKeyDown(eventKey);
}
return cancelEvent;
}
private boolean handleKeyDown(int eventKey) {
for (IKeyable keyable : keyables) {
if (keyable.isOpen() && keyable.hasKeyboardFocus()) {
if (isInventoryCloseKey(eventKey)) {
keyable.setKeyboardFocus(false);
return true;
} else if (keyable.onKeyPressed(eventKey)) {
return true;
}
}
}
if (isInventoryCloseKey(eventKey) || isInventoryToggleKey(eventKey)) {
if (recipesGui.isOpen()) {
recipesGui.close();
return true;
}
}
if (eventKey == KeyBindings.showRecipe.getKeyCode()) {
Focus focus = getFocusUnderMouseForKey(mouseHelper.getX(), mouseHelper.getY());
if (focus != null) {
recipesGui.showRecipes(focus);
return true;
}
} else if (eventKey == KeyBindings.showUses.getKeyCode()) {
Focus focus = getFocusUnderMouseForKey(mouseHelper.getX(), mouseHelper.getY());
if (focus != null) {
recipesGui.showUses(focus);
return true;
}
} else if (eventKey == KeyBindings.toggleOverlay.getKeyCode() && GuiScreen.isCtrlKeyDown()) {
itemListOverlay.toggleEnabled();
return false;
} else if (eventKey == Keyboard.KEY_F && GuiScreen.isCtrlKeyDown()) {
itemListOverlay.setKeyboardFocus(true);
return true;
} else if (eventKey == Keyboard.KEY_BACK) {
if (recipesGui.isOpen()) {
recipesGui.back();
return true;
}
}
for (IKeyable keyable : keyables) {
if (keyable.isOpen() && keyable.onKeyPressed(eventKey)) {
return true;
}
}
return false;
}
private boolean isInventoryToggleKey(int keyCode) {
return keyCode == Minecraft.getMinecraft().gameSettings.keyBindInventory.getKeyCode();
}
private boolean isInventoryCloseKey(int keyCode) {
return keyCode == Keyboard.KEY_ESCAPE;
}
}