AutoTransferHandler.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.minecraft.client.MinecraftClient;
  27. import net.minecraft.client.gui.screen.ingame.ContainerScreen;
  28. import net.minecraft.container.Container;
  29. import org.jetbrains.annotations.ApiStatus;
  30. import java.util.function.Supplier;
  31. public interface AutoTransferHandler {
  32. /**
  33. * @return the priority of this handler, higher priorities will be called first.
  34. */
  35. default double getPriority() {
  36. return 0d;
  37. }
  38. Result handle(Context context);
  39. interface Result {
  40. static Result createSuccessful() {
  41. return new ResultImpl();
  42. }
  43. static Result createSuccessfulReturningToScreen() {
  44. return new ResultImpl(true, true, true);
  45. }
  46. static Result createNotApplicable() {
  47. return new ResultImpl(false);
  48. }
  49. static Result createFailed(String errorKey) {
  50. return new ResultImpl(errorKey, new IntArrayList(), 1744764928);
  51. }
  52. static Result createFailedCustomButtonColor(String errorKey, int color) {
  53. return new ResultImpl(errorKey, new IntArrayList(), color);
  54. }
  55. static Result createFailed(String errorKey, IntList redSlots) {
  56. return new ResultImpl(errorKey, redSlots, 1744764928);
  57. }
  58. static Result createFailedCustomButtonColor(String errorKey, IntList redSlots, int color) {
  59. return new ResultImpl(errorKey, redSlots, color);
  60. }
  61. int getColor();
  62. boolean isSuccessful();
  63. /**
  64. * Applicable if {@link #isSuccessful()} is true. Will return
  65. * to the previous screen rather than staying open.
  66. */
  67. boolean isReturningToScreen();
  68. boolean isApplicable();
  69. String getErrorKey();
  70. IntList getIntegers();
  71. }
  72. interface Context {
  73. static Context create(boolean actuallyCrafting, ContainerScreen<?> containerScreen, RecipeDisplay recipeDisplay) {
  74. return new ContextImpl(actuallyCrafting, containerScreen, () -> recipeDisplay);
  75. }
  76. default MinecraftClient getMinecraft() {
  77. return MinecraftClient.getInstance();
  78. }
  79. boolean isActuallyCrafting();
  80. ContainerScreen<?> getContainerScreen();
  81. @Deprecated
  82. default ContainerScreen<?> getHandledScreen() {
  83. return getContainerScreen();
  84. }
  85. RecipeDisplay getRecipe();
  86. @Deprecated
  87. default Container getScreenHandler() {
  88. return getContainer();
  89. }
  90. default Container getContainer() {
  91. return getHandledScreen().getContainer();
  92. }
  93. }
  94. @ApiStatus.Internal
  95. final class ResultImpl implements Result {
  96. private boolean successful, applicable, returningToScreen;
  97. private String errorKey;
  98. private IntList integers = new IntArrayList();
  99. private int color;
  100. private ResultImpl() {
  101. this(true, true, false);
  102. }
  103. public ResultImpl(boolean applicable) {
  104. this(false, applicable, false);
  105. }
  106. public ResultImpl(boolean successful, boolean applicable, boolean returningToScreen) {
  107. this.successful = successful;
  108. this.applicable = applicable;
  109. this.returningToScreen = returningToScreen;
  110. }
  111. public ResultImpl(String errorKey, IntList integers, int color) {
  112. this.successful = false;
  113. this.applicable = true;
  114. this.errorKey = errorKey;
  115. if (integers != null)
  116. this.integers = integers;
  117. this.color = color;
  118. }
  119. @Override
  120. public int getColor() {
  121. return color;
  122. }
  123. @Override
  124. public boolean isSuccessful() {
  125. return successful;
  126. }
  127. @Override
  128. public boolean isApplicable() {
  129. return applicable;
  130. }
  131. @Override
  132. public boolean isReturningToScreen() {
  133. return returningToScreen;
  134. }
  135. @Override
  136. public String getErrorKey() {
  137. return errorKey;
  138. }
  139. @Override
  140. public IntList getIntegers() {
  141. return integers;
  142. }
  143. }
  144. @ApiStatus.Internal
  145. final class ContextImpl implements Context {
  146. boolean actuallyCrafting;
  147. ContainerScreen<?> containerScreen;
  148. Supplier<RecipeDisplay> recipeDisplaySupplier;
  149. private ContextImpl(boolean actuallyCrafting, ContainerScreen<?> containerScreen, Supplier<RecipeDisplay> recipeDisplaySupplier) {
  150. this.actuallyCrafting = actuallyCrafting;
  151. this.containerScreen = containerScreen;
  152. this.recipeDisplaySupplier = recipeDisplaySupplier;
  153. }
  154. @Override
  155. public boolean isActuallyCrafting() {
  156. return actuallyCrafting;
  157. }
  158. @Override
  159. public ContainerScreen<?> getContainerScreen() {
  160. return containerScreen;
  161. }
  162. @Override
  163. public RecipeDisplay getRecipe() {
  164. return recipeDisplaySupplier.get();
  165. }
  166. }
  167. }