Written Book Attributes

This commit is contained in:
Colman Davenport 2020-10-17 01:16:32 -04:00
parent c1bb1e64c9
commit 562abdea31
3 changed files with 132 additions and 3 deletions

View file

@ -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) {

View file

@ -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<ItemAttribute> listAttributesOf(ItemStack itemStack) {
String name = extractAuthor(itemStack);
List<ItemAttribute> 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 "";
}
}

View file

@ -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<ItemAttribute> listAttributesOf(ItemStack itemStack) {
int generation = extractGeneration(itemStack);
List<ItemAttribute> 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;
}
}