From a7ea986cde523d0b9fff16575d235c0ddfe5e439 Mon Sep 17 00:00:00 2001 From: Gegy Date: Tue, 19 Nov 2024 03:23:14 +0100 Subject: [PATCH] Fix: correct hint display for shaped recipes smaller than 3 columns --- .../game/handler/GameCraftingBeeHandler.java | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/lovetropics/minigames/client/game/handler/GameCraftingBeeHandler.java b/src/main/java/com/lovetropics/minigames/client/game/handler/GameCraftingBeeHandler.java index e0c0590c..8b72db1a 100644 --- a/src/main/java/com/lovetropics/minigames/client/game/handler/GameCraftingBeeHandler.java +++ b/src/main/java/com/lovetropics/minigames/client/game/handler/GameCraftingBeeHandler.java @@ -31,6 +31,7 @@ import net.minecraft.core.component.DataComponents; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; +import net.minecraft.util.Mth; import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.ItemDisplayContext; import net.minecraft.world.item.ItemStack; @@ -98,12 +99,12 @@ public void renderImage(Font font, int x, int y, GuiGraphics guiGraphics) { var ingredient = recipeHint.grid.get(i); if (ingredient.isEmpty()) continue; - var size = recipeHint.grid.size() == 4 ? 2 : 3; + var width = recipeHint.width(); guiGraphics.renderFakeItem( resolveIngredient(ingredient), - x + 1 + 18 * (i % size), - y + 1 + 18 * (i / size) + x + 1 + 18 * (i % width), + y + 1 + 18 * (i / width) ); } } @@ -138,7 +139,7 @@ protected void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, flo var tooltipLines = new ArrayList<>(Screen.getTooltipFromItem(Minecraft.getInstance(), craft.output())); if (craft.done()) { tooltipLines.set(0, tooltipLines.getFirst().copy().withStyle(ChatFormatting.GREEN)); - } else if (hint == null || hint.expectedIngredientCount() != hint.grid().stream().filter(Predicate.not(Ingredient::isEmpty)).count()) { + } else if (hint == null || hint.expectedIngredientCount() != hint.filledIngredientCount()) { tooltipLines.add(CraftingBeeTexts.HINT); tooltipLines.add(CraftingBeeTexts.HINTS_LEFT.apply(hintsRemaining).withStyle(ChatFormatting.AQUA)); } @@ -163,14 +164,25 @@ public void onClick(double mouseX, double mouseY, int button) { var recipe = new SelectedRecipe(craft.recipeId(), Minecraft.getInstance().player.connection.getRecipeManager()); var ingredients = recipe.decompose(); - var grid = hintGrids.computeIfAbsent(craft.recipeId(), k -> new RecipeHint( - NonNullList.withSize( - recipe.recipe().map(shaped -> shaped.getWidth() * shaped.getHeight(), shapeless -> shapeless.getIngredients().size() > 3 ? 9 : shapeless.getIngredients().size()), - Ingredient.EMPTY), - (int)ingredients.stream().filter(Predicate.not(Ingredient::isEmpty)).count() - )); - - int filledGridAmount = (int) grid.grid().stream().filter(Predicate.not(Ingredient::isEmpty)).count(); + var grid = hintGrids.computeIfAbsent(craft.recipeId(), k -> { + int expectedIngredientCount = (int) ingredients.stream().filter(Predicate.not(Ingredient::isEmpty)).count(); + return recipe.recipe().map( + shaped -> new RecipeHint( + NonNullList.withSize(shaped.getWidth() * shaped.getHeight(), Ingredient.EMPTY), + expectedIngredientCount, + shaped.getWidth(), + shaped.getHeight() + ), + shapeless -> new RecipeHint( + NonNullList.withSize(shapeless.getIngredients().size(), Ingredient.EMPTY), + expectedIngredientCount, + Math.min(shapeless.getIngredients().size(), 3), + Mth.positiveCeilDiv(shapeless.getIngredients().size(), 3) + ) + ); + }); + + int filledGridAmount = grid.filledIngredientCount(); if (grid.expectedIngredientCount() == filledGridAmount) return; record PositionedIngredient(Ingredient ingredient, int position) {} @@ -333,7 +345,13 @@ public TintedVertexConsumer(VertexConsumer wrapped, float red, float green, floa public record RecipeHint( NonNullList grid, - int expectedIngredientCount - ) implements TooltipComponent {} + int expectedIngredientCount, + int width, + int height + ) implements TooltipComponent { + public int filledIngredientCount() { + return (int) grid.stream().filter(ingredient -> !ingredient.isEmpty()).count(); + } + } }