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 8b2bd991c..aee1da7d9 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,9 +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 com.simibubi.create.content.logistics.item.filter.attribute.FluidContentsAttribute; -import com.simibubi.create.content.logistics.item.filter.attribute.ItemNameAttribute; +import com.simibubi.create.content.logistics.item.filter.attribute.*; import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import org.apache.commons.lang3.StringUtils; @@ -50,6 +48,8 @@ public interface ItemAttribute { static ItemAttribute hasEnchant = register(new EnchantAttribute("dummy")); static ItemAttribute hasFluid = register(new FluidContentsAttribute("dummy")); static ItemAttribute hasName = register(new ItemNameAttribute("dummy")); + static ItemAttribute bookAuthor = register(new BookAuthorAttribute("dummy")); + static ItemAttribute bookCopy = register(new BookCopyAttribute(-1)); 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/BookAuthorAttribute.java b/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/BookAuthorAttribute.java new file mode 100644 index 000000000..44c97ed28 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/BookAuthorAttribute.java @@ -0,0 +1,63 @@ +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.item.WrittenBookItem; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.util.text.ITextComponent; + +import java.util.ArrayList; +import java.util.List; + +public class BookAuthorAttribute implements ItemAttribute { + String author; + + public BookAuthorAttribute(String author) { + this.author = author; + } + + @Override + public boolean appliesTo(ItemStack itemStack) { + return extractAuthor(itemStack).equals(author); + } + + @Override + public List listAttributesOf(ItemStack itemStack) { + String name = extractAuthor(itemStack); + + List atts = new ArrayList<>(); + if(name.length() > 0) { + atts.add(new BookAuthorAttribute(name)); + } + return atts; + } + + @Override + public String getTranslationKey() { + return "book_author"; + } + + @Override + public Object[] getTranslationParameters() { + return new Object[] {author}; + } + + @Override + public void writeNBT(CompoundNBT nbt) { + nbt.putString("author", this.author); + } + + @Override + public ItemAttribute readNBT(CompoundNBT nbt) { + return new BookAuthorAttribute(nbt.getString("author")); + } + + private String extractAuthor(ItemStack stack) { + CompoundNBT nbt = stack.getTag(); + if (nbt != null && nbt.contains("author")) { + return nbt.getString("author"); + } + return ""; + } +} diff --git a/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/BookCopyAttribute.java b/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/BookCopyAttribute.java new file mode 100644 index 000000000..e379a3bdc --- /dev/null +++ b/src/main/java/com/simibubi/create/content/logistics/item/filter/attribute/BookCopyAttribute.java @@ -0,0 +1,66 @@ +package com.simibubi.create.content.logistics.item.filter.attribute; + +import com.simibubi.create.content.logistics.item.filter.ItemAttribute; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.item.WrittenBookItem; +import net.minecraft.nbt.CompoundNBT; + +import java.util.ArrayList; +import java.util.List; + +public class BookCopyAttribute implements ItemAttribute { + int generation; + + public BookCopyAttribute(int generation) { + this.generation = generation; + } + + @Override + public boolean appliesTo(ItemStack itemStack) { + return extractGeneration(itemStack) == generation; + } + + @Override + public List listAttributesOf(ItemStack itemStack) { + int generation = extractGeneration(itemStack); + + List atts = new ArrayList<>(); + if(generation >= 0) { + atts.add(new BookCopyAttribute(generation)); + } + return atts; + } + + @Override + public String getTranslationKey() { + switch(generation){ + case 0: + return "book_copy_original"; + case 1: + return "book_copy_first"; + case 2: + return "book_copy_second"; + default: + return "book_copy_tattered"; + } + } + + @Override + public void writeNBT(CompoundNBT nbt) { + nbt.putInt("generation", this.generation); + } + + @Override + public ItemAttribute readNBT(CompoundNBT nbt) { + return new BookCopyAttribute(nbt.getInt("generation")); + } + + private int extractGeneration(ItemStack stack) { + CompoundNBT nbt = stack.getTag(); + if (nbt != null && stack.getItem() instanceof WrittenBookItem) { + return nbt.getInt("generation"); + } + return -1; + } +}