From b3e4108019abc71a40e46fd23e9dbf624d877cb3 Mon Sep 17 00:00:00 2001 From: Reece Mackie <20544390+Rover656@users.noreply.github.com> Date: Mon, 30 Sep 2024 16:07:49 +0100 Subject: [PATCH] chore: Make filter slot handling more generic. --- .../integrations/jei/BaseGhostSlotHandler.java | 16 ++++------------ .../base/common/menu/EntityFilterSlot.java | 5 +---- .../com/enderio/base/common/menu/FilterSlot.java | 2 +- .../base/common/menu/FluidFilterSlot.java | 7 +++++-- .../enderio/base/common/menu/ItemFilterSlot.java | 2 +- .../mods/mekanism/ChemicalFilterSlot.java | 9 ++++----- 6 files changed, 16 insertions(+), 25 deletions(-) diff --git a/enderio-base/src/main/java/com/enderio/base/common/integrations/jei/BaseGhostSlotHandler.java b/enderio-base/src/main/java/com/enderio/base/common/integrations/jei/BaseGhostSlotHandler.java index eb66dad3b..c89d5452b 100644 --- a/enderio-base/src/main/java/com/enderio/base/common/integrations/jei/BaseGhostSlotHandler.java +++ b/enderio-base/src/main/java/com/enderio/base/common/integrations/jei/BaseGhostSlotHandler.java @@ -1,10 +1,8 @@ package com.enderio.base.common.integrations.jei; -import com.enderio.base.common.menu.EntityFilterSlot; import com.enderio.base.common.menu.FilterSlot; import com.enderio.base.common.menu.FluidFilterSlot; import com.enderio.base.common.menu.ItemFilterSlot; -import com.enderio.base.common.tag.EIOTags; import com.enderio.core.client.gui.screen.EIOScreen; import mezz.jei.api.constants.VanillaTypes; import mezz.jei.api.gui.handlers.IGhostIngredientHandler; @@ -12,8 +10,6 @@ import mezz.jei.api.neoforge.NeoForgeTypes; import net.minecraft.client.renderer.Rect2i; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.SpawnEggItem; -import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import java.util.ArrayList; @@ -34,16 +30,12 @@ public List> getTargetsTyped(EIOScreen gui, ITypedIngredient in if (ingredient.getType() == VanillaTypes.ITEM_STACK) { ItemStack currentIngredient = (ItemStack)ingredient.getIngredient(); - // Any filter slot will accept items too, in case they are supported (like entity for example) if (slot instanceof ItemFilterSlot itemFilterSlot) { targets.add(new ItemStackTarget<>(bounds, itemFilterSlot)); - } else if (slot instanceof FluidFilterSlot fluidFilterSlot) { - if (currentIngredient.getCapability(Capabilities.FluidHandler.ITEM) != null) { - targets.add(new IndirectItemStackTarget<>(bounds, fluidFilterSlot)); - } - } else if (slot instanceof EntityFilterSlot entityFilterSlot) { - if (currentIngredient.is(EIOTags.Items.ENTITY_STORAGE) || currentIngredient.getItem() instanceof SpawnEggItem) { - targets.add(new IndirectItemStackTarget<>(bounds, entityFilterSlot)); + } else if (slot instanceof FilterSlot otherFilterSlot) { + // If the item can be converted to the resource, allow it to be dragged too. + if (otherFilterSlot.getResourceFrom(currentIngredient).isPresent()) { + targets.add(new IndirectItemStackTarget<>(bounds, otherFilterSlot)); } } } else if (ingredient.getType() == NeoForgeTypes.FLUID_STACK) { diff --git a/enderio-base/src/main/java/com/enderio/base/common/menu/EntityFilterSlot.java b/enderio-base/src/main/java/com/enderio/base/common/menu/EntityFilterSlot.java index 41a9fef52..65eb6dabd 100644 --- a/enderio-base/src/main/java/com/enderio/base/common/menu/EntityFilterSlot.java +++ b/enderio-base/src/main/java/com/enderio/base/common/menu/EntityFilterSlot.java @@ -4,11 +4,8 @@ import com.enderio.base.common.init.EIODataComponents; import com.enderio.base.common.tag.EIOTags; import net.minecraft.client.Minecraft; -import net.minecraft.world.Container; -import net.minecraft.world.SimpleContainer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.SpawnEggItem; @@ -22,7 +19,7 @@ public EntityFilterSlot(Consumer consumer, int pSlot, int pX, } @Override - protected Optional getResourceFrom(ItemStack itemStack) { + public Optional getResourceFrom(ItemStack itemStack) { if (itemStack.is(EIOTags.Items.ENTITY_STORAGE)) { StoredEntityData ghost = itemStack.get(EIODataComponents.STORED_ENTITY); return Optional.of(ghost); diff --git a/enderio-base/src/main/java/com/enderio/base/common/menu/FilterSlot.java b/enderio-base/src/main/java/com/enderio/base/common/menu/FilterSlot.java index 3f2fa20c8..8c7dbd334 100644 --- a/enderio-base/src/main/java/com/enderio/base/common/menu/FilterSlot.java +++ b/enderio-base/src/main/java/com/enderio/base/common/menu/FilterSlot.java @@ -46,7 +46,7 @@ public final void setResource(T resource) { consumer.accept(processResource(resource)); } - protected abstract Optional getResourceFrom(ItemStack itemStack); + public abstract Optional getResourceFrom(ItemStack itemStack); public T processResource(T resource) { return resource; diff --git a/enderio-base/src/main/java/com/enderio/base/common/menu/FluidFilterSlot.java b/enderio-base/src/main/java/com/enderio/base/common/menu/FluidFilterSlot.java index a0a39f064..192de920d 100644 --- a/enderio-base/src/main/java/com/enderio/base/common/menu/FluidFilterSlot.java +++ b/enderio-base/src/main/java/com/enderio/base/common/menu/FluidFilterSlot.java @@ -15,10 +15,13 @@ public FluidFilterSlot(Consumer consumer, int pSlot, int pX, int pY) } @Override - protected Optional getResourceFrom(ItemStack itemStack) { + public Optional getResourceFrom(ItemStack itemStack) { IFluidHandlerItem capability = itemStack.getCapability(Capabilities.FluidHandler.ITEM); if (capability != null) { - return Optional.of(capability.getFluidInTank(0).copy()); + var fluid = capability.getFluidInTank(0).copy(); + if (!fluid.isEmpty()) { + return Optional.of(fluid); + } } return Optional.empty(); diff --git a/enderio-base/src/main/java/com/enderio/base/common/menu/ItemFilterSlot.java b/enderio-base/src/main/java/com/enderio/base/common/menu/ItemFilterSlot.java index 68e190bf7..211d2dd51 100644 --- a/enderio-base/src/main/java/com/enderio/base/common/menu/ItemFilterSlot.java +++ b/enderio-base/src/main/java/com/enderio/base/common/menu/ItemFilterSlot.java @@ -26,7 +26,7 @@ public ItemStack processResource(ItemStack resource) { } @Override - protected Optional getResourceFrom(ItemStack itemStack) { + public Optional getResourceFrom(ItemStack itemStack) { return Optional.of(itemStack); } } diff --git a/enderio-conduits-modded/src/main/java/com/enderio/modconduits/mods/mekanism/ChemicalFilterSlot.java b/enderio-conduits-modded/src/main/java/com/enderio/modconduits/mods/mekanism/ChemicalFilterSlot.java index 4c8010344..4f50c57e5 100644 --- a/enderio-conduits-modded/src/main/java/com/enderio/modconduits/mods/mekanism/ChemicalFilterSlot.java +++ b/enderio-conduits-modded/src/main/java/com/enderio/modconduits/mods/mekanism/ChemicalFilterSlot.java @@ -3,9 +3,6 @@ import com.enderio.base.common.menu.FilterSlot; import mekanism.api.chemical.ChemicalStack; import mekanism.api.chemical.IChemicalHandler; -import net.minecraft.world.Container; -import net.minecraft.world.SimpleContainer; -import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.ItemStack; import java.util.Optional; @@ -18,11 +15,13 @@ public ChemicalFilterSlot(Consumer consumer, int pSlot, int pX, i } @Override - protected Optional getResourceFrom(ItemStack itemStack) { + public Optional getResourceFrom(ItemStack itemStack) { IChemicalHandler capability = itemStack.getCapability(MekanismModule.Capabilities.Item.CHEMICAL); if (capability != null) { var ghost = capability.getChemicalInTank(0).copy(); - return Optional.of(ghost); + if (!ghost.isEmpty()) { + return Optional.of(ghost); + } } return Optional.empty();