| 1 | package net.minecraft.client.gui.screens.recipebook;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.Collections;
|
| 6 | import java.util.List;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 11 | import net.minecraft.client.gui.components.Renderable;
|
| 12 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 13 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 14 | import net.minecraft.client.gui.screens.recipebook.OverlayRecipeComponent.OverlayRecipeButton.Pos;
|
| 15 | import net.minecraft.client.input.MouseButtonEvent;
|
| 16 | import net.minecraft.client.renderer.RenderPipelines;
|
| 17 | import net.minecraft.network.chat.CommonComponents;
|
| 18 | import net.minecraft.recipebook.PlaceRecipeHelper;
|
| 19 | import net.minecraft.resources.Identifier;
|
| 20 | import net.minecraft.util.Mth;
|
| 21 | import net.minecraft.util.context.ContextMap;
|
| 22 | import net.minecraft.world.item.ItemStack;
|
| 23 | import net.minecraft.world.item.crafting.display.FurnaceRecipeDisplay;
|
| 24 | import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
| 25 | import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;
|
| 26 | import net.minecraft.world.item.crafting.display.RecipeDisplayId;
|
| 27 | import net.minecraft.world.item.crafting.display.ShapedCraftingRecipeDisplay;
|
| 28 | import net.minecraft.world.item.crafting.display.ShapelessCraftingRecipeDisplay;
|
| 29 | import net.minecraft.world.item.crafting.display.SlotDisplay;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 |
|
| 32 | @Environment(EnvType.CLIENT)
|
| 33 | public class OverlayRecipeComponent implements GuiEventListener, Renderable {
|
| 34 | private static final Identifier OVERLAY_RECIPE_SPRITE = Identifier.withDefaultNamespace("recipe_book/overlay_recipe");
|
| 35 | private static final int MAX_ROW = 4;
|
| 36 | private static final int MAX_ROW_LARGE = 5;
|
| 37 | private static final float ITEM_RENDER_SCALE = 0.375F;
|
| 38 | public static final int BUTTON_SIZE = 25;
|
| 39 | private final List<OverlayRecipeComponent.OverlayRecipeButton> recipeButtons = Lists.newArrayList();
|
| 40 | private boolean isVisible;
|
| 41 | private int x;
|
| 42 | private int y;
|
| 43 | private RecipeCollection collection = RecipeCollection.EMPTY;
|
| 44 | private @Nullable RecipeDisplayId lastRecipeClicked;
|
| 45 | private final SlotSelectTime slotSelectTime;
|
| 46 | private final boolean isFurnaceMenu;
|
| 47 |
|
| 48 | public OverlayRecipeComponent(final SlotSelectTime slotSelectTime, final boolean isFurnaceMenu) {
|
| 49 | this.slotSelectTime = slotSelectTime;
|
| 50 | this.isFurnaceMenu = isFurnaceMenu;
|
| 51 | }
|
| 52 |
|
| 53 | public void init(
|
| 54 | final RecipeCollection collection,
|
| 55 | final ContextMap context,
|
| 56 | final boolean isFiltering,
|
| 57 | final int buttonX,
|
| 58 | final int buttonY,
|
| 59 | final int centerX,
|
| 60 | final int centerY,
|
| 61 | final float buttonWidth
|
| 62 | ) {
|
| 63 | this.collection = collection;
|
| 64 | List<RecipeDisplayEntry> craftable = collection.getSelectedRecipes(RecipeCollection.CraftableStatus.CRAFTABLE);
|
| 65 | List<RecipeDisplayEntry> unCraftable = isFiltering ? Collections.emptyList() : collection.getSelectedRecipes(RecipeCollection.CraftableStatus.NOT_CRAFTABLE);
|
| 66 | int craftables = craftable.size();
|
| 67 | int total = craftables + unCraftable.size();
|
| 68 | int maxRow = total <= 16 ? 4 : 5;
|
| 69 | int rows = (int)Math.ceil((float)total / maxRow);
|
| 70 | this.x = buttonX;
|
| 71 | this.y = buttonY;
|
| 72 | float rightPos = this.x + Math.min(total, maxRow) * 25;
|
| 73 | float maxLeftPos = centerX + 50;
|
| 74 | if (rightPos > maxLeftPos) {
|
| 75 | this.x = (int)(this.x - buttonWidth * (int)((rightPos - maxLeftPos) / buttonWidth));
|
| 76 | }
|
| 77 |
|
| 78 | float bottomPos = this.y + rows * 25;
|
| 79 | float maxBottomPos = centerY + 50;
|
| 80 | if (bottomPos > maxBottomPos) {
|
| 81 | this.y = (int)(this.y - buttonWidth * Mth.ceil((bottomPos - maxBottomPos) / buttonWidth));
|
| 82 | }
|
| 83 |
|
| 84 | float topPos = this.y;
|
| 85 | float maxTopPos = centerY - 100;
|
| 86 | if (topPos < maxTopPos) {
|
| 87 | this.y = (int)(this.y - buttonWidth * Mth.ceil((topPos - maxTopPos) / buttonWidth));
|
| 88 | }
|
| 89 |
|
| 90 | this.isVisible = true;
|
| 91 | this.recipeButtons.clear();
|
| 92 |
|
| 93 | for (int i = 0; i < total; i++) {
|
| 94 | boolean canCraft = i < craftables;
|
| 95 | RecipeDisplayEntry recipe = canCraft ? craftable.get(i) : unCraftable.get(i - craftables);
|
| 96 | int x = this.x + 4 + 25 * (i % maxRow);
|
| 97 | int y = this.y + 5 + 25 * (i / maxRow);
|
| 98 | if (this.isFurnaceMenu) {
|
| 99 | this.recipeButtons.add(new OverlayRecipeComponent.OverlaySmeltingRecipeButton(x, y, recipe.id(), recipe.display(), context, canCraft));
|
| 100 | } else {
|
| 101 | this.recipeButtons.add(new OverlayRecipeComponent.OverlayCraftingRecipeButton(x, y, recipe.id(), recipe.display(), context, canCraft));
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | this.lastRecipeClicked = null;
|
| 106 | }
|
| 107 |
|
| 108 | public RecipeCollection getRecipeCollection() {
|
| 109 | return this.collection;
|
| 110 | }
|
| 111 |
|
| 112 | public @Nullable RecipeDisplayId getLastRecipeClicked() {
|
| 113 | return this.lastRecipeClicked;
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 118 | if (event.button() != 1) {
|
| 119 | return false;
|
| 120 | }
|
| 121 |
|
| 122 | for (OverlayRecipeComponent.OverlayRecipeButton recipeButton : this.recipeButtons) {
|
| 123 | if (recipeButton.mouseClicked(event, doubleClick)) {
|
| 124 | this.lastRecipeClicked = recipeButton.recipe;
|
| 125 | return true;
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | return false;
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public boolean isMouseOver(final double mouseX, final double mouseY) {
|
| 134 | return false;
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 139 | if (this.isVisible) {
|
| 140 | int maxRow = this.recipeButtons.size() <= 16 ? 4 : 5;
|
| 141 | int width = Math.min(this.recipeButtons.size(), maxRow);
|
| 142 | int height = Mth.ceil((float)this.recipeButtons.size() / maxRow);
|
| 143 | int border = 4;
|
| 144 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, OVERLAY_RECIPE_SPRITE, this.x, this.y, width * 25 + 8, height * 25 + 8);
|
| 145 |
|
| 146 | for (OverlayRecipeComponent.OverlayRecipeButton component : this.recipeButtons) {
|
| 147 | component.extractRenderState(graphics, mouseX, mouseY, a);
|
| 148 | }
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | public void setVisible(final boolean visible) {
|
| 153 | this.isVisible = visible;
|
| 154 | }
|
| 155 |
|
| 156 | public boolean isVisible() {
|
| 157 | return this.isVisible;
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | public void setFocused(final boolean focused) {
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public boolean isFocused() {
|
| 166 | return false;
|
| 167 | }
|
| 168 |
|
| 169 | @Environment(EnvType.CLIENT)
|
| 170 | private class OverlayCraftingRecipeButton extends OverlayRecipeComponent.OverlayRecipeButton {
|
| 171 | private static final Identifier ENABLED_SPRITE = Identifier.withDefaultNamespace("recipe_book/crafting_overlay");
|
| 172 | private static final Identifier HIGHLIGHTED_ENABLED_SPRITE = Identifier.withDefaultNamespace("recipe_book/crafting_overlay_highlighted");
|
| 173 | private static final Identifier DISABLED_SPRITE = Identifier.withDefaultNamespace("recipe_book/crafting_overlay_disabled");
|
| 174 | private static final Identifier HIGHLIGHTED_DISABLED_SPRITE = Identifier.withDefaultNamespace("recipe_book/crafting_overlay_disabled_highlighted");
|
| 175 | private static final int GRID_WIDTH = 3;
|
| 176 | private static final int GRID_HEIGHT = 3;
|
| 177 |
|
| 178 | public OverlayCraftingRecipeButton(
|
| 179 | final int x, final int y, final RecipeDisplayId id, final RecipeDisplay recipe, final ContextMap context, final boolean isCraftable
|
| 180 | ) {
|
| 181 | super(x, y, id, isCraftable, calculateIngredientsPositions(recipe, context));
|
| 182 | }
|
| 183 |
|
| 184 | private static List<Pos> calculateIngredientsPositions(final RecipeDisplay recipe, final ContextMap context) {
|
| 185 | List<Pos> result = new ArrayList<>();
|
| 186 | switch (recipe) {
|
| 187 | case ShapedCraftingRecipeDisplay shaped:
|
| 188 | PlaceRecipeHelper.placeRecipe(3, 3, shaped.width(), shaped.height(), shaped.ingredients(), (ingredient, gridIndex, gridXPos, gridYPos) -> {
|
| 189 | List<ItemStack> itemsx = ingredient.resolveForStacks(context);
|
| 190 | if (!itemsx.isEmpty()) {
|
| 191 | result.add(createGridPos(gridXPos, gridYPos, itemsx));
|
| 192 | }
|
| 193 | });
|
| 194 | break;
|
| 195 | case ShapelessCraftingRecipeDisplay shapeless:
|
| 196 | label19: {
|
| 197 | List<SlotDisplay> ingredients = shapeless.ingredients();
|
| 198 |
|
| 199 | for (int i = 0; i < ingredients.size(); i++) {
|
| 200 | List<ItemStack> items = ingredients.get(i).resolveForStacks(context);
|
| 201 | if (!items.isEmpty()) {
|
| 202 | result.add(createGridPos(i % 3, i / 3, items));
|
| 203 | }
|
| 204 | }
|
| 205 | break label19;
|
| 206 | }
|
| 207 | default:
|
| 208 | }
|
| 209 |
|
| 210 | return result;
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | protected Identifier getSprite(final boolean isCraftable) {
|
| 215 | if (isCraftable) {
|
| 216 | return this.isHoveredOrFocused() ? HIGHLIGHTED_ENABLED_SPRITE : ENABLED_SPRITE;
|
| 217 | } else {
|
| 218 | return this.isHoveredOrFocused() ? HIGHLIGHTED_DISABLED_SPRITE : DISABLED_SPRITE;
|
| 219 | }
|
| 220 | }
|
| 221 | }
|
| 222 |
|
| 223 | @Environment(EnvType.CLIENT)
|
| 224 | private abstract class OverlayRecipeButton extends AbstractWidget {
|
| 225 | private final RecipeDisplayId recipe;
|
| 226 | private final boolean isCraftable;
|
| 227 | private final List<Pos> slots;
|
| 228 |
|
| 229 | public OverlayRecipeButton(final int x, final int y, final RecipeDisplayId recipe, final boolean isCraftable, final List<Pos> slots) {
|
| 230 | super(x, y, 24, 24, CommonComponents.EMPTY);
|
| 231 | this.slots = slots;
|
| 232 | this.recipe = recipe;
|
| 233 | this.isCraftable = isCraftable;
|
| 234 | }
|
| 235 |
|
| 236 | protected static Pos createGridPos(final int gridXPos, final int gridYPos, final List<ItemStack> itemStacks) {
|
| 237 | return new Pos(3 + gridXPos * 7, 3 + gridYPos * 7, itemStacks);
|
| 238 | }
|
| 239 |
|
| 240 | protected abstract Identifier getSprite(boolean isCraftable);
|
| 241 |
|
| 242 | @Override
|
| 243 | public void updateWidgetNarration(final NarrationElementOutput output) {
|
| 244 | this.defaultButtonNarrationText(output);
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 249 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, this.getSprite(this.isCraftable), this.getX(), this.getY(), this.width, this.height);
|
| 250 | float gridPosX = this.getX() + 2;
|
| 251 | float gridPosY = this.getY() + 2;
|
| 252 |
|
| 253 | for (Pos pos : this.slots) {
|
| 254 | graphics.pose().pushMatrix();
|
| 255 | graphics.pose().translate(gridPosX + pos.x, gridPosY + pos.y);
|
| 256 | graphics.pose().scale(0.375F, 0.375F);
|
| 257 | graphics.pose().translate(-8.0F, -8.0F);
|
| 258 | graphics.item(pos.selectIngredient(OverlayRecipeComponent.this.slotSelectTime.currentIndex()), 0, 0);
|
| 259 | graphics.pose().popMatrix();
|
| 260 | }
|
| 261 | }
|
| 262 | }
|
| 263 |
|
| 264 | @Environment(EnvType.CLIENT)
|
| 265 | private class OverlaySmeltingRecipeButton extends OverlayRecipeComponent.OverlayRecipeButton {
|
| 266 | private static final Identifier ENABLED_SPRITE = Identifier.withDefaultNamespace("recipe_book/furnace_overlay");
|
| 267 | private static final Identifier HIGHLIGHTED_ENABLED_SPRITE = Identifier.withDefaultNamespace("recipe_book/furnace_overlay_highlighted");
|
| 268 | private static final Identifier DISABLED_SPRITE = Identifier.withDefaultNamespace("recipe_book/furnace_overlay_disabled");
|
| 269 | private static final Identifier HIGHLIGHTED_DISABLED_SPRITE = Identifier.withDefaultNamespace("recipe_book/furnace_overlay_disabled_highlighted");
|
| 270 |
|
| 271 | public OverlaySmeltingRecipeButton(
|
| 272 | final int x, final int y, final RecipeDisplayId id, final RecipeDisplay recipe, final ContextMap context, final boolean isCraftable
|
| 273 | ) {
|
| 274 | super(x, y, id, isCraftable, calculateIngredientsPositions(recipe, context));
|
| 275 | }
|
| 276 |
|
| 277 | private static List<Pos> calculateIngredientsPositions(final RecipeDisplay recipe, final ContextMap context) {
|
| 278 | if (recipe instanceof FurnaceRecipeDisplay furnaceRecipe) {
|
| 279 | List<ItemStack> items = furnaceRecipe.ingredient().resolveForStacks(context);
|
| 280 | if (!items.isEmpty()) {
|
| 281 | return List.of(createGridPos(1, 1, items));
|
| 282 | }
|
| 283 | }
|
| 284 |
|
| 285 | return List.of();
|
| 286 | }
|
| 287 |
|
| 288 | @Override
|
| 289 | protected Identifier getSprite(final boolean isCraftable) {
|
| 290 | if (isCraftable) {
|
| 291 | return this.isHoveredOrFocused() ? HIGHLIGHTED_ENABLED_SPRITE : ENABLED_SPRITE;
|
| 292 | } else {
|
| 293 | return this.isHoveredOrFocused() ? HIGHLIGHTED_DISABLED_SPRITE : DISABLED_SPRITE;
|
| 294 | }
|
| 295 | }
|
| 296 | }
|
| 297 | }
|
| 298 |
|