diff --git a/src/main/java/com/simibubi/create/content/logistics/item/filter/ItemAttribute.java b/src/main/java/com/simibubi/create/content/logistics/item/filter/ItemAttribute.java index a3283eb3e..3170f104e 100644 --- a/src/main/java/com/simibubi/create/content/logistics/item/filter/ItemAttribute.java +++ b/src/main/java/com/simibubi/create/content/logistics/item/filter/ItemAttribute.java @@ -10,6 +10,8 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import com.simibubi.create.content.logistics.item.filter.attribute.EnchantAttribute; +import com.simibubi.create.content.logistics.item.filter.attribute.FluidContentsAttribute; +import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import org.apache.commons.lang3.StringUtils; import com.google.common.base.Predicates; @@ -45,6 +47,7 @@ public interface ItemAttribute { static ItemAttribute inTag = register(new InTag(new ResourceLocation("dummy"))); static ItemAttribute inItemGroup = register(new InItemGroup(ItemGroup.MISC)); static ItemAttribute hasEnchant = register(new EnchantAttribute("dummy")); + static ItemAttribute hasFluid = register(new FluidContentsAttribute("dummy")); static ItemAttribute addedBy = register(new AddedBy("dummy")); static ItemAttribute register(ItemAttribute attributeType) { @@ -108,6 +111,7 @@ public interface ItemAttribute { DUMMY(s -> false), PLACEABLE(s -> s.getItem() instanceof BlockItem), CONSUMABLE(ItemStack::isFood), + FLUID_CONTAINER(s -> s.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY).isPresent()), ENCHANTED(ItemStack::isEnchanted), DAMAGED(ItemStack::isDamaged), BADLY_DAMAGED(s -> s.isDamaged() && s.getDamage() / s.getMaxDamage() > 3 / 4f), diff --git a/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/FluidContentsAttribute.java b/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/FluidContentsAttribute.java new file mode 100644 index 000000000..421282b9b --- /dev/null +++ b/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/FluidContentsAttribute.java @@ -0,0 +1,83 @@ +package com.simibubi.create.content.logistics.item.filter.attribute; + +import com.simibubi.create.content.logistics.item.filter.ItemAttribute; +import net.minecraft.fluid.Fluid; +import net.minecraft.item.BucketItem; +import net.minecraft.item.GlassBottleItem; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.nbt.ListNBT; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.text.TranslationTextComponent; +import net.minecraftforge.common.util.LazyOptional; +import net.minecraftforge.fluids.capability.CapabilityFluidHandler; +import net.minecraftforge.fluids.capability.IFluidHandlerItem; +import net.minecraftforge.registries.ForgeRegistries; + +import java.util.ArrayList; +import java.util.List; + +public class FluidContentsAttribute implements ItemAttribute { + String fluidName; + + public FluidContentsAttribute(String fluidName) { + this.fluidName = fluidName; + } + + @Override + public boolean appliesTo(ItemStack itemStack) { + return extractFluidNames(itemStack).contains(fluidName); + } + + @Override + public List listAttributesOf(ItemStack itemStack) { + List names = extractFluidNames(itemStack); + + List atts = new ArrayList<>(); + for(String name : names) { + atts.add(new FluidContentsAttribute(name)); + } + return atts; + } + + @Override + public String getTranslationKey() { + return "has_fluid"; + } + + @Override + public Object[] getTranslationParameters() { + ResourceLocation fluidResource = new ResourceLocation(fluidName); + Fluid fluid = ForgeRegistries.FLUIDS.getValue(fluidResource); + String trans = ""; + if(fluid != null) + trans = new TranslationTextComponent(fluid.getAttributes().getTranslationKey()).getString(); + return new Object[] {trans}; + } + + @Override + public void writeNBT(CompoundNBT nbt) { + nbt.putString("fluidName", this.fluidName); + } + + @Override + public ItemAttribute readNBT(CompoundNBT nbt) { + return new FluidContentsAttribute(nbt.getString("fluidName")); + } + + private List extractFluidNames(ItemStack stack) { + List fluids = new ArrayList<>(); + + LazyOptional capability = + stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY); + + capability.ifPresent((cap) -> { + for(int i = 0; i < cap.getTanks(); i++) { + fluids.add(cap.getFluidInTank(i).getFluid().getRegistryName().toString()); + } + }); + + return fluids; + } +}