diff options
| author | shedaniel <daniel@shedaniel.me> | 2023-09-26 10:54:35 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2023-09-26 10:54:35 +0800 |
| commit | 2183ee93d46099837999b539c7463c24eda9d6fb (patch) | |
| tree | 6bfe6d710245b02b0df0bab3bb8daca693f1c559 /default-plugin | |
| parent | afb450760b7cc80c7341ae67b5e969a48a0cda2b (diff) | |
| parent | 001fc846c6002bf70a48dca67cee1437cc11c5e5 (diff) | |
| download | RoughlyEnoughItems-2183ee93d46099837999b539c7463c24eda9d6fb.tar.gz RoughlyEnoughItems-2183ee93d46099837999b539c7463c24eda9d6fb.tar.bz2 RoughlyEnoughItems-2183ee93d46099837999b539c7463c24eda9d6fb.zip | |
Merge remote-tracking branch 'origin/12.x-1.20' into 13.x-1.20.2
# Conflicts:
# runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java
Diffstat (limited to 'default-plugin')
6 files changed, 131 insertions, 16 deletions
diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/autocrafting/InventoryCraftingTransferHandler.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/autocrafting/InventoryCraftingTransferHandler.java new file mode 100644 index 000000000..d377c2add --- /dev/null +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/autocrafting/InventoryCraftingTransferHandler.java @@ -0,0 +1,64 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.plugin.autocrafting; + +import me.shedaniel.rei.api.client.registry.transfer.TransferHandler; +import me.shedaniel.rei.api.client.registry.transfer.simple.SimpleTransferHandler; +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.InputIngredient; +import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; +import me.shedaniel.rei.api.common.util.CollectionUtils; +import me.shedaniel.rei.plugin.common.displays.crafting.DefaultCraftingDisplay; +import net.minecraft.network.chat.Component; + +import java.util.List; + +public class InventoryCraftingTransferHandler implements TransferHandler { + private final SimpleTransferHandler parent; + + public InventoryCraftingTransferHandler(SimpleTransferHandler parent) { + this.parent = parent; + } + + @Override + public ApplicabilityResult checkApplicable(Context context) { + ApplicabilityResult applicable = parent.checkApplicable(context); + if (!applicable.isApplicable()) return applicable; + + DefaultCraftingDisplay<?> display = (DefaultCraftingDisplay<?>) context.getDisplay(); + if (display != null && (display.getWidth() > 2 || display.getHeight() > 2)) { + return ApplicabilityResult.createApplicableWithError(Component.translatable("error.rei.transfer.too_small", 2, 2)); + } + + return applicable; + } + + @Override + public Result handle(Context context) { + List<InputIngredient<EntryStack<?>>> inputs = ((DefaultCraftingDisplay<?>) context.getDisplay()).getInputIngredients(2, 2); + return parent.handleSimpleTransfer(context, parent.getMissingInputRenderer(), + CollectionUtils.map(inputs, entry -> InputIngredient.withType(entry, VanillaEntryTypes.ITEM)), + parent.getInputSlots(context), parent.getInventorySlots(context)); + } +} diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/autocrafting/recipebook/DefaultRecipeBookHandler.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/autocrafting/recipebook/DefaultRecipeBookHandler.java index 05a877f75..5fc2affee 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/autocrafting/recipebook/DefaultRecipeBookHandler.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/autocrafting/recipebook/DefaultRecipeBookHandler.java @@ -27,6 +27,7 @@ import me.shedaniel.rei.api.client.ClientHelper; import me.shedaniel.rei.api.client.registry.transfer.TransferHandler; import me.shedaniel.rei.api.common.display.Display; import me.shedaniel.rei.api.common.display.SimpleGridMenuDisplay; +import me.shedaniel.rei.api.common.transfer.info.MenuTransferException; import me.shedaniel.rei.plugin.common.displays.cooking.DefaultCookingDisplay; import me.shedaniel.rei.plugin.common.displays.crafting.DefaultCraftingDisplay; import net.fabricmc.api.EnvType; @@ -39,17 +40,26 @@ import net.minecraft.world.inventory.RecipeBookMenu; import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.RecipeHolder; +import java.util.Optional; + @Environment(EnvType.CLIENT) public class DefaultRecipeBookHandler implements TransferHandler { @Override - public Result handle(Context context) { + public ApplicabilityResult checkApplicable(Context context) { if (context.getDisplay() instanceof SimpleGridMenuDisplay && ClientHelper.getInstance().canUseMovePackets()) - return Result.createNotApplicable(); + return ApplicabilityResult.createNotApplicable(); Display display = context.getDisplay(); if (!(context.getMenu() instanceof RecipeBookMenu<?> container)) - return Result.createNotApplicable(); + return ApplicabilityResult.createNotApplicable(); if (container == null) - return Result.createNotApplicable(); + return ApplicabilityResult.createNotApplicable(); + return ApplicabilityResult.createApplicable(); + } + + @Override + public Result handle(Context context) { + RecipeBookMenu<?> container = (RecipeBookMenu<?>) context.getMenu(); + Display display = context.getDisplay(); if (display instanceof DefaultCraftingDisplay<?> craftingDisplay) { if (craftingDisplay.getOptionalRecipe().isPresent()) { int h = -1, w = -1; diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java index f96bf9011..d18033475 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java @@ -40,12 +40,14 @@ import me.shedaniel.rei.api.client.registry.entry.EntryRegistry; import me.shedaniel.rei.api.client.registry.screen.ExclusionZones; import me.shedaniel.rei.api.client.registry.screen.ScreenRegistry; import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerRegistry; +import me.shedaniel.rei.api.client.registry.transfer.simple.SimpleTransferHandler; import me.shedaniel.rei.api.common.display.basic.BasicDisplay; import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.util.EntryIngredients; import me.shedaniel.rei.api.common.util.EntryStacks; import me.shedaniel.rei.impl.ClientInternals; +import me.shedaniel.rei.plugin.autocrafting.InventoryCraftingTransferHandler; import me.shedaniel.rei.plugin.autocrafting.recipebook.DefaultRecipeBookHandler; import me.shedaniel.rei.plugin.client.categories.*; import me.shedaniel.rei.plugin.client.categories.anvil.DefaultAnvilCategory; @@ -59,6 +61,7 @@ import me.shedaniel.rei.plugin.client.exclusionzones.DefaultRecipeBookExclusionZ import me.shedaniel.rei.plugin.client.favorites.GameModeFavoriteEntry; import me.shedaniel.rei.plugin.client.favorites.TimeFavoriteEntry; import me.shedaniel.rei.plugin.client.favorites.WeatherFavoriteEntry; +import me.shedaniel.rei.plugin.common.BuiltinPlugin; import me.shedaniel.rei.plugin.common.displays.*; import me.shedaniel.rei.plugin.common.displays.anvil.AnvilRecipe; import me.shedaniel.rei.plugin.common.displays.anvil.DefaultAnvilDisplay; @@ -86,6 +89,7 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.BlockTags; import net.minecraft.tags.ItemTags; import net.minecraft.tags.TagKey; +import net.minecraft.world.inventory.*; import net.minecraft.world.item.*; import net.minecraft.world.item.alchemy.Potion; import net.minecraft.world.item.alchemy.PotionBrewing; @@ -390,6 +394,16 @@ public class DefaultClientPlugin implements REIClientPlugin, BuiltinClientPlugin @Override public void registerTransferHandlers(TransferHandlerRegistry registry) { + registry.register(SimpleTransferHandler.create(CraftingMenu.class, BuiltinPlugin.CRAFTING, + new SimpleTransferHandler.IntRange(1, 10))); + registry.register(new InventoryCraftingTransferHandler(SimpleTransferHandler.create(InventoryMenu.class, BuiltinPlugin.CRAFTING, + new SimpleTransferHandler.IntRange(1, 5)))); + registry.register(SimpleTransferHandler.create(FurnaceMenu.class, BuiltinPlugin.SMELTING, + new SimpleTransferHandler.IntRange(0, 1))); + registry.register(SimpleTransferHandler.create(SmokerMenu.class, BuiltinPlugin.SMOKING, + new SimpleTransferHandler.IntRange(0, 1))); + registry.register(SimpleTransferHandler.create(BlastFurnaceMenu.class, BuiltinPlugin.BLASTING, + new SimpleTransferHandler.IntRange(0, 1))); registry.register(new DefaultRecipeBookHandler()); } diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/crafting/DefaultCraftingCategory.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/crafting/DefaultCraftingCategory.java index 83e435a5a..963717e45 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/crafting/DefaultCraftingCategory.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/crafting/DefaultCraftingCategory.java @@ -33,6 +33,7 @@ import me.shedaniel.rei.api.client.gui.widgets.Widgets; import me.shedaniel.rei.api.client.registry.display.DisplayCategory; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.DisplayMerger; +import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.entry.InputIngredient; import me.shedaniel.rei.api.common.util.EntryStacks; @@ -44,6 +45,7 @@ import net.minecraft.network.chat.Component; import net.minecraft.world.level.block.Blocks; import org.jetbrains.annotations.Nullable; +import java.util.Iterator; import java.util.List; @Environment(EnvType.CLIENT) @@ -89,6 +91,32 @@ public class DefaultCraftingCategory implements DisplayCategory<DefaultCraftingD @Override @Nullable public DisplayMerger<DefaultCraftingDisplay<?>> getDisplayMerger() { - return DisplayCategory.getContentMerger(); + return new DisplayMerger<>() { + @Override + public boolean canMerge(DefaultCraftingDisplay<?> first, DefaultCraftingDisplay<?> second) { + if (!first.getCategoryIdentifier().equals(second.getCategoryIdentifier())) return false; + if (!equals(first.getOrganisedInputEntries(3, 3), second.getInputEntries())) return false; + if (!equals(first.getOutputEntries(), second.getOutputEntries())) return false; + if (first.isShapeless() != second.isShapeless()) return false; + if (first.getWidth() != second.getWidth()) return false; + if (first.getHeight() != second.getHeight()) return false; + return true; + } + + @Override + public int hashOf(DefaultCraftingDisplay<?> display) { + return display.getCategoryIdentifier().hashCode() * 31 * 31 * 31 + display.getOrganisedInputEntries(3, 3).hashCode() * 31 * 31 + display.getOutputEntries().hashCode(); + } + + private boolean equals(List<EntryIngredient> l1, List<EntryIngredient> l2) { + if (l1.size() != l2.size()) return false; + Iterator<EntryIngredient> it1 = l1.iterator(); + Iterator<EntryIngredient> it2 = l2.iterator(); + while (it1.hasNext() && it2.hasNext()) { + if (!it1.next().equals(it2.next())) return false; + } + return true; + } + }; } } diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/DefaultPlugin.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/DefaultPlugin.java index c3465adff..e6a3e33ce 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/DefaultPlugin.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/DefaultPlugin.java @@ -49,7 +49,7 @@ import me.shedaniel.rei.plugin.common.displays.tag.TagNodes; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import net.minecraft.nbt.Tag; -import net.minecraft.world.inventory.*; +import net.minecraft.world.inventory.InventoryMenu; import net.minecraft.world.item.*; import net.minecraft.world.level.material.Fluid; import org.jetbrains.annotations.ApiStatus; @@ -124,15 +124,6 @@ public class DefaultPlugin implements BuiltinPlugin, REIServerPlugin { } @Override - public void registerMenuInfo(MenuInfoRegistry registry) { - registry.register(BuiltinPlugin.CRAFTING, CraftingMenu.class, SimpleMenuInfoProvider.of(RecipeBookGridMenuInfo::new)); - registry.register(BuiltinPlugin.CRAFTING, InventoryMenu.class, SimpleMenuInfoProvider.of(RecipeBookGridMenuInfo::new)); - registry.register(BuiltinPlugin.SMELTING, FurnaceMenu.class, SimpleMenuInfoProvider.of(RecipeBookGridMenuInfo::new)); - registry.register(BuiltinPlugin.SMOKING, SmokerMenu.class, SimpleMenuInfoProvider.of(RecipeBookGridMenuInfo::new)); - registry.register(BuiltinPlugin.BLASTING, BlastFurnaceMenu.class, SimpleMenuInfoProvider.of(RecipeBookGridMenuInfo::new)); - } - - @Override public double getPriority() { return -100; } diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/crafting/DefaultCraftingDisplay.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/crafting/DefaultCraftingDisplay.java index 543f487ab..57fa27cca 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/crafting/DefaultCraftingDisplay.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/crafting/DefaultCraftingDisplay.java @@ -41,6 +41,7 @@ import me.shedaniel.rei.api.common.util.EntryIngredients; import me.shedaniel.rei.plugin.common.BuiltinPlugin; import net.minecraft.core.NonNullList; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.*; @@ -185,6 +186,11 @@ public abstract class DefaultCraftingDisplay<C extends Recipe<?>> extends BasicD return getInputIngredients(craftingWidth, craftingHeight); } + @Override + public List<InputIngredient<EntryStack<?>>> getInputIngredients(@Nullable AbstractContainerMenu menu, @Nullable Player player) { + return getInputIngredients(3, 3); + } + public List<InputIngredient<EntryStack<?>>> getInputIngredients(int craftingWidth, int craftingHeight) { int inputWidth = getInputWidth(craftingWidth, craftingHeight); int inputHeight = getInputHeight(craftingWidth, craftingHeight); @@ -198,7 +204,9 @@ public abstract class DefaultCraftingDisplay<C extends Recipe<?>> extends BasicD continue; } int index = getSlotWithSize(inputWidth, i, craftingWidth); - grid.put(new IntIntImmutablePair(i % inputWidth, i / inputWidth), InputIngredient.of(index, stacks)); + int x = i % inputWidth; + int y = i / inputWidth; + grid.put(new IntIntImmutablePair(x, y), InputIngredient.of(index, 3 * y + x, stacks)); } List<InputIngredient<EntryStack<?>>> list = new ArrayList<>(craftingWidth * craftingHeight); |
