-
Notifications
You must be signed in to change notification settings - Fork 755
/
ModifierRecipeCategory.java
289 lines (265 loc) · 13.2 KB
/
ModifierRecipeCategory.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package slimeknights.tconstruct.plugin.jei.modifiers;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import lombok.Getter;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.ingredient.IRecipeSlotsView;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.api.recipe.IFocusGroup;
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.recipe.category.IRecipeCategory;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import slimeknights.mantle.client.model.NBTKeyModel;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.client.GuiUtil;
import slimeknights.tconstruct.library.json.IntRange;
import slimeknights.tconstruct.library.materials.IMaterialRegistry;
import slimeknights.tconstruct.library.materials.MaterialRegistry;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierHooks;
import slimeknights.tconstruct.library.recipe.modifiers.adding.IDisplayModifierRecipe;
import slimeknights.tconstruct.library.tools.SlotType;
import slimeknights.tconstruct.library.tools.SlotType.SlotCount;
import slimeknights.tconstruct.library.tools.helper.ToolBuildHandler;
import slimeknights.tconstruct.library.tools.item.IModifiable;
import slimeknights.tconstruct.library.tools.nbt.MaterialNBT;
import slimeknights.tconstruct.plugin.jei.TConstructJEIConstants;
import slimeknights.tconstruct.tools.TinkerModifiers;
import slimeknights.tconstruct.tools.TinkerTools;
import slimeknights.tconstruct.tools.item.ArmorSlotType;
import slimeknights.tconstruct.tools.item.CreativeSlotItem;
import slimeknights.tconstruct.tools.stats.SkullStats;
import javax.annotation.Nullable;
import java.awt.Color;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ModifierRecipeCategory implements IRecipeCategory<IDisplayModifierRecipe> {
protected static final ResourceLocation BACKGROUND_LOC = TConstruct.getResource("textures/gui/jei/tinker_station.png");
private static final Component TITLE = TConstruct.makeTranslation("jei", "modifiers.title");
// translation
private static final List<Component> TEXT_FREE = Collections.singletonList(TConstruct.makeTranslation("jei", "modifiers.free"));
private static final List<Component> TEXT_INCREMENTAL = Collections.singletonList(TConstruct.makeTranslation("jei", "modifiers.incremental"));
private static final String KEY_SLOT = TConstruct.makeTranslationKey("jei", "modifiers.slot");
private static final String KEY_SLOTS = TConstruct.makeTranslationKey("jei", "modifiers.slots");
private static final String KEY_MIN = TConstruct.makeTranslationKey("jei", "modifiers.level.min");
private static final String KEY_MAX = TConstruct.makeTranslationKey("jei", "modifiers.level.max");
private static final String KEY_RANGE = TConstruct.makeTranslationKey("jei", "modifiers.level.range");
private static final String KEY_EXACT = TConstruct.makeTranslationKey("jei", "modifiers.level.exact");
private final ModifierIngredientRenderer modifierRenderer = new ModifierIngredientRenderer(124, 10);
@Getter
private final IDrawable background;
@Getter
private final IDrawable icon;
private final IDrawable requirements, incremental;
private final IDrawable[] slotIcons;
private final Map<SlotType,TextureAtlasSprite> slotTypeSprites = new HashMap<>();
public ModifierRecipeCategory(IGuiHelper helper) {
this.background = helper.createDrawable(BACKGROUND_LOC, 0, 0, 128, 77);
this.icon = helper.createDrawableIngredient(VanillaTypes.ITEM_STACK, CreativeSlotItem.withSlot(new ItemStack(TinkerModifiers.creativeSlotItem), SlotType.UPGRADE));
this.slotIcons = new IDrawable[6];
for (int i = 0; i < 6; i++) {
slotIcons[i] = helper.createDrawable(BACKGROUND_LOC, 128 + i * 16, 0, 16, 16);
}
this.requirements = helper.createDrawable(BACKGROUND_LOC, 128, 17, 16, 16);
this.incremental = helper.createDrawable(BACKGROUND_LOC, 128, 33, 16, 16);
}
@Override
public RecipeType<IDisplayModifierRecipe> getRecipeType() {
return TConstructJEIConstants.MODIFIERS;
}
@Override
public Component getTitle() {
return TITLE;
}
/** Draws a single slot icon */
private void drawSlot(PoseStack matrices, IDisplayModifierRecipe recipe, int slot, int x, int y) {
List<ItemStack> stacks = recipe.getDisplayItems(slot);
if (stacks.isEmpty()) {
// -1 as the item list includes the output slot, we skip that
slotIcons[slot].draw(matrices, x + 1, y + 1);
}
}
/** Draws the icon for the given slot type */
private void drawSlotType(PoseStack matrices, @Nullable SlotType slotType, int x, int y) {
Minecraft minecraft = Minecraft.getInstance();
TextureAtlasSprite sprite;
if (slotTypeSprites.containsKey(slotType)) {
sprite = slotTypeSprites.get(slotType);
} else {
ModelManager modelManager = minecraft.getModelManager();
// gets the model for the item, its a sepcial one that gives us texture info
BakedModel model = minecraft.getItemRenderer().getItemModelShaper().getItemModel(TinkerModifiers.creativeSlotItem.get());
if (model != null && model.getOverrides() instanceof NBTKeyModel.Overrides) {
Material material = ((NBTKeyModel.Overrides)model.getOverrides()).getTexture(slotType == null ? "slotless" : slotType.getName());
sprite = modelManager.getAtlas(material.atlasLocation()).getSprite(material.texture());
} else {
// failed to use the model, use missing texture
sprite = modelManager.getAtlas(InventoryMenu.BLOCK_ATLAS).getSprite(MissingTextureAtlasSprite.getLocation());
}
slotTypeSprites.put(slotType, sprite);
}
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderTexture(0, InventoryMenu.BLOCK_ATLAS);
Screen.blit(matrices, x, y, 0, 16, 16, sprite);
}
@Override
public void draw(IDisplayModifierRecipe recipe, IRecipeSlotsView recipeSlotsView, PoseStack matrices, double mouseX, double mouseY) {
drawSlot(matrices, recipe, 0, 2, 32);
drawSlot(matrices, recipe, 1, 24, 14);
drawSlot(matrices, recipe, 2, 46, 32);
drawSlot(matrices, recipe, 3, 42, 57);
drawSlot(matrices, recipe, 4, 6, 57);
// draw info icons
ModifierEntry result = recipe.getDisplayResult();
if (result.getHook(ModifierHooks.REQUIREMENTS).requirementsError(result) != null) {
requirements.draw(matrices, 66, 58);
}
if (recipe.isIncremental()) {
incremental.draw(matrices, 83, 59);
}
// draw level requirements
Font fontRenderer = Minecraft.getInstance().font;
IntRange level = recipe.getLevel();
int min = level.min();
int max = level.max();
// min being 1 means we only have a max level, we check this first as Max Level is better than exact typiclly
Component levelText = null;
if (min == 1) {
if (max < ModifierEntry.VALID_LEVEL.max()) {
levelText = Component.translatable(KEY_MAX, max);
}
} else if (min == max) {
levelText = Component.translatable(KEY_EXACT, min);
} else if (max == ModifierEntry.VALID_LEVEL.max()) {
levelText = Component.translatable(KEY_MIN, min);
} else {
levelText = Component.translatable(KEY_RANGE, min, max);
}
if (levelText != null) {
fontRenderer.draw(matrices, levelText, 60, 16, Color.GRAY.getRGB());
}
// draw slot cost
SlotCount slots = recipe.getSlots();
if (slots == null) {
drawSlotType(matrices, null, 110, 58);
} else {
drawSlotType(matrices, slots.type(), 110, 58);
String text = Integer.toString(slots.count());
int x = 111 - fontRenderer.width(text);
fontRenderer.draw(matrices, text, x, 63, Color.GRAY.getRGB());
}
}
@Override
public List<Component> getTooltipStrings(IDisplayModifierRecipe recipe, IRecipeSlotsView recipeSlotsView, double mouseX, double mouseY) {
int checkX = (int) mouseX;
int checkY = (int) mouseY;
ModifierEntry result = recipe.getDisplayResult();
if (GuiUtil.isHovered(checkX, checkY, 66, 58, 16, 16)) {
Component requirements = result.getHook(ModifierHooks.REQUIREMENTS).requirementsError(result);
if (requirements != null) {
return Collections.singletonList(requirements);
}
}
if (recipe.isIncremental() && GuiUtil.isHovered(checkX, checkY, 83, 59, 16, 16)) {
return TEXT_INCREMENTAL;
}
if (GuiUtil.isHovered(checkX, checkY, 98, 58, 24, 16)) {
// slot tooltip over icon
SlotCount slots = recipe.getSlots();
if (slots != null) {
int count = slots.count();
if (count == 1) {
return Collections.singletonList(Component.translatable(KEY_SLOT, slots.type().getDisplayName()));
} else if (count > 1) {
return Collections.singletonList(Component.translatable(KEY_SLOTS, slots, slots.type().getDisplayName()));
}
} else {
return TEXT_FREE;
}
}
return Collections.emptyList();
}
@Override
public void setRecipe(IRecipeLayoutBuilder builder, IDisplayModifierRecipe recipe, IFocusGroup focuses) {
// inputs
builder.addSlot(RecipeIngredientRole.INPUT, 3, 33).addItemStacks(recipe.getDisplayItems(0));
builder.addSlot(RecipeIngredientRole.INPUT, 25, 15).addItemStacks(recipe.getDisplayItems(1));
builder.addSlot(RecipeIngredientRole.INPUT, 47, 33).addItemStacks(recipe.getDisplayItems(2));
builder.addSlot(RecipeIngredientRole.INPUT, 43, 58).addItemStacks(recipe.getDisplayItems(3));
builder.addSlot(RecipeIngredientRole.INPUT, 7, 58).addItemStacks(recipe.getDisplayItems(4));
// modifiers
builder.addSlot(RecipeIngredientRole.OUTPUT, 3, 3)
.setCustomRenderer(TConstructJEIConstants.MODIFIER_TYPE, modifierRenderer)
.addIngredient(TConstructJEIConstants.MODIFIER_TYPE, recipe.getDisplayResult());
// tool
List<ItemStack> toolWithoutModifier = recipe.getToolWithoutModifier();
List<ItemStack> toolWithModifier = recipe.getToolWithModifier();
// hack: if any slimeskull is selected, add all known variants to the recipe lookup
Item slimeskull = TinkerTools.slimesuit.get(ArmorSlotType.HELMET);
for (ItemStack stack : toolWithoutModifier) {
if (stack.is(slimeskull)) {
builder.addInvisibleIngredients(RecipeIngredientRole.CATALYST).addItemStacks(getSlimeskullHelmets());
break;
}
}
// JEI is currently being dumb and using ingredient subtypes within recipe focuses
// we use a more strict subtype for tools in ingredients so they all show in JEI, but do not care in recipes
// thus, manually handle the focuses
IFocus<ItemStack> focus = focuses.getFocuses(VanillaTypes.ITEM_STACK).filter(f -> f.getRole() == RecipeIngredientRole.CATALYST).findFirst().orElse(null);
if (focus != null) {
Item item = focus.getTypedValue().getIngredient().getItem();
for (ItemStack stack : toolWithoutModifier) {
if (stack.is(item)) {
toolWithoutModifier = List.of(stack);
break;
}
}
for (ItemStack stack : toolWithModifier) {
if (stack.is(item)) {
toolWithModifier = List.of(stack);
break;
}
}
}
builder.addSlot(RecipeIngredientRole.CATALYST, 25, 38).addItemStacks(toolWithoutModifier);
builder.addSlot(RecipeIngredientRole.CATALYST, 105, 34).addItemStacks(toolWithModifier);
}
/* Slimeskull workaround */
/** internal list of slimeskulls for the sake of ingredient lookup, needed since they are technically distinct but modifiers treat them as the same */
private static List<ItemStack> SLIMESKULL_HELMETS = null;
/** called to clear the cache on ingredient reload as materials may have changed */
public static void clearSlimeskullCache() {
SLIMESKULL_HELMETS = null;
}
/** gets the list of slimeskull helmets, loading it if needed */
private static List<ItemStack> getSlimeskullHelmets() {
if (SLIMESKULL_HELMETS == null) {
IMaterialRegistry registry = MaterialRegistry.getInstance();
IModifiable slimeskull = TinkerTools.slimesuit.get(ArmorSlotType.HELMET);
SLIMESKULL_HELMETS = registry.getAllMaterials().stream()
.filter(material -> registry.getMaterialStats(material.getIdentifier(), SkullStats.ID).isPresent())
.map(material -> ToolBuildHandler.buildItemFromMaterials(slimeskull, MaterialNBT.of(material)))
.toList();
}
return SLIMESKULL_HELMETS;
}
}