AutoTransferHandler.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 it.unimi.dsi.fastutil.ints.IntArrayList;
  25. import it.unimi.dsi.fastutil.ints.IntList;
  26. import net.fabricmc.api.EnvType;
  27. import net.fabricmc.api.Environment;
  28. import net.minecraft.client.Minecraft;
  29. import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
  30. import net.minecraft.world.inventory.AbstractContainerMenu;
  31. import org.jetbrains.annotations.ApiStatus;
  32. import org.jetbrains.annotations.NotNull;
  33. import org.jetbrains.annotations.Nullable;
  34. import java.util.function.Supplier;
  35. @Environment(EnvType.CLIENT)
  36. public interface AutoTransferHandler {
  37. /**
  38. * @return the priority of this handler, higher priorities will be called first.
  39. */
  40. default double getPriority() {
  41. return 0d;
  42. }
  43. @NotNull
  44. Result handle(@NotNull Context context);
  45. @ApiStatus.NonExtendable
  46. interface Result {
  47. /**
  48. * Creates a successful result, no further handlers will be called.
  49. */
  50. static Result createSuccessful() {
  51. return new ResultImpl();
  52. }
  53. /**
  54. * Creates a successful result, no further handlers will be called.
  55. * Will return to the previous screen rather than staying open.
  56. *
  57. * @deprecated use {@link #blocksFurtherHandling(boolean)}
  58. */
  59. @Deprecated
  60. static Result createSuccessfulReturningToScreen() {
  61. return createSuccessful().blocksFurtherHandling(true);
  62. }
  63. /**
  64. * Creates a passing result, further handlers will be called.
  65. * This will also mark the handler as not applicable.
  66. */
  67. static Result createNotApplicable() {
  68. return new ResultImpl(false);
  69. }
  70. /**
  71. * Creates a passing result, further handlers will be called.
  72. *
  73. * @param errorKey The error itself
  74. */
  75. static Result createFailed(String errorKey) {
  76. return new ResultImpl(errorKey, new IntArrayList(), 1744764928);
  77. }
  78. /**
  79. * Creates a passing result, further handlers will be called.
  80. * The special color will be applied if this is the last handler.
  81. *
  82. * @param errorKey The error itself
  83. * @param color A special color for the button
  84. */
  85. static Result createFailedCustomButtonColor(String errorKey, int color) {
  86. return new ResultImpl(errorKey, new IntArrayList(), color);
  87. }
  88. /**
  89. * Creates a passing result, further handlers will be called.
  90. *
  91. * @param errorKey The error itself
  92. * @param redSlots A list of slots to be marked as red. Will be passed to {@link TransferRecipeCategory}.
  93. */
  94. static Result createFailed(String errorKey, IntList redSlots) {
  95. return new ResultImpl(errorKey, redSlots, 1744764928);
  96. }
  97. /**
  98. * Creates a passing result, further handlers will be called.
  99. * The special color will be applied if this is the last handler.
  100. *
  101. * @param errorKey The error itself
  102. * @param color A special color for the button
  103. * @param redSlots A list of slots to be marked as red. Will be passed to {@link TransferRecipeCategory}.
  104. */
  105. static Result createFailedCustomButtonColor(String errorKey, IntList redSlots, int color) {
  106. return new ResultImpl(errorKey, redSlots, color);
  107. }
  108. /**
  109. * Forces this handler to be the last handler, no further handlers will be called.
  110. */
  111. default Result blocksFurtherHandling() {
  112. return blocksFurtherHandling(true);
  113. }
  114. /**
  115. * Forces this handler to be the last handler, no further handlers will be called.
  116. */
  117. Result blocksFurtherHandling(boolean returnsToScreen);
  118. /**
  119. * @return the color in which the button should be displayed in.
  120. */
  121. int getColor();
  122. /**
  123. * @return whether this handler has successfully handled the transfer.
  124. */
  125. boolean isSuccessful();
  126. /**
  127. * @return whether this handler should be the last handler.
  128. */
  129. boolean isBlocking();
  130. /**
  131. * Applicable if {@link #isSuccessful()} is true.
  132. *
  133. * @return whether to return to the previous screen rather than staying open
  134. */
  135. boolean isReturningToScreen();
  136. /**
  137. * @return whether the handler is applicable.
  138. */
  139. boolean isApplicable();
  140. /**
  141. * Applicable if {@link #isSuccessful()} is false.
  142. *
  143. * @return the error message
  144. */
  145. String getErrorKey();
  146. /**
  147. * @return a list of slots to be marked as red. Will be passed to {@link TransferRecipeCategory}.
  148. */
  149. IntList getIntegers();
  150. }
  151. @ApiStatus.NonExtendable
  152. interface Context {
  153. static Context create(boolean actuallyCrafting, AbstractContainerScreen<?> containerScreen, RecipeDisplay recipeDisplay) {
  154. return new ContextImpl(actuallyCrafting, containerScreen, () -> recipeDisplay);
  155. }
  156. default Minecraft getMinecraft() {
  157. return Minecraft.getInstance();
  158. }
  159. boolean isActuallyCrafting();
  160. @Nullable
  161. AbstractContainerScreen<?> getContainerScreen();
  162. @Nullable
  163. @Deprecated
  164. @ApiStatus.ScheduledForRemoval
  165. default AbstractContainerScreen<?> getHandledScreen() {
  166. return getContainerScreen();
  167. }
  168. RecipeDisplay getRecipe();
  169. @Nullable
  170. @Deprecated
  171. @ApiStatus.ScheduledForRemoval
  172. default AbstractContainerMenu getScreenHandler() {
  173. return getContainer();
  174. }
  175. @Nullable
  176. default AbstractContainerMenu getContainer() {
  177. return getContainerScreen() == null ? null : getContainerScreen().getMenu();
  178. }
  179. }
  180. @ApiStatus.Internal
  181. final class ResultImpl implements Result {
  182. private boolean successful, applicable, returningToScreen, blocking;
  183. private String errorKey;
  184. private IntList integers = new IntArrayList();
  185. private int color;
  186. private ResultImpl() {
  187. this(true, true);
  188. }
  189. public ResultImpl(boolean applicable) {
  190. this(false, applicable);
  191. }
  192. public ResultImpl(boolean successful, boolean applicable) {
  193. this.successful = successful;
  194. this.applicable = applicable;
  195. }
  196. public ResultImpl(String errorKey, IntList integers, int color) {
  197. this.successful = false;
  198. this.applicable = true;
  199. this.errorKey = errorKey;
  200. if (integers != null)
  201. this.integers = integers;
  202. this.color = color;
  203. }
  204. @Override
  205. public Result blocksFurtherHandling(boolean returningToScreen) {
  206. this.blocking = true;
  207. this.returningToScreen = returningToScreen;
  208. return this;
  209. }
  210. @Override
  211. public int getColor() {
  212. return color;
  213. }
  214. @Override
  215. public boolean isSuccessful() {
  216. return successful;
  217. }
  218. @Override
  219. public boolean isBlocking() {
  220. return successful || blocking;
  221. }
  222. @Override
  223. public boolean isApplicable() {
  224. return applicable;
  225. }
  226. @Override
  227. public boolean isReturningToScreen() {
  228. return returningToScreen;
  229. }
  230. @Override
  231. public String getErrorKey() {
  232. return errorKey;
  233. }
  234. @Override
  235. public IntList getIntegers() {
  236. return integers;
  237. }
  238. }
  239. @ApiStatus.Internal
  240. final class ContextImpl implements Context {
  241. private boolean actuallyCrafting;
  242. private AbstractContainerScreen<?> containerScreen;
  243. private Supplier<RecipeDisplay> recipeDisplaySupplier;
  244. private ContextImpl(boolean actuallyCrafting, AbstractContainerScreen<?> containerScreen, Supplier<RecipeDisplay> recipeDisplaySupplier) {
  245. this.actuallyCrafting = actuallyCrafting;
  246. this.containerScreen = containerScreen;
  247. this.recipeDisplaySupplier = recipeDisplaySupplier;
  248. }
  249. @Override
  250. public boolean isActuallyCrafting() {
  251. return actuallyCrafting;
  252. }
  253. @Override
  254. public AbstractContainerScreen<?> getContainerScreen() {
  255. return containerScreen;
  256. }
  257. @Override
  258. public RecipeDisplay getRecipe() {
  259. return recipeDisplaySupplier.get();
  260. }
  261. }
  262. }