-
Notifications
You must be signed in to change notification settings - Fork 18
/
TextureRecipeExtension.java
145 lines (125 loc) · 5.86 KB
/
TextureRecipeExtension.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package knightminer.inspirations.plugins.jei;
import com.google.common.collect.ImmutableList;
import knightminer.inspirations.library.recipe.TextureRecipe;
import knightminer.inspirations.library.util.TextureBlockUtil;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.gui.ingredient.IGuiIngredientGroup;
import mezz.jei.api.gui.ingredient.IGuiItemStackGroup;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.api.recipe.category.extensions.vanilla.crafting.ICraftingCategoryExtension;
import mezz.jei.api.recipe.category.extensions.vanilla.crafting.ICustomCraftingCategoryExtension;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.common.util.Size2i;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class TextureRecipeExtension implements ICraftingCategoryExtension, ICustomCraftingCategoryExtension {
private final TextureRecipe recipe;
private final Size2i size;
/** List of all possible outputs, for the sake of recipe lookups */
private final List<List<ItemStack>> allOutputs;
/** List of all textured variants, fallback for JEI display */
private final List<ItemStack> displayOutputs;
public TextureRecipeExtension(TextureRecipe recipe) {
this.recipe = recipe;
this.size = new Size2i(recipe.getRecipeWidth(), recipe.getRecipeHeight());
// gets the outputs of this recipe
ItemStack output = this.recipe.getRecipeOutput();
// fetch all stacks from the ingredient
List<ItemStack> displayVariants = Arrays.stream(recipe.texture.getMatchingStacks())
.filter((s) -> s.getItem() instanceof BlockItem)
.map((s) -> TextureBlockUtil.setStackTexture(output.copy(), ((BlockItem)s.getItem()).getBlock()))
.collect(Collectors.toList());
ImmutableList.Builder<ItemStack> builder = new ImmutableList.Builder<>();
builder.addAll(displayVariants);
builder.add(output);
// we have all variants done
List<ItemStack> allVariants = builder.build();
this.allOutputs = ImmutableList.of(allVariants);
// empty display means the tag found nothing
this.displayOutputs = displayVariants.isEmpty() ? allVariants : displayVariants;
}
@Override
@Nullable
public ResourceLocation getRegistryName() {
return this.recipe.getId();
}
@Override
public void setIngredients(IIngredients ingredients) {
ingredients.setInputIngredients(this.recipe.getIngredients());
// use all list so textureless works for the lookup
ingredients.setOutputLists(VanillaTypes.ITEM, allOutputs);
}
@Override
public Size2i getSize() {
return size;
}
@Override
public void setRecipe(IRecipeLayout recipeLayout, IIngredients ingredients) {
IGuiItemStackGroup guiItemStacks = recipeLayout.getItemStacks();
List<List<ItemStack>> inputs = ingredients.getInputs(VanillaTypes.ITEM);
List<ItemStack> outputs = displayOutputs;
// determine the focused stack
IFocus<?> ifocus = recipeLayout.getFocus();
if(ifocus != null && ifocus.getValue() instanceof ItemStack) {
IGuiIngredientGroup<ItemStack> guiIngredients = recipeLayout.getIngredientsGroup(VanillaTypes.ITEM);
ItemStack focus = (ItemStack) ifocus.getValue();
IFocus.Mode mode = ifocus.getMode();
// input means we clicked on an ingredient, so if it affects the texture set the output texture
if(mode == IFocus.Mode.INPUT && recipe.texture.test(focus)) {
outputs = ImmutableList.of(TextureBlockUtil.setStackTexture(recipe.getRecipeOutput().copy(), Block.getBlockFromItem(focus.getItem())));
}
// if we clicked the textured block, remove all items which affect the texture that are not the proper texture
else if(mode == IFocus.Mode.OUTPUT) {
// the focus might not be the same count as the output
ItemStack output = focus.copy();
output.setCount(recipe.getRecipeOutput().getCount());
outputs = ImmutableList.of(output);
// focus texture may be undefined for the mixed planks bookshelf or missing NBT
Block textureBlock = TextureBlockUtil.getTextureBlock(focus);
if(textureBlock != Blocks.AIR) {
guiIngredients.setOverrideDisplayFocus(JEIPlugin.recipeManager.createFocus(IFocus.Mode.INPUT, new ItemStack(textureBlock)));
}
}
}
// add the itemstacks to the grid
JEIPlugin.vanillaCraftingHelper.setInputs(guiItemStacks, inputs, size.width, size.height);
guiItemStacks.set(0, outputs);
// reimplemeting JEI logic
ResourceLocation registryName = this.getRegistryName();
if (registryName != null) {
guiItemStacks.addTooltipCallback((slotIndex, input, ingredient, tooltip) -> {
if (slotIndex == 0) {
if (JEIPlugin.modIdHelper.isDisplayingModNameEnabled()) {
String recipeModId = registryName.getNamespace();
boolean modIdDifferent = false;
ResourceLocation itemRegistryName = ingredient.getItem().getRegistryName();
if (itemRegistryName != null) {
String itemModId = itemRegistryName.getNamespace();
modIdDifferent = !recipeModId.equals(itemModId);
}
if (modIdDifferent) {
String modName = JEIPlugin.modIdHelper.getFormattedModNameForModId(recipeModId);
tooltip.add(TextFormatting.GRAY + I18n.format("jei.tooltip.recipe.by", modName));
}
}
boolean showAdvanced = Minecraft.getInstance().gameSettings.advancedItemTooltips || Screen.hasShiftDown();
if (showAdvanced) {
tooltip.add(TextFormatting.DARK_GRAY + I18n.format("jei.tooltip.recipe.id", registryName.toString()));
}
}
});
}
}
}