/
AnvilRecipeCategory.java
148 lines (123 loc) · 5.02 KB
/
AnvilRecipeCategory.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
package mezz.jei.plugins.vanilla.anvil;
import com.mojang.blaze3d.matrix.MatrixStack;
import java.util.Map;
import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import mezz.jei.api.constants.VanillaRecipeCategoryUid;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.ingredient.IGuiIngredient;
import mezz.jei.api.gui.ingredient.IGuiItemStackGroup;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.recipe.category.IRecipeCategory;
import mezz.jei.config.Constants;
public class AnvilRecipeCategory implements IRecipeCategory<AnvilRecipe> {
private final IDrawable background;
private final IDrawable icon;
private final LoadingCache<AnvilRecipe, AnvilRecipeDisplayData> cachedDisplayData;
public AnvilRecipeCategory(IGuiHelper guiHelper) {
background = guiHelper.drawableBuilder(Constants.RECIPE_GUI_VANILLA, 0, 168, 125, 18)
.addPadding(0, 20, 0, 0)
.build();
icon = guiHelper.createDrawableIngredient(new ItemStack(Blocks.ANVIL));
cachedDisplayData = CacheBuilder.newBuilder()
.maximumSize(25)
.build(new CacheLoader<AnvilRecipe, AnvilRecipeDisplayData>() {
@Override
public AnvilRecipeDisplayData load(AnvilRecipe key) {
return new AnvilRecipeDisplayData();
}
});
}
@Override
public ResourceLocation getUid() {
return VanillaRecipeCategoryUid.ANVIL;
}
@Override
public Class<? extends AnvilRecipe> getRecipeClass() {
return AnvilRecipe.class;
}
@Override
public String getTitle() {
return Blocks.ANVIL.getTranslatedName().getString();
}
@Override
public IDrawable getBackground() {
return background;
}
@Override
public IDrawable getIcon() {
return icon;
}
@Override
public void setIngredients(AnvilRecipe recipe, IIngredients ingredients) {
ingredients.setInputLists(VanillaTypes.ITEM, recipe.getInputs());
ingredients.setOutputLists(VanillaTypes.ITEM, recipe.getOutputs());
}
@Override
public void setRecipe(IRecipeLayout recipeLayout, AnvilRecipe recipe, IIngredients ingredients) {
IGuiItemStackGroup guiItemStacks = recipeLayout.getItemStacks();
guiItemStacks.init(0, true, 0, 0);
guiItemStacks.init(1, true, 49, 0);
guiItemStacks.init(2, false, 107, 0);
guiItemStacks.set(ingredients);
AnvilRecipeDisplayData displayData = cachedDisplayData.getUnchecked(recipe);
displayData.setCurrentIngredients(guiItemStacks.getGuiIngredients());
}
@Override
public void draw(AnvilRecipe recipe, MatrixStack matrixStack, double mouseX, double mouseY) {
AnvilRecipeDisplayData displayData = cachedDisplayData.getUnchecked(recipe);
Map<Integer, ? extends IGuiIngredient<ItemStack>> currentIngredients = displayData.getCurrentIngredients();
if (currentIngredients == null) {
return;
}
ItemStack newLeftStack = currentIngredients.get(0).getDisplayedIngredient();
ItemStack newRightStack = currentIngredients.get(1).getDisplayedIngredient();
if (newLeftStack == null || newRightStack == null) {
return;
}
ItemStack lastLeftStack = displayData.getLastLeftStack();
ItemStack lastRightStack = displayData.getLastRightStack();
int lastCost = displayData.getLastCost();
if (lastLeftStack == null || lastRightStack == null
|| !ItemStack.areItemStacksEqual(lastLeftStack, newLeftStack)
|| !ItemStack.areItemStacksEqual(lastRightStack, newRightStack)) {
lastCost = AnvilRecipeMaker.findLevelsCost(newLeftStack, newRightStack);
displayData.setLast(newLeftStack, newRightStack, lastCost);
}
if (lastCost != 0) {
String costText = lastCost < 0 ? "err" : Integer.toString(lastCost);
String text = I18n.format("container.repair.cost", costText);
Minecraft minecraft = Minecraft.getInstance();
int mainColor = 0xFF80FF20;
ClientPlayerEntity player = minecraft.player;
if (player != null &&
(lastCost >= 40 || lastCost > player.experienceLevel) &&
!player.isCreative()) {
// Show red if the player doesn't have enough levels
mainColor = 0xFFFF6060;
}
drawRepairCost(minecraft, matrixStack, text, mainColor);
}
}
private void drawRepairCost(Minecraft minecraft, MatrixStack matrixStack, String text, int mainColor) {
int shadowColor = 0xFF000000 | (mainColor & 0xFCFCFC) >> 2;
int width = minecraft.fontRenderer.getStringWidth(text);
int x = background.getWidth() - 2 - width;
int y = 27;
// TODO 1.13 match the new GuiRepair style
minecraft.fontRenderer.drawString(matrixStack, text, x + 1, y, shadowColor);
minecraft.fontRenderer.drawString(matrixStack, text, x, y + 1, shadowColor);
minecraft.fontRenderer.drawString(matrixStack, text, x + 1, y + 1, shadowColor);
minecraft.fontRenderer.drawString(matrixStack, text, x, y, mainColor);
}
}