-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModIntegrationJEI.java
More file actions
255 lines (223 loc) · 11 KB
/
ModIntegrationJEI.java
File metadata and controls
255 lines (223 loc) · 11 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
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
/*******************************************************************************
* HellFirePvP / Modular Machinery 2019
*
* This project is licensed under GNU GENERAL PUBLIC LICENSE Version 3.
* The source code is available on github: https://github.com/HellFirePvP/ModularMachinery
* For further details, see the License file there.
******************************************************************************/
package hellfirepvp.modularmachinery.common.integration;
import com.google.common.collect.Lists;
import github.kasuminova.mmce.common.container.handler.MEInputRecipeTransferHandler;
import hellfirepvp.modularmachinery.ModularMachinery;
import hellfirepvp.modularmachinery.common.base.Mods;
import hellfirepvp.modularmachinery.common.block.BlockController;
import hellfirepvp.modularmachinery.common.block.BlockFactoryController;
import hellfirepvp.modularmachinery.common.crafting.MachineRecipe;
import hellfirepvp.modularmachinery.common.crafting.RecipeRegistry;
import hellfirepvp.modularmachinery.common.integration.ingredient.HybridFluid;
import hellfirepvp.modularmachinery.common.integration.ingredient.HybridFluidGas;
import hellfirepvp.modularmachinery.common.integration.ingredient.HybridFluidRenderer;
import hellfirepvp.modularmachinery.common.integration.ingredient.HybridStackHelper;
import hellfirepvp.modularmachinery.common.integration.preview.CategoryStructurePreview;
import hellfirepvp.modularmachinery.common.integration.preview.StructurePreviewWrapper;
import hellfirepvp.modularmachinery.common.integration.recipe.CategoryDynamicRecipe;
import hellfirepvp.modularmachinery.common.integration.recipe.DynamicRecipeWrapper;
import hellfirepvp.modularmachinery.common.integration.recipe.RecipeLayoutHelper;
import hellfirepvp.modularmachinery.common.item.ItemBlueprint;
import hellfirepvp.modularmachinery.common.lib.BlocksMM;
import hellfirepvp.modularmachinery.common.lib.ItemsMM;
import hellfirepvp.modularmachinery.common.machine.DynamicMachine;
import hellfirepvp.modularmachinery.common.machine.MachineRegistry;
import mezz.jei.Internal;
import mezz.jei.api.IJeiHelpers;
import mezz.jei.api.IJeiRuntime;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.IModRegistry;
import mezz.jei.api.IRecipeRegistry;
import mezz.jei.api.ISubtypeRegistry;
import mezz.jei.api.JEIPlugin;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.api.ingredients.IModIngredientRegistration;
import mezz.jei.api.recipe.IRecipeCategoryRegistration;
import mezz.jei.api.recipe.IStackHelper;
import mezz.jei.bookmarks.BookmarkList;
import mezz.jei.config.Config;
import mezz.jei.input.InputHandler;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.Optional;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* This class is part of the Modular Machinery Mod
* The complete source code for this mod can be found on github.
* Class: ModIntegrationJEI
* Created by HellFirePvP
* Date: 10.07.2017 / 19:22
*/
@JEIPlugin
public class ModIntegrationJEI implements IModPlugin {
public static final String CATEGORY_PREVIEW = "modularmachinery.preview";
public static final List<StructurePreviewWrapper> PREVIEW_WRAPPERS = Lists.newArrayList();
private static final Map<DynamicMachine, CategoryDynamicRecipe> RECIPE_CATEGORIES = new HashMap<>();
private static final Map<DynamicMachine, Map<ResourceLocation, DynamicRecipeWrapper>> MACHINE_RECIPE_WRAPPERS = new HashMap<>();
public static Field inputHandler = null;
public static Field bookmarkList = null;
public static IStackHelper stackHelper;
public static IJeiHelpers jeiHelpers;
public static IIngredientRegistry ingredientRegistry;
public static IRecipeRegistry recipeRegistry;
public static IJeiRuntime jeiRuntime;
static {
// I Just want to get the BookmarkList...
try {
Field inputHandler = Internal.class.getDeclaredField("inputHandler");
inputHandler.setAccessible(true);
ModIntegrationJEI.inputHandler = inputHandler;
Field bookmarkList = InputHandler.class.getDeclaredField("bookmarkList");
bookmarkList.setAccessible(true);
ModIntegrationJEI.bookmarkList = bookmarkList;
} catch (NoSuchFieldException e) {
ModularMachinery.log.warn(e);
}
}
public static String getCategoryStringFor(DynamicMachine machine) {
return "modularmachinery.recipes." + machine.getRegistryName().getPath();
}
public static CategoryDynamicRecipe getCategory(DynamicMachine machine) {
return RECIPE_CATEGORIES.get(machine);
}
public static void reloadRecipeWrappers() {
RECIPE_CATEGORIES.values().forEach(CategoryDynamicRecipe::reloadCategory);
for (DynamicMachine machine : MachineRegistry.getRegistry()) {
Iterable<MachineRecipe> recipes = RecipeRegistry.getRecipesFor(machine);
Map<ResourceLocation, DynamicRecipeWrapper> wrappers = MACHINE_RECIPE_WRAPPERS.computeIfAbsent(machine, v -> new LinkedHashMap<>());
for (MachineRecipe recipe : recipes) {
if (!recipe.getLoadJEI()) {
continue;
}
DynamicRecipeWrapper wrapper = wrappers.get(recipe.getRegistryName());
if (wrapper != null) {
wrapper.reloadWrapper(recipe);
}
}
}
}
public static void reloadPreviewWrappers() {
// PREVIEW_WRAPPERS.forEach(StructurePreviewWrapper::flushContext);
}
public static void addItemStackToBookmarkList(ItemStack stack) {
if (inputHandler == null || bookmarkList == null || stack.isEmpty()) {
return;
}
try {
InputHandler handler = (InputHandler) inputHandler.get(null);
BookmarkList bookmark = (BookmarkList) bookmarkList.get(handler);
if (!Config.isBookmarkOverlayEnabled()) {
Config.toggleBookmarkEnabled();
}
bookmark.add(stack);
} catch (IllegalAccessException e) {
ModularMachinery.log.warn(e);
}
}
@Override
public void registerItemSubtypes(ISubtypeRegistry subtypeRegistry) {
subtypeRegistry.registerSubtypeInterpreter(ItemsMM.blueprint, (s) -> {
DynamicMachine machine = ItemBlueprint.getAssociatedMachine(s);
if (machine == null) {
return ISubtypeRegistry.ISubtypeInterpreter.NONE;
}
return machine.getRegistryName().toString();
});
}
/**
* AE2FCR need this.
*/
@Override
@Deprecated
public void registerIngredients(@NotNull IModIngredientRegistration registry) {
try {
registry.register(() -> HybridFluid.class, Lists.newArrayList(), new HybridStackHelper<>(), new HybridFluidRenderer<>());
if (Mods.MEKANISM.isPresent()) {
registerHybridGas(registry);
}
} catch (Exception exc) {
ModularMachinery.log.warn("Error setting up HybridFluid JEI registration! Check the log after this for more details! Report this error!");
exc.printStackTrace();
throw exc;
}
}
@Optional.Method(modid = "mekanism")
private void registerHybridGas(IModIngredientRegistration registry) {
registry.register(() -> HybridFluidGas.class, Lists.newArrayList(), new HybridStackHelper<>(), new HybridFluidRenderer<>());
}
@Override
public void registerCategories(IRecipeCategoryRegistration registry) {
jeiHelpers = registry.getJeiHelpers();
RecipeLayoutHelper.init();
registry.addRecipeCategories(new CategoryStructurePreview());
for (DynamicMachine machine : MachineRegistry.getRegistry()) {
CategoryDynamicRecipe recipe = new CategoryDynamicRecipe(machine);
RECIPE_CATEGORIES.put(machine, recipe);
registry.addRecipeCategories(recipe);
}
}
@Override
public void register(IModRegistry registry) {
jeiHelpers = registry.getJeiHelpers();
ingredientRegistry = registry.getIngredientRegistry();
RecipeLayoutHelper.init();
registry.addRecipeCatalyst(new ItemStack(BlocksMM.blockController), CATEGORY_PREVIEW);
registry.addRecipeCatalyst(new ItemStack(BlocksMM.blockFactoryController), CATEGORY_PREVIEW);
for (DynamicMachine machine : MachineRegistry.getRegistry()) {
ItemStack stack = new ItemStack(ItemsMM.blueprint);
ItemBlueprint.setAssociatedMachine(stack, machine);
String machineCategory = getCategoryStringFor(machine);
registry.addRecipeCatalyst(stack, machineCategory);
}
// 仅在 AE2 存在时注册转移处理器,避免类加载失败导致 JEI 插件注册中断
if (Mods.AE2.isPresent()) {
for (DynamicMachine machine : MachineRegistry.getRegistry()) {
String machineCategory = getCategoryStringFor(machine);
registry.getRecipeTransferRegistry().addRecipeTransferHandler(new MEInputRecipeTransferHandler(), machineCategory);
}
}
for (DynamicMachine machine : MachineRegistry.getRegistry()) {
PREVIEW_WRAPPERS.add(new StructurePreviewWrapper(machine));
}
registry.addRecipes(PREVIEW_WRAPPERS, CATEGORY_PREVIEW);
for (DynamicMachine machine : MachineRegistry.getRegistry()) {
Iterable<MachineRecipe> recipes = RecipeRegistry.getRecipesFor(machine);
Map<ResourceLocation, DynamicRecipeWrapper> wrappers = MACHINE_RECIPE_WRAPPERS.computeIfAbsent(machine, v -> new LinkedHashMap<>());
for (MachineRecipe recipe : recipes) {
if (!recipe.getLoadJEI()) {
continue;
}
wrappers.put(recipe.getRegistryName(), new DynamicRecipeWrapper(recipe));
}
registry.addRecipes(wrappers.values(), getCategoryStringFor(machine));
}
BlockController.MACHINE_CONTROLLERS.values().forEach(controller ->
registry.addRecipeCatalyst(new ItemStack(controller),
getCategoryStringFor(controller.getParentMachine()))
);
BlockController.MOC_MACHINE_CONTROLLERS.values().forEach(controller ->
registry.addRecipeCatalyst(new ItemStack(controller),
getCategoryStringFor(controller.getParentMachine()))
);
BlockFactoryController.FACTORY_CONTROLLERS.values().forEach(controller ->
registry.addRecipeCatalyst(new ItemStack(controller),
getCategoryStringFor(controller.getParentMachine()))
);
}
@Override
public void onRuntimeAvailable(IJeiRuntime jeiRuntime) {
recipeRegistry = jeiRuntime.getRecipeRegistry();
ModIntegrationJEI.jeiRuntime = jeiRuntime;
}
}