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 fe8dca539..a3283eb3e 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 @@ -9,6 +9,7 @@ import java.util.function.BiPredicate; import java.util.function.Predicate; import java.util.stream.Collectors; +import com.simibubi.create.content.logistics.item.filter.attribute.EnchantAttribute; import org.apache.commons.lang3.StringUtils; import com.google.common.base.Predicates; @@ -43,6 +44,7 @@ public interface ItemAttribute { static ItemAttribute standard = register(StandardTraits.DUMMY); 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 addedBy = register(new AddedBy("dummy")); static ItemAttribute register(ItemAttribute attributeType) { diff --git a/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/EnchantAttribute.java b/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/EnchantAttribute.java new file mode 100644 index 000000000..a30548862 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/EnchantAttribute.java @@ -0,0 +1,71 @@ +package com.simibubi.create.content.logistics.item.filter.attribute; + +import com.simibubi.create.content.logistics.item.filter.ItemAttribute; +import net.minecraft.enchantment.Enchantment; +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.registries.ForgeRegistries; + +import java.util.ArrayList; +import java.util.List; + +public class EnchantAttribute implements ItemAttribute { + String enchantName; + + public EnchantAttribute(String enchantName) { + this.enchantName = enchantName; + } + + @Override + public boolean appliesTo(ItemStack itemStack) { + ListNBT enchants = extractEnchants(itemStack); + for (int i = 0; i < enchants.size(); i++) { + if(enchants.getCompound(i).getString("id").equals(this.enchantName)) + return true; + } + return false; + } + + @Override + public List listAttributesOf(ItemStack itemStack) { + ListNBT enchants = extractEnchants(itemStack); + List atts = new ArrayList<>(); + for (int i = 0; i < enchants.size(); i++) { + atts.add(new EnchantAttribute(enchants.getCompound(i).getString("id"))); + } + return atts; + } + + @Override + public String getTranslationKey() { + return "has_enchant"; + } + + @Override + public Object[] getTranslationParameters() { + String something = ""; + Enchantment enchant = ForgeRegistries.ENCHANTMENTS.getValue(ResourceLocation.tryCreate(enchantName)); + if(enchant != null) { + something = new TranslationTextComponent(enchant.getName()).getString(); + } + return new Object[] { something }; + } + + @Override + public void writeNBT(CompoundNBT nbt) { + nbt.putString("id", this.enchantName); + } + + @Override + public ItemAttribute readNBT(CompoundNBT nbt) { + return new EnchantAttribute(nbt.getString("id")); + } + + private ListNBT extractEnchants(ItemStack stack) { + CompoundNBT tag = stack.getTag() != null ? stack.getTag() : new CompoundNBT(); + return tag.contains("Enchantments") ? stack.getEnchantmentTagList() : tag.getList("StoredEnchantments", 10); + } +}