/
IngredientListOverlay.java
409 lines (365 loc) · 13.5 KB
/
IngredientListOverlay.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package mezz.jei.gui.overlay;
import com.mojang.blaze3d.matrix.MatrixStack;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Set;
import com.mojang.blaze3d.systems.RenderSystem;
import mezz.jei.api.ingredients.IIngredientType;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.renderer.Rectangle2d;
import net.minecraft.client.util.InputMappings;
import net.minecraft.item.ItemStack;
import com.google.common.collect.ImmutableList;
import mezz.jei.api.gui.handlers.IGuiProperties;
import mezz.jei.api.runtime.IIngredientListOverlay;
import mezz.jei.config.ClientConfig;
import mezz.jei.config.IWorldConfig;
import mezz.jei.config.KeyBindings;
import mezz.jei.gui.GuiScreenHelper;
import mezz.jei.gui.elements.GuiIconToggleButton;
import mezz.jei.gui.ghost.GhostIngredientDragManager;
import mezz.jei.gui.ingredients.IIngredientListElement;
import mezz.jei.gui.recipes.RecipesGui;
import mezz.jei.ingredients.IngredientFilter;
import mezz.jei.ingredients.IngredientManager;
import mezz.jei.input.GuiTextFieldFilter;
import mezz.jei.input.IClickedIngredient;
import mezz.jei.input.IMouseHandler;
import mezz.jei.input.IShowsRecipeFocuses;
import mezz.jei.util.CommandUtil;
import mezz.jei.util.MathUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class IngredientListOverlay implements IIngredientListOverlay, IMouseHandler, IShowsRecipeFocuses {
private static final Logger LOGGER = LogManager.getLogger();
private static final int BORDER_PADDING = 2;
private static final int BUTTON_SIZE = 20;
private static final int SEARCH_HEIGHT = 20;
private boolean hasRoom;
private static boolean isSearchBarCentered(IGuiProperties guiProperties) {
return ClientConfig.getInstance().isCenterSearchBarEnabled() &&
guiProperties.getGuiTop() + guiProperties.getGuiYSize() + SEARCH_HEIGHT < guiProperties.getScreenHeight();
}
private final IngredientFilter ingredientFilter;
private final GuiIconToggleButton configButton;
private final IngredientGridWithNavigation contents;
private final IWorldConfig worldConfig;
private final GuiScreenHelper guiScreenHelper;
private final GuiTextFieldFilter searchField;
private final GhostIngredientDragManager ghostIngredientDragManager;
private Rectangle2d displayArea = new Rectangle2d(0, 0, 0, 0);
// properties of the gui we're beside
@Nullable
private IGuiProperties guiProperties;
public IngredientListOverlay(
IngredientFilter ingredientFilter,
IngredientManager ingredientManager,
GuiScreenHelper guiScreenHelper,
IngredientGridWithNavigation contents,
IWorldConfig worldConfig
) {
this.ingredientFilter = ingredientFilter;
this.guiScreenHelper = guiScreenHelper;
this.contents = contents;
this.worldConfig = worldConfig;
ingredientFilter.addListener(() -> onSetFilterText(worldConfig.getFilterText()));
this.searchField = new GuiTextFieldFilter(ingredientFilter, worldConfig);
this.configButton = ConfigButton.create(this, worldConfig);
this.ghostIngredientDragManager = new GhostIngredientDragManager(this.contents, guiScreenHelper, ingredientManager, worldConfig);
this.setKeyboardFocus(false);
}
public void rebuildIngredientFilter() {
LOGGER.info("Updating ingredient filter...");
long start_time = System.currentTimeMillis();
this.ingredientFilter.modesChanged();
LOGGER.info("Updated ingredient filter in {} ms", System.currentTimeMillis() - start_time);
updateLayout(true);
}
public boolean isListDisplayed() {
return worldConfig.isOverlayEnabled() && this.guiProperties != null && this.hasRoom;
}
private static Rectangle2d getDisplayArea(IGuiProperties guiProperties) {
final int x = guiProperties.getGuiLeft() + guiProperties.getGuiXSize() + BORDER_PADDING;
final int y = BORDER_PADDING;
final int width = guiProperties.getScreenWidth() - x - BORDER_PADDING;
final int height = guiProperties.getScreenHeight() - y - BORDER_PADDING;
return new Rectangle2d(x, y, width, height);
}
public void updateScreen(@Nullable Screen guiScreen, boolean forceUpdate) {
final boolean wasDisplayed = isListDisplayed();
IGuiProperties guiProperties = guiScreenHelper.getGuiProperties(guiScreen);
if (guiProperties == null) {
if (this.guiProperties != null) {
this.guiProperties = null;
setKeyboardFocus(false);
this.ghostIngredientDragManager.stopDrag();
}
} else {
if (forceUpdate || this.guiProperties == null || !GuiProperties.areEqual(this.guiProperties, guiProperties)) {
this.guiProperties = guiProperties;
this.displayArea = getDisplayArea(guiProperties);
final boolean searchBarCentered = isSearchBarCentered(guiProperties);
final int searchHeight = searchBarCentered ? 0 : SEARCH_HEIGHT + BORDER_PADDING;
Set<Rectangle2d> guiExclusionAreas = guiScreenHelper.getGuiExclusionAreas();
Rectangle2d availableContentsArea = new Rectangle2d(
displayArea.getX(),
displayArea.getY(),
displayArea.getWidth(),
displayArea.getHeight() - searchHeight
);
hasRoom = this.contents.updateBounds(availableContentsArea, guiExclusionAreas, 4 * BUTTON_SIZE);
// update area to match contents size
Rectangle2d contentsArea = this.contents.getArea();
displayArea = new Rectangle2d(
contentsArea.getX(),
displayArea.getY(),
contentsArea.getWidth(),
displayArea.getHeight()
);
if (searchBarCentered && isListDisplayed()) {
searchField.updateBounds(new Rectangle2d(
guiProperties.getGuiLeft(),
guiProperties.getScreenHeight() - SEARCH_HEIGHT - BORDER_PADDING,
guiProperties.getGuiXSize() - BUTTON_SIZE + 1,
SEARCH_HEIGHT
));
} else {
searchField.updateBounds(new Rectangle2d(
displayArea.getX(),
displayArea.getY() + displayArea.getHeight() - SEARCH_HEIGHT - BORDER_PADDING,
displayArea.getWidth() - BUTTON_SIZE + 1,
SEARCH_HEIGHT
));
}
this.configButton.updateBounds(new Rectangle2d(
searchField.x + searchField.getWidth() - 1,
searchField.y,
BUTTON_SIZE,
BUTTON_SIZE
));
updateLayout(false);
}
}
if (wasDisplayed && !isListDisplayed()) {
worldConfig.saveFilterText();
}
}
public void updateLayout(boolean filterChanged) {
this.contents.updateLayout(filterChanged);
this.searchField.update();
}
public void drawScreen(Minecraft minecraft, MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
if (this.guiProperties != null) {
if (isListDisplayed()) {
RenderSystem.disableLighting();
this.searchField.renderButton(matrixStack, mouseX, mouseY, partialTicks);
this.contents.draw(minecraft, matrixStack, mouseX, mouseY, partialTicks);
this.configButton.draw(matrixStack, mouseX, mouseY, partialTicks);
} else {
this.configButton.draw(matrixStack, mouseX, mouseY, partialTicks);
}
}
}
public void drawTooltips(Minecraft minecraft, MatrixStack matrixStack, int mouseX, int mouseY) {
if (isListDisplayed()) {
this.configButton.drawTooltips(matrixStack, mouseX, mouseY);
this.ghostIngredientDragManager.drawTooltips(minecraft, matrixStack, mouseX, mouseY);
this.contents.drawTooltips(minecraft, matrixStack, mouseX, mouseY);
} else if (this.guiProperties != null) {
this.configButton.drawTooltips(matrixStack, mouseX, mouseY);
}
}
public void drawOnForeground(Minecraft minecraft, MatrixStack matrixStack, ContainerScreen<?> gui, int mouseX, int mouseY) {
if (isListDisplayed()) {
matrixStack.push();
matrixStack.translate(-gui.getGuiLeft(), -gui.getGuiTop(), 0);
this.ghostIngredientDragManager.drawOnForeground(minecraft, matrixStack, mouseX, mouseY);
matrixStack.pop();
}
}
public void handleTick() {
if (this.isListDisplayed()) {
this.searchField.tick();
}
}
@Override
public boolean isMouseOver(double mouseX, double mouseY) {
if (isListDisplayed()) {
if (ClientConfig.getInstance().isCenterSearchBarEnabled() && searchField.isMouseOver(mouseX, mouseY)) {
return true;
}
return MathUtil.contains(displayArea, mouseX, mouseY);
} else if (this.guiProperties != null) {
return this.configButton.isMouseOver(mouseX, mouseY);
}
return false;
}
@Override
@Nullable
public IClickedIngredient<?> getIngredientUnderMouse(double mouseX, double mouseY) {
if (isListDisplayed()) {
IClickedIngredient<?> clicked = this.contents.getIngredientUnderMouse(mouseX, mouseY);
if (clicked != null) {
clicked.setOnClickHandler(() -> setKeyboardFocus(false));
return clicked;
}
}
return null;
}
@Override
public boolean canSetFocusWithMouse() {
return this.isListDisplayed() && this.contents.canSetFocusWithMouse();
}
@Override
public boolean handleMouseClicked(double mouseX, double mouseY, int mouseButton) {
if (isListDisplayed()) {
if (this.ghostIngredientDragManager.handleMouseClicked(mouseX, mouseY)) {
return true;
}
if (this.configButton.handleMouseClick(mouseX, mouseY, mouseButton)) {
return true;
}
if (!isMouseOver(mouseX, mouseY)) {
setKeyboardFocus(false);
return false;
}
if (this.contents.handleMouseClicked(mouseX, mouseY, mouseButton)) {
setKeyboardFocus(false);
return true;
}
boolean searchClicked = this.searchField.isMouseOver(mouseX, mouseY);
setKeyboardFocus(searchClicked);
if (searchClicked) {
final boolean updated = this.searchField.handleMouseClicked(mouseX, mouseY, mouseButton);
if (updated) {
updateLayout(false);
}
return true;
}
Minecraft minecraft = Minecraft.getInstance();
Screen currentScreen = minecraft.currentScreen;
InputMappings.Input input = InputMappings.Type.MOUSE.getOrMakeInput(mouseButton);
if (currentScreen != null && !(currentScreen instanceof RecipesGui) &&
(mouseButton == 0 || mouseButton == 1 || minecraft.gameSettings.keyBindPickBlock.isActiveAndMatches(input))) {
IClickedIngredient<?> clicked = getIngredientUnderMouse(mouseX, mouseY);
if (clicked != null) {
if (worldConfig.isCheatItemsEnabled()) {
ItemStack itemStack = clicked.getCheatItemStack();
if (!itemStack.isEmpty()) {
CommandUtil.giveStack(itemStack, input);
}
clicked.onClickHandled();
return true;
}
ClientPlayerEntity player = minecraft.player;
if (player != null) {
ItemStack mouseItem = player.inventory.getItemStack();
if (mouseItem.isEmpty() && this.ghostIngredientDragManager.handleClickGhostIngredient(currentScreen, clicked)) {
return true;
}
}
}
}
} else if (this.guiProperties != null) {
return this.configButton.handleMouseClick(mouseX, mouseY, mouseButton);
}
return false;
}
@Override
public boolean handleMouseScrolled(double mouseX, double mouseY, double scrollDelta) {
return isListDisplayed() &&
isMouseOver(mouseX, mouseY) &&
this.contents.handleMouseScrolled(mouseX, mouseY, scrollDelta);
}
@Override
public boolean hasKeyboardFocus() {
return isListDisplayed() && this.searchField.isFocused();
}
public void setKeyboardFocus(boolean keyboardFocus) {
this.searchField.setFocused(keyboardFocus);
}
public boolean onGlobalKeyPressed(InputMappings.Input input) {
if (isListDisplayed()) {
if (KeyBindings.toggleCheatMode.isActiveAndMatches(input)) {
worldConfig.toggleCheatItemsEnabled();
return true;
}
if (KeyBindings.toggleEditMode.isActiveAndMatches(input)) {
worldConfig.toggleEditModeEnabled();
return true;
}
if (KeyBindings.focusSearch.isActiveAndMatches(input)) {
setKeyboardFocus(true);
return true;
}
}
return false;
}
public boolean onCharTyped(char codePoint, int modifiers) {
if (isListDisplayed() &&
hasKeyboardFocus() &&
searchField.charTyped(codePoint, modifiers)) {
boolean changed = worldConfig.setFilterText(searchField.getText());
if (changed) {
updateLayout(true);
}
return true;
}
return false;
}
public boolean onKeyPressed(int keyCode, int scanCode, int modifiers) {
if (isListDisplayed()) {
if (hasKeyboardFocus() &&
searchField.keyPressed(keyCode, scanCode, modifiers)) {
boolean changed = worldConfig.setFilterText(searchField.getText());
if (changed) {
updateLayout(true);
}
return true;
}
return this.contents.onKeyPressed(keyCode, scanCode, modifiers);
}
return false;
}
@Nullable
@Override
public Object getIngredientUnderMouse() {
if (isListDisplayed()) {
IIngredientListElement<?> elementUnderMouse = this.contents.getElementUnderMouse();
if (elementUnderMouse != null) {
return elementUnderMouse.getIngredient();
}
}
return null;
}
@SuppressWarnings("unchecked")
@Nullable
@Override
public <T> T getIngredientUnderMouse(IIngredientType<T> ingredientType) {
Object ingredient = getIngredientUnderMouse();
if (ingredientType.getIngredientClass().isInstance(ingredient)) {
return (T) ingredient;
}
return null;
}
public void onSetFilterText(String filterText) {
this.searchField.setText(filterText);
updateLayout(true);
}
@Override
public ImmutableList<Object> getVisibleIngredients() {
if (isListDisplayed()) {
ImmutableList.Builder<Object> visibleIngredients = ImmutableList.builder();
List<IIngredientListElement<?>> visibleElements = this.contents.getVisibleElements();
for (IIngredientListElement<?> element : visibleElements) {
Object ingredient = element.getIngredient();
visibleIngredients.add(ingredient);
}
return visibleIngredients.build();
}
return ImmutableList.of();
}
}