ClientHelper.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.rei.impl.Internals;
  25. import me.shedaniel.rei.utils.CollectionUtils;
  26. import me.shedaniel.rei.utils.FormattingUtils;
  27. import net.fabricmc.api.EnvType;
  28. import net.fabricmc.api.Environment;
  29. import net.minecraft.ChatFormatting;
  30. import net.minecraft.client.gui.chat.NarratorChatListener;
  31. import net.minecraft.core.Registry;
  32. import net.minecraft.network.chat.Component;
  33. import net.minecraft.network.chat.TextComponent;
  34. import net.minecraft.resources.ResourceLocation;
  35. import net.minecraft.world.item.Item;
  36. import net.minecraft.world.item.ItemStack;
  37. import net.minecraft.world.item.Items;
  38. import org.jetbrains.annotations.ApiStatus;
  39. import org.jetbrains.annotations.NotNull;
  40. import org.jetbrains.annotations.Nullable;
  41. import java.util.Collection;
  42. import java.util.List;
  43. import java.util.Map;
  44. import java.util.Set;
  45. @Environment(EnvType.CLIENT)
  46. public interface ClientHelper {
  47. /**
  48. * @return the instance of {@link me.shedaniel.rei.api.ClientHelper}
  49. */
  50. static ClientHelper getInstance() {
  51. return Internals.getClientHelper();
  52. }
  53. /**
  54. * Checks if cheating is enabled
  55. *
  56. * @return whether cheating is enabled
  57. */
  58. boolean isCheating();
  59. /**
  60. * Sets current cheating mode
  61. * Should save the config in {@link ConfigManager}.
  62. *
  63. * @param cheating the new cheating mode
  64. */
  65. void setCheating(boolean cheating);
  66. @Deprecated
  67. @ApiStatus.ScheduledForRemoval
  68. List<ItemStack> getInventoryItemsTypes();
  69. /**
  70. * Opens a recipe viewing screen:
  71. * Opens {@link me.shedaniel.rei.gui.PreRecipeViewingScreen} if not set
  72. * Opens {@link me.shedaniel.rei.gui.RecipeViewingScreen} if set to default
  73. * Opens {@link me.shedaniel.rei.gui.VillagerRecipeViewingScreen} if set to villager
  74. *
  75. * @param map the map of recipes
  76. */
  77. @ApiStatus.ScheduledForRemoval
  78. @Deprecated
  79. void openRecipeViewingScreen(Map<RecipeCategory<?>, List<RecipeDisplay>> map);
  80. /**
  81. * Tries to cheat stack using either packets or commands.
  82. *
  83. * @param stack the stack to cheat in
  84. * @return whether it failed
  85. */
  86. boolean tryCheatingEntry(EntryStack stack);
  87. default boolean tryCheatingStack(ItemStack stack) {
  88. return tryCheatingEntry(EntryStack.create(stack));
  89. }
  90. /**
  91. * Finds recipe for the stack and opens the recipe screen.
  92. *
  93. * @param stack the stack to find recipe for
  94. * @return whether the stack has any recipes to show
  95. */
  96. @Deprecated
  97. @ApiStatus.ScheduledForRemoval
  98. default boolean executeRecipeKeyBind(EntryStack stack) {
  99. return openView(ViewSearchBuilder.builder().addRecipesFor(stack).setOutputNotice(stack).fillPreferredOpenedCategory());
  100. }
  101. @Deprecated
  102. @ApiStatus.ScheduledForRemoval
  103. default boolean executeRecipeKeyBind(ItemStack stack) {
  104. return executeRecipeKeyBind(EntryStack.create(stack));
  105. }
  106. /**
  107. * Finds usage for the stack and opens the recipe screen.
  108. *
  109. * @param stack the stack to find usage for
  110. * @return whether the stack has any usages to show
  111. */
  112. @Deprecated
  113. @ApiStatus.ScheduledForRemoval
  114. default boolean executeUsageKeyBind(EntryStack stack) {
  115. return openView(ViewSearchBuilder.builder().addUsagesFor(stack).setInputNotice(stack).fillPreferredOpenedCategory());
  116. }
  117. @Deprecated
  118. @ApiStatus.ScheduledForRemoval
  119. default boolean executeUsageKeyBind(ItemStack stack) {
  120. return executeUsageKeyBind(EntryStack.create(stack));
  121. }
  122. /**
  123. * Gets the mod from an item
  124. *
  125. * @param item the item to find
  126. * @return the mod name
  127. */
  128. default String getModFromItem(Item item) {
  129. if (item.equals(Items.AIR))
  130. return "";
  131. return getModFromIdentifier(Registry.ITEM.getKey(item));
  132. }
  133. /**
  134. * Tries to delete the player's cursor item
  135. */
  136. void sendDeletePacket();
  137. /**
  138. * Gets the formatted mod from an item
  139. *
  140. * @param item the item to find
  141. * @return the mod name with blue and italic formatting
  142. */
  143. default Component getFormattedModFromItem(Item item) {
  144. String mod = getModFromItem(item);
  145. if (mod.isEmpty())
  146. return NarratorChatListener.NO_TITLE;
  147. return new TextComponent(mod).withStyle(ChatFormatting.BLUE, ChatFormatting.ITALIC);
  148. }
  149. /**
  150. * Gets the formatted mod from an identifier
  151. *
  152. * @param identifier the identifier to find
  153. * @return the mod name with blue and italic formatting
  154. */
  155. default Component getFormattedModFromIdentifier(ResourceLocation identifier) {
  156. String mod = getModFromIdentifier(identifier);
  157. if (mod.isEmpty())
  158. return NarratorChatListener.NO_TITLE;
  159. return new TextComponent(mod).withStyle(ChatFormatting.BLUE, ChatFormatting.ITALIC);
  160. }
  161. /**
  162. * Gets the mod from a modid
  163. *
  164. * @param modid the modid of the mod
  165. * @return the mod name with blue and italic formatting
  166. */
  167. default Component getFormattedModFromModId(String modid) {
  168. String mod = getModFromModId(modid);
  169. if (mod.isEmpty())
  170. return NarratorChatListener.NO_TITLE;
  171. return new TextComponent(mod).withStyle(ChatFormatting.BLUE, ChatFormatting.ITALIC);
  172. }
  173. default List<Component> appendModIdToTooltips(List<Component> components, String modId) {
  174. final String modName = ClientHelper.getInstance().getModFromModId(modId);
  175. boolean alreadyHasMod = false;
  176. for (Component s : components)
  177. if (FormattingUtils.stripFormatting(s.getString()).equalsIgnoreCase(modName)) {
  178. alreadyHasMod = true;
  179. break;
  180. }
  181. if (!alreadyHasMod)
  182. components.add(ClientHelper.getInstance().getFormattedModFromModId(modId));
  183. return components;
  184. }
  185. /**
  186. * Gets the mod from an identifier
  187. *
  188. * @param identifier the identifier to find
  189. * @return the mod name
  190. */
  191. default String getModFromIdentifier(ResourceLocation identifier) {
  192. if (identifier == null)
  193. return "";
  194. return getModFromModId(identifier.getNamespace());
  195. }
  196. /**
  197. * Gets the mod from a modid
  198. *
  199. * @param modid the modid of the mod
  200. * @return the mod name
  201. */
  202. String getModFromModId(String modid);
  203. /**
  204. * Finds all recipes and open them in a recipe screen.
  205. *
  206. * @return whether there are any recipes to show
  207. */
  208. @Deprecated
  209. @ApiStatus.ScheduledForRemoval
  210. default boolean executeViewAllRecipesKeyBind() {
  211. return openView(ViewSearchBuilder.builder().addAllCategories().fillPreferredOpenedCategory());
  212. }
  213. @Deprecated
  214. @ApiStatus.ScheduledForRemoval
  215. default boolean executeViewAllRecipesFromCategory(ResourceLocation category) {
  216. return openView(ViewSearchBuilder.builder().addCategory(category).fillPreferredOpenedCategory());
  217. }
  218. @Deprecated
  219. @ApiStatus.ScheduledForRemoval
  220. default boolean executeViewAllRecipesFromCategories(List<ResourceLocation> categories) {
  221. return openView(ViewSearchBuilder.builder().addCategories(categories).fillPreferredOpenedCategory());
  222. }
  223. boolean openView(ViewSearchBuilder builder);
  224. boolean canUseMovePackets();
  225. interface ViewSearchBuilder {
  226. static ViewSearchBuilder builder() {
  227. return Internals.createViewSearchBuilder();
  228. }
  229. ViewSearchBuilder addCategory(ResourceLocation category);
  230. ViewSearchBuilder addCategories(Collection<ResourceLocation> categories);
  231. default ViewSearchBuilder addAllCategories() {
  232. return addCategories(CollectionUtils.map(RecipeHelper.getInstance().getAllCategories(), RecipeCategory::getIdentifier));
  233. }
  234. @NotNull Set<ResourceLocation> getCategories();
  235. ViewSearchBuilder addRecipesFor(EntryStack stack);
  236. @NotNull List<EntryStack> getRecipesFor();
  237. ViewSearchBuilder addUsagesFor(EntryStack stack);
  238. @NotNull List<EntryStack> getUsagesFor();
  239. ViewSearchBuilder setPreferredOpenedCategory(@Nullable ResourceLocation category);
  240. @Nullable
  241. ResourceLocation getPreferredOpenedCategory();
  242. ViewSearchBuilder fillPreferredOpenedCategory();
  243. ViewSearchBuilder setInputNotice(@Nullable EntryStack stack);
  244. @Nullable
  245. EntryStack getInputNotice();
  246. ViewSearchBuilder setOutputNotice(@Nullable EntryStack stack);
  247. @Nullable
  248. EntryStack getOutputNotice();
  249. @NotNull
  250. Map<RecipeCategory<?>, List<RecipeDisplay>> buildMap();
  251. }
  252. }