From 083a9c1140da263c2c7496a9c5db3b2b77b1b97c Mon Sep 17 00:00:00 2001 From: Colman Davenport Date: Sat, 17 Oct 2020 11:24:15 -0400 Subject: [PATCH] Refactor FluidContentsAttribute --- .../logistics/item/filter/ItemAttribute.java | 2 +- .../attribute/FluidContentsAttribute.java | 47 +++++++++---------- 2 files changed, 23 insertions(+), 26 deletions(-) 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 de66e9d59..a07a82ca2 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 @@ -50,7 +50,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(EnchantAttribute.EMPTY); - static ItemAttribute hasFluid = register(new FluidContentsAttribute("dummy")); + static ItemAttribute hasFluid = register(FluidContentsAttribute.EMPTY); static ItemAttribute hasName = register(new ItemNameAttribute("dummy")); static ItemAttribute astralAmulet = register(new AstralSorceryAmuletAttribute("dummy", -1)); static ItemAttribute astralAttunement = register(new AstralSorceryAttunementAttribute("dummy")); 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 index 421282b9b..94aa9ed1f 100644 --- 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 @@ -2,12 +2,8 @@ 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; @@ -15,30 +11,28 @@ import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandlerItem; import net.minecraftforge.registries.ForgeRegistries; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class FluidContentsAttribute implements ItemAttribute { - String fluidName; + public static final FluidContentsAttribute EMPTY = new FluidContentsAttribute(null); - public FluidContentsAttribute(String fluidName) { - this.fluidName = fluidName; + private final Fluid fluid; + + public FluidContentsAttribute(@Nullable Fluid fluid) { + this.fluid = fluid; } @Override public boolean appliesTo(ItemStack itemStack) { - return extractFluidNames(itemStack).contains(fluidName); + return extractFluids(itemStack).contains(fluid); } @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; + return extractFluids(itemStack).stream().map(FluidContentsAttribute::new).collect(Collectors.toList()); } @Override @@ -48,33 +42,36 @@ public class FluidContentsAttribute implements ItemAttribute { @Override public Object[] getTranslationParameters() { - ResourceLocation fluidResource = new ResourceLocation(fluidName); - Fluid fluid = ForgeRegistries.FLUIDS.getValue(fluidResource); - String trans = ""; + String parameter = ""; if(fluid != null) - trans = new TranslationTextComponent(fluid.getAttributes().getTranslationKey()).getString(); - return new Object[] {trans}; + parameter = new TranslationTextComponent(fluid.getAttributes().getTranslationKey()).getString(); + return new Object[] { parameter }; } @Override public void writeNBT(CompoundNBT nbt) { - nbt.putString("fluidName", this.fluidName); + if (fluid == null) + return; + ResourceLocation id = ForgeRegistries.FLUIDS.getKey(fluid); + if (id == null) + return; + nbt.putString("id", id.toString()); } @Override public ItemAttribute readNBT(CompoundNBT nbt) { - return new FluidContentsAttribute(nbt.getString("fluidName")); + return nbt.contains("id") ? new FluidContentsAttribute(ForgeRegistries.FLUIDS.getValue(ResourceLocation.tryCreate(nbt.getString("id")))) : EMPTY; } - private List extractFluidNames(ItemStack stack) { - List fluids = new ArrayList<>(); + private List extractFluids(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()); + fluids.add(cap.getFluidInTank(i).getFluid()); } });