Skip to content

Commit cd62599

Browse files
committed
Cleanup renderItems
1 parent fe07231 commit cd62599

3 files changed

Lines changed: 50 additions & 124 deletions

File tree

src/main/java/turing/tmb/client/TMBRenderer.java

Lines changed: 46 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import java.util.Collection;
3232
import java.util.List;
33+
import java.util.function.Function;
3334
import java.util.stream.Collectors;
3435

3536
@Environment(EnvType.CLIENT)
@@ -117,12 +118,16 @@ public static void onTick() {
117118
scroll(Mouse.getDWheel());
118119
}
119120

121+
protected static void playClickSound(Minecraft mc) {
122+
mc.sndManager.playSound("random.click", SoundCategory.GUI_SOUNDS, 1.0F, 1.0F);
123+
}
124+
120125
public static void mouseClicked(int mouseX, int mouseY, int width, int height, Minecraft mc) {
121126
if (!show) return;
122127
boolean left = leftButton.mouseClicked(mc, mouseX, mouseY);
123128
boolean right = rightButton.mouseClicked(mc, mouseX, mouseY);
124129
if (left || right) {
125-
mc.sndManager.playSound("random.click", SoundCategory.GUI_SOUNDS, 1.0F, 1.0F);
130+
playClickSound(mc);
126131
int change = 1;
127132
if (left) {
128133
change = -1;
@@ -135,7 +140,7 @@ public static void mouseClicked(int mouseX, int mouseY, int width, int height, M
135140
boolean left2 = favoritesLeftButton.mouseClicked(mc, mouseX, mouseY);
136141
boolean right2 = favoritesRightButton.mouseClicked(mc, mouseX, mouseY);
137142
if (left2 || right2) {
138-
mc.sndManager.playSound("random.click", SoundCategory.GUI_SOUNDS, 1.0F, 1.0F);
143+
playClickSound(mc);
139144
int change = 1;
140145
if (left2) {
141146
change = -1;
@@ -163,152 +168,65 @@ private static void scroll(int direction) {
163168
}
164169

165170
@SuppressWarnings("unchecked")
166-
public static void renderItems(int mouseX, int mouseY, int width, int height, Minecraft mc, float pt, @Nullable IGuiProperties properties) {
171+
public static void renderList(Function<ITMBRuntime, Collection<ITypedIngredient<?>>> listToDisplay, int startX, int currentPage, int mouseX, int mouseY, int width, int height, Minecraft mc, @Nullable IGuiProperties properties) {
167172
if (!initialized) {
168173
init(mc);
169174
return;
170175
}
171-
if (!show) {
172-
return;
173-
}
176+
if (!show) return;
177+
if (debounce > 0) debounce--;
174178

175-
Screen currentScreen = mc.currentScreen;
176179
ITMBRuntime runtime = TMB.getRuntime();
177-
Collection<ITypedIngredient<?>> toDisplay = getToDisplay(runtime);
178-
179-
int startX = (int) (width / 3.5F);
180-
181-
if (currentScreen instanceof ScreenContainerAbstract) {
182-
startX = Math.min(((width / 2) - ((ScreenContainerAbstract) (currentScreen)).xSize / 2) - buttonWidth, startX);
183-
} else if (properties != null) {
184-
startX = Math.min(((width / 2) - properties.guiXSize() / 2) - buttonWidth, startX);
185-
}
186-
187-
int itemsX = (startX / buttonWidth);
188-
int itemsY = Math.min((height / buttonWidth) - 1, ((height - 28) / buttonWidth));
189-
int xOffset = 0;
190-
int yOffset = 1;
191-
192-
int itemsPerPage = itemsX * itemsY;
193-
if (itemsPerPage <= 0) return;
194-
pageCount = toDisplay.size() / itemsPerPage;
195-
196-
if (currentPage > pageCount) currentPage = pageCount;
197-
198-
List<ITypedIngredient<?>> pageList = toDisplay.stream().skip((long) itemsPerPage * currentPage).limit(itemsPerPage).collect(Collectors.toList());
199-
200-
TooltipBuilder tooltipBuilder = new TooltipBuilder();
201-
boolean isCtrl = Keyboard.isKeyDown(29) || Keyboard.isKeyDown(157);
202-
boolean isShift = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
203-
204-
hoveredItem = null;
205-
int i = 0;
206-
loop: for (int y = 0; y < itemsY; y++) {
207-
for (int x = 0; x < itemsX; x++) {
208-
if (i >= pageList.size()) break loop;
209-
ITypedIngredient<Object> ingredient = (ITypedIngredient<Object>) pageList.get(i);
210-
int xOff = width - startX + (xOffset * 16) + (2 * xOffset);
211-
int yOff = 8 + (yOffset * 16) + (2 * yOffset);
212-
213-
ingredient.getType().getRenderer(runtime).render(runtime.getGuiHelper(), ingredient.getIngredient(), xOff, yOff);
214-
215-
if (mouseX >= xOff && mouseX < xOff + 16 && mouseY >= yOff && mouseY < yOff + 16) {
216-
hoveredItem = ingredient;
217-
ingredient.getType().getRenderer(runtime).getTooltip(tooltipBuilder, hoveredItem.getIngredient(), isCtrl, isShift);
218-
RenderUtil.renderItemSelected(runtime.getGuiHelper(), xOff, yOff);
219-
}
220-
221-
i++;
222-
xOffset++;
223-
if (xOffset >= itemsX) {
224-
xOffset = 0;
225-
}
226-
}
227-
yOffset++;
228-
}
229-
230-
if (hoveredItem != null) {
231-
if (!tooltipBuilder.getLines().isEmpty()) {
232-
StringBuilder builder = new StringBuilder();
233-
tooltipBuilder.getLines().forEach(str -> builder.append(str).append("\n"));
234-
GLRenderer.pushFrame();
235-
tooltip.render(builder.toString(), mouseX, mouseY, 8, -8);
236-
GLRenderer.popFrame();
237-
}
238-
239-
if (enabledRecipes && debounce <= 0) {
240-
if (GameSettings.KEY_SHOW_RECIPE.isPressed()) {
241-
debounce = 20;
242-
runtime.showRecipe(hoveredItem, RecipeIngredientRole.OUTPUT);
243-
} else if (GameSettings.KEY_SHOW_USAGE.isPressed()) {
244-
debounce = 20;
245-
runtime.showRecipe(hoveredItem, RecipeIngredientRole.INPUT);
246-
}
247-
}
248-
249-
if (TMBOptions.keyAddFavourite.isPressed()) {
250-
if (runtime.getFavourites().stream().noneMatch(it -> it.matches(hoveredItem.getIngredient()))) {
251-
runtime.getFavourites().add(hoveredItem);
252-
}
253-
}
254-
}
255-
}
256-
257-
public static void renderItems2(int mouseX, int mouseY, int width, int height, Minecraft mc, float pt, @Nullable IGuiProperties properties) {
258-
if (!initialized) {
259-
init(mc);
260-
return;
261-
}
262-
if (!show) {
263-
return;
264-
}
265-
if(debounce > 0) debounce--;
180+
Collection<ITypedIngredient<?>> list = listToDisplay.apply(runtime);
266181

182+
int X = (int) (width / 3.5F);
267183
Screen currentScreen = mc.currentScreen;
268-
ITMBRuntime runtime = TMB.getRuntime();
269-
Collection<ITypedIngredient<?>> toDisplay = runtime.getFavourites();
270-
271-
int startX = (int) (width / 3.5F);
272-
273-
if (currentScreen instanceof ScreenContainerAbstract) {
274-
startX = Math.min(((width / 2) - ((ScreenContainerAbstract) (currentScreen)).xSize / 2) - buttonWidth, startX);
184+
if (currentScreen instanceof ScreenContainerAbstract containerScreen) {
185+
X = Math.min(((width / 2) - containerScreen.xSize / 2) - buttonWidth, X);
275186
} else if (properties != null) {
276-
startX = Math.min(((width / 2) - properties.guiXSize() / 2) - buttonWidth, startX);
187+
X = Math.min(((width / 2) - properties.guiXSize() / 2) - buttonWidth, X);
277188
}
278189

279-
int itemsX = (startX / buttonWidth);
280-
int itemsY = Math.min((height / buttonWidth) - 1, ((height - 28) / buttonWidth));
281190
int xOffset = 0;
282191
int yOffset = 1;
283-
284-
startX = 0;
192+
int itemsX = X / buttonWidth;
193+
int itemsY = Math.min((height / buttonWidth) - 1, ((height - 28) / buttonWidth));
285194

286195
int itemsPerPage = itemsX * itemsY;
287196
if (itemsPerPage <= 0) return;
288-
favouritePageCount = toDisplay.size() / itemsPerPage;
289197

290-
if (currentFavouritePage > favouritePageCount) currentFavouritePage = favouritePageCount;
198+
int numPages = list.size() / itemsPerPage;
199+
if (currentPage > numPages) currentPage = numPages;
291200

292-
List<ITypedIngredient<?>> pageList = toDisplay.stream().skip((long) itemsPerPage * currentFavouritePage).limit(itemsPerPage).collect(Collectors.toList());
201+
if (startX == 0) {
202+
favouritePageCount = numPages;
203+
} else {
204+
pageCount = numPages;
205+
startX = X;
206+
}
293207

208+
boolean isHoldingCtrl = Keyboard.isKeyDown(29) || Keyboard.isKeyDown(157);
209+
boolean isHoldingShift = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
294210
TooltipBuilder tooltipBuilder = new TooltipBuilder();
295-
boolean isCtrl = Keyboard.isKeyDown(29) || Keyboard.isKeyDown(157);
296-
boolean isShift = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
211+
212+
List<ITypedIngredient<?>> pageList = list.stream().skip((long) itemsPerPage * currentPage).limit(itemsPerPage).toList();
297213

298214
hoveredItem = null;
215+
299216
int i = 0;
300-
loop: for (int y = 0; y < itemsY; y++) {
217+
yLoop: for (int y = 0; y < itemsY; y++) {
301218
for (int x = 0; x < itemsX; x++) {
302-
if (i >= pageList.size()) break loop;
219+
if (i >= pageList.size()) break yLoop;
220+
303221
ITypedIngredient<Object> ingredient = (ITypedIngredient<Object>) pageList.get(i);
304-
int xOff = /*width -*/ startX + (xOffset * 16) + (2 * xOffset);
222+
int xOff = (startX != 0 ? width - startX : 0) + (xOffset * 16) + (2 * xOffset);
305223
int yOff = 8 + (yOffset * 16) + (2 * yOffset);
306224

307225
ingredient.getType().getRenderer(runtime).render(runtime.getGuiHelper(), ingredient.getIngredient(), xOff, yOff);
308226

309227
if (mouseX >= xOff && mouseX < xOff + 16 && mouseY >= yOff && mouseY < yOff + 16) {
310228
hoveredItem = ingredient;
311-
ingredient.getType().getRenderer(runtime).getTooltip(tooltipBuilder, hoveredItem.getIngredient(), isCtrl, isShift);
229+
ingredient.getType().getRenderer(runtime).getTooltip(tooltipBuilder, hoveredItem.getIngredient(), isHoldingCtrl, isHoldingShift);
312230
RenderUtil.renderItemSelected(runtime.getGuiHelper(), xOff, yOff);
313231
}
314232

@@ -342,15 +260,23 @@ public static void renderItems2(int mouseX, int mouseY, int width, int height, M
342260

343261
if (TMBOptions.keyAddFavourite.isPressed() && debounce <= 0) {
344262
debounce = 10;
345-
if (runtime.getFavourites().stream().anyMatch(it -> it.matches(hoveredItem.getIngredient()))) {
346-
runtime.getFavourites().removeIf(it -> it.matches(hoveredItem.getIngredient()));
347-
} else {
263+
if (runtime.getFavourites().stream().noneMatch(ingredient -> ingredient.matches(hoveredItem.getIngredient()))) {
348264
runtime.getFavourites().add(hoveredItem);
265+
} else if (startX == 0) {
266+
runtime.getFavourites().removeIf(ingredient -> ingredient.matches(hoveredItem.getIngredient()));
349267
}
350268
}
351269
}
352270
}
353271

272+
public static void renderFavorites(int mouseX, int mouseY, int width, int height, Minecraft mc, @Nullable IGuiProperties properties) {
273+
renderList(ITMBRuntime::getFavourites, 0, currentFavouritePage, mouseX, mouseY, width, height, mc, properties);
274+
}
275+
276+
public static void renderItems(int mouseX, int mouseY, int width, int height, Minecraft mc, @Nullable IGuiProperties properties) {
277+
renderList(TMBRenderer::getToDisplay, (int) (width / 3.5F), currentPage, mouseX, mouseY, width, height, mc, properties);
278+
}
279+
354280
private static Collection<ITypedIngredient<?>> getToDisplay(ITMBRuntime runtime) {
355281
IIngredientIndex index = runtime.getIngredientIndex();
356282

src/main/java/turing/tmb/mixin/client/ScreenContainerAbstractMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public ScreenContainerAbstractMixin() {
3030
@Inject(method = "render", at = @At("TAIL"))
3131
public void render(int mouseX, int mouseY, float pt, CallbackInfo ci) {
3232
TMBRenderer.renderHeader(false, mouseX, mouseY, width, height, mc, null);
33-
TMBRenderer.renderItems(mouseX, mouseY, width, height, mc, pt, null);
33+
TMBRenderer.renderItems(mouseX, mouseY, width, height, mc, null);
3434
TMBRenderer.renderHeader(true, mouseX, mouseY, width, height, mc, null);
35-
TMBRenderer.renderItems2(mouseX, mouseY, width, height, mc, pt, null);
35+
TMBRenderer.renderFavorites(mouseX, mouseY, width, height, mc, null);
3636
}
3737

3838
@Inject(method = "tick", at = @At("HEAD"))

src/main/java/turing/tmb/mixin/client/ScreenMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public void render(int mouseX, int mouseY, float partialTick, CallbackInfo ci) {
3030
if (GuiHelper.extraScreens.containsKey(t.getClass().getCanonicalName())) {
3131
IGuiProperties properties = GuiHelper.extraScreens.get(t.getClass().getCanonicalName()).apply(t);
3232
TMBRenderer.renderHeader(false, mouseX, mouseY, width, height, mc, properties);
33-
TMBRenderer.renderItems(mouseX, mouseY, width, height, mc, partialTick, properties);
33+
TMBRenderer.renderItems(mouseX, mouseY, width, height, mc, properties);
3434
TMBRenderer.renderHeader(true, mouseX, mouseY, width, height, mc, properties);
35-
TMBRenderer.renderItems2(mouseX, mouseY, width, height, mc, partialTick, properties);
35+
TMBRenderer.renderFavorites(mouseX, mouseY, width, height, mc, properties);
3636
}
3737
}
3838

0 commit comments

Comments
 (0)