/
ItemDescriptionRecipe.java
125 lines (107 loc) · 4.36 KB
/
ItemDescriptionRecipe.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
package mezz.jei.plugins.jei.description;
import javax.annotation.Nonnull;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import mezz.jei.api.JEIManager;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.recipe.BlankRecipeWrapper;
import mezz.jei.config.Config;
import mezz.jei.util.MathUtil;
import mezz.jei.util.Translator;
public class ItemDescriptionRecipe extends BlankRecipeWrapper {
private static final int lineSpacing = 2;
@Nonnull
private final List<String> description;
@Nonnull
private final List<List<ItemStack>> outputs;
public static List<ItemDescriptionRecipe> create(@Nonnull List<ItemStack> itemStacks, String... descriptionKeys) {
List<ItemDescriptionRecipe> recipes = new ArrayList<>();
List<String> descriptionLines = translateDescriptionLines(descriptionKeys);
descriptionLines = expandNewlines(descriptionLines);
descriptionLines = wrapDescriptionLines(descriptionLines);
final int lineCount = descriptionLines.size();
Minecraft minecraft = Minecraft.getMinecraft();
final int maxLinesPerPage = (ItemDescriptionRecipeCategory.recipeHeight - 20) / (minecraft.fontRendererObj.FONT_HEIGHT + lineSpacing);
final int pageCount = MathUtil.divideCeil(lineCount, maxLinesPerPage);
for (int i = 0; i < pageCount; i++) {
int startLine = i * maxLinesPerPage;
int endLine = Math.min((i + 1) * maxLinesPerPage, lineCount);
List<String> description = descriptionLines.subList(startLine, endLine);
ItemDescriptionRecipe recipe = new ItemDescriptionRecipe(itemStacks, description);
recipes.add(recipe);
}
return recipes;
}
@Nonnull
private static List<String> translateDescriptionLines(String... descriptionKeys) {
List<String> descriptionLines = new ArrayList<>();
for (String descriptionKey : descriptionKeys) {
String translatedLine = Translator.translateToLocal(descriptionKey);
descriptionLines.add(translatedLine);
}
return descriptionLines;
}
@Nonnull
private static List<String> expandNewlines(@Nonnull List<String> descriptionLines) {
List<String> descriptionLinesExpanded = new ArrayList<>();
for (String descriptionLine : descriptionLines) {
String[] descriptionLineExpanded = descriptionLine.split("\\\\n");
Collections.addAll(descriptionLinesExpanded, descriptionLineExpanded);
}
return descriptionLinesExpanded;
}
@Nonnull
private static List<String> wrapDescriptionLines(@Nonnull List<String> descriptionLines) {
Minecraft minecraft = Minecraft.getMinecraft();
List<String> descriptionLinesWrapped = new ArrayList<>();
for (String descriptionLine : descriptionLines) {
@SuppressWarnings("unchecked")
List<String> textLines = minecraft.fontRendererObj.listFormattedStringToWidth(descriptionLine, ItemDescriptionRecipeCategory.recipeWidth);
descriptionLinesWrapped.addAll(textLines);
}
return descriptionLinesWrapped;
}
private ItemDescriptionRecipe(@Nonnull List<ItemStack> itemStacks, @Nonnull List<String> description) {
this.description = description;
this.outputs = Collections.singletonList(itemStacks);
}
@Nonnull
@Override
public List<List<ItemStack>> getOutputs() {
return outputs;
}
@Override
public List<FluidStack> getFluidInputs() {
if (Config.debugModeEnabled) {
return Arrays.asList(
new FluidStack(FluidRegistry.WATER, 1000 + (int) (Math.random() * 1000)),
new FluidStack(FluidRegistry.LAVA, 1000 + (int) (Math.random() * 1000))
);
}
return super.getFluidInputs();
}
@Override
public void drawInfo(@Nonnull Minecraft minecraft, int recipeWidth, int recipeHeight) {
IDrawable slotDrawable = JEIManager.guiHelper.getSlotDrawable();
int xPos = (recipeWidth - slotDrawable.getWidth()) / 2;
int yPos = 0;
slotDrawable.draw(minecraft, xPos, yPos);
xPos = 0;
yPos += slotDrawable.getHeight() + 4;
for (String descriptionLine : description) {
minecraft.fontRendererObj.drawString(descriptionLine, xPos, yPos, Color.black.getRGB());
yPos += minecraft.fontRendererObj.FONT_HEIGHT + lineSpacing;
}
}
@Nonnull
public List<String> getDescription() {
return description;
}
}