|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright (c) 2021-2024 Team Galacticraft |
| 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 | + |
| 24 | +package dev.galacticraft.machinelib.api.compat.vanilla; |
| 25 | + |
| 26 | +import com.google.common.collect.Iterators; |
| 27 | +import dev.galacticraft.machinelib.api.storage.SlottedStorageAccess; |
| 28 | +import dev.galacticraft.machinelib.api.storage.slot.ItemResourceSlot; |
| 29 | +import dev.galacticraft.machinelib.api.util.ItemStackUtil; |
| 30 | +import net.minecraft.world.entity.player.StackedContents; |
| 31 | +import net.minecraft.world.inventory.CraftingContainer; |
| 32 | +import net.minecraft.world.item.Item; |
| 33 | +import net.minecraft.world.item.ItemStack; |
| 34 | +import org.jetbrains.annotations.Contract; |
| 35 | +import org.jetbrains.annotations.NotNull; |
| 36 | + |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Iterator; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +/** |
| 42 | + * A container for testing recipes using the vanilla recipe system. |
| 43 | + * |
| 44 | + * @see RecipeTestContainer |
| 45 | + * @see CraftingContainer |
| 46 | + */ |
| 47 | +public class CraftingRecipeTestContainer extends RecipeTestContainer implements CraftingContainer { |
| 48 | + /** |
| 49 | + * The width of the crafting grid |
| 50 | + */ |
| 51 | + private final int width; |
| 52 | + /** |
| 53 | + * The height of the crafting grid |
| 54 | + */ |
| 55 | + private final int height; |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a new container with the given slots. |
| 59 | + * |
| 60 | + * @param slots the slots to use |
| 61 | + * @return a new test container |
| 62 | + */ |
| 63 | + @Contract(value = "_, _, _ -> new", pure = true) |
| 64 | + public static @NotNull CraftingRecipeTestContainer create(int width, int height, ItemResourceSlot @NotNull ... slots) { |
| 65 | + assert slots.length > 0 && slots.length == width * height; |
| 66 | + return new CraftingRecipeTestContainer(width, height, slots); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Creates a new container with the slots specified by a slice of the given storage access |
| 71 | + * |
| 72 | + * @param access the storage access providing the slots |
| 73 | + * @param start the index of the first slot to include in the container |
| 74 | + * @param len the number of slots to include in the container |
| 75 | + * @return a new test container |
| 76 | + */ |
| 77 | + @Contract(value = "_, _, _, _, _-> new", pure = true) |
| 78 | + public static @NotNull CraftingRecipeTestContainer create(int width, int height, SlottedStorageAccess<Item, ItemResourceSlot> access, int start, int len) { |
| 79 | + assert len == width * height; |
| 80 | + Iterator<ItemResourceSlot> iterator = access.iterator(); |
| 81 | + Iterators.advance(iterator, start); |
| 82 | + ItemResourceSlot[] slots = new ItemResourceSlot[len]; |
| 83 | + for (int i = 0; i < len; i++) { |
| 84 | + slots[i] = iterator.next(); |
| 85 | + } |
| 86 | + return new CraftingRecipeTestContainer(width, height, slots); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Constructs a new RecipeTestContainer with the provided slots. |
| 91 | + * |
| 92 | + * @param width the width of the crafting grid |
| 93 | + * @param height the height of the crafting grid |
| 94 | + * @param slots the slots to be included in the container |
| 95 | + */ |
| 96 | + private CraftingRecipeTestContainer(int width, int height, ItemResourceSlot[] slots) { |
| 97 | + super(slots); |
| 98 | + this.width = width; |
| 99 | + this.height = height; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public int getWidth() { |
| 104 | + return this.width; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public int getHeight() { |
| 109 | + return this.height; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public @NotNull List<ItemStack> getItems() { |
| 114 | + List<ItemStack> list = new ArrayList<>(this.slots.length); |
| 115 | + for (ItemResourceSlot slot : this.slots) { |
| 116 | + list.add(ItemStackUtil.create(slot)); |
| 117 | + } |
| 118 | + return list; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void fillStackedContents(StackedContents finder) { |
| 123 | + } |
| 124 | +} |
0 commit comments