| 1 | package net.minecraft.client.gui.screens.recipebook;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.components.WidgetSprites;
|
| 7 | import net.minecraft.network.chat.Component;
|
| 8 | import net.minecraft.recipebook.PlaceRecipeHelper;
|
| 9 | import net.minecraft.resources.Identifier;
|
| 10 | import net.minecraft.util.context.ContextMap;
|
| 11 | import net.minecraft.world.entity.player.StackedItemContents;
|
| 12 | import net.minecraft.world.inventory.AbstractCraftingMenu;
|
| 13 | import net.minecraft.world.inventory.Slot;
|
| 14 | import net.minecraft.world.item.Items;
|
| 15 | import net.minecraft.world.item.crafting.RecipeBookCategories;
|
| 16 | import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
| 17 | import net.minecraft.world.item.crafting.display.ShapedCraftingRecipeDisplay;
|
| 18 | import net.minecraft.world.item.crafting.display.ShapelessCraftingRecipeDisplay;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class CraftingRecipeBookComponent extends RecipeBookComponent<AbstractCraftingMenu> {
|
| 22 | private static final WidgetSprites FILTER_BUTTON_SPRITES = new WidgetSprites(
|
| 23 | Identifier.withDefaultNamespace("recipe_book/filter_enabled"),
|
| 24 | Identifier.withDefaultNamespace("recipe_book/filter_disabled"),
|
| 25 | Identifier.withDefaultNamespace("recipe_book/filter_enabled_highlighted"),
|
| 26 | Identifier.withDefaultNamespace("recipe_book/filter_disabled_highlighted")
|
| 27 | );
|
| 28 | private static final Component ONLY_CRAFTABLES_TOOLTIP = Component.translatable("gui.recipebook.toggleRecipes.craftable");
|
| 29 | private static final List<RecipeBookComponent.TabInfo> TABS = List.of(
|
| 30 | new RecipeBookComponent.TabInfo(SearchRecipeBookCategory.CRAFTING),
|
| 31 | new RecipeBookComponent.TabInfo(Items.IRON_AXE, Items.GOLDEN_SWORD, RecipeBookCategories.CRAFTING_EQUIPMENT),
|
| 32 | new RecipeBookComponent.TabInfo(Items.BRICKS, RecipeBookCategories.CRAFTING_BUILDING_BLOCKS),
|
| 33 | new RecipeBookComponent.TabInfo(Items.LAVA_BUCKET, Items.APPLE, RecipeBookCategories.CRAFTING_MISC),
|
| 34 | new RecipeBookComponent.TabInfo(Items.REDSTONE, RecipeBookCategories.CRAFTING_REDSTONE)
|
| 35 | );
|
| 36 |
|
| 37 | public CraftingRecipeBookComponent(final AbstractCraftingMenu menu) {
|
| 38 | super(menu, TABS);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected boolean isCraftingSlot(final Slot slot) {
|
| 43 | return this.menu.getResultSlot() == slot || this.menu.getInputGridSlots().contains(slot);
|
| 44 | }
|
| 45 |
|
| 46 | private boolean canDisplay(final RecipeDisplay display) {
|
| 47 | int gridWidth = this.menu.getGridWidth();
|
| 48 | int gridHeight = this.menu.getGridHeight();
|
| 49 |
|
| 50 | return switch (display) {
|
| 51 | case ShapedCraftingRecipeDisplay shaped -> gridWidth >= shaped.width() && gridHeight >= shaped.height();
|
| 52 | case ShapelessCraftingRecipeDisplay shapeless -> gridWidth * gridHeight >= shapeless.ingredients().size();
|
| 53 | default -> false;
|
| 54 | };
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | protected void fillGhostRecipe(final GhostSlots ghostSlots, final RecipeDisplay recipe, final ContextMap context) {
|
| 59 | ghostSlots.setResult(this.menu.getResultSlot(), context, recipe.result());
|
| 60 | switch (recipe) {
|
| 61 | case ShapedCraftingRecipeDisplay shaped: {
|
| 62 | List<Slot> inputSlots = this.menu.getInputGridSlots();
|
| 63 | PlaceRecipeHelper.placeRecipe(
|
| 64 | this.menu.getGridWidth(),
|
| 65 | this.menu.getGridHeight(),
|
| 66 | shaped.width(),
|
| 67 | shaped.height(),
|
| 68 | shaped.ingredients(),
|
| 69 | (ingredient, gridIndex, gridXPos, gridYPos) -> {
|
| 70 | Slot slot = inputSlots.get(gridIndex);
|
| 71 | ghostSlots.setInput(slot, context, ingredient);
|
| 72 | }
|
| 73 | );
|
| 74 | break;
|
| 75 | }
|
| 76 | case ShapelessCraftingRecipeDisplay shapeless: {
|
| 77 | label15: {
|
| 78 | List<Slot> inputSlots = this.menu.getInputGridSlots();
|
| 79 | int slotCount = Math.min(shapeless.ingredients().size(), inputSlots.size());
|
| 80 |
|
| 81 | for (int i = 0; i < slotCount; i++) {
|
| 82 | ghostSlots.setInput(inputSlots.get(i), context, shapeless.ingredients().get(i));
|
| 83 | }
|
| 84 | break label15;
|
| 85 | }
|
| 86 | }
|
| 87 | default:
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | protected WidgetSprites getFilterButtonTextures() {
|
| 93 | return FILTER_BUTTON_SPRITES;
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | protected Component getRecipeFilterName() {
|
| 98 | return ONLY_CRAFTABLES_TOOLTIP;
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | protected void selectMatchingRecipes(final RecipeCollection collection, final StackedItemContents stackedContents) {
|
| 103 | collection.selectRecipes(stackedContents, this::canDisplay);
|
| 104 | }
|
| 105 | }
|
| 106 |
|