-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathDescription.java
More file actions
153 lines (133 loc) · 6.96 KB
/
Copy pathDescription.java
File metadata and controls
153 lines (133 loc) · 6.96 KB
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
package com.cleanroommc.groovyscript.compat.mods.jei;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.cleanroommc.groovyscript.api.GroovyBlacklist;
import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.api.documentation.annotations.Example;
import com.cleanroommc.groovyscript.api.documentation.annotations.MethodDescription;
import com.cleanroommc.groovyscript.api.documentation.annotations.RegistryDescription;
import com.cleanroommc.groovyscript.core.mixin.jei.IngredientInfoRecipeAccessor;
import com.cleanroommc.groovyscript.registry.VirtualizedRegistry;
import mezz.jei.api.IModRegistry;
import mezz.jei.api.IRecipeRegistry;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.api.recipe.IIngredientType;
import mezz.jei.api.recipe.VanillaRecipeCategoryUid;
import mezz.jei.plugins.jei.info.IngredientInfoRecipeCategory;
@RegistryDescription(category = RegistryDescription.Category.ENTRIES)
public class Description extends VirtualizedRegistry<Description.DescriptionEntry> {
public static class DescriptionEntry {
List<Object> ingredients;
String[] descriptionKeys;
@SuppressWarnings("unchecked")
DescriptionEntry(@Nonnull List<?> ingredients, @Nullable List<String> descriptionKeys) {
if (ingredients.isEmpty()) {
throw new IllegalArgumentException("JEI Description Entries must not have an empty list of ingredients, got " + ingredients);
}
// This cast is not exactly correct, but Java/JVM allows it due to type erasure
this.ingredients = (List<Object>) ingredients;
this.descriptionKeys = descriptionKeys == null ? new String[0] : descriptionKeys.toArray(new String[0]);
}
@Nullable
IIngredientType<Object> validateIngredientType(IModRegistry modRegistry) {
try {
Object first = ingredients.get(0);
IIngredientType<Object> ingredientType = modRegistry.getIngredientRegistry().getIngredientType(first);
if (ingredientType == null) {
return null;
}
for (Object o : ingredients) {
if (!ingredientType.equals(modRegistry.getIngredientRegistry().getIngredientType(o))) {
return null;
}
}
return ingredientType;
} catch (IllegalArgumentException e) {
// unchecked exception thrown when there's an unknown ingredient class
return null;
}
}
}
private <T> void addIngredientInfo(IModRegistry modRegistry, List<T> ingredients, IIngredientType<T> ingredientType, String[] description) {
// Force Java to pick the correct overload we use
// This is needed because the method has overloads in a way T = Object cannot be substituted
modRegistry.addIngredientInfo(ingredients, ingredientType, description);
}
/**
* Called by {@link JeiPlugin#register}
*/
@GroovyBlacklist
public void applyAdditions(IModRegistry modRegistry) {
for (DescriptionEntry entry : this.getScriptedRecipes()) {
IIngredientType<Object> ingredientType = entry.validateIngredientType(modRegistry);
if (ingredientType != null) {
addIngredientInfo(modRegistry, entry.ingredients, ingredientType, entry.descriptionKeys);
} else {
GroovyLog.msg("Unable to obtain an ingredient type for the ingredients [{}]", entry.ingredients.toArray())
.error()
.post();
}
}
}
/**
* Called by {@link JeiPlugin#afterRuntimeAvailable()}
*/
@SuppressWarnings("unchecked")
@GroovyBlacklist
public void applyRemovals(IModRegistry modRegistry, IRecipeRegistry recipeRegistry) {
IIngredientRegistry ingredientRegistry = modRegistry.getIngredientRegistry();
IngredientInfoRecipeCategory category = (IngredientInfoRecipeCategory) recipeRegistry.getRecipeCategory(VanillaRecipeCategoryUid.INFORMATION);
if (category != null) {
recipeRegistry.getRecipeWrappers(category).forEach(wrapper -> {
IngredientInfoRecipeAccessor<?> accessor = (IngredientInfoRecipeAccessor<?>) wrapper;
for (DescriptionEntry entry : this.getBackupRecipes()) {
IIngredientType<Object> ingredientType = entry.validateIngredientType(modRegistry);
IIngredientHelper<Object> helper = ingredientRegistry.getIngredientHelper(ingredientType);
if (ingredientType == null || !ingredientType.equals(accessor.getIngredientType())) continue;
if (entry.ingredients
.stream()
.anyMatch(x -> accessor.getIngredients().stream().anyMatch(a -> helper.getUniqueId(a).equals(helper.getUniqueId(x))))) {
// the API seems to be broken hence the cast
entry.descriptionKeys = ((List<String>)wrapper.getDescription()).toArray(new String[0]);
recipeRegistry.hideRecipe(wrapper, VanillaRecipeCategoryUid.INFORMATION);
}
}
});
}
}
@Override
public void onReload() {
restoreFromBackup();
removeScripted();
}
@MethodDescription(type = MethodDescription.Type.ADDITION, example = @Example("[item('minecraft:diamond'), item('minecraft:emerald')], 'groovyscript.recipe.fluid_recipe'"))
public void add(List<Object> target, String... description) {
if (target != null) {
addScripted(new DescriptionEntry(target, Arrays.asList(description)));
}
}
@MethodDescription(type = MethodDescription.Type.ADDITION)
public void add(List<Object> target, List<String> description) {
add(target, description.toArray(new String[0]));
}
@MethodDescription(type = MethodDescription.Type.ADDITION, example = @Example("item('minecraft:clay'), ['wow', 'this', 'is', 'neat']"))
public void add(Object target, List<String> description) {
List<Object> objects = new ArrayList<>();
objects.add(target);
add(objects, description);
}
@MethodDescription(type = MethodDescription.Type.ADDITION, example = {@Example("item('minecraft:gold_ingot'), 'groovyscript.recipe.fluid_recipe'"), @Example("fluid('lava') * 500, 'some moderately cold fluid'")})
public void add(Object target, String... description) {
List<Object> objects = new ArrayList<>();
objects.add(target);
add(objects, description);
}
@MethodDescription(example = @Example(value = "item('thaumcraft:triple_meat_treat')", commented = true))
public void remove(Object target) {
addBackup(new DescriptionEntry(Arrays.asList(target), null));
}
}