|
| 1 | +package com.robotgryphon.compactcrafting.compat.jei; |
| 2 | + |
| 3 | +import com.mojang.blaze3d.matrix.MatrixStack; |
| 4 | +import com.robotgryphon.compactcrafting.CompactCrafting; |
| 5 | +import com.robotgryphon.compactcrafting.core.Registration; |
| 6 | +import com.robotgryphon.compactcrafting.recipes.MiniaturizationRecipe; |
| 7 | +import mezz.jei.api.constants.VanillaTypes; |
| 8 | +import mezz.jei.api.gui.IRecipeLayout; |
| 9 | +import mezz.jei.api.gui.drawable.IDrawable; |
| 10 | +import mezz.jei.api.gui.drawable.IDrawableStatic; |
| 11 | +import mezz.jei.api.gui.ingredient.IGuiItemStackGroup; |
| 12 | +import mezz.jei.api.helpers.IGuiHelper; |
| 13 | +import mezz.jei.api.ingredients.IIngredients; |
| 14 | +import mezz.jei.api.recipe.category.IRecipeCategory; |
| 15 | +import net.minecraft.block.BlockState; |
| 16 | +import net.minecraft.client.resources.I18n; |
| 17 | +import net.minecraft.item.Item; |
| 18 | +import net.minecraft.item.ItemStack; |
| 19 | +import net.minecraft.util.ResourceLocation; |
| 20 | +import net.minecraft.util.text.IFormattableTextComponent; |
| 21 | +import net.minecraft.util.text.TextFormatting; |
| 22 | +import net.minecraft.util.text.TranslationTextComponent; |
| 23 | + |
| 24 | +import java.util.*; |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; |
| 26 | + |
| 27 | +public class JeiMiniaturizationCraftingCategory implements IRecipeCategory<MiniaturizationRecipe> { |
| 28 | + |
| 29 | + public static final ResourceLocation UID = new ResourceLocation(CompactCrafting.MOD_ID, "miniaturization"); |
| 30 | + private final IDrawable icon; |
| 31 | + private IGuiHelper guiHelper; |
| 32 | + private final IDrawableStatic background; |
| 33 | + private final IDrawableStatic slotDrawable; |
| 34 | + |
| 35 | + |
| 36 | + public JeiMiniaturizationCraftingCategory(IGuiHelper guiHelper) { |
| 37 | + int width = (9 * 18) + 10; |
| 38 | + int height = 10 + (18 * 3) + 5; |
| 39 | + |
| 40 | + this.guiHelper = guiHelper; |
| 41 | + this.background = guiHelper.createBlankDrawable(width, height); |
| 42 | + this.slotDrawable = guiHelper.getSlotDrawable(); |
| 43 | + this.icon = guiHelper.createDrawableIngredient(new ItemStack(Registration.FIELD_PROJECTOR_BLOCK.get())); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public ResourceLocation getUid() { |
| 48 | + return UID; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Class<? extends MiniaturizationRecipe> getRecipeClass() { |
| 53 | + return MiniaturizationRecipe.class; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String getTitle() { |
| 58 | + return I18n.format(CompactCrafting.MOD_ID + ".jei.miniaturization.title"); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public IDrawable getBackground() { |
| 63 | + return background; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public IDrawable getIcon() { |
| 68 | + return this.icon; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void setIngredients(MiniaturizationRecipe recipe, IIngredients ing) { |
| 73 | + List<ItemStack> inputs = new ArrayList<>(); |
| 74 | + |
| 75 | + for (String compKey : recipe.getComponentKeys()) { |
| 76 | + Optional<BlockState> requiredBlock = recipe.getRecipeComponent(compKey); |
| 77 | + requiredBlock.ifPresent(bs -> { |
| 78 | + Item bi = Item.getItemFromBlock(bs.getBlock()); |
| 79 | + inputs.add(new ItemStack(bi)); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + inputs.add(recipe.getCatalyst()); |
| 84 | + |
| 85 | + ing.setInputs(VanillaTypes.ITEM, inputs); |
| 86 | + ing.setOutputs(VanillaTypes.ITEM, Arrays.asList(recipe.getOutputs())); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void setRecipe(IRecipeLayout recipeLayout, MiniaturizationRecipe recipe, IIngredients iIngredients) { |
| 91 | + |
| 92 | + int GUTTER_X = 5; |
| 93 | + int OFFSET_Y = 10; |
| 94 | + |
| 95 | + IGuiItemStackGroup guiItemStacks = recipeLayout.getItemStacks(); |
| 96 | + int numComponentSlots = 18; |
| 97 | + |
| 98 | + addMaterialSlots(recipe, GUTTER_X, OFFSET_Y, guiItemStacks, numComponentSlots); |
| 99 | + |
| 100 | + int catalystSlot = addCatalystSlots(recipe, GUTTER_X, OFFSET_Y, guiItemStacks, numComponentSlots); |
| 101 | + |
| 102 | + addOutputSlots(recipe, GUTTER_X, OFFSET_Y, guiItemStacks, numComponentSlots); |
| 103 | + |
| 104 | + guiItemStacks.addTooltipCallback((slot, b, itemStack, tooltip) -> { |
| 105 | + if (slot >= 0 && slot < recipe.getComponentKeys().size()) { |
| 106 | + IFormattableTextComponent text = |
| 107 | + new TranslationTextComponent(CompactCrafting.MOD_ID + ".jei.miniaturization.component") |
| 108 | + .mergeStyle(TextFormatting.GRAY) |
| 109 | + .mergeStyle(TextFormatting.ITALIC); |
| 110 | + |
| 111 | + tooltip.add(text); |
| 112 | + } |
| 113 | + |
| 114 | + if (slot == catalystSlot) { |
| 115 | + IFormattableTextComponent text = new TranslationTextComponent(CompactCrafting.MOD_ID + ".jei.miniaturization.catalyst") |
| 116 | + .mergeStyle(TextFormatting.YELLOW) |
| 117 | + .mergeStyle(TextFormatting.ITALIC); |
| 118 | + |
| 119 | + tooltip.add(text); |
| 120 | + } |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + private int addCatalystSlots(MiniaturizationRecipe recipe, int GUTTER_X, int OFFSET_Y, IGuiItemStackGroup guiItemStacks, int numComponentSlots) { |
| 125 | + int catalystSlot = numComponentSlots + 5 + 1; |
| 126 | + guiItemStacks.init(catalystSlot, true, GUTTER_X, OFFSET_Y); |
| 127 | + guiItemStacks.set(catalystSlot, recipe.getCatalyst()); |
| 128 | + guiItemStacks.setBackground(catalystSlot, slotDrawable); |
| 129 | + return catalystSlot; |
| 130 | + } |
| 131 | + |
| 132 | + private void addMaterialSlots(MiniaturizationRecipe recipe, int GUTTER_X, int OFFSET_Y, IGuiItemStackGroup guiItemStacks, int numComponentSlots) { |
| 133 | + for (int slot = 0; slot < numComponentSlots; slot++) { |
| 134 | + int slotX = GUTTER_X + (slot % 9) * 18; |
| 135 | + int slotY = (OFFSET_Y + 24) + ((slot / 9) * 18); |
| 136 | + |
| 137 | + guiItemStacks.init(slot, true, slotX, slotY); |
| 138 | + guiItemStacks.setBackground(slot, this.slotDrawable); |
| 139 | + } |
| 140 | + |
| 141 | + AtomicInteger inputOffset = new AtomicInteger(); |
| 142 | + recipe.getRecipeComponentTotals() |
| 143 | + .entrySet() |
| 144 | + .stream() |
| 145 | + .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) |
| 146 | + .forEach((comp) -> { |
| 147 | + String component = comp.getKey(); |
| 148 | + int required = comp.getValue(); |
| 149 | + int finalInputOffset = inputOffset.get(); |
| 150 | + |
| 151 | + BlockState bs = recipe.getRecipeComponent(component).get(); |
| 152 | + Item bi = Item.getItemFromBlock(bs.getBlock()); |
| 153 | + guiItemStacks.set(finalInputOffset, new ItemStack(bi, required)); |
| 154 | + |
| 155 | + inputOffset.getAndIncrement(); |
| 156 | + }); |
| 157 | + } |
| 158 | + |
| 159 | + private void addOutputSlots(MiniaturizationRecipe recipe, int GUTTER_X, int OFFSET_Y, IGuiItemStackGroup guiItemStacks, int numComponentSlots) { |
| 160 | + int outputOffset = numComponentSlots; |
| 161 | + for (int outputNum = 0; outputNum < 5; outputNum++) { |
| 162 | + guiItemStacks.init(outputOffset + outputNum, false, GUTTER_X + (outputNum * 18) + (4 * 18), OFFSET_Y); |
| 163 | + guiItemStacks.setBackground(outputOffset + outputNum, this.slotDrawable); |
| 164 | + } |
| 165 | + |
| 166 | + for (ItemStack output : recipe.getOutputs()) { |
| 167 | + guiItemStacks.set(outputOffset, output); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void draw(MiniaturizationRecipe recipe, MatrixStack mx, double mouseX, double mouseY) { |
| 173 | + mx.push(); |
| 174 | + // mx.scale(3, 3, 1); |
| 175 | + // this.getIcon().draw(mx, 7, 0); |
| 176 | + mx.pop(); |
| 177 | + } |
| 178 | +} |
0 commit comments