AutoTransferHandler.java 9.8 KB

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