Fluid Container Attributes

This commit is contained in:
Colman Davenport 2020-10-17 00:26:16 -04:00
parent a7e2d842af
commit 6ff57ca65c
2 changed files with 87 additions and 0 deletions

View file

@ -10,6 +10,8 @@ import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.simibubi.create.content.logistics.item.filter.attribute.EnchantAttribute; 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 org.apache.commons.lang3.StringUtils;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
@ -45,6 +47,7 @@ public interface ItemAttribute {
static ItemAttribute inTag = register(new InTag(new ResourceLocation("dummy"))); static ItemAttribute inTag = register(new InTag(new ResourceLocation("dummy")));
static ItemAttribute inItemGroup = register(new InItemGroup(ItemGroup.MISC)); static ItemAttribute inItemGroup = register(new InItemGroup(ItemGroup.MISC));
static ItemAttribute hasEnchant = register(new EnchantAttribute("dummy")); static ItemAttribute hasEnchant = register(new EnchantAttribute("dummy"));
static ItemAttribute hasFluid = register(new FluidContentsAttribute("dummy"));
static ItemAttribute addedBy = register(new AddedBy("dummy")); static ItemAttribute addedBy = register(new AddedBy("dummy"));
static ItemAttribute register(ItemAttribute attributeType) { static ItemAttribute register(ItemAttribute attributeType) {
@ -108,6 +111,7 @@ public interface ItemAttribute {
DUMMY(s -> false), DUMMY(s -> false),
PLACEABLE(s -> s.getItem() instanceof BlockItem), PLACEABLE(s -> s.getItem() instanceof BlockItem),
CONSUMABLE(ItemStack::isFood), CONSUMABLE(ItemStack::isFood),
FLUID_CONTAINER(s -> s.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY).isPresent()),
ENCHANTED(ItemStack::isEnchanted), ENCHANTED(ItemStack::isEnchanted),
DAMAGED(ItemStack::isDamaged), DAMAGED(ItemStack::isDamaged),
BADLY_DAMAGED(s -> s.isDamaged() && s.getDamage() / s.getMaxDamage() > 3 / 4f), BADLY_DAMAGED(s -> s.isDamaged() && s.getDamage() / s.getMaxDamage() > 3 / 4f),

View file

@ -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<ItemAttribute> listAttributesOf(ItemStack itemStack) {
List<String> names = extractFluidNames(itemStack);
List<ItemAttribute> 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<String> extractFluidNames(ItemStack stack) {
List<String> fluids = new ArrayList<>();
LazyOptional<IFluidHandlerItem> 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;
}
}