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 3170f104e..8b2bd991c 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 @@ -11,6 +11,7 @@ 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 com.simibubi.create.content.logistics.item.filter.attribute.ItemNameAttribute; import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import org.apache.commons.lang3.StringUtils; @@ -48,6 +49,7 @@ public interface ItemAttribute { static ItemAttribute inItemGroup = register(new InItemGroup(ItemGroup.MISC)); static ItemAttribute hasEnchant = register(new EnchantAttribute("dummy")); static ItemAttribute hasFluid = register(new FluidContentsAttribute("dummy")); + static ItemAttribute hasName = register(new ItemNameAttribute("dummy")); static ItemAttribute addedBy = register(new AddedBy("dummy")); static ItemAttribute register(ItemAttribute attributeType) { @@ -113,6 +115,7 @@ public interface ItemAttribute { CONSUMABLE(ItemStack::isFood), FLUID_CONTAINER(s -> s.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY).isPresent()), ENCHANTED(ItemStack::isEnchanted), + RENAMED(ItemStack::hasDisplayName), DAMAGED(ItemStack::isDamaged), BADLY_DAMAGED(s -> s.isDamaged() && s.getDamage() / s.getMaxDamage() > 3 / 4f), NOT_STACKABLE(Predicates.not(ItemStack::isStackable)), diff --git a/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/ItemNameAttribute.java b/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/ItemNameAttribute.java new file mode 100644 index 000000000..2f83e648a --- /dev/null +++ b/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/ItemNameAttribute.java @@ -0,0 +1,71 @@ +package com.simibubi.create.content.logistics.item.filter.attribute; + +import com.google.gson.JsonParseException; +import com.simibubi.create.content.logistics.item.filter.ItemAttribute; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.StringTextComponent; +import net.minecraft.util.text.TranslationTextComponent; + +import java.util.ArrayList; +import java.util.List; + +public class ItemNameAttribute implements ItemAttribute { + String itemName; + + public ItemNameAttribute(String itemName) { + this.itemName = itemName; + } + + @Override + public boolean appliesTo(ItemStack itemStack) { + return extractCustomName(itemStack).equals(itemName); + } + + @Override + public List listAttributesOf(ItemStack itemStack) { + String name = extractCustomName(itemStack); + + List atts = new ArrayList<>(); + if(name.length() > 0) { + atts.add(new ItemNameAttribute(name)); + } + return atts; + } + + @Override + public String getTranslationKey() { + return "has_name"; + } + + @Override + public Object[] getTranslationParameters() { + return new Object[] { itemName }; + } + + @Override + public void writeNBT(CompoundNBT nbt) { + nbt.putString("name", this.itemName); + } + + @Override + public ItemAttribute readNBT(CompoundNBT nbt) { + return new ItemNameAttribute(nbt.getString("name")); + } + + private String extractCustomName(ItemStack stack) { + CompoundNBT compoundnbt = stack.getChildTag("display"); + if (compoundnbt != null && compoundnbt.contains("Name", 8)) { + try { + ITextComponent itextcomponent = ITextComponent.Serializer.fromJson(compoundnbt.getString("Name")); + if (itextcomponent != null) { + return itextcomponent.getString(); + } + } catch (JsonParseException ignored) { + } + } + return ""; + } +}