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 ++++++++++++ .../shedaniel/rei/plugin/common/DefaultPlugin.java | 4 +- .../me/shedaniel/rei/RoughlyEnoughItemsCore.java | 26 +++--- .../rei/impl/common/transfer/InputSlotCrafter.java | 26 +++--- .../impl/common/transfer/MenuInfoRegistryImpl.java | 41 +++++++--- .../autocrafting/DefaultCategoryHandler.java | 2 +- 20 files changed, 372 insertions(+), 238 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 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); + } +} 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 7e0410bb4..136ab6a61 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 @@ -110,13 +110,13 @@ public class DefaultPlugin implements BuiltinPlugin, REIServerPlugin { public void registerMenuInfo(MenuInfoRegistry registry) { registry.register(BuiltinPlugin.CRAFTING, CraftingMenu.class, new RecipeBookGridMenuInfo() { @Override - public List> getDisplayInputs(MenuInfoContext context) { + public List> getInputs(MenuInfoContext context) { return context.getDisplay().getOrganisedInputEntries(this, context.getMenu()); } }); registry.register(BuiltinPlugin.CRAFTING, InventoryMenu.class, new RecipeBookGridMenuInfo() { @Override - public List> getDisplayInputs(MenuInfoContext context) { + public List> getInputs(MenuInfoContext context) { return context.getDisplay().getOrganisedInputEntries(this, context.getMenu()); } }); diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java index 0ed0e6f78..6f7e36118 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java @@ -142,18 +142,18 @@ import java.util.stream.Stream; public class RoughlyEnoughItemsCore { @ApiStatus.Internal public static final Logger LOGGER = LogManager.getFormatterLogger("REI"); - private static final ExecutorService RELOAD_PLUGINS; + private static ExecutorService reloadPlugins; @ApiStatus.Experimental public static boolean isLeftMousePressed = false; static { - RELOAD_PLUGINS = Executors.newSingleThreadScheduledExecutor(r -> { - Thread thread = new Thread(r, "REI-ReloadPlugins"); - thread.setDaemon(true); - return thread; - }); attachCommonInternals(); if (Platform.getEnvironment() == Env.CLIENT) { + reloadPlugins = Executors.newSingleThreadScheduledExecutor(r -> { + Thread thread = new Thread(r, "REI-ReloadPlugins"); + thread.setDaemon(true); + return thread; + }); attachClientInternals(); } } @@ -401,15 +401,19 @@ public class RoughlyEnoughItemsCore { lastReload.setValue(System.currentTimeMillis()); } if (ConfigObject.getInstance().doesRegisterRecipesInAnotherThread()) { - CompletableFuture.runAsync(RoughlyEnoughItemsCore::_reloadPlugins, RELOAD_PLUGINS); + CompletableFuture.runAsync(RoughlyEnoughItemsCore::_reloadPlugins, reloadPlugins); } else { _reloadPlugins(); } } private static void _reloadPlugins() { - for (PluginManager> instance : PluginManager.getActiveInstances()) { - instance.startReload(); + try { + for (PluginManager> instance : PluginManager.getActiveInstances()) { + instance.startReload(); + } + } catch (Throwable throwable) { + throwable.printStackTrace(); } } @@ -421,9 +425,7 @@ public class RoughlyEnoughItemsCore { if (Platform.getEnvironment() == Env.SERVER) { MutableLong lastReload = new MutableLong(-1); ReloadListeners.registerReloadListener(PackType.SERVER_DATA, (preparationBarrier, resourceManager, profilerFiller, profilerFiller2, executor, executor2) -> { - return preparationBarrier.wait(Unit.INSTANCE).thenRunAsync(() -> { - CompletableFuture.runAsync(RoughlyEnoughItemsCore::_reloadPlugins, RELOAD_PLUGINS); - }, executor2); + return preparationBarrier.wait(Unit.INSTANCE).thenRunAsync(RoughlyEnoughItemsCore::_reloadPlugins, executor2); }); } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/InputSlotCrafter.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/InputSlotCrafter.java index 17812fec9..f17ae6faa 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/InputSlotCrafter.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/InputSlotCrafter.java @@ -31,7 +31,7 @@ import me.shedaniel.rei.api.common.transfer.RecipeFinder; 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.MenuInfoRegistry; -import me.shedaniel.rei.api.common.transfer.info.stack.StackAccessor; +import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor; import me.shedaniel.rei.api.common.util.CollectionUtils; import net.minecraft.core.NonNullList; import net.minecraft.nbt.CompoundTag; @@ -51,8 +51,8 @@ public class InputSlotCrafter menuInfo; - private Iterable inputStacks; - private Iterable inventoryStacks; + private Iterable inputStacks; + private Iterable inventoryStacks; private ServerPlayer player; private InputSlotCrafter(CategoryIdentifier category, T container, CompoundTag display, MenuInfo menuInfo) { @@ -71,8 +71,8 @@ public class InputSlotCrafter ingredients = NonNullList.create(); - for (List itemStacks : this.menuInfo.getDisplayInputs(this)) { + for (List itemStacks : this.menuInfo.getInputs(this)) { ingredients.add(CollectionUtils.toIngredient(itemStacks)); } @@ -98,8 +98,8 @@ public class InputSlotCrafter inputStacks, Iterator recipeItemIds, int craftsAmount) { - for (StackAccessor inputStack : inputStacks) { + public void alignRecipeToGrid(Iterable inputStacks, Iterator recipeItemIds, int craftsAmount) { + for (SlotAccessor inputStack : inputStacks) { if (!recipeItemIds.hasNext()) { return; } @@ -108,7 +108,7 @@ public class InputSlotCrafter, Map, MenuInfo>> map = Maps.newLinkedHashMap(); - private final Map>, List>> mapGeneric = Maps.newLinkedHashMap(); + private final Map, Map, List>>> map = Maps.newLinkedHashMap(); + private final Map>, List>> mapGeneric = Maps.newLinkedHashMap(); @Override - public void register(CategoryIdentifier category, Class menuClass, MenuInfo menuInfo) { - map.computeIfAbsent(category, id -> Maps.newLinkedHashMap()).put(menuClass, menuInfo); + public void register(CategoryIdentifier category, Class menuClass, MenuInfoProvider menuInfo) { + map.computeIfAbsent(category, id -> Maps.newLinkedHashMap()) + .computeIfAbsent(menuClass, c -> Lists.newArrayList()) + .add(menuInfo); } @Override - public void registerGeneric(Predicate> categoryPredicate, MenuInfo menuInfo) { + public void registerGeneric(Predicate> categoryPredicate, MenuInfoProvider menuInfo) { mapGeneric.computeIfAbsent(categoryPredicate, id -> Lists.newArrayList()).add(menuInfo); } @Override public MenuInfo get(CategoryIdentifier category, Class menuClass) { - Map, MenuInfo> infoMap = map.get(category); + Map, List>> infoMap = map.get(category); if (infoMap != null && !infoMap.isEmpty()) { if (infoMap.containsKey(menuClass)) { - return (MenuInfo) infoMap.get(menuClass); + for (MenuInfoProvider provider : infoMap.get(menuClass)) { + Optional> info = ((MenuInfoProvider) provider).provide(category, menuClass); + if (info.isPresent()) { + return info.get(); + } + } } - for (Map.Entry, MenuInfo> entry : infoMap.entrySet()) { + for (Map.Entry, List>> entry : infoMap.entrySet()) { if (entry.getKey().isAssignableFrom(menuClass)) { - return (MenuInfo) entry.getValue(); + for (MenuInfoProvider provider : entry.getValue()) { + Optional> info = ((MenuInfoProvider) provider).provide(category, menuClass); + if (info.isPresent()) { + return info.get(); + } + } } } } - for (Map.Entry>, List>> entry : mapGeneric.entrySet()) { + for (Map.Entry>, List>> entry : mapGeneric.entrySet()) { if (entry.getKey().test(category) && !entry.getValue().isEmpty()) { - List> infoList = entry.getValue(); + List> infoList = entry.getValue(); if (!infoList.isEmpty()) { - return (MenuInfo) infoList.get(0); + Optional> info = ((MenuInfoProvider) infoList.get(0)).provide(category, menuClass); + if (info.isPresent()) { + return info.get(); + } } } } diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java b/runtime/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java index dba6fe449..a400c0155 100644 --- a/runtime/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java +++ b/runtime/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java @@ -74,7 +74,7 @@ public class DefaultCategoryHandler implements TransferHandler { } catch (MenuTransferException e) { return Result.createFailed(e.getError()); } - List> input = menuInfo.getDisplayInputs(menuInfoContext); + List> input = menuInfo.getInputs(menuInfoContext); IntList intList = hasItems(menu, menuInfo, display, input); if (!intList.isEmpty()) { return Result.createFailed(new TranslatableComponent("error.rei.not.enough.materials"), intList); -- cgit From a015ddaba5ba8b02e93db164d219b0c12d8bfa3a Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 14 Apr 2021 22:23:17 +0800 Subject: Apply generics to FilteringScreen Signed-off-by: shedaniel --- .../shedaniel/rei/impl/client/config/entries/FilteringScreen.java | 8 ++++---- .../src/main/java/me/shedaniel/rei/plugin/test/REITestPlugin.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringScreen.java index 9717f1cf1..032359ff1 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringScreen.java @@ -68,7 +68,7 @@ import static me.shedaniel.rei.impl.client.gui.widget.EntryListWidget.entrySize; @ApiStatus.Internal public class FilteringScreen extends Screen { - protected List selected = Lists.newArrayList(); + protected List> selected = Lists.newArrayList(); protected final ScrollingContainer scrolling = new ScrollingContainer() { @Override public int getMaxScrollHeight() { @@ -89,7 +89,7 @@ public class FilteringScreen extends Screen { Screen parent; private FilteringEntry filteringEntry; private Tooltip tooltip = null; - private List entryStacks = null; + private List> entryStacks = null; private Rectangle innerBounds; private List entries = Collections.emptyList(); private List elements = Collections.emptyList(); @@ -129,7 +129,7 @@ public class FilteringScreen extends Screen { Component hideText = new TranslatableComponent("config.roughlyenoughitems.filteredEntries.hide"); this.hideButton = new Button(0, 0, Minecraft.getInstance().font.width(hideText) + 10, 20, hideText, button -> { for (int i = 0; i < entryStacks.size(); i++) { - EntryStack stack = entryStacks.get(i); + EntryStack stack = entryStacks.get(i); EntryListEntry entry = entries.get(i); entry.getBounds().y = (int) (entry.backupY - scrolling.scrollAmount); if (entry.isSelected() && !entry.isFiltered()) { @@ -144,7 +144,7 @@ public class FilteringScreen extends Screen { Component showText = new TranslatableComponent("config.roughlyenoughitems.filteredEntries.show"); this.showButton = new Button(0, 0, Minecraft.getInstance().font.width(showText) + 10, 20, showText, button -> { for (int i = 0; i < entryStacks.size(); i++) { - EntryStack stack = entryStacks.get(i); + EntryStack stack = entryStacks.get(i); EntryListEntry entry = entries.get(i); entry.getBounds().y = (int) (entry.backupY - scrolling.scrollAmount); if (entry.isSelected() && filteringEntry.configFiltered.remove(stack)) { diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/test/REITestPlugin.java b/runtime/src/main/java/me/shedaniel/rei/plugin/test/REITestPlugin.java index f0f1dae76..c31c1e864 100644 --- a/runtime/src/main/java/me/shedaniel/rei/plugin/test/REITestPlugin.java +++ b/runtime/src/main/java/me/shedaniel/rei/plugin/test/REITestPlugin.java @@ -53,7 +53,7 @@ public class REITestPlugin implements REIClientPlugin { @Override public void registerEntries(EntryRegistry registry) { - int times = 1000; + int times = 100; for (Item item : Registry.ITEM) { EntryStack base = EntryStacks.of(item); registry.addEntriesAfter(base, IntStream.range(0, times).mapToObj(value -> transformStack(EntryStacks.of(item))).collect(Collectors.toList())); -- cgit From 4e2c0be9a75e435956ca80d56df2ca609245671d Mon Sep 17 00:00:00 2001 From: shedaniel Date: Fri, 16 Apr 2021 17:29:21 +0800 Subject: Normalize fluid type Signed-off-by: shedaniel --- .../me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java b/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java index 9344eb9e8..75871dbca 100644 --- a/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java +++ b/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java @@ -63,6 +63,7 @@ import net.minecraft.tags.TagContainer; import net.minecraft.util.Mth; import net.minecraft.world.inventory.InventoryMenu; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.material.FlowingFluid; import net.minecraft.world.level.material.Fluid; import org.jetbrains.annotations.Nullable; @@ -116,7 +117,9 @@ public class FluidEntryDefinition implements EntryDefinition, EntryS @Override public FluidStack normalize(EntryStack entry, FluidStack value) { - FluidStack copy = value.copy(); + Fluid fluid = value.getFluid(); + if (fluid instanceof FlowingFluid) fluid = ((FlowingFluid) fluid).getSource(); + FluidStack copy = FluidStack.create(fluid, value.getAmount(), value.getTag()); copy.setAmount(FluidStack.bucketAmount()); return copy; } -- cgit