RecipeHelper.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.api;
  24. import me.shedaniel.math.Rectangle;
  25. import me.shedaniel.rei.impl.Internals;
  26. import net.fabricmc.api.EnvType;
  27. import net.fabricmc.api.Environment;
  28. import net.minecraft.client.gui.screens.Screen;
  29. import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
  30. import net.minecraft.resources.ResourceLocation;
  31. import net.minecraft.world.item.crafting.Recipe;
  32. import net.minecraft.world.item.crafting.RecipeManager;
  33. import org.jetbrains.annotations.ApiStatus;
  34. import org.jetbrains.annotations.NotNull;
  35. import org.jetbrains.annotations.Nullable;
  36. import java.util.List;
  37. import java.util.Map;
  38. import java.util.Optional;
  39. import java.util.function.Function;
  40. import java.util.function.Predicate;
  41. @Environment(EnvType.CLIENT)
  42. public interface RecipeHelper {
  43. /**
  44. * @return the instance of {@link me.shedaniel.rei.api.RecipeHelper}
  45. */
  46. @NotNull
  47. static RecipeHelper getInstance() {
  48. return Internals.getRecipeHelper();
  49. }
  50. AutoTransferHandler registerAutoCraftingHandler(AutoTransferHandler handler);
  51. void registerFocusedStackProvider(FocusedStackProvider provider);
  52. @Nullable
  53. @ApiStatus.Internal
  54. EntryStack getScreenFocusedStack(Screen screen);
  55. List<AutoTransferHandler> getSortedAutoCraftingHandler();
  56. /**
  57. * Gets the total recipe count registered
  58. *
  59. * @return the recipe count
  60. */
  61. int getRecipeCount();
  62. /**
  63. * @return a list of sorted recipes
  64. */
  65. List<Recipe> getAllSortedRecipes();
  66. /**
  67. * Gets all craftable items from materials.
  68. *
  69. * @param inventoryItems the materials
  70. * @return the list of craftable entries
  71. */
  72. List<EntryStack> findCraftableEntriesByItems(Iterable<EntryStack> inventoryItems);
  73. /**
  74. * Gets all craftable items from materials.
  75. *
  76. * @param inventoryItems the materials
  77. * @return the list of craftable entries
  78. */
  79. default List<EntryStack> findCraftableEntriesByItems(List<EntryStack> inventoryItems) {
  80. return findCraftableEntriesByItems((Iterable<EntryStack>) inventoryItems);
  81. }
  82. /**
  83. * Registers a category
  84. *
  85. * @param category the category to register
  86. */
  87. void registerCategory(RecipeCategory<?> category);
  88. default void registerCategories(Iterable<RecipeCategory<?>> categories) {
  89. for (RecipeCategory<?> category : categories) {
  90. registerCategory(category);
  91. }
  92. }
  93. default void registerCategories(RecipeCategory<?>... categories) {
  94. for (RecipeCategory<?> category : categories) {
  95. registerCategory(category);
  96. }
  97. }
  98. /**
  99. * Registers the working stations of a category
  100. *
  101. * @param category the category
  102. * @param workingStations the working stations
  103. */
  104. void registerWorkingStations(ResourceLocation category, List<EntryStack>... workingStations);
  105. /**
  106. * Registers the working stations of a category
  107. *
  108. * @param category the category
  109. * @param workingStations the working stations
  110. */
  111. void registerWorkingStations(ResourceLocation category, EntryStack... workingStations);
  112. List<List<EntryStack>> getWorkingStations(ResourceLocation category);
  113. /**
  114. * Registers a recipe display.
  115. *
  116. * @param display the recipe display
  117. */
  118. void registerDisplay(RecipeDisplay display);
  119. /**
  120. * Registers a recipe display.
  121. *
  122. * @param categoryIdentifier the category to display in
  123. * @param display the recipe display
  124. * @deprecated Use {@link RecipeHelper#registerDisplay(RecipeDisplay)}
  125. */
  126. @ApiStatus.ScheduledForRemoval
  127. @Deprecated
  128. default void registerDisplay(ResourceLocation categoryIdentifier, RecipeDisplay display) {
  129. registerDisplay(display);
  130. }
  131. Map<RecipeCategory<?>, List<RecipeDisplay>> buildMapFor(ClientHelper.ViewSearchBuilder builder);
  132. /**
  133. * Gets a map of recipes for an entry
  134. *
  135. * @param stack the stack to be crafted
  136. * @return the map of recipes
  137. */
  138. Map<RecipeCategory<?>, List<RecipeDisplay>> getRecipesFor(EntryStack stack);
  139. RecipeCategory<?> getCategory(ResourceLocation identifier);
  140. /**
  141. * Gets the vanilla recipe manager
  142. *
  143. * @return the recipe manager
  144. */
  145. RecipeManager getRecipeManager();
  146. /**
  147. * Gets all registered categories
  148. *
  149. * @return the list of categories
  150. */
  151. List<RecipeCategory<?>> getAllCategories();
  152. /**
  153. * Gets a map of usages for an entry
  154. *
  155. * @param stack the stack to be used
  156. * @return the map of recipes
  157. */
  158. Map<RecipeCategory<?>, List<RecipeDisplay>> getUsagesFor(EntryStack stack);
  159. /**
  160. * Gets the optional of the auto crafting button area from a category
  161. *
  162. * @param category the category of the display
  163. * @return the optional of auto crafting button area
  164. */
  165. Optional<ButtonAreaSupplier> getAutoCraftButtonArea(RecipeCategory<?> category);
  166. /**
  167. * Registers a auto crafting button area
  168. *
  169. * @param category the category of the button area
  170. * @param rectangle the button area
  171. */
  172. void registerAutoCraftButtonArea(ResourceLocation category, ButtonAreaSupplier rectangle);
  173. /**
  174. * Removes the auto crafting button
  175. *
  176. * @param category the category of the button
  177. */
  178. default void removeAutoCraftButton(ResourceLocation category) {
  179. registerAutoCraftButtonArea(category, bounds -> null);
  180. }
  181. /**
  182. * Gets the map of all recipes visible to the player
  183. *
  184. * @return the map of recipes
  185. */
  186. Map<RecipeCategory<?>, List<RecipeDisplay>> getAllRecipes();
  187. Map<RecipeCategory<?>, List<RecipeDisplay>> getAllRecipesNoHandlers();
  188. List<RecipeDisplay> getAllRecipesFromCategory(RecipeCategory<?> category);
  189. /**
  190. * Registers a recipe visibility handler
  191. *
  192. * @param visibilityHandler the handler to be registered
  193. */
  194. void registerRecipeVisibilityHandler(DisplayVisibilityHandler visibilityHandler);
  195. /**
  196. * Unregisters a recipe visibility handler
  197. *
  198. * @param visibilityHandler the handler to be unregistered
  199. */
  200. void unregisterRecipeVisibilityHandler(DisplayVisibilityHandler visibilityHandler);
  201. /**
  202. * Gets an unmodifiable list of recipe visibility handlers
  203. *
  204. * @return the unmodifiable list of handlers
  205. */
  206. List<DisplayVisibilityHandler> getDisplayVisibilityHandlers();
  207. boolean isDisplayNotVisible(RecipeDisplay display);
  208. /**
  209. * Checks if the display is visible by asking recipe visibility handlers
  210. *
  211. * @param display the display to be checked
  212. * @return whether the display should be visible
  213. */
  214. boolean isDisplayVisible(RecipeDisplay display);
  215. <T extends Recipe<?>> void registerRecipes(ResourceLocation category, Predicate<Recipe> recipeFilter, Function<T, RecipeDisplay> mappingFunction);
  216. /**
  217. * Registers a live recipe generator.
  218. *
  219. * @param liveRecipeGenerator the generator to register
  220. * @apiNote Still work in progress
  221. */
  222. void registerLiveRecipeGenerator(LiveRecipeGenerator<?> liveRecipeGenerator);
  223. /**
  224. * @deprecated Use {@link #registerContainerClickArea(Rectangle, Class, ResourceLocation...)} for the same result.
  225. */
  226. @Deprecated
  227. @ApiStatus.ScheduledForRemoval
  228. default void registerScreenClickArea(Rectangle rectangle, Class<? extends AbstractContainerScreen<?>> screenClass, ResourceLocation... categories) {
  229. registerContainerClickArea(rectangle, screenClass, categories);
  230. }
  231. /**
  232. * Registers a click area for a container screen.
  233. *
  234. * @param rectangle The click area that is offset to the container screen's top left corner.
  235. * @param screenClass The class of the screen.
  236. * @param categories The categories of result.
  237. * @param <T> The screen type to be registered to.
  238. */
  239. default <T extends AbstractContainerScreen<?>> void registerContainerClickArea(Rectangle rectangle, Class<T> screenClass, ResourceLocation... categories) {
  240. registerContainerClickArea(screen -> rectangle, screenClass, categories);
  241. }
  242. /**
  243. * Registers a click area for a container screen.
  244. *
  245. * @param rectangleSupplier The click area supplier that is offset to the container screen's top left corner.
  246. * @param screenClass The class of the screen.
  247. * @param categories The categories of result.
  248. * @param <T> The screen type to be registered to.
  249. */
  250. <T extends AbstractContainerScreen<?>> void registerContainerClickArea(ScreenClickAreaProvider<T> rectangleSupplier, Class<T> screenClass, ResourceLocation... categories);
  251. /**
  252. * Registers a click area for a screen.
  253. *
  254. * @param rectangleSupplier The click area supplier that is offset to the window's top left corner.
  255. * @param screenClass The class of the screen.
  256. * @param categories The categories of result.
  257. * @param <T> The screen type to be registered to.
  258. */
  259. <T extends Screen> void registerClickArea(ScreenClickAreaProvider<T> rectangleSupplier, Class<T> screenClass, ResourceLocation... categories);
  260. <T extends Recipe<?>> void registerRecipes(ResourceLocation category, Class<T> recipeClass, Function<T, RecipeDisplay> mappingFunction);
  261. <T extends Recipe<?>> void registerRecipes(ResourceLocation category, Function<Recipe, Boolean> recipeFilter, Function<T, RecipeDisplay> mappingFunction);
  262. @ApiStatus.Internal
  263. List<RecipeHelper.ScreenClickArea> getScreenClickAreas();
  264. @ApiStatus.Internal
  265. boolean arePluginsLoading();
  266. @ApiStatus.Internal
  267. interface ScreenClickArea {
  268. Class<? extends Screen> getScreenClass();
  269. Rectangle getRectangle();
  270. ResourceLocation[] getCategories();
  271. }
  272. }