AutoTransferHandler.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Roughly Enough Items by Danielshe.
  3. * Licensed under the MIT License.
  4. */
  5. package me.shedaniel.rei.api;
  6. import it.unimi.dsi.fastutil.ints.IntArrayList;
  7. import it.unimi.dsi.fastutil.ints.IntList;
  8. import me.shedaniel.rei.gui.ContainerScreenOverlay;
  9. import me.shedaniel.rei.impl.ScreenHelper;
  10. import net.minecraft.client.MinecraftClient;
  11. import net.minecraft.client.gui.screen.ingame.AbstractContainerScreen;
  12. import net.minecraft.container.Container;
  13. import java.util.function.Supplier;
  14. public interface AutoTransferHandler {
  15. default double getPriority() {
  16. return 0d;
  17. }
  18. Result handle(Context context);
  19. public interface Result {
  20. static Result createSuccessful() {
  21. return new ResultImpl();
  22. }
  23. static Result createNotApplicable() {
  24. return new ResultImpl(false);
  25. }
  26. static Result createFailed(String errorKey) {
  27. return new ResultImpl(errorKey, new IntArrayList(), 1744764928);
  28. }
  29. static Result createFailedCustomButtonColor(String errorKey, int color) {
  30. return new ResultImpl(errorKey, new IntArrayList(), color);
  31. }
  32. static Result createFailed(String errorKey, IntList redSlots) {
  33. return new ResultImpl(errorKey, redSlots, 1744764928);
  34. }
  35. static Result createFailedCustomButtonColor(String errorKey, IntList redSlots, int color) {
  36. return new ResultImpl(errorKey, redSlots, color);
  37. }
  38. int getColor();
  39. boolean isSuccessful();
  40. boolean isApplicable();
  41. String getErrorKey();
  42. IntList getIntegers();
  43. }
  44. public interface Context {
  45. static Context create(boolean actuallyCrafting, AbstractContainerScreen<?> containerScreen, RecipeDisplay recipeDisplay) {
  46. return new ContextImpl(actuallyCrafting, containerScreen, () -> recipeDisplay);
  47. }
  48. default MinecraftClient getMinecraft() {
  49. return MinecraftClient.getInstance();
  50. }
  51. boolean isActuallyCrafting();
  52. AbstractContainerScreen<?> getContainerScreen();
  53. RecipeDisplay getRecipe();
  54. default Container getContainer() {
  55. return getContainerScreen().getContainer();
  56. }
  57. default ContainerScreenOverlay getOverlay() {
  58. return ScreenHelper.getLastOverlay();
  59. }
  60. }
  61. public final class ResultImpl implements Result {
  62. private boolean successful, applicable;
  63. private String errorKey;
  64. private IntList integers = new IntArrayList();
  65. private int color;
  66. private ResultImpl() {
  67. this.successful = true;
  68. this.applicable = true;
  69. }
  70. public ResultImpl(boolean applicable) {
  71. this.successful = false;
  72. this.applicable = applicable;
  73. }
  74. public ResultImpl(String errorKey, IntList integers, int color) {
  75. this.successful = false;
  76. this.applicable = true;
  77. this.errorKey = errorKey;
  78. if (integers != null)
  79. this.integers = integers;
  80. this.color = color;
  81. }
  82. @Override
  83. public int getColor() {
  84. return color;
  85. }
  86. @Override
  87. public boolean isSuccessful() {
  88. return successful;
  89. }
  90. @Override
  91. public boolean isApplicable() {
  92. return applicable;
  93. }
  94. @Override
  95. public String getErrorKey() {
  96. return errorKey;
  97. }
  98. @Override
  99. public IntList getIntegers() {
  100. return integers;
  101. }
  102. }
  103. public final class ContextImpl implements Context {
  104. boolean actuallyCrafting;
  105. AbstractContainerScreen<?> containerScreen;
  106. Supplier<RecipeDisplay> recipeDisplaySupplier;
  107. private ContextImpl(boolean actuallyCrafting, AbstractContainerScreen<?> containerScreen, Supplier<RecipeDisplay> recipeDisplaySupplier) {
  108. this.actuallyCrafting = actuallyCrafting;
  109. this.containerScreen = containerScreen;
  110. this.recipeDisplaySupplier = recipeDisplaySupplier;
  111. }
  112. @Override
  113. public boolean isActuallyCrafting() {
  114. return actuallyCrafting;
  115. }
  116. @Override
  117. public AbstractContainerScreen<?> getContainerScreen() {
  118. return containerScreen;
  119. }
  120. @Override
  121. public RecipeDisplay getRecipe() {
  122. return recipeDisplaySupplier.get();
  123. }
  124. }
  125. }