diff --git a/src/main/java/com/github/elenterius/biomancy/block/bioforge/BioForgeBlockEntity.java b/src/main/java/com/github/elenterius/biomancy/block/bioforge/BioForgeBlockEntity.java index c254e25b0..16c1d4d1b 100644 --- a/src/main/java/com/github/elenterius/biomancy/block/bioforge/BioForgeBlockEntity.java +++ b/src/main/java/com/github/elenterius/biomancy/block/bioforge/BioForgeBlockEntity.java @@ -55,7 +55,7 @@ public class BioForgeBlockEntity extends BlockEntity implements MenuProvider, Pl private final BioForgeStateData stateData; private final FuelHandler fuelHandler; - private final InventoryHandler fuelInventory; + private final InventoryHandler fuelInventory; private final ContainerOpenersCounter openersCounter = new ContainerOpenersCounter() { @@ -150,7 +150,7 @@ public AbstractContainerMenu createMenu(int containerId, Inventory playerInvento return BioForgeMenu.createServerMenu(containerId, playerInventory, this); } - public InventoryHandler getFuelInventory() { + public InventoryHandler getFuelInventory() { return fuelInventory; } diff --git a/src/main/java/com/github/elenterius/biomancy/block/biolab/BioLabBlockEntity.java b/src/main/java/com/github/elenterius/biomancy/block/biolab/BioLabBlockEntity.java index 6d2a28d51..b542ded9c 100644 --- a/src/main/java/com/github/elenterius/biomancy/block/biolab/BioLabBlockEntity.java +++ b/src/main/java/com/github/elenterius/biomancy/block/biolab/BioLabBlockEntity.java @@ -10,14 +10,13 @@ import com.github.elenterius.biomancy.init.ModCapabilities; import com.github.elenterius.biomancy.init.ModRecipes; import com.github.elenterius.biomancy.init.ModSoundEvents; +import com.github.elenterius.biomancy.inventory.BehavioralItemHandler; import com.github.elenterius.biomancy.inventory.InventoryHandler; import com.github.elenterius.biomancy.inventory.InventoryHandlers; import com.github.elenterius.biomancy.inventory.ItemHandlerUtil; import com.github.elenterius.biomancy.menu.BioLabMenu; import com.github.elenterius.biomancy.styles.TextComponentUtil; import com.github.elenterius.biomancy.util.ILoopingSoundHelper; -import com.github.elenterius.biomancy.util.ItemStackFilter; -import com.github.elenterius.biomancy.util.ItemStackFilterList; import com.github.elenterius.biomancy.util.SoundUtil; import com.github.elenterius.biomancy.util.fuel.FluidFuelConsumerHandler; import com.github.elenterius.biomancy.util.fuel.FuelHandler; @@ -69,12 +68,11 @@ public class BioLabBlockEntity extends MachineBlockEntity fuelInventory; - private final InventoryHandler inputInventory; - private final ItemStackFilterList inputSlotsFilter; + private final InventoryHandler inputInventory; - private final InventoryHandler outputInventory; + private final InventoryHandler outputInventory; private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); private ILoopingSoundHelper loopingSoundHelper = ILoopingSoundHelper.NULL; @@ -85,8 +83,7 @@ public class BioLabBlockEntity extends MachineBlockEntity new FluidFuelConsumerHandler(fuelHandler)); + + stateData = new BioLabStateData(fuelHandler, inputInventory.get()); } private LazyOptional createCombinedInventory() { @@ -135,15 +133,15 @@ public BioLabStateData getStateData() { } @Override - public InventoryHandler getInputInventory() { + public InventoryHandler getInputInventory() { return inputInventory; } - public InventoryHandler getFuelInventory() { + public InventoryHandler getFuelInventory() { return fuelInventory; } - public InventoryHandler getOutputInventory() { + public InventoryHandler getOutputInventory() { return outputInventory; } @@ -185,7 +183,6 @@ protected void saveAdditional(CompoundTag tag) { tag.put("Fuel", fuelHandler.serializeNBT()); tag.put("FuelSlots", fuelInventory.serializeNBT()); tag.put("InputSlots", inputInventory.serializeNBT()); - tag.put("InputSlotsFilter", inputSlotsFilter.serializeNBT()); tag.put("OutputSlots", outputInventory.serializeNBT()); } @@ -196,7 +193,6 @@ public void load(CompoundTag tag) { fuelHandler.deserializeNBT(tag.getCompound("Fuel")); fuelInventory.deserializeNBT(tag.getCompound("FuelSlots")); inputInventory.deserializeNBT(tag.getCompound("InputSlots")); - inputSlotsFilter.deserializeNBT(tag.getCompound("InputSlotsFilter")); outputInventory.deserializeNBT(tag.getCompound("OutputSlots")); } diff --git a/src/main/java/com/github/elenterius/biomancy/block/biolab/BioLabStateData.java b/src/main/java/com/github/elenterius/biomancy/block/biolab/BioLabStateData.java index 0833a1e6f..a12aacd09 100644 --- a/src/main/java/com/github/elenterius/biomancy/block/biolab/BioLabStateData.java +++ b/src/main/java/com/github/elenterius/biomancy/block/biolab/BioLabStateData.java @@ -2,13 +2,19 @@ import com.github.elenterius.biomancy.crafting.recipe.BioLabRecipe; import com.github.elenterius.biomancy.crafting.state.FuelConsumingRecipeCraftingStateData; +import com.github.elenterius.biomancy.inventory.BehavioralItemHandler; import com.github.elenterius.biomancy.util.fuel.IFuelHandler; import net.minecraft.world.item.crafting.Recipe; public class BioLabStateData extends FuelConsumingRecipeCraftingStateData { - public BioLabStateData(IFuelHandler fuelHandler) { + public static final int LOCK_INDEX = 4; + + private final BehavioralItemHandler.LockableItemStackFilterInput inputFilterLock; + + public BioLabStateData(IFuelHandler fuelHandler, BehavioralItemHandler.LockableItemStackFilterInput inputFilterLock) { super(fuelHandler); + this.inputFilterLock = inputFilterLock; } @Override @@ -16,4 +22,28 @@ protected boolean isRecipeOfInstance(Recipe recipe) { return recipe instanceof BioLabRecipe; } + public boolean isFilterLocked() { + return inputFilterLock.isLocked(); + } + + @Override + public int get(int index) { + if (index == LOCK_INDEX) return inputFilterLock.isLocked() ? 1 : 0; + return super.get(index); + } + + @Override + public void set(int index, int value) { + if (index == LOCK_INDEX) { + inputFilterLock.setLocked(value != 0); + return; + } + super.set(index, value); + } + + @Override + public int getCount() { + return 5; + } + } diff --git a/src/main/java/com/github/elenterius/biomancy/block/decomposer/DecomposerBlockEntity.java b/src/main/java/com/github/elenterius/biomancy/block/decomposer/DecomposerBlockEntity.java index 43a42c8a1..b601afe60 100644 --- a/src/main/java/com/github/elenterius/biomancy/block/decomposer/DecomposerBlockEntity.java +++ b/src/main/java/com/github/elenterius/biomancy/block/decomposer/DecomposerBlockEntity.java @@ -71,9 +71,9 @@ public class DecomposerBlockEntity extends MachineBlockEntity fuelInventory; + private final InventoryHandler inputInventory; + private final InventoryHandler outputInventory; private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); private ILoopingSoundHelper loopingSoundHelper = ILoopingSoundHelper.NULL; @@ -123,15 +123,15 @@ public DecomposerStateData getStateData() { } @Override - public InventoryHandler getInputInventory() { + public InventoryHandler getInputInventory() { return inputInventory; } - public InventoryHandler getFuelInventory() { + public InventoryHandler getFuelInventory() { return fuelInventory; } - public InventoryHandler getOutputInventory() { + public InventoryHandler getOutputInventory() { return outputInventory; } diff --git a/src/main/java/com/github/elenterius/biomancy/block/digester/DigesterBlockEntity.java b/src/main/java/com/github/elenterius/biomancy/block/digester/DigesterBlockEntity.java index e78b96b6f..9ad400082 100644 --- a/src/main/java/com/github/elenterius/biomancy/block/digester/DigesterBlockEntity.java +++ b/src/main/java/com/github/elenterius/biomancy/block/digester/DigesterBlockEntity.java @@ -65,9 +65,9 @@ public class DigesterBlockEntity extends MachineBlockEntity fuelInventory; + private final InventoryHandler inputInventory; + private final InventoryHandler outputInventory; private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); private ILoopingSoundHelper loopingSoundHelper = ILoopingSoundHelper.NULL; @@ -116,15 +116,15 @@ public DigesterStateData getStateData() { } @Override - public InventoryHandler getInputInventory() { + public InventoryHandler getInputInventory() { return inputInventory; } - public InventoryHandler getFuelInventory() { + public InventoryHandler getFuelInventory() { return fuelInventory; } - public InventoryHandler getOutputInventory() { + public InventoryHandler getOutputInventory() { return outputInventory; } diff --git a/src/main/java/com/github/elenterius/biomancy/block/fleshkinchest/FleshkinChestBlockEntity.java b/src/main/java/com/github/elenterius/biomancy/block/fleshkinchest/FleshkinChestBlockEntity.java index 2ae504d04..1e8e30cfe 100644 --- a/src/main/java/com/github/elenterius/biomancy/block/fleshkinchest/FleshkinChestBlockEntity.java +++ b/src/main/java/com/github/elenterius/biomancy/block/fleshkinchest/FleshkinChestBlockEntity.java @@ -47,7 +47,7 @@ public class FleshkinChestBlockEntity extends OwnableContainerBlockEntity implem public static final int SLOTS = 6 * 7; - private final InventoryHandler inventory; + private final InventoryHandler inventory; private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); @@ -149,7 +149,7 @@ public AbstractContainerMenu createMenu(int containerId, Inventory playerInvento return FleshkinChestMenu.createServerMenu(containerId, playerInventory, this); } - public InventoryHandler getInventory() { + public InventoryHandler getInventory() { return inventory; } diff --git a/src/main/java/com/github/elenterius/biomancy/block/storagesac/StorageSacBlockEntity.java b/src/main/java/com/github/elenterius/biomancy/block/storagesac/StorageSacBlockEntity.java index 3ed424c93..034635677 100644 --- a/src/main/java/com/github/elenterius/biomancy/block/storagesac/StorageSacBlockEntity.java +++ b/src/main/java/com/github/elenterius/biomancy/block/storagesac/StorageSacBlockEntity.java @@ -38,7 +38,7 @@ public class StorageSacBlockEntity extends SimpleContainerBlockEntity implements public static final int SLOTS = 3 * 5; public static final String TOP5_BY_COUNT_KEY = "Top5ByCount"; public static final String INVENTORY_KEY = "Inventory"; - private final InventoryHandler inventory; + private final InventoryHandler inventory; protected final ItemStackCounter itemCounter = new ItemStackCounter(); private List top5ItemsByCount = List.of(); @@ -54,7 +54,7 @@ public AbstractContainerMenu createMenu(int containerId, Inventory playerInvento return StorageSacMenu.createServerMenu(containerId, playerInventory, this); } - public InventoryHandler getInventory() { + public InventoryHandler getInventory() { return inventory; } diff --git a/src/main/java/com/github/elenterius/biomancy/client/gui/BioLabScreen.java b/src/main/java/com/github/elenterius/biomancy/client/gui/BioLabScreen.java index c14672649..34f78555e 100644 --- a/src/main/java/com/github/elenterius/biomancy/client/gui/BioLabScreen.java +++ b/src/main/java/com/github/elenterius/biomancy/client/gui/BioLabScreen.java @@ -26,6 +26,18 @@ protected void init() { super.init(); } + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + int x = leftPos + 130; + int y = topPos + 34; + if (GuiUtil.isInRect(x, y, 8, 10, mouseX, mouseY)) { + minecraft.gameMode.handleInventoryButtonClick(menu.containerId, 0); + return true; + } + + return super.mouseClicked(mouseX, mouseY, button); + } + @Override protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) { //don't draw any labels @@ -43,6 +55,7 @@ protected void renderBg(GuiGraphics guiGraphics, float partialTick, int mouseX, guiGraphics.blit(BACKGROUND_TEXTURE, leftPos, topPos, 0, 0, imageWidth, imageHeight); drawProgressBar(guiGraphics, menu.getCraftingProgressNormalized()); drawFuelBar(guiGraphics, menu.getFuelAmountNormalized()); + drawLock(guiGraphics, menu.isFilterLocked()); } private void drawProgressBar(GuiGraphics guiGraphics, float craftingPct) { @@ -55,6 +68,11 @@ private void drawFuelBar(GuiGraphics guiGraphics, float fuelPct) { guiGraphics.blit(BACKGROUND_TEXTURE, leftPos + 36, topPos + 48 + 36 - vHeight, 178, 58 - vHeight, 5, vHeight); } + private void drawLock(GuiGraphics guiGraphics, boolean isLocked) { + int offset = isLocked ? 0 : 8; + guiGraphics.blit(BACKGROUND_TEXTURE, leftPos + 130, topPos + 34, 178 + offset, 58 + 2, 8, 10); + } + @Override protected void renderTooltip(GuiGraphics guiGraphics, int mouseX, int mouseY) { if (menu.getCarried().isEmpty()) { diff --git a/src/main/java/com/github/elenterius/biomancy/init/ModCapabilities.java b/src/main/java/com/github/elenterius/biomancy/init/ModCapabilities.java index 61b16b145..06019f016 100644 --- a/src/main/java/com/github/elenterius/biomancy/init/ModCapabilities.java +++ b/src/main/java/com/github/elenterius/biomancy/init/ModCapabilities.java @@ -18,7 +18,9 @@ @Mod.EventBusSubscriber(modid = BiomancyMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD) public final class ModCapabilities { + @Deprecated public static final Capability NO_KNOCKBACK_FLAG_CAP = CapabilityManager.get(new CapabilityToken<>() {}); + public static final Capability ITEM_HANDLER = ForgeCapabilities.ITEM_HANDLER; public static final Capability FLUID_HANDLER = ForgeCapabilities.FLUID_HANDLER; diff --git a/src/main/java/com/github/elenterius/biomancy/inventory/BehavioralItemHandler.java b/src/main/java/com/github/elenterius/biomancy/inventory/BehavioralItemHandler.java index 454f2643e..daf3742f0 100644 --- a/src/main/java/com/github/elenterius/biomancy/inventory/BehavioralItemHandler.java +++ b/src/main/java/com/github/elenterius/biomancy/inventory/BehavioralItemHandler.java @@ -1,6 +1,9 @@ package com.github.elenterius.biomancy.inventory; +import com.github.elenterius.biomancy.util.ItemStackFilter; +import com.github.elenterius.biomancy.util.ItemStackFilterList; import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.Tag; import net.minecraft.world.item.ItemStack; import net.minecraftforge.items.IItemHandler; import org.jetbrains.annotations.NotNull; @@ -100,11 +103,11 @@ public void setStackInSlot(int slot, @NotNull ItemStack stack) { /** * only allows item insertion of matching items */ - class FilterInput> extends Wrapper implements SerializableItemHandler { + class PredicateFilterInput extends Wrapper implements SerializableItemHandler { - private final List filters; + protected final List> filters; - public FilterInput(SerializableItemHandler itemHandler, List slotFilters) { + public PredicateFilterInput(SerializableItemHandler itemHandler, List> slotFilters) { super(itemHandler); assert slotFilters.size() == itemHandler.getSlots(); filters = slotFilters; @@ -144,4 +147,81 @@ public void setStackInSlot(int slot, @NotNull ItemStack stack) { } } + class ItemStackFilterInput extends Wrapper implements SerializableItemHandler { + + protected final ItemStackFilterList filters; + + public ItemStackFilterInput(SerializableItemHandler itemHandler) { + super(itemHandler); + filters = ItemStackFilterList.of(ItemStackFilter.ALLOW_ANY, itemHandler.getSlots()); + } + + private boolean isInvalidSlot(int slot) { + return slot < 0 || slot >= filters.size(); + } + + @Override + public boolean isItemValid(int slot, @Nonnull ItemStack stack) { + if (isInvalidSlot(slot)) return false; + return filters.get(slot).test(stack) && itemHandler.isItemValid(slot, stack); + } + + @Override + @Nonnull + public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) { + if (isInvalidSlot(slot)) return stack; + if (!filters.get(slot).test(stack)) return stack; + return itemHandler.insertItem(slot, stack, simulate); + } + + @Override + public CompoundTag serializeNBT() { + CompoundTag tag = new CompoundTag(); + tag.put("Handler", itemHandler.serializeNBT()); + tag.put("Filters", filters.serializeNBT()); + return tag; + } + + @Override + public void deserializeNBT(CompoundTag tag) { + itemHandler.deserializeNBT(tag.getCompound("Handler")); + filters.deserializeNBT(tag.getList("Filters", Tag.TAG_COMPOUND)); + } + + @Override + public void setStackInSlot(int slot, @NotNull ItemStack stack) { + itemHandler.setStackInSlot(slot, stack); + } + } + + class LockableItemStackFilterInput extends ItemStackFilterInput { + + private boolean locked = false; + + public LockableItemStackFilterInput(SerializableItemHandler itemHandler) { + super(itemHandler); + } + + public void toggleLock() { + setLocked(!locked); + } + + public boolean isLocked() { + return locked; + } + + public void setLocked(boolean locked) { + this.locked = locked; + + if (locked) { + for (int i = 0; i < itemHandler.getSlots(); i++) { + ItemStack stack = itemHandler.getStackInSlot(i); + filters.set(i, ItemStackFilter.of(stack)); + } + } + else { + filters.setAllFilters(ItemStackFilter.ALLOW_ANY); + } + } + } } diff --git a/src/main/java/com/github/elenterius/biomancy/inventory/InventoryHandler.java b/src/main/java/com/github/elenterius/biomancy/inventory/InventoryHandler.java index a3e0477f4..c9ab28cae 100644 --- a/src/main/java/com/github/elenterius/biomancy/inventory/InventoryHandler.java +++ b/src/main/java/com/github/elenterius/biomancy/inventory/InventoryHandler.java @@ -7,14 +7,14 @@ import net.minecraftforge.items.wrapper.RecipeWrapper; import org.jetbrains.annotations.NotNull; -public final class InventoryHandler implements SerializableItemHandler { +public final class InventoryHandler implements SerializableItemHandler { - private final SerializableItemHandler itemHandler; + private final T itemHandler; private LazyOptional optionalItemHandler; private final RecipeWrapper recipeWrapper; - public InventoryHandler(SerializableItemHandler itemHandler) { + public InventoryHandler(T itemHandler) { this.itemHandler = itemHandler; recipeWrapper = new RecipeWrapper(itemHandler); optionalItemHandler = LazyOptional.of(() -> itemHandler); @@ -37,7 +37,7 @@ public void deserializeNBT(CompoundTag nbt) { itemHandler.deserializeNBT(nbt); } - public SerializableItemHandler get() { + public T get() { return itemHandler; } diff --git a/src/main/java/com/github/elenterius/biomancy/inventory/InventoryHandlers.java b/src/main/java/com/github/elenterius/biomancy/inventory/InventoryHandlers.java index 22d079eab..c144d4750 100644 --- a/src/main/java/com/github/elenterius/biomancy/inventory/InventoryHandlers.java +++ b/src/main/java/com/github/elenterius/biomancy/inventory/InventoryHandlers.java @@ -51,51 +51,51 @@ private InventoryHandlers() {} /** * default item handler behavior */ - public static InventoryHandler standard(int slotAmount) { - return new InventoryHandler(new FixedSizeItemStackHandler(slotAmount)); + public static InventoryHandler standard(int slotAmount) { + return new InventoryHandler<>(new FixedSizeItemStackHandler(slotAmount)); } - public static InventoryHandler standard(int slotAmount, Notify onInventoryChanged) { + public static InventoryHandler standard(int slotAmount, Notify onInventoryChanged) { FixedSizeItemStackHandler handler = new FixedSizeItemStackHandler(slotAmount) { @Override protected void onContentsChanged(int slot) { onInventoryChanged.invoke(); } }; - return new InventoryHandler(handler); + return new InventoryHandler<>(handler); } /** * prevents item insertion, only item extraction is possible (e.g. output inventories) */ - public static InventoryHandler denyInput(T itemStackHandler) { - return new InventoryHandler(new BehavioralItemHandler.DenyInput(itemStackHandler)); + public static InventoryHandler denyInput(T itemStackHandler) { + return new InventoryHandler<>(new BehavioralItemHandler.DenyInput(itemStackHandler)); } /** * prevents item insertion, only item extraction is possible (e.g. output inventories) */ - public static InventoryHandler denyInput(int slotAmount, Notify onInventoryChanged) { + public static InventoryHandler denyInput(int slotAmount, Notify onInventoryChanged) { FixedSizeItemStackHandler handler = new FixedSizeItemStackHandler(slotAmount) { @Override protected void onContentsChanged(int slot) { onInventoryChanged.invoke(); } }; - return new InventoryHandler(new BehavioralItemHandler.DenyInput(handler)); + return new InventoryHandler<>(new BehavioralItemHandler.DenyInput(handler)); } /** * only allows item insertion of valid items */ - public static > InventoryHandler filterInput(int slotAmount, List slotFilters) { + public static InventoryHandler filterInput(int slotAmount, List> slotFilters) { return filterInput(new FixedSizeItemStackHandler(slotAmount), slotFilters); } /** * only allows item insertion of valid items */ - public static > InventoryHandler filterInput(int slotAmount, List slotFilters, Notify onInventoryChanged) { + public static InventoryHandler filterInput(int slotAmount, List> slotFilters, Notify onInventoryChanged) { FixedSizeItemStackHandler handler = new FixedSizeItemStackHandler(slotAmount) { @Override protected void onContentsChanged(int slot) { @@ -108,23 +108,64 @@ protected void onContentsChanged(int slot) { /** * only allows item insertion of valid items */ - public static > InventoryHandler filterInput(T itemStackHandler, List slotFilters) { - return new InventoryHandler(new BehavioralItemHandler.FilterInput<>(itemStackHandler, slotFilters)); + public static InventoryHandler filterInput(T itemStackHandler, List> slotFilters) { + return new InventoryHandler<>(new BehavioralItemHandler.PredicateFilterInput(itemStackHandler, slotFilters)); + } + + /** + * only allows item insertion of valid items + */ + public static InventoryHandler filterInput(int slotAmount) { + return filterInput(new FixedSizeItemStackHandler(slotAmount)); + } + + /** + * only allows item insertion of valid items + */ + public static InventoryHandler filterInput(int slotAmount, Notify onInventoryChanged) { + FixedSizeItemStackHandler handler = new FixedSizeItemStackHandler(slotAmount) { + @Override + protected void onContentsChanged(int slot) { + onInventoryChanged.invoke(); + } + }; + return filterInput(handler); + } + + /** + * only allows item insertion of valid items + */ + public static InventoryHandler filterInput(T itemStackHandler) { + return new InventoryHandler<>(new BehavioralItemHandler.ItemStackFilterInput(itemStackHandler)); + } + + public static InventoryHandler lockableFilterInput(int slotAmount, Notify onInventoryChanged) { + FixedSizeItemStackHandler handler = new FixedSizeItemStackHandler(slotAmount) { + @Override + protected void onContentsChanged(int slot) { + onInventoryChanged.invoke(); + } + }; + return lockableFilterInput(handler); + } + + public static InventoryHandler lockableFilterInput(T itemStackHandler) { + return new InventoryHandler<>(new BehavioralItemHandler.LockableItemStackFilterInput(itemStackHandler)); } /** * prevents nesting of items with inventories,
* i.e. insertion of filled shulker boxes and items with filled inventories (item handler capability) */ - public static InventoryHandler denyItemWithFilledInventory(T itemHandler) { - return new InventoryHandler(new BehavioralItemHandler.FilterInput<>(itemHandler, IntStream.range(0, itemHandler.getSlots()).mapToObj(x -> EMPTY_ITEM_INVENTORY_PREDICATE).toList())); + public static InventoryHandler denyItemWithFilledInventory(T itemHandler) { + return new InventoryHandler<>(new BehavioralItemHandler.PredicateFilterInput(itemHandler, IntStream.range(0, itemHandler.getSlots()).mapToObj(x -> EMPTY_ITEM_INVENTORY_PREDICATE).toList())); } /** * prevents nesting of items with inventories,
* i.e. insertion of filled shulker boxes and items with filled inventories (item handler capability) */ - public static InventoryHandler denyItemWithFilledInventory(int slotAmount, Notify onInventoryChanged) { + public static InventoryHandler denyItemWithFilledInventory(int slotAmount, Notify onInventoryChanged) { FixedSizeItemStackHandler handler = new FixedSizeItemStackHandler(slotAmount) { @Override protected void onContentsChanged(int slot) { @@ -137,14 +178,14 @@ protected void onContentsChanged(int slot) { /** * only allows the insertion of items that are biofuel (solid & fluid container) */ - public static InventoryHandler filterFuel(int slotAmount) { + public static InventoryHandler filterFuel(int slotAmount) { return filterFuel(new FixedSizeItemStackHandler(slotAmount)); } /** * only allows the insertion of items that are biofuel (solid & fluid container) */ - public static InventoryHandler filterFuel(int slotAmount, Notify onInventoryChanged) { + public static InventoryHandler filterFuel(int slotAmount, Notify onInventoryChanged) { FixedSizeItemStackHandler handler = new FixedSizeItemStackHandler(slotAmount) { @Override protected void onContentsChanged(int slot) { @@ -157,7 +198,7 @@ protected void onContentsChanged(int slot) { /** * only allows the insertion of items that are biofuel (solid & fluid container) */ - public static InventoryHandler filterFuel(T itemHandler) { + public static InventoryHandler filterFuel(T itemHandler) { return filterInput(itemHandler, IntStream.range(0, itemHandler.getSlots()).mapToObj(x -> Nutrients.FUEL_PREDICATE).toList()); } diff --git a/src/main/java/com/github/elenterius/biomancy/menu/BioForgeMenu.java b/src/main/java/com/github/elenterius/biomancy/menu/BioForgeMenu.java index d56d9fa15..5d7f2d2a8 100644 --- a/src/main/java/com/github/elenterius/biomancy/menu/BioForgeMenu.java +++ b/src/main/java/com/github/elenterius/biomancy/menu/BioForgeMenu.java @@ -280,7 +280,12 @@ public void onTake(Player player, ItemStack stack) { } BioForgeRecipe recipe = getSelectedRecipe(); - if (recipe != null) consumeCraftingIngredients(player, recipe); + if (recipe != null) { + consumeCraftingIngredients(player.getInventory(), recipe); + broadcastChanges(); + } + + setChanged(); checkTakeAchievements(stack); onPlayerMainInventoryChanged(player.getInventory()); //ensures the recipe output slot is filled again if possible, integral for quick crafting to work @@ -291,8 +296,6 @@ public void onTake(Player player, ItemStack stack) { prevSoundTime = time; } } - - setChanged(); } } @@ -301,7 +304,7 @@ private boolean canCraft(@Nullable BioForgeRecipe recipe) { return recipe != null && getFuelAmount() >= recipe.getCraftingCostNutrients() && recipe.isCraftable(itemCounter); } - private void consumeCraftingIngredients(Player player, BioForgeRecipe recipe) { + private void consumeCraftingIngredients(Inventory inventory, BioForgeRecipe recipe) { List ingredients = recipe.getIngredientQuantities(); int[] ingredientCost = new int[ingredients.size()]; @@ -309,8 +312,6 @@ private void consumeCraftingIngredients(Player player, BioForgeRecipe recipe) { ingredientCost[i] = ingredients.get(i).count(); } - Inventory inventory = player.getInventory(); - //consume ingredients for (int idx = 0; idx < inventory.items.size(); idx++) { ItemStack foundStack = inventory.items.get(idx); diff --git a/src/main/java/com/github/elenterius/biomancy/menu/BioLabMenu.java b/src/main/java/com/github/elenterius/biomancy/menu/BioLabMenu.java index 0543d3e82..1329c9d1b 100644 --- a/src/main/java/com/github/elenterius/biomancy/menu/BioLabMenu.java +++ b/src/main/java/com/github/elenterius/biomancy/menu/BioLabMenu.java @@ -57,6 +57,17 @@ public static BioLabMenu createClientMenu(int containerId, Inventory playerInven return new BioLabMenu(containerId, playerInventory, blockEntity instanceof BioLabBlockEntity bioLab ? bioLab : null); } + @Override + public boolean clickMenuButton(Player player, int id) { + + if (id == 0 && !level.isClientSide) { + bioLab.getInputInventory().get().toggleLock(); + return true; + } + + return false; + } + @Override public boolean stillValid(Player player) { return bioLab != null && bioLab.canPlayerInteract(player); @@ -83,6 +94,10 @@ public int getMaxFuelAmount() { return bioLab.getStateData().fuelHandler.getMaxFuelAmount(); } + public boolean isFilterLocked() { + return bioLab.getStateData().isFilterLocked(); + } + @Override public ItemStack quickMoveStack(Player player, int index) { Slot slot = slots.get(index); diff --git a/src/main/java/com/github/elenterius/biomancy/util/ItemStackFilter.java b/src/main/java/com/github/elenterius/biomancy/util/ItemStackFilter.java index 051f9c1d1..35e2f6462 100644 --- a/src/main/java/com/github/elenterius/biomancy/util/ItemStackFilter.java +++ b/src/main/java/com/github/elenterius/biomancy/util/ItemStackFilter.java @@ -5,20 +5,23 @@ import net.minecraft.world.item.ItemStack; import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.items.ItemHandlerHelper; +import org.jetbrains.annotations.Nullable; import java.util.function.Predicate; public class ItemStackFilter implements Predicate, INBTSerializable { - public static final String FILTER_KEY = "filter"; - public static final String STRICT_KEY = "strict"; + public static final String FILTER_KEY = "Filter"; + public static final String STRICT_KEY = "Strict"; - public static final ItemStackFilter ALLOW_ALL = of(ItemStack.EMPTY, false); + public static final ItemStackFilter ALLOW_ANY = new ItemStackFilter(null, false); + public static final ItemStackFilter ALLOW_NONE = new ItemStackFilter(ItemStack.EMPTY, false); + @Nullable private ItemStack filter; private boolean isStrict; - protected ItemStackFilter(ItemStack filter, boolean isStrict) { + protected ItemStackFilter(@Nullable ItemStack filter, boolean isStrict) { this.filter = filter; this.isStrict = isStrict; } @@ -40,6 +43,10 @@ public static ItemStackFilter of(ItemStack filter) { } public static ItemStackFilter of(ItemStack filter, boolean isStrict) { + if (filter.isEmpty()) return ALLOW_NONE; + + filter = filter.copyWithCount(1); + if (filter.hasTag()) { CompoundTag stackTag = filter.getTag(); assert stackTag != null; @@ -52,7 +59,9 @@ public static ItemStackFilter of(ItemStack filter, boolean isStrict) { @Override public boolean test(ItemStack stack) { if (stack.isEmpty()) return false; - if (filter.isEmpty()) return true; + + if (filter == null) return true; + if (filter.isEmpty()) return false; if (isStrict) { return ItemHandlerHelper.canItemStacksStack(filter, stack); @@ -65,15 +74,21 @@ public boolean test(ItemStack stack) { @Override public CompoundTag serializeNBT() { CompoundTag tag = new CompoundTag(); - tag.put(FILTER_KEY, filter.serializeNBT()); - tag.putBoolean(STRICT_KEY, isStrict); + if (filter != null) { + tag.put(FILTER_KEY, filter.serializeNBT()); + tag.putBoolean(STRICT_KEY, isStrict); + } return tag; } @Override public void deserializeNBT(CompoundTag tag) { - filter = ItemStack.of(tag.getCompound(FILTER_KEY)); + filter = tag.contains(FILTER_KEY) ? ItemStack.of(tag.getCompound(FILTER_KEY)) : null; isStrict = tag.getBoolean(STRICT_KEY); } + public boolean allowsAny() { + return filter == null; + } + } diff --git a/src/main/java/com/github/elenterius/biomancy/util/ItemStackFilterList.java b/src/main/java/com/github/elenterius/biomancy/util/ItemStackFilterList.java index b38b19767..9442c318a 100644 --- a/src/main/java/com/github/elenterius/biomancy/util/ItemStackFilterList.java +++ b/src/main/java/com/github/elenterius/biomancy/util/ItemStackFilterList.java @@ -1,8 +1,6 @@ package com.github.elenterius.biomancy.util; -import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; -import net.minecraft.nbt.Tag; import net.minecraft.world.item.ItemStack; import net.minecraftforge.common.util.INBTSerializable; @@ -11,7 +9,7 @@ import java.util.List; import java.util.stream.IntStream; -public class ItemStackFilterList extends AbstractList implements INBTSerializable { +public class ItemStackFilterList extends AbstractList implements INBTSerializable { private List filters; @@ -31,12 +29,8 @@ public static ItemStackFilterList of(List filters) { return new ItemStackFilterList(filters); } - public void setAllFilters(List filters) { - this.filters = filters; - } - public void setAllFilters(ItemStackFilter filter) { - filters = IntStream.range(0, filters.size()).mapToObj(x -> filter).toList(); + filters.replaceAll(ignored -> filter); } public void setFilter(int index, ItemStackFilter filter) { @@ -64,9 +58,7 @@ public boolean test(int index, ItemStack stack) { } @Override - public void deserializeNBT(CompoundTag tag) { - ListTag listTag = tag.getList("filters", Tag.TAG_COMPOUND); - + public void deserializeNBT(ListTag listTag) { List newFilters = new ArrayList<>(); for (int i = 0; i < listTag.size(); i++) { newFilters.add(ItemStackFilter.of(listTag.getCompound(i))); @@ -76,17 +68,13 @@ public void deserializeNBT(CompoundTag tag) { } @Override - public CompoundTag serializeNBT() { - + public ListTag serializeNBT() { ListTag listTag = new ListTag(); for (ItemStackFilter filter : filters) { listTag.add(filter.serializeNBT()); } - CompoundTag tag = new CompoundTag(); - tag.put("filters", listTag); - - return tag; + return listTag; } } diff --git a/src/main/resources/assets/biomancy/textures/gui/menu_bio_lab.png b/src/main/resources/assets/biomancy/textures/gui/menu_bio_lab.png index 2f8f297c0..c964a54f8 100644 Binary files a/src/main/resources/assets/biomancy/textures/gui/menu_bio_lab.png and b/src/main/resources/assets/biomancy/textures/gui/menu_bio_lab.png differ