Refactor FluidContentsAttribute

This commit is contained in:
Colman Davenport 2020-10-17 11:24:15 -04:00
parent 42ef3796a2
commit 083a9c1140
2 changed files with 23 additions and 26 deletions

View file

@ -50,7 +50,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(EnchantAttribute.EMPTY); 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 hasName = register(new ItemNameAttribute("dummy"));
static ItemAttribute astralAmulet = register(new AstralSorceryAmuletAttribute("dummy", -1)); static ItemAttribute astralAmulet = register(new AstralSorceryAmuletAttribute("dummy", -1));
static ItemAttribute astralAttunement = register(new AstralSorceryAttunementAttribute("dummy")); static ItemAttribute astralAttunement = register(new AstralSorceryAttunementAttribute("dummy"));

View file

@ -2,12 +2,8 @@ package com.simibubi.create.content.logistics.item.filter.attribute;
import com.simibubi.create.content.logistics.item.filter.ItemAttribute; import com.simibubi.create.content.logistics.item.filter.ItemAttribute;
import net.minecraft.fluid.Fluid; 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.item.ItemStack;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.util.LazyOptional; 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.fluids.capability.IFluidHandlerItem;
import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.ForgeRegistries;
import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class FluidContentsAttribute implements ItemAttribute { public class FluidContentsAttribute implements ItemAttribute {
String fluidName; public static final FluidContentsAttribute EMPTY = new FluidContentsAttribute(null);
public FluidContentsAttribute(String fluidName) { private final Fluid fluid;
this.fluidName = fluidName;
public FluidContentsAttribute(@Nullable Fluid fluid) {
this.fluid = fluid;
} }
@Override @Override
public boolean appliesTo(ItemStack itemStack) { public boolean appliesTo(ItemStack itemStack) {
return extractFluidNames(itemStack).contains(fluidName); return extractFluids(itemStack).contains(fluid);
} }
@Override @Override
public List<ItemAttribute> listAttributesOf(ItemStack itemStack) { public List<ItemAttribute> listAttributesOf(ItemStack itemStack) {
List<String> names = extractFluidNames(itemStack); return extractFluids(itemStack).stream().map(FluidContentsAttribute::new).collect(Collectors.toList());
List<ItemAttribute> atts = new ArrayList<>();
for(String name : names) {
atts.add(new FluidContentsAttribute(name));
}
return atts;
} }
@Override @Override
@ -48,33 +42,36 @@ public class FluidContentsAttribute implements ItemAttribute {
@Override @Override
public Object[] getTranslationParameters() { public Object[] getTranslationParameters() {
ResourceLocation fluidResource = new ResourceLocation(fluidName); String parameter = "";
Fluid fluid = ForgeRegistries.FLUIDS.getValue(fluidResource);
String trans = "";
if(fluid != null) if(fluid != null)
trans = new TranslationTextComponent(fluid.getAttributes().getTranslationKey()).getString(); parameter = new TranslationTextComponent(fluid.getAttributes().getTranslationKey()).getString();
return new Object[] {trans}; return new Object[] { parameter };
} }
@Override @Override
public void writeNBT(CompoundNBT nbt) { 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 @Override
public ItemAttribute readNBT(CompoundNBT nbt) { 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<String> extractFluidNames(ItemStack stack) { private List<Fluid> extractFluids(ItemStack stack) {
List<String> fluids = new ArrayList<>(); List<Fluid> fluids = new ArrayList<>();
LazyOptional<IFluidHandlerItem> capability = LazyOptional<IFluidHandlerItem> capability =
stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY); stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY);
capability.ifPresent((cap) -> { capability.ifPresent((cap) -> {
for(int i = 0; i < cap.getTanks(); i++) { for(int i = 0; i < cap.getTanks(); i++) {
fluids.add(cap.getFluidInTank(i).getFluid().getRegistryName().toString()); fluids.add(cap.getFluidInTank(i).getFluid());
} }
}); });