diff options
| author | shedaniel <daniel@shedaniel.me> | 2020-07-24 14:00:54 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2020-07-24 14:01:21 +0800 |
| commit | 929ca0ed6de9dd25208304cd0f51a8f2d0f22ceb (patch) | |
| tree | 52f7bb2ebaa47599bb097d5593af7eb9cc161a4b /src/main/java | |
| parent | d4d23b0b0170f605463c49f5790e7326c7e38cf2 (diff) | |
| download | RoughlyEnoughItems-929ca0ed6de9dd25208304cd0f51a8f2d0f22ceb.tar.gz RoughlyEnoughItems-929ca0ed6de9dd25208304cd0f51a8f2d0f22ceb.tar.bz2 RoughlyEnoughItems-929ca0ed6de9dd25208304cd0f51a8f2d0f22ceb.zip | |
Add blocksFurtherHandling to AutoTransferHandler.Result
Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'src/main/java')
4 files changed, 123 insertions, 25 deletions
diff --git a/src/main/java/me/shedaniel/rei/api/AutoTransferHandler.java b/src/main/java/me/shedaniel/rei/api/AutoTransferHandler.java index aff7a5e2b..45341c88e 100644 --- a/src/main/java/me/shedaniel/rei/api/AutoTransferHandler.java +++ b/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; @@ -150,6 +231,13 @@ public interface AutoTransferHandler { } @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; diff --git a/src/main/java/me/shedaniel/rei/impl/InternalWidgets.java b/src/main/java/me/shedaniel/rei/impl/InternalWidgets.java index a74b8121e..a88f20c2d 100644 --- a/src/main/java/me/shedaniel/rei/impl/InternalWidgets.java +++ b/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(); } diff --git a/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java b/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java index a4b2f297d..31ee4024e 100644 --- a/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java +++ b/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(); diff --git a/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultRecipeBookHandler.java b/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultRecipeBookHandler.java index 5f300e731..7683b23af 100644 --- a/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultRecipeBookHandler.java +++ b/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(); |
