RecipeHelperImpl.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * This file is licensed under the MIT License, part of Roughly Enough Items.
  3. * Copyright (c) 2018, 2019, 2020 shedaniel
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package me.shedaniel.rei.impl;
  24. import com.google.common.collect.Lists;
  25. import com.google.common.collect.Maps;
  26. import com.google.common.collect.Sets;
  27. import me.shedaniel.math.Rectangle;
  28. import me.shedaniel.rei.RoughlyEnoughItemsCore;
  29. import me.shedaniel.rei.api.*;
  30. import me.shedaniel.rei.api.plugins.REIPluginV0;
  31. import me.shedaniel.rei.api.subsets.SubsetsRegistry;
  32. import me.shedaniel.rei.impl.subsets.SubsetsRegistryImpl;
  33. import me.shedaniel.rei.utils.CollectionUtils;
  34. import net.minecraft.client.gui.screen.ingame.HandledScreen;
  35. import net.minecraft.recipe.Recipe;
  36. import net.minecraft.recipe.RecipeManager;
  37. import net.minecraft.util.ActionResult;
  38. import net.minecraft.util.Identifier;
  39. import org.jetbrains.annotations.ApiStatus;
  40. import java.util.*;
  41. import java.util.function.Function;
  42. import java.util.function.Predicate;
  43. import java.util.stream.Collectors;
  44. @ApiStatus.Internal
  45. public class RecipeHelperImpl implements RecipeHelper {
  46. private static final Comparator<DisplayVisibilityHandler> VISIBILITY_HANDLER_COMPARATOR;
  47. @SuppressWarnings("rawtypes")
  48. private static final Comparator<Recipe> RECIPE_COMPARATOR = Comparator.comparing((Recipe o) -> o.getId().getNamespace()).thenComparing(o -> o.getId().getPath());
  49. static {
  50. Comparator<DisplayVisibilityHandler> comparator = Comparator.comparingDouble(DisplayVisibilityHandler::getPriority);
  51. VISIBILITY_HANDLER_COMPARATOR = comparator.reversed();
  52. }
  53. private final List<AutoTransferHandler> autoTransferHandlers = Lists.newLinkedList();
  54. private final List<RecipeFunction> recipeFunctions = Lists.newLinkedList();
  55. private final List<ScreenClickArea> screenClickAreas = Lists.newLinkedList();
  56. private final int[] recipeCount = {0};
  57. private final Map<Identifier, List<RecipeDisplay>> recipeCategoryListMap = Maps.newLinkedHashMap();
  58. private final Map<RecipeCategory<?>, Identifier> categories = Maps.newLinkedHashMap();
  59. private final Map<Identifier, RecipeCategory<?>> reversedCategories = Maps.newHashMap();
  60. private final Map<Identifier, ButtonAreaSupplier> autoCraftAreaSupplierMap = Maps.newLinkedHashMap();
  61. private final Map<Identifier, List<List<EntryStack>>> categoryWorkingStations = Maps.newLinkedHashMap();
  62. private final List<DisplayVisibilityHandler> displayVisibilityHandlers = Lists.newLinkedList();
  63. private final List<LiveRecipeGenerator<RecipeDisplay>> liveRecipeGenerators = Lists.newLinkedList();
  64. private RecipeManager recipeManager;
  65. private boolean arePluginsLoading = false;
  66. @Override
  67. public List<EntryStack> findCraftableEntriesByItems(List<EntryStack> inventoryItems) {
  68. List<EntryStack> craftables = new ArrayList<>();
  69. for (List<RecipeDisplay> value : recipeCategoryListMap.values())
  70. for (RecipeDisplay recipeDisplay : Lists.newArrayList(value)) {
  71. int slotsCraftable = 0;
  72. List<List<EntryStack>> requiredInput = recipeDisplay.getRequiredEntries();
  73. for (List<EntryStack> slot : requiredInput) {
  74. if (slot.isEmpty()) {
  75. slotsCraftable++;
  76. continue;
  77. }
  78. back:
  79. for (EntryStack possibleType : inventoryItems) {
  80. for (EntryStack slotPossible : slot)
  81. if (possibleType.equals(slotPossible)) {
  82. slotsCraftable++;
  83. break back;
  84. }
  85. }
  86. }
  87. if (slotsCraftable == recipeDisplay.getRequiredEntries().size())
  88. craftables.addAll(recipeDisplay.getOutputEntries());
  89. }
  90. return craftables.stream().distinct().collect(Collectors.toList());
  91. }
  92. @Override
  93. public boolean arePluginsLoading() {
  94. return arePluginsLoading;
  95. }
  96. @Override
  97. public void registerCategory(RecipeCategory<?> category) {
  98. categories.put(category, category.getIdentifier());
  99. reversedCategories.put(category.getIdentifier(), category);
  100. recipeCategoryListMap.put(category.getIdentifier(), Lists.newArrayList());
  101. categoryWorkingStations.put(category.getIdentifier(), Lists.newArrayList());
  102. }
  103. @SafeVarargs
  104. @Override
  105. public final void registerWorkingStations(Identifier category, List<EntryStack>... workingStations) {
  106. categoryWorkingStations.get(category).addAll(Arrays.asList(workingStations));
  107. }
  108. @Override
  109. public void registerWorkingStations(Identifier category, EntryStack... workingStations) {
  110. categoryWorkingStations.get(category).addAll(Arrays.stream(workingStations).map(Collections::singletonList).collect(Collectors.toList()));
  111. }
  112. @Override
  113. public List<List<EntryStack>> getWorkingStations(Identifier category) {
  114. return categoryWorkingStations.get(category);
  115. }
  116. @Deprecated
  117. @Override
  118. public void registerDisplay(Identifier categoryIdentifier, RecipeDisplay display) {
  119. if (!recipeCategoryListMap.containsKey(categoryIdentifier))
  120. return;
  121. recipeCount[0]++;
  122. recipeCategoryListMap.get(categoryIdentifier).add(display);
  123. }
  124. @Override
  125. public void registerDisplay(RecipeDisplay display) {
  126. Identifier identifier = Objects.requireNonNull(display.getRecipeCategory());
  127. if (!recipeCategoryListMap.containsKey(identifier))
  128. return;
  129. recipeCount[0]++;
  130. recipeCategoryListMap.get(identifier).add(display);
  131. }
  132. private void registerDisplay(Identifier categoryIdentifier, RecipeDisplay display, int index) {
  133. if (!recipeCategoryListMap.containsKey(categoryIdentifier))
  134. return;
  135. recipeCount[0]++;
  136. recipeCategoryListMap.get(categoryIdentifier).add(index, display);
  137. }
  138. @Override
  139. public Map<RecipeCategory<?>, List<RecipeDisplay>> getRecipesFor(EntryStack stack) {
  140. Map<RecipeCategory<?>, List<RecipeDisplay>> result = Maps.newLinkedHashMap();
  141. for (Map.Entry<RecipeCategory<?>, Identifier> entry : categories.entrySet()) {
  142. RecipeCategory<?> category = entry.getKey();
  143. Identifier categoryId = entry.getValue();
  144. Set<RecipeDisplay> set = Sets.newLinkedHashSet();
  145. for (RecipeDisplay display : Lists.newArrayList(recipeCategoryListMap.get(categoryId))) {
  146. for (EntryStack outputStack : display.getOutputEntries())
  147. if (stack.equals(outputStack) && isDisplayVisible(display)) {
  148. set.add(display);
  149. break;
  150. }
  151. }
  152. if (!set.isEmpty())
  153. CollectionUtils.getOrPutEmptyList(result, category).addAll(set);
  154. }
  155. for (LiveRecipeGenerator<RecipeDisplay> liveRecipeGenerator : liveRecipeGenerators) {
  156. RecipeCategory<?> category = getCategory(liveRecipeGenerator.getCategoryIdentifier());
  157. Optional<List<RecipeDisplay>> recipeFor = liveRecipeGenerator.getRecipeFor(stack);
  158. if (recipeFor.isPresent()) {
  159. Set<RecipeDisplay> set = Sets.newLinkedHashSet();
  160. for (RecipeDisplay display : recipeFor.get()) {
  161. if (isDisplayVisible(display))
  162. set.add(display);
  163. }
  164. if (!set.isEmpty())
  165. CollectionUtils.getOrPutEmptyList(result, category).addAll(set);
  166. }
  167. }
  168. return result;
  169. }
  170. @Override
  171. public RecipeCategory<?> getCategory(Identifier identifier) {
  172. return reversedCategories.get(identifier);
  173. }
  174. @Override
  175. public RecipeManager getRecipeManager() {
  176. return recipeManager;
  177. }
  178. private boolean isStackWorkStationOfCategory(Identifier category, EntryStack stack) {
  179. for (List<EntryStack> stacks : getWorkingStations(category)) {
  180. for (EntryStack entryStack : stacks) {
  181. if (entryStack.equalsIgnoreTagsAndAmount(stack))
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. @Override
  188. public Map<RecipeCategory<?>, List<RecipeDisplay>> getUsagesFor(EntryStack stack) {
  189. Map<RecipeCategory<?>, List<RecipeDisplay>> result = Maps.newLinkedHashMap();
  190. for (Map.Entry<RecipeCategory<?>, Identifier> entry : categories.entrySet()) {
  191. Set<RecipeDisplay> set = Sets.newLinkedHashSet();
  192. RecipeCategory<?> category = entry.getKey();
  193. Identifier categoryId = entry.getValue();
  194. for (RecipeDisplay display : Lists.newArrayList(recipeCategoryListMap.get(categoryId))) {
  195. back:
  196. for (List<EntryStack> input : display.getInputEntries()) {
  197. for (EntryStack otherEntry : input) {
  198. if (otherEntry.equals(stack)) {
  199. if (isDisplayVisible(display))
  200. set.add(display);
  201. break back;
  202. }
  203. }
  204. }
  205. }
  206. if (isStackWorkStationOfCategory(categoryId, stack)) {
  207. set.addAll(Lists.newArrayList(recipeCategoryListMap.get(categoryId)));
  208. }
  209. if (!set.isEmpty())
  210. CollectionUtils.getOrPutEmptyList(result, category).addAll(set);
  211. }
  212. for (LiveRecipeGenerator<RecipeDisplay> liveRecipeGenerator : liveRecipeGenerators) {
  213. RecipeCategory<?> category = getCategory(liveRecipeGenerator.getCategoryIdentifier());
  214. Optional<List<RecipeDisplay>> recipeFor = liveRecipeGenerator.getUsageFor(stack);
  215. if (recipeFor.isPresent()) {
  216. Set<RecipeDisplay> set = Sets.newLinkedHashSet();
  217. for (RecipeDisplay display : recipeFor.get()) {
  218. if (isDisplayVisible(display))
  219. set.add(display);
  220. }
  221. if (!set.isEmpty())
  222. CollectionUtils.getOrPutEmptyList(result, category).addAll(set);
  223. }
  224. }
  225. return result;
  226. }
  227. @Override
  228. public List<RecipeCategory<?>> getAllCategories() {
  229. return Lists.newArrayList(categories.keySet());
  230. }
  231. @Override
  232. public Optional<ButtonAreaSupplier> getAutoCraftButtonArea(RecipeCategory<?> category) {
  233. if (!autoCraftAreaSupplierMap.containsKey(category.getIdentifier()))
  234. return Optional.ofNullable(bounds -> new Rectangle(bounds.getMaxX() - 16, bounds.getMaxY() - 16, 10, 10));
  235. return Optional.ofNullable(autoCraftAreaSupplierMap.get(category.getIdentifier()));
  236. }
  237. @Override
  238. public void registerAutoCraftButtonArea(Identifier category, ButtonAreaSupplier rectangle) {
  239. if (rectangle == null) {
  240. autoCraftAreaSupplierMap.remove(category);
  241. } else
  242. autoCraftAreaSupplierMap.put(category, rectangle);
  243. }
  244. public void recipesLoaded(RecipeManager recipeManager) {
  245. long startTime = System.currentTimeMillis();
  246. arePluginsLoading = true;
  247. ScreenHelper.clearLastRecipeScreenData();
  248. recipeCount[0] = 0;
  249. this.recipeManager = recipeManager;
  250. this.recipeCategoryListMap.clear();
  251. this.categories.clear();
  252. this.reversedCategories.clear();
  253. this.autoCraftAreaSupplierMap.clear();
  254. this.screenClickAreas.clear();
  255. this.categoryWorkingStations.clear();
  256. this.recipeFunctions.clear();
  257. this.displayVisibilityHandlers.clear();
  258. this.liveRecipeGenerators.clear();
  259. this.autoTransferHandlers.clear();
  260. ((SubsetsRegistryImpl) SubsetsRegistry.INSTANCE).reset();
  261. ((DisplayHelperImpl) DisplayHelper.getInstance()).resetData();
  262. ((DisplayHelperImpl) DisplayHelper.getInstance()).resetCache();
  263. BaseBoundsHandler baseBoundsHandler = new BaseBoundsHandlerImpl();
  264. DisplayHelper.getInstance().registerHandler(baseBoundsHandler);
  265. ((DisplayHelperImpl) DisplayHelper.getInstance()).setBaseBoundsHandler(baseBoundsHandler);
  266. List<REIPluginEntry> plugins = RoughlyEnoughItemsCore.getPlugins();
  267. plugins.sort(Comparator.comparingInt(REIPluginEntry::getPriority).reversed());
  268. RoughlyEnoughItemsCore.LOGGER.info("Loading %d plugins: %s", plugins.size(), plugins.stream().map(REIPluginEntry::getPluginIdentifier).map(Identifier::toString).collect(Collectors.joining(", ")));
  269. Collections.reverse(plugins);
  270. ((EntryRegistryImpl) EntryRegistry.getInstance()).reset();
  271. List<REIPluginV0> reiPluginV0s = new ArrayList<>();
  272. for (REIPluginEntry plugin : plugins) {
  273. try {
  274. if (plugin instanceof REIPluginV0) {
  275. ((REIPluginV0) plugin).preRegister();
  276. reiPluginV0s.add((REIPluginV0) plugin);
  277. }
  278. } catch (Throwable e) {
  279. RoughlyEnoughItemsCore.LOGGER.error(plugin.getPluginIdentifier().toString() + " plugin failed to pre register!", e);
  280. }
  281. }
  282. for (REIPluginV0 plugin : reiPluginV0s) {
  283. Identifier identifier = plugin.getPluginIdentifier();
  284. try {
  285. plugin.registerBounds(DisplayHelper.getInstance());
  286. plugin.registerEntries(EntryRegistry.getInstance());
  287. plugin.registerPluginCategories(this);
  288. plugin.registerRecipeDisplays(this);
  289. plugin.registerOthers(this);
  290. } catch (Throwable e) {
  291. RoughlyEnoughItemsCore.LOGGER.error(identifier.toString() + " plugin failed to load!", e);
  292. }
  293. }
  294. for (REIPluginV0 plugin : reiPluginV0s) {
  295. Identifier identifier = plugin.getPluginIdentifier();
  296. try {
  297. plugin.postRegister();
  298. } catch (Throwable e) {
  299. RoughlyEnoughItemsCore.LOGGER.error(identifier.toString() + " plugin failed to post register!", e);
  300. }
  301. }
  302. if (!recipeFunctions.isEmpty()) {
  303. @SuppressWarnings("rawtypes") List<Recipe> allSortedRecipes = getAllSortedRecipes();
  304. Collections.reverse(allSortedRecipes);
  305. for (RecipeFunction recipeFunction : recipeFunctions) {
  306. try {
  307. for (Recipe<?> recipe : CollectionUtils.filter(allSortedRecipes, recipe -> recipeFunction.recipeFilter.test(recipe))) {
  308. registerDisplay(recipeFunction.category, (RecipeDisplay) recipeFunction.mappingFunction.apply(recipe), 0);
  309. }
  310. } catch (Exception e) {
  311. RoughlyEnoughItemsCore.LOGGER.error("Failed to add recipes!", e);
  312. }
  313. }
  314. }
  315. if (getDisplayVisibilityHandlers().isEmpty())
  316. registerRecipeVisibilityHandler(new DisplayVisibilityHandler() {
  317. @Override
  318. public ActionResult handleDisplay(RecipeCategory<?> category, RecipeDisplay display) {
  319. return ActionResult.SUCCESS;
  320. }
  321. @Override
  322. public float getPriority() {
  323. return -1f;
  324. }
  325. });
  326. DisplayHelper.getInstance().registerHandler(new OverlayDecider() {
  327. @Override
  328. public boolean isHandingScreen(Class<?> screen) {
  329. return true;
  330. }
  331. @Override
  332. public ActionResult shouldScreenBeOverlayed(Class<?> screen) {
  333. return HandledScreen.class.isAssignableFrom(screen) ? ActionResult.SUCCESS : ActionResult.PASS;
  334. }
  335. @Override
  336. public float getPriority() {
  337. return -10;
  338. }
  339. });
  340. // Clear Cache
  341. ((DisplayHelperImpl) DisplayHelper.getInstance()).resetCache();
  342. ScreenHelper.getOptionalOverlay().ifPresent(overlay -> overlay.shouldReInit = true);
  343. // Remove duplicate entries
  344. ((EntryRegistryImpl) EntryRegistry.getInstance()).distinct();
  345. arePluginsLoading = false;
  346. ((EntryRegistryImpl) EntryRegistry.getInstance()).refilter();
  347. // Clear Cache Again!
  348. ((DisplayHelperImpl) DisplayHelper.getInstance()).resetCache();
  349. ScreenHelper.getOptionalOverlay().ifPresent(overlay -> overlay.shouldReInit = true);
  350. displayVisibilityHandlers.sort(VISIBILITY_HANDLER_COMPARATOR);
  351. long usedTime = System.currentTimeMillis() - startTime;
  352. RoughlyEnoughItemsCore.LOGGER.info("Registered %d stack entries, %d recipes displays, %d exclusion zones suppliers, %d overlay decider, %d visibility handlers and %d categories (%s) in %d ms.", EntryRegistry.getInstance().getStacksList().size(), recipeCount[0], BaseBoundsHandler.getInstance().supplierSize(), DisplayHelper.getInstance().getAllOverlayDeciders().size(), getDisplayVisibilityHandlers().size(), categories.size(), categories.keySet().stream().map(RecipeCategory::getCategoryName).collect(Collectors.joining(", ")), usedTime);
  353. }
  354. @Override
  355. public AutoTransferHandler registerAutoCraftingHandler(AutoTransferHandler handler) {
  356. autoTransferHandlers.add(handler);
  357. return handler;
  358. }
  359. @Override
  360. public List<AutoTransferHandler> getSortedAutoCraftingHandler() {
  361. return autoTransferHandlers.stream().sorted(Comparator.comparingDouble(AutoTransferHandler::getPriority).reversed()).collect(Collectors.toList());
  362. }
  363. @Override
  364. public int getRecipeCount() {
  365. return recipeCount[0];
  366. }
  367. @Override
  368. @SuppressWarnings("rawtypes")
  369. public List<Recipe> getAllSortedRecipes() {
  370. return getRecipeManager().values().stream().sorted(RECIPE_COMPARATOR).collect(Collectors.toList());
  371. }
  372. @Override
  373. public Map<RecipeCategory<?>, List<RecipeDisplay>> getAllRecipes() {
  374. Map<RecipeCategory<?>, List<RecipeDisplay>> result = Maps.newLinkedHashMap();
  375. for (Map.Entry<RecipeCategory<?>, Identifier> entry : categories.entrySet()) {
  376. RecipeCategory<?> category = entry.getKey();
  377. Identifier categoryId = entry.getValue();
  378. List<RecipeDisplay> displays = Lists.newArrayList(recipeCategoryListMap.get(categoryId));
  379. if (displays != null) {
  380. displays.removeIf(this::isDisplayNotVisible);
  381. if (!displays.isEmpty())
  382. result.put(category, displays);
  383. }
  384. }
  385. return result;
  386. }
  387. @Override
  388. public List<RecipeDisplay> getAllRecipesFromCategory(RecipeCategory<?> category) {
  389. return Lists.newArrayList(recipeCategoryListMap.get(category.getIdentifier()));
  390. }
  391. @Override
  392. public void registerRecipeVisibilityHandler(DisplayVisibilityHandler visibilityHandler) {
  393. displayVisibilityHandlers.add(visibilityHandler);
  394. }
  395. @Override
  396. public void unregisterRecipeVisibilityHandler(DisplayVisibilityHandler visibilityHandler) {
  397. displayVisibilityHandlers.remove(visibilityHandler);
  398. }
  399. @Override
  400. public List<DisplayVisibilityHandler> getDisplayVisibilityHandlers() {
  401. return Collections.unmodifiableList(displayVisibilityHandlers);
  402. }
  403. @Override
  404. public boolean isDisplayNotVisible(RecipeDisplay display) {
  405. return !isDisplayVisible(display);
  406. }
  407. @Override
  408. public boolean isDisplayVisible(RecipeDisplay display) {
  409. RecipeCategory<?> category = getCategory(display.getRecipeCategory());
  410. try {
  411. for (DisplayVisibilityHandler displayVisibilityHandler : displayVisibilityHandlers) {
  412. ActionResult visibility = displayVisibilityHandler.handleDisplay(category, display);
  413. if (visibility != ActionResult.PASS)
  414. return visibility == ActionResult.SUCCESS;
  415. }
  416. } catch (Throwable throwable) {
  417. RoughlyEnoughItemsCore.LOGGER.error("Failed to check if the recipe is visible!", throwable);
  418. }
  419. return true;
  420. }
  421. @Override
  422. public void registerScreenClickArea(Rectangle rectangle, Class<? extends HandledScreen<?>> screenClass, Identifier... categories) {
  423. this.screenClickAreas.add(new ScreenClickAreaImpl(screenClass, rectangle, categories));
  424. }
  425. @Override
  426. public <T extends Recipe<?>> void registerRecipes(Identifier category, Class<T> recipeClass, Function<T, RecipeDisplay> mappingFunction) {
  427. recipeFunctions.add(new RecipeFunction(category, recipe -> recipeClass.isAssignableFrom(recipe.getClass()), mappingFunction));
  428. }
  429. @Override
  430. public <T extends Recipe<?>> void registerRecipes(Identifier category,
  431. @SuppressWarnings("rawtypes") Function<Recipe, Boolean> recipeFilter, Function<T, RecipeDisplay> mappingFunction) {
  432. recipeFunctions.add(new RecipeFunction(category, recipeFilter::apply, mappingFunction));
  433. }
  434. @Override
  435. public <T extends Recipe<?>> void registerRecipes(Identifier category,
  436. @SuppressWarnings("rawtypes") Predicate<Recipe> recipeFilter, Function<T, RecipeDisplay> mappingFunction) {
  437. recipeFunctions.add(new RecipeFunction(category, recipeFilter, mappingFunction));
  438. }
  439. @Override
  440. public void registerLiveRecipeGenerator(LiveRecipeGenerator<?> liveRecipeGenerator) {
  441. liveRecipeGenerators.add((LiveRecipeGenerator<RecipeDisplay>) liveRecipeGenerator);
  442. }
  443. @Override
  444. public List<ScreenClickArea> getScreenClickAreas() {
  445. return screenClickAreas;
  446. }
  447. private static class ScreenClickAreaImpl implements ScreenClickArea {
  448. Class<? extends HandledScreen<?>> screenClass;
  449. Rectangle rectangle;
  450. Identifier[] categories;
  451. private ScreenClickAreaImpl(Class<? extends HandledScreen<?>> screenClass, Rectangle rectangle, Identifier[] categories) {
  452. this.screenClass = screenClass;
  453. this.rectangle = rectangle;
  454. this.categories = categories;
  455. }
  456. public Class<? extends HandledScreen<?>> getScreenClass() {
  457. return screenClass;
  458. }
  459. public Rectangle getRectangle() {
  460. return rectangle;
  461. }
  462. public Identifier[] getCategories() {
  463. return categories;
  464. }
  465. }
  466. @SuppressWarnings("rawtypes")
  467. private static class RecipeFunction {
  468. Identifier category;
  469. Predicate<Recipe> recipeFilter;
  470. Function mappingFunction;
  471. public RecipeFunction(Identifier category, Predicate<Recipe> recipeFilter, Function<?, RecipeDisplay> mappingFunction) {
  472. this.category = category;
  473. this.recipeFilter = recipeFilter;
  474. this.mappingFunction = mappingFunction;
  475. }
  476. }
  477. }