From 7c2a510545e8280ff073e289e5d600e4614b27cc Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 14 Apr 2021 20:08:47 +0800 Subject: Refactor MenuInfo into a provider system and add more docs Signed-off-by: shedaniel --- .../rei/api/common/display/SimpleMenuDisplay.java | 6 ++ .../rei/api/common/transfer/info/MenuInfo.java | 95 ++++++++++++++++++---- .../api/common/transfer/info/MenuInfoProvider.java | 18 ++++ .../api/common/transfer/info/MenuInfoRegistry.java | 4 +- .../transfer/info/clean/InputCleanHandler.java | 8 +- .../common/transfer/info/simple/DumpHandler.java | 10 +-- .../info/simple/RecipeBookGridMenuInfo.java | 4 +- .../transfer/info/simple/SimpleGridMenuInfo.java | 19 +++-- .../info/simple/SimplePlayerInventoryMenuInfo.java | 40 +++++++-- .../transfer/info/stack/ContainerSlotAccessor.java | 55 +++++++++++++ .../info/stack/ContainerStackAccessor.java | 55 ------------- .../common/transfer/info/stack/SlotAccessor.java | 47 +++++++++++ .../transfer/info/stack/SlotStackAccessor.java | 53 ------------ .../common/transfer/info/stack/StackAccessor.java | 44 ---------- .../transfer/info/stack/VanillaSlotAccessor.java | 53 ++++++++++++ 15 files changed, 313 insertions(+), 198 deletions(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoProvider.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/ContainerSlotAccessor.java delete mode 100644 api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/ContainerStackAccessor.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/SlotAccessor.java delete mode 100644 api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/SlotStackAccessor.java delete mode 100644 api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/StackAccessor.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/VanillaSlotAccessor.java (limited to 'api/src/main/java/me/shedaniel') diff --git a/api/src/main/java/me/shedaniel/rei/api/common/display/SimpleMenuDisplay.java b/api/src/main/java/me/shedaniel/rei/api/common/display/SimpleMenuDisplay.java index bcedad7fd..623386752 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/display/SimpleMenuDisplay.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/display/SimpleMenuDisplay.java @@ -23,6 +23,12 @@ package me.shedaniel.rei.api.common.display; +/** + * An display to be used alongside {@link me.shedaniel.rei.api.common.transfer.info.simple.SimpleGridMenuInfo}, + * to provide a {@code width} and {@code height} for the grid of the recipe. + * + * @see me.shedaniel.rei.api.common.transfer.info.simple.SimpleGridMenuInfo + */ public interface SimpleMenuDisplay extends Display { int getWidth(); diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfo.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfo.java index bdb54adbb..268934f2a 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfo.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfo.java @@ -23,24 +23,28 @@ package me.shedaniel.rei.api.common.transfer.info; +import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; import me.shedaniel.rei.api.common.display.DisplaySerializerRegistry; import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; import me.shedaniel.rei.api.common.transfer.RecipeFinder; import me.shedaniel.rei.api.common.transfer.RecipeFinderPopulator; import me.shedaniel.rei.api.common.transfer.info.clean.InputCleanHandler; -import me.shedaniel.rei.api.common.transfer.info.stack.StackAccessor; +import me.shedaniel.rei.api.common.transfer.info.simple.SimplePlayerInventoryMenuInfo; +import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor; import me.shedaniel.rei.api.common.util.CollectionUtils; import net.minecraft.nbt.CompoundTag; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; +import java.util.Collections; import java.util.List; +import java.util.Optional; /** * Provider of information for {@link AbstractContainerMenu}, for the default REI {@link me.shedaniel.rei.api.client.registry.transfer.TransferHandler}. - * Allows custom slots by the interface {@link StackAccessor}, populates and syncs the {@link Display} to the server. + * Allows custom slots by the interface {@link SlotAccessor}, populates and syncs the {@link Display} to the server. * * @param the type of the menu * @param the type of display @@ -48,22 +52,50 @@ import java.util.List; * @see me.shedaniel.rei.api.common.transfer.info.simple.SimplePlayerInventoryMenuInfo * @see me.shedaniel.rei.api.common.transfer.info.simple.SimpleGridMenuInfo */ -public interface MenuInfo { - default RecipeFinderPopulator getRecipeFinderPopulator() { - return (context, recipeFinder) -> { - for (StackAccessor inventoryStack : getInventoryStacks(context)) { - recipeFinder.addNormalItem(inventoryStack.getItemStack()); - } - populateRecipeFinder(context.getMenu(), recipeFinder); - }; +public interface MenuInfo extends MenuInfoProvider { + @Override + default Optional> provide(CategoryIdentifier categoryId, Class menuClass) { + return Optional.of(this); } + /** + * Returns a {@link RecipeFinderPopulator}, used to populate a {@link RecipeFinder} with + * available ingredients. + * + * @return a {@link RecipeFinderPopulator} + */ + RecipeFinderPopulator getRecipeFinderPopulator(); + + /** + * Returns an {@link InputCleanHandler} that cleans the input grid. + * + * @return an {@link InputCleanHandler} that cleans the input grid + * @see SimplePlayerInventoryMenuInfo#getInputCleanHandler() + */ InputCleanHandler getInputCleanHandler(); - Iterable getInputStacks(MenuInfoContext context); + /** + * Returns an {@link Iterable} of {@link SlotAccessor}, of the slots that houses the inputs of the transfer. + * + * @param context the context of the transfer + * @return an {@link Iterable} of the input slots. + */ + Iterable getInputSlots(MenuInfoContext context); - Iterable getInventoryStacks(MenuInfoContext context); + /** + * Returns an {@link Iterable} of {@link SlotAccessor}, of the slots that provides ingredients. + * + * @param context the context of the transfer + * @return an {@link Iterable} of the inventory slots. + */ + Iterable getInventorySlots(MenuInfoContext context); + /** + * Marks the transfer as dirty, as in something has changed. + * This denotes that an update should be sent to the {@link ServerPlayer}. + * + * @param context the context of the transfer + */ default void markDirty(MenuInfoContext context) { context.getPlayerEntity().inventory.setChanged(); context.getMenu().broadcastChanges(); @@ -72,19 +104,46 @@ public interface MenuInfo { context.getPlayerEntity().refreshContainer(containerMenu, containerMenu.getItems()); } - default void populateRecipeFinder(T container, RecipeFinder finder) {} - - default void validate(MenuInfoContext context) throws MenuTransferException {} + /** + * Validates the transfer, throws {@link MenuTransferException} if something is wrong. + * + * @param context the context of the transfer + * @throws MenuTransferException the exception to throw if something is wrong, + * this exception should be caught by the invoker + */ + default void validate(MenuInfoContext context) throws MenuTransferException { + } - default List> getDisplayInputs(MenuInfoContext context) { - return CollectionUtils.map(context.getDisplay().getInputEntries(), inputEntry -> CollectionUtils.filterAndMap(inputEntry, stack -> stack.getType() == VanillaEntryTypes.ITEM, - stack -> stack.cast().getValue())); + /** + * Returns the inputs of the {@link Display}. The nested lists are possible stacks for that specific slot. + * + * @param context the context of the transfer + * @return the list of lists of items + */ + default List> getInputs(MenuInfoContext context) { + if (context.getDisplay() == null) return Collections.emptyList(); + return CollectionUtils.map(context.getDisplay().getInputEntries(), inputEntry -> + CollectionUtils.filterAndMap(inputEntry, stack -> stack.getType() == VanillaEntryTypes.ITEM, stack -> stack.cast().getValue())); } + /** + * Serializes the {@link Display} as {@link CompoundTag}, sent to the server for further info for the transfer. + * + * @param context the context of the transfer + * @param display the display to serialize + * @return the {@link CompoundTag} serialized + */ default CompoundTag save(MenuSerializationContext context, D display) { return DisplaySerializerRegistry.getInstance().save(display.getCategoryIdentifier(), display, new CompoundTag()); } + /** + * Deserializes the {@link Display} from {@link CompoundTag}, sent from the client for further info of the transfer. + * + * @param context the context of the transfer + * @param tag the nbt tag to deserialize from + * @return the {@link Display} deserialized + */ default D read(MenuSerializationContext context, CompoundTag tag) { return DisplaySerializerRegistry.getInstance().read(context.getCategoryIdentifier(), tag); } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoProvider.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoProvider.java new file mode 100644 index 000000000..1e43dbbdc --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoProvider.java @@ -0,0 +1,18 @@ +package me.shedaniel.rei.api.common.transfer.info; + +import me.shedaniel.rei.api.common.category.CategoryIdentifier; +import me.shedaniel.rei.api.common.display.Display; +import net.minecraft.world.inventory.AbstractContainerMenu; + +import java.util.Optional; + +/** + * A provider of {@link MenuInfo}, to provide info conditionally, or dynamically. + * + * @param the type of the menu + * @param the type of display + */ +@FunctionalInterface +public interface MenuInfoProvider { + Optional> provide(CategoryIdentifier categoryId, Class menuClass); +} diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoRegistry.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoRegistry.java index a6e7b665b..c1c0b79b0 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoRegistry.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoRegistry.java @@ -40,9 +40,9 @@ public interface MenuInfoRegistry extends Reloadable { return PluginManager.getServerInstance().get(MenuInfoRegistry.class); } - void register(CategoryIdentifier category, Class menuClass, MenuInfo menuInfo); + void register(CategoryIdentifier category, Class menuClass, MenuInfoProvider menuInfo); - void registerGeneric(Predicate> categoryPredicate, MenuInfo menuInfo); + void registerGeneric(Predicate> categoryPredicate, MenuInfoProvider menuInfo); MenuInfo get(CategoryIdentifier category, Class menuClass); diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/clean/InputCleanHandler.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/clean/InputCleanHandler.java index d6731225e..6b99fe701 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/clean/InputCleanHandler.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/clean/InputCleanHandler.java @@ -26,7 +26,7 @@ package me.shedaniel.rei.api.common.transfer.info.clean; import me.shedaniel.rei.api.common.display.Display; import me.shedaniel.rei.api.common.transfer.info.MenuInfoContext; import me.shedaniel.rei.api.common.transfer.info.simple.DumpHandler; -import me.shedaniel.rei.api.common.transfer.info.stack.StackAccessor; +import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; @@ -40,10 +40,10 @@ public interface InputCleanHandler void returnSlotsToPlayerInventory(MenuInfoContext context, DumpHandler dumpHandler, StackAccessor stackAccessor) { - ItemStack stackToReturn = stackAccessor.getItemStack(); + static void returnSlotsToPlayerInventory(MenuInfoContext context, DumpHandler dumpHandler, SlotAccessor slotAccessor) { + ItemStack stackToReturn = slotAccessor.getItemStack(); if (!stackToReturn.isEmpty()) { - for (; stackToReturn.getCount() > 0; stackAccessor.takeStack(1)) { + for (; stackToReturn.getCount() > 0; slotAccessor.takeStack(1)) { ItemStack stackToInsert = stackToReturn.copy(); stackToInsert.setCount(1); if (!dumpGenericsFtw(context, dumpHandler, stackToInsert)) { diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/DumpHandler.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/DumpHandler.java index f3f9fdaee..056ab3941 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/DumpHandler.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/DumpHandler.java @@ -25,7 +25,7 @@ package me.shedaniel.rei.api.common.transfer.info.simple; import me.shedaniel.rei.api.common.display.Display; import me.shedaniel.rei.api.common.transfer.info.MenuInfoContext; -import me.shedaniel.rei.api.common.transfer.info.stack.StackAccessor; +import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; @@ -33,8 +33,8 @@ import net.minecraft.world.item.ItemStack; public interface DumpHandler { boolean dump(MenuInfoContext context, ItemStack stackToDump); - static StackAccessor getOccupiedSlotWithRoomForStack(ItemStack stack, Iterable inventoryStacks) { - for (StackAccessor inventoryStack : inventoryStacks) { + static SlotAccessor getOccupiedSlotWithRoomForStack(ItemStack stack, Iterable inventoryStacks) { + for (SlotAccessor inventoryStack : inventoryStacks) { if (canStackAddMore(inventoryStack.getItemStack(), stack)) { return inventoryStack; } @@ -43,8 +43,8 @@ public interface DumpHandler return null; } - static StackAccessor getEmptySlot(Iterable inventoryStacks) { - for (StackAccessor inventoryStack : inventoryStacks) { + static SlotAccessor getEmptySlot(Iterable inventoryStacks) { + for (SlotAccessor inventoryStack : inventoryStacks) { if (inventoryStack.getItemStack().isEmpty()) { return inventoryStack; } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/RecipeBookGridMenuInfo.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/RecipeBookGridMenuInfo.java index f1a4bb84a..982307433 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/RecipeBookGridMenuInfo.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/RecipeBookGridMenuInfo.java @@ -50,8 +50,8 @@ public class RecipeBookGridMenuInfo, D extends Simpl } @Override - public void populateRecipeFinder(T container, RecipeFinder finder) { - container.fillCraftSlotsStackedContents(new net.minecraft.world.entity.player.StackedContents() { + public void populateRecipeFinder(T menu, RecipeFinder finder) { + menu.fillCraftSlotsStackedContents(new net.minecraft.world.entity.player.StackedContents() { @Override public void accountSimpleStack(ItemStack stack) { finder.addNormalItem(stack); diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/SimpleGridMenuInfo.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/SimpleGridMenuInfo.java index 24fbe807c..fbb47f5ca 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/SimpleGridMenuInfo.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/SimpleGridMenuInfo.java @@ -27,7 +27,7 @@ import me.shedaniel.rei.api.common.display.SimpleMenuDisplay; import me.shedaniel.rei.api.common.transfer.info.MenuInfo; import me.shedaniel.rei.api.common.transfer.info.MenuInfoContext; import me.shedaniel.rei.api.common.transfer.info.MenuTransferException; -import me.shedaniel.rei.api.common.transfer.info.stack.StackAccessor; +import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor; import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.world.inventory.AbstractContainerMenu; @@ -35,22 +35,27 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; /** - * A simple implementation of {@link MenuInfo} that provides {@link StackAccessor} by {@link net.minecraft.world.inventory.Slot}. + * A simple implementation of {@link MenuInfo} that provides {@link SlotAccessor} by {@link net.minecraft.world.inventory.Slot}. *

* Designed to be used for {@link net.minecraft.world.inventory.RecipeBookMenu}, and expects a width and height for a grid for the input. * Requires the display to be a implementation of {@link SimpleMenuDisplay}, to provide the width and height of the display. * * @param the type of the menu * @param the type of display + * @see SimpleMenuDisplay */ public interface SimpleGridMenuInfo extends SimplePlayerInventoryMenuInfo { - default Iterable getInputStacks(MenuInfoContext context) { - return IntStream.range(0, getCraftingWidth(context.getMenu()) * getCraftingHeight(context.getMenu()) + 1) - .filter(value -> value != getCraftingResultSlotIndex(context.getMenu())) - .mapToObj(value -> StackAccessor.fromSlot(context.getMenu().getSlot(value))) + default Iterable getInputSlots(MenuInfoContext context) { + return getInputStackSlotIds(context) + .mapToObj(value -> SlotAccessor.fromSlot(context.getMenu().getSlot(value))) .collect(Collectors.toList()); } + default IntStream getInputStackSlotIds(MenuInfoContext context) { + return IntStream.range(0, getCraftingWidth(context.getMenu()) * getCraftingHeight(context.getMenu()) + 1) + .filter(value -> value != getCraftingResultSlotIndex(context.getMenu())); + } + int getCraftingResultSlotIndex(T menu); int getCraftingWidth(T menu); @@ -62,7 +67,7 @@ public interface SimpleGridMenuInfo width || display.getHeight() > height) { + if (display != null && (display.getWidth() > width || display.getHeight() > height)) { throw new MenuTransferException(new TranslatableComponent("error.rei.transfer.too_small", width, height)); } } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/SimplePlayerInventoryMenuInfo.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/SimplePlayerInventoryMenuInfo.java index 67f5d2bb1..d06b13c22 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/SimplePlayerInventoryMenuInfo.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/simple/SimplePlayerInventoryMenuInfo.java @@ -25,10 +25,12 @@ package me.shedaniel.rei.api.common.transfer.info.simple; import com.google.common.base.MoreObjects; import me.shedaniel.rei.api.common.display.Display; +import me.shedaniel.rei.api.common.transfer.RecipeFinder; +import me.shedaniel.rei.api.common.transfer.RecipeFinderPopulator; import me.shedaniel.rei.api.common.transfer.info.MenuInfo; import me.shedaniel.rei.api.common.transfer.info.MenuInfoContext; import me.shedaniel.rei.api.common.transfer.info.clean.InputCleanHandler; -import me.shedaniel.rei.api.common.transfer.info.stack.StackAccessor; +import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; @@ -37,19 +39,33 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; /** - * A simple implementation of {@link MenuInfo} that provides {@link StackAccessor} by {@link Inventory} of the player. + * A simple implementation of {@link MenuInfo} that provides {@link SlotAccessor} by {@link Inventory} of the player. *

* Provides default implementation for {@link SimplePlayerInventoryMenuInfo#getInputCleanHandler()}, which dumps slots from - * {@link SimplePlayerInventoryMenuInfo#getInputStacks(MenuInfoContext)} to the {@link SimplePlayerInventoryMenuInfo#getDumpHandler()}. + * {@link SimplePlayerInventoryMenuInfo#getInputSlots(MenuInfoContext)} to the {@link SimplePlayerInventoryMenuInfo#getDumpHandler()}. * * @param the type of the menu * @param the type of display */ public interface SimplePlayerInventoryMenuInfo extends MenuInfo { + default RecipeFinderPopulator getRecipeFinderPopulator() { + return (context, finder) -> { + for (SlotAccessor inventoryStack : getInventorySlots(context)) { + finder.addNormalItem(inventoryStack.getItemStack()); + } + populateRecipeFinder(context.getMenu(), finder); + }; + } + + /** + * Returns a simple {@link InputCleanHandler} that returns any stacks on the grid back to the player's inventory. + * + * @return a simple implementation of {@link InputCleanHandler} + */ default InputCleanHandler getInputCleanHandler() { return context -> { T container = context.getMenu(); - for (StackAccessor gridStack : getInputStacks(context)) { + for (SlotAccessor gridStack : getInputSlots(context)) { InputCleanHandler.returnSlotsToPlayerInventory(context, getDumpHandler(), gridStack); } @@ -59,9 +75,9 @@ public interface SimplePlayerInventoryMenuInfo getDumpHandler() { return (context, stackToDump) -> { - Iterable inventoryStacks = getInventoryStacks(context); + Iterable inventoryStacks = getInventorySlots(context); - StackAccessor nextSlot = MoreObjects.firstNonNull( + SlotAccessor nextSlot = MoreObjects.firstNonNull( DumpHandler.getOccupiedSlotWithRoomForStack(stackToDump, inventoryStacks), DumpHandler.getEmptySlot(inventoryStacks) ); @@ -76,12 +92,20 @@ public interface SimplePlayerInventoryMenuInfo getInventoryStacks(MenuInfoContext context) { + default Iterable getInventorySlots(MenuInfoContext context) { Inventory inventory = context.getPlayerEntity().inventory; return IntStream.range(0, inventory.items.size()) - .mapToObj(index -> StackAccessor.fromContainer(inventory, index)) + .mapToObj(index -> SlotAccessor.fromContainer(inventory, index)) .collect(Collectors.toList()); } + default void populateRecipeFinder(T menu, RecipeFinder finder) {} + + /** + * Used to forcefully clear the input slots, if things did not dump to the player's inventory successfully. + * The default implementation here is to void the items, as with vanilla, + * + * @param menu the menu to clean + */ default void clearInputSlots(T menu) {} } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/ContainerSlotAccessor.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/ContainerSlotAccessor.java new file mode 100644 index 000000000..22570d47c --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/ContainerSlotAccessor.java @@ -0,0 +1,55 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020 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.api.common.transfer.info.stack; + +import net.minecraft.world.Container; +import net.minecraft.world.item.ItemStack; + +/** + * Simple implementation of {@link SlotAccessor} that wraps around a slot in {@link Container}. + */ +public class ContainerSlotAccessor implements SlotAccessor { + protected Container container; + protected int index; + + public ContainerSlotAccessor(Container container, int index) { + this.container = container; + this.index = index; + } + + @Override + public ItemStack getItemStack() { + return container.getItem(index); + } + + @Override + public void setItemStack(ItemStack stack) { + this.container.setItem(index, stack); + } + + @Override + public ItemStack takeStack(int amount) { + return this.container.removeItem(index, amount); + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/ContainerStackAccessor.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/ContainerStackAccessor.java deleted file mode 100644 index 90a6a937a..000000000 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/ContainerStackAccessor.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * This file is licensed under the MIT License, part of Roughly Enough Items. - * Copyright (c) 2018, 2019, 2020 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.api.common.transfer.info.stack; - -import net.minecraft.world.Container; -import net.minecraft.world.item.ItemStack; - -/** - * Simple implementation of {@link StackAccessor} that wraps around a slot in {@link Container}. - */ -public class ContainerStackAccessor implements StackAccessor { - protected Container container; - protected int index; - - public ContainerStackAccessor(Container container, int index) { - this.container = container; - this.index = index; - } - - @Override - public ItemStack getItemStack() { - return container.getItem(index); - } - - @Override - public void setItemStack(ItemStack stack) { - this.container.setItem(index, stack); - } - - @Override - public ItemStack takeStack(int amount) { - return this.container.removeItem(index, amount); - } -} diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/SlotAccessor.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/SlotAccessor.java new file mode 100644 index 000000000..2bb339219 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/SlotAccessor.java @@ -0,0 +1,47 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020 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.api.common.transfer.info.stack; + +import net.minecraft.world.Container; +import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.ItemStack; + +/** + * A wrapper for accessing {@link Slot}, can be wrapped for non-vanilla {@link Slot} implementations. + */ +public interface SlotAccessor { + ItemStack getItemStack(); + + void setItemStack(ItemStack stack); + + ItemStack takeStack(int amount); + + static SlotAccessor fromSlot(Slot slot) { + return new VanillaSlotAccessor(slot); + } + + static SlotAccessor fromContainer(Container container, int index) { + return new ContainerSlotAccessor(container, index); + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/SlotStackAccessor.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/SlotStackAccessor.java deleted file mode 100644 index 427f32fb5..000000000 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/SlotStackAccessor.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * This file is licensed under the MIT License, part of Roughly Enough Items. - * Copyright (c) 2018, 2019, 2020 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.api.common.transfer.info.stack; - -import net.minecraft.world.inventory.Slot; -import net.minecraft.world.item.ItemStack; - -/** - * Simple implementation of {@link StackAccessor} that wraps around {@link Slot}. - */ -public class SlotStackAccessor implements StackAccessor { - protected Slot slot; - - public SlotStackAccessor(Slot slot) { - this.slot = slot; - } - - @Override - public ItemStack getItemStack() { - return slot.getItem(); - } - - @Override - public void setItemStack(ItemStack stack) { - this.slot.set(stack); - } - - @Override - public ItemStack takeStack(int amount) { - return slot.remove(amount); - } -} diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/StackAccessor.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/StackAccessor.java deleted file mode 100644 index 767483bb3..000000000 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/StackAccessor.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * This file is licensed under the MIT License, part of Roughly Enough Items. - * Copyright (c) 2018, 2019, 2020 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.api.common.transfer.info.stack; - -import net.minecraft.world.Container; -import net.minecraft.world.inventory.Slot; -import net.minecraft.world.item.ItemStack; - -public interface StackAccessor { - ItemStack getItemStack(); - - void setItemStack(ItemStack stack); - - ItemStack takeStack(int amount); - - static StackAccessor fromSlot(Slot slot) { - return new SlotStackAccessor(slot); - } - - static StackAccessor fromContainer(Container container, int index) { - return new ContainerStackAccessor(container, index); - } -} diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/VanillaSlotAccessor.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/VanillaSlotAccessor.java new file mode 100644 index 000000000..f7208e92b --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/stack/VanillaSlotAccessor.java @@ -0,0 +1,53 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020 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.api.common.transfer.info.stack; + +import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.ItemStack; + +/** + * Simple implementation of {@link SlotAccessor} that wraps around {@link Slot}. + */ +public class VanillaSlotAccessor implements SlotAccessor { + protected Slot slot; + + public VanillaSlotAccessor(Slot slot) { + this.slot = slot; + } + + @Override + public ItemStack getItemStack() { + return slot.getItem(); + } + + @Override + public void setItemStack(ItemStack stack) { + this.slot.set(stack); + } + + @Override + public ItemStack takeStack(int amount) { + return slot.remove(amount); + } +} -- cgit