Przeglądaj źródła

Add blocksFurtherHandling to AutoTransferHandler.Result

Signed-off-by: shedaniel <daniel@shedaniel.me>
shedaniel 5 lat temu
rodzic
commit
f189de74b4

+ 1 - 1
gradle.properties

@@ -1,5 +1,5 @@
 org.gradle.jvmargs=-Xmx3G
-mod_version=4.10.2
+mod_version=4.10.3
 supported_version=1.16.x
 minecraft_version=1.16.1
 yarn_version=1.16.1+build.4+legacy.20w09a+build.8

+ 113 - 20
src/main/java/me/shedaniel/rei/api/AutoTransferHandler.java

@@ -25,13 +25,17 @@ package me.shedaniel.rei.api;
 
 import it.unimi.dsi.fastutil.ints.IntArrayList;
 import it.unimi.dsi.fastutil.ints.IntList;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
 import net.minecraft.client.MinecraftClient;
 import net.minecraft.client.gui.screen.ingame.ContainerScreen;
 import net.minecraft.container.Container;
 import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
 
 import java.util.function.Supplier;
 
+@Environment(EnvType.CLIENT)
 public interface AutoTransferHandler {
     
     /**
@@ -41,51 +45,127 @@ public interface AutoTransferHandler {
         return 0d;
     }
     
-    Result handle(Context context);
+    @NotNull
+    Result handle(@NotNull Context context);
     
     interface Result {
+        /**
+         * Creates a successful result, no further handlers will be called.
+         */
         static Result createSuccessful() {
             return new ResultImpl();
         }
-
+        
+        /**
+         * Creates a successful result, no further handlers will be called.
+         * Will return to the previous screen rather than staying open.
+         *
+         * @deprecated use {@link #blocksFurtherHandling(boolean)}
+         */
+        @Deprecated
         static Result createSuccessfulReturningToScreen() {
-            return new ResultImpl(true, true, true);
+            return createSuccessful().blocksFurtherHandling(true);
         }
-
+        
+        /**
+         * Creates a passing result, further handlers will be called.
+         * This will also mark the handler as not applicable.
+         */
         static Result createNotApplicable() {
             return new ResultImpl(false);
         }
         
+        /**
+         * Creates a passing result, further handlers will be called.
+         *
+         * @param errorKey The error itself
+         */
         static Result createFailed(String errorKey) {
             return new ResultImpl(errorKey, new IntArrayList(), 1744764928);
         }
         
+        /**
+         * Creates a passing result, further handlers will be called.
+         * The special color will be applied if this is the last handler.
+         *
+         * @param errorKey The error itself
+         * @param color    A special color for the button
+         */
         static Result createFailedCustomButtonColor(String errorKey, int color) {
             return new ResultImpl(errorKey, new IntArrayList(), color);
         }
         
+        /**
+         * Creates a passing result, further handlers will be called.
+         *
+         * @param errorKey The error itself
+         * @param redSlots A list of slots to be marked as red. Will be passed to {@link TransferRecipeCategory}.
+         */
         static Result createFailed(String errorKey, IntList redSlots) {
             return new ResultImpl(errorKey, redSlots, 1744764928);
         }
         
+        /**
+         * Creates a passing result, further handlers will be called.
+         * The special color will be applied if this is the last handler.
+         *
+         * @param errorKey The error itself
+         * @param color    A special color for the button
+         * @param redSlots A list of slots to be marked as red. Will be passed to {@link TransferRecipeCategory}.
+         */
         static Result createFailedCustomButtonColor(String errorKey, IntList redSlots, int color) {
             return new ResultImpl(errorKey, redSlots, color);
         }
         
+        /**
+         * Forces this handler to be the last handler, no further handlers will be called.
+         */
+        default Result blocksFurtherHandling() {
+            return blocksFurtherHandling(true);
+        }
+        
+        /**
+         * Forces this handler to be the last handler, no further handlers will be called.
+         */
+        Result blocksFurtherHandling(boolean returnsToScreen);
+        
+        /**
+         * @return the color in which the button should be displayed in.
+         */
         int getColor();
         
+        /**
+         * @return whether this handler has successfully handled the transfer.
+         */
         boolean isSuccessful();
-
+        
+        /**
+         * @return whether this handler should be the last handler.
+         */
+        boolean isBlocking();
+        
         /**
-         * Applicable if {@link #isSuccessful()} is true. Will return
-         * to the previous screen rather than staying open.
+         * Applicable if {@link #isSuccessful()} is true.
+         *
+         * @return whether to return to the previous screen rather than staying open
          */
         boolean isReturningToScreen();
         
+        /**
+         * @return whether the handler is applicable.
+         */
         boolean isApplicable();
         
+        /**
+         * Applicable if {@link #isSuccessful()} is false.
+         *
+         * @return the error message
+         */
         String getErrorKey();
         
+        /**
+         * @return a list of slots to be marked as red. Will be passed to {@link TransferRecipeCategory}.
+         */
         IntList getIntegers();
     }
     
@@ -103,6 +183,7 @@ public interface AutoTransferHandler {
         ContainerScreen<?> getContainerScreen();
         
         @Deprecated
+        @ApiStatus.ScheduledForRemoval
         default ContainerScreen<?> getHandledScreen() {
             return getContainerScreen();
         }
@@ -110,6 +191,7 @@ public interface AutoTransferHandler {
         RecipeDisplay getRecipe();
         
         @Deprecated
+        @ApiStatus.ScheduledForRemoval
         default Container getScreenHandler() {
             return getContainer();
         }
@@ -121,25 +203,24 @@ public interface AutoTransferHandler {
     
     @ApiStatus.Internal
     final class ResultImpl implements Result {
-        private boolean successful, applicable, returningToScreen;
+        private boolean successful, applicable, returningToScreen, blocking;
         private String errorKey;
         private IntList integers = new IntArrayList();
         private int color;
         
         private ResultImpl() {
-            this(true, true, false);
+            this(true, true);
         }
         
         public ResultImpl(boolean applicable) {
-            this(false, applicable, false);
+            this(false, applicable);
         }
-
-        public ResultImpl(boolean successful, boolean applicable, boolean returningToScreen) {
+        
+        public ResultImpl(boolean successful, boolean applicable) {
             this.successful = successful;
             this.applicable = applicable;
-            this.returningToScreen = returningToScreen;
         }
-
+        
         public ResultImpl(String errorKey, IntList integers, int color) {
             this.successful = false;
             this.applicable = true;
@@ -149,6 +230,13 @@ public interface AutoTransferHandler {
             this.color = color;
         }
         
+        @Override
+        public Result blocksFurtherHandling(boolean returningToScreen) {
+            this.blocking = true;
+            this.returningToScreen = returningToScreen;
+            return this;
+        }
+        
         @Override
         public int getColor() {
             return color;
@@ -158,17 +246,22 @@ public interface AutoTransferHandler {
         public boolean isSuccessful() {
             return successful;
         }
-
+        
+        @Override
+        public boolean isBlocking() {
+            return successful || blocking;
+        }
+        
         @Override
         public boolean isApplicable() {
             return applicable;
         }
-
+        
         @Override
         public boolean isReturningToScreen() {
             return returningToScreen;
         }
-
+        
         @Override
         public String getErrorKey() {
             return errorKey;
@@ -182,9 +275,9 @@ public interface AutoTransferHandler {
     
     @ApiStatus.Internal
     final class ContextImpl implements Context {
-        boolean actuallyCrafting;
-        ContainerScreen<?> containerScreen;
-        Supplier<RecipeDisplay> recipeDisplaySupplier;
+        private boolean actuallyCrafting;
+        private ContainerScreen<?> containerScreen;
+        private Supplier<RecipeDisplay> recipeDisplaySupplier;
         
         private ContextImpl(boolean actuallyCrafting, ContainerScreen<?> containerScreen, Supplier<RecipeDisplay> recipeDisplaySupplier) {
             this.actuallyCrafting = actuallyCrafting;

+ 4 - 3
src/main/java/me/shedaniel/rei/impl/InternalWidgets.java

@@ -68,9 +68,9 @@ public final class InternalWidgets {
                     for (AutoTransferHandler autoTransferHandler : RecipeHelper.getInstance().getSortedAutoCraftingHandler())
                         try {
                             AutoTransferHandler.Result result = autoTransferHandler.handle(context);
-                            if (result.isSuccessful()) {
+                            if (result.isBlocking()) {
                                 if (result.isReturningToScreen()) {
-                                    break; // Same as failing, but doesn't ask other handlers
+                                    break;
                                 }
                                 return;
                             }
@@ -97,7 +97,6 @@ public final class InternalWidgets {
                                 error = null;
                                 color = 0;
                                 redSlots = null;
-                                break;
                             } else if (result.isApplicable()) {
                                 if (error == null) {
                                     error = Lists.newArrayList();
@@ -107,6 +106,8 @@ public final class InternalWidgets {
                                 if (result.getIntegers() != null && !result.getIntegers().isEmpty())
                                     redSlots = result.getIntegers();
                             }
+                            
+                            if (result.isBlocking()) break;
                         } catch (Exception e) {
                             e.printStackTrace();
                         }

+ 3 - 1
src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java

@@ -44,6 +44,7 @@ import net.minecraft.container.Container;
 import net.minecraft.item.ItemStack;
 import net.minecraft.network.PacketByteBuf;
 import net.minecraft.util.collection.DefaultedList;
+import org.jetbrains.annotations.NotNull;
 
 import java.util.List;
 
@@ -54,8 +55,9 @@ public class DefaultCategoryHandler implements AutoTransferHandler {
         return ClientSidePacketRegistry.INSTANCE.canServerReceive(RoughlyEnoughItemsNetwork.MOVE_ITEMS_PACKET);
     }
     
+    @NotNull
     @Override
-    public Result handle(Context context) {
+    public Result handle(@NotNull Context context) {
         if (!(context.getRecipe() instanceof TransferRecipeDisplay))
             return Result.createNotApplicable();
         TransferRecipeDisplay recipe = (TransferRecipeDisplay) context.getRecipe();

+ 3 - 1
src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultRecipeBookHandler.java

@@ -38,11 +38,13 @@ import net.minecraft.container.CraftingContainer;
 import net.minecraft.container.CraftingTableContainer;
 import net.minecraft.container.PlayerContainer;
 import net.minecraft.recipe.Recipe;
+import org.jetbrains.annotations.NotNull;
 
 @Environment(EnvType.CLIENT)
 public class DefaultRecipeBookHandler implements AutoTransferHandler {
+    @NotNull
     @Override
-    public Result handle(Context context) {
+    public Result handle(@NotNull Context context) {
         if (context.getRecipe() instanceof TransferRecipeDisplay && DefaultCategoryHandler.canUseMovePackets())
             return Result.createNotApplicable();
         RecipeDisplay display = context.getRecipe();