Merge pull request #484 from gamrguy/extra-attributes

Extra attribute filters
This commit is contained in:
simibubi 2020-10-20 12:58:35 +02:00 committed by GitHub
commit 7ca8325b5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 674 additions and 0 deletions

View File

@ -843,6 +843,10 @@
"create.item_attributes.blastable.inverted": "is not smeltable in Blast Furnace", "create.item_attributes.blastable.inverted": "is not smeltable in Blast Furnace",
"create.item_attributes.enchanted": "is enchanted", "create.item_attributes.enchanted": "is enchanted",
"create.item_attributes.enchanted.inverted": "is unenchanted", "create.item_attributes.enchanted.inverted": "is unenchanted",
"create.item_attributes.renamed": "has a custom name",
"create.item_attributes.renamed.inverted": "does not have a custom name",
"create.item_attributes.fluid_container": "can hold fluid",
"create.item_attributes.fluid_container.inverted": "cannot hold fluid",
"create.item_attributes.damaged": "is damaged", "create.item_attributes.damaged": "is damaged",
"create.item_attributes.damaged.inverted": "is not damaged", "create.item_attributes.damaged.inverted": "is not damaged",
"create.item_attributes.badly_damaged": "is heavily damaged", "create.item_attributes.badly_damaged": "is heavily damaged",
@ -859,6 +863,30 @@
"create.item_attributes.in_item_group.inverted": "is not in group '%1$s'", "create.item_attributes.in_item_group.inverted": "is not in group '%1$s'",
"create.item_attributes.added_by": "was added by %1$s", "create.item_attributes.added_by": "was added by %1$s",
"create.item_attributes.added_by.inverted": "was not added by %1$s", "create.item_attributes.added_by.inverted": "was not added by %1$s",
"create.item_attributes.has_enchant": "is enchanted with %1$s",
"create.item_attributes.has_enchant.inverted": "is not enchanted with %1$s",
"create.item_attributes.has_fluid": "contains %1$s",
"create.item_attributes.has_fluid.inverted": "does not contain %1$s",
"create.item_attributes.has_name": "has the custom name %1$s",
"create.item_attributes.has_name.inverted": "does not have the custom name %1$s",
"create.item_attributes.book_author": "was authored by %1$s",
"create.item_attributes.book_author.inverted": "was not authored by %1$s",
"create.item_attributes.book_copy_original": "is an original",
"create.item_attributes.book_copy_original.inverted": "is not an original",
"create.item_attributes.book_copy_first": "is a first-generation copy",
"create.item_attributes.book_copy_first.inverted": "is not a first-generation copy",
"create.item_attributes.book_copy_second": "is a second-generation copy",
"create.item_attributes.book_copy_second.inverted": "is not a second-generation copy",
"create.item_attributes.book_copy_tattered": "is a tattered mess",
"create.item_attributes.book_copy_tattered.inverted": "is not a tattered mess",
"create.item_attributes.astralsorcery_crystal": "has crystal attribute %1$s",
"create.item_attributes.astralsorcery_crystal.inverted": "does not have crystal attribute %1$s",
"create.item_attributes.astralsorcery_constellation": "is attuned to %1$s",
"create.item_attributes.astralsorcery_constellation.inverted": "is not attuned to %1$s",
"create.item_attributes.astralsorcery_perk_gem": "has perk attribute %1$s",
"create.item_attributes.astralsorcery_perk_gem.inverted": "does not have perk attribute %1$s",
"create.item_attributes.astralsorcery_amulet": "improves %1$s",
"create.item_attributes.astralsorcery_amulet.inverted": "does not improve %1$s",
"create.gui.attribute_filter.no_selected_attributes": "No attributes selected", "create.gui.attribute_filter.no_selected_attributes": "No attributes selected",
"create.gui.attribute_filter.selected_attributes": "Selected attributes:", "create.gui.attribute_filter.selected_attributes": "Selected attributes:",

View File

@ -9,6 +9,12 @@ import java.util.function.BiPredicate;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.simibubi.create.content.logistics.item.filter.attribute.*;
import com.simibubi.create.content.logistics.item.filter.attribute.astralsorcery.AstralSorceryAmuletAttribute;
import com.simibubi.create.content.logistics.item.filter.attribute.astralsorcery.AstralSorceryAttunementAttribute;
import com.simibubi.create.content.logistics.item.filter.attribute.astralsorcery.AstralSorceryCrystalAttribute;
import com.simibubi.create.content.logistics.item.filter.attribute.astralsorcery.AstralSorceryPerkGemAttribute;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
@ -43,6 +49,15 @@ public interface ItemAttribute {
static ItemAttribute standard = register(StandardTraits.DUMMY); static ItemAttribute standard = register(StandardTraits.DUMMY);
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 hasFluid = register(FluidContentsAttribute.EMPTY);
static ItemAttribute hasName = register(new ItemNameAttribute("dummy"));
static ItemAttribute astralAmulet = register(new AstralSorceryAmuletAttribute("dummy", -1));
static ItemAttribute astralAttunement = register(new AstralSorceryAttunementAttribute("dummy"));
static ItemAttribute astralCrystal = register(new AstralSorceryCrystalAttribute("dummy"));
static ItemAttribute astralPerkGem = register(new AstralSorceryPerkGemAttribute("dummy"));
static ItemAttribute bookAuthor = register(new BookAuthorAttribute("dummy"));
static ItemAttribute bookCopy = register(new BookCopyAttribute(-1));
static ItemAttribute addedBy = register(new AddedBy("dummy")); static ItemAttribute addedBy = register(new AddedBy("dummy"));
static ItemAttribute register(ItemAttribute attributeType) { static ItemAttribute register(ItemAttribute attributeType) {
@ -106,7 +121,9 @@ public interface ItemAttribute {
DUMMY(s -> false), DUMMY(s -> false),
PLACEABLE(s -> s.getItem() instanceof BlockItem), PLACEABLE(s -> s.getItem() instanceof BlockItem),
CONSUMABLE(ItemStack::isFood), CONSUMABLE(ItemStack::isFood),
FLUID_CONTAINER(s -> s.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY).isPresent()),
ENCHANTED(ItemStack::isEnchanted), ENCHANTED(ItemStack::isEnchanted),
RENAMED(ItemStack::hasDisplayName),
DAMAGED(ItemStack::isDamaged), DAMAGED(ItemStack::isDamaged),
BADLY_DAMAGED(s -> s.isDamaged() && s.getDamage() / s.getMaxDamage() > 3 / 4f), BADLY_DAMAGED(s -> s.isDamaged() && s.getDamage() / s.getMaxDamage() > 3 / 4f),
NOT_STACKABLE(Predicates.not(ItemStack::isStackable)), NOT_STACKABLE(Predicates.not(ItemStack::isStackable)),

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;
}
}

View File

@ -0,0 +1,62 @@
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.enchantment.EnchantmentHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.registries.ForgeRegistries;
import javax.annotation.Nullable;
import java.util.List;
import java.util.stream.Collectors;
public class EnchantAttribute implements ItemAttribute {
public static final EnchantAttribute EMPTY = new EnchantAttribute(null);
private final Enchantment enchantment;
public EnchantAttribute(@Nullable Enchantment enchantment) {
this.enchantment = enchantment;
}
@Override
public boolean appliesTo(ItemStack itemStack) {
return EnchantmentHelper.getEnchantments(itemStack).containsKey(enchantment);
}
@Override
public List<ItemAttribute> listAttributesOf(ItemStack itemStack) {
return EnchantmentHelper.getEnchantments(itemStack).keySet().stream().map(EnchantAttribute::new).collect(Collectors.toList());
}
@Override
public String getTranslationKey() {
return "has_enchant";
}
@Override
public Object[] getTranslationParameters() {
String parameter = "";
if(enchantment != null)
parameter = new TranslationTextComponent(enchantment.getName()).getString();
return new Object[] { parameter };
}
@Override
public void writeNBT(CompoundNBT nbt) {
if (enchantment == null)
return;
ResourceLocation id = ForgeRegistries.ENCHANTMENTS.getKey(enchantment);
if (id == null)
return;
nbt.putString("id", id.toString());
}
@Override
public ItemAttribute readNBT(CompoundNBT nbt) {
return nbt.contains("id") ? new EnchantAttribute(ForgeRegistries.ENCHANTMENTS.getValue(ResourceLocation.tryCreate(nbt.getString("id")))) : EMPTY;
}
}

View File

@ -0,0 +1,80 @@
package com.simibubi.create.content.logistics.item.filter.attribute;
import com.simibubi.create.content.logistics.item.filter.ItemAttribute;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import net.minecraftforge.registries.ForgeRegistries;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class FluidContentsAttribute implements ItemAttribute {
public static final FluidContentsAttribute EMPTY = new FluidContentsAttribute(null);
private final Fluid fluid;
public FluidContentsAttribute(@Nullable Fluid fluid) {
this.fluid = fluid;
}
@Override
public boolean appliesTo(ItemStack itemStack) {
return extractFluids(itemStack).contains(fluid);
}
@Override
public List<ItemAttribute> listAttributesOf(ItemStack itemStack) {
return extractFluids(itemStack).stream().map(FluidContentsAttribute::new).collect(Collectors.toList());
}
@Override
public String getTranslationKey() {
return "has_fluid";
}
@Override
public Object[] getTranslationParameters() {
String parameter = "";
if(fluid != null)
parameter = new TranslationTextComponent(fluid.getAttributes().getTranslationKey()).getString();
return new Object[] { parameter };
}
@Override
public void writeNBT(CompoundNBT nbt) {
if (fluid == null)
return;
ResourceLocation id = ForgeRegistries.FLUIDS.getKey(fluid);
if (id == null)
return;
nbt.putString("id", id.toString());
}
@Override
public ItemAttribute readNBT(CompoundNBT nbt) {
return nbt.contains("id") ? new FluidContentsAttribute(ForgeRegistries.FLUIDS.getValue(ResourceLocation.tryCreate(nbt.getString("id")))) : EMPTY;
}
private List<Fluid> extractFluids(ItemStack stack) {
List<Fluid> fluids = new ArrayList<>();
LazyOptional<IFluidHandlerItem> capability =
stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY);
capability.ifPresent((cap) -> {
for(int i = 0; i < cap.getTanks(); i++) {
fluids.add(cap.getFluidInTank(i).getFluid());
}
});
return fluids;
}
}

View File

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

View File

@ -0,0 +1,81 @@
package com.simibubi.create.content.logistics.item.filter.attribute.astralsorcery;
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.INBT;
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 AstralSorceryAmuletAttribute implements ItemAttribute {
String enchName;
int enchType;
public AstralSorceryAmuletAttribute(String enchName, int enchType) {
this.enchName = enchName;
this.enchType = enchType;
}
@Override
public boolean appliesTo(ItemStack itemStack) {
for (INBT trait : extractTraitList(itemStack)) {
if(((CompoundNBT) trait).getString("ench").equals(this.enchName)
&& ((CompoundNBT)trait).getInt("type") == this.enchType)
return true;
}
return false;
}
@Override
public List<ItemAttribute> listAttributesOf(ItemStack itemStack) {
ListNBT traits = extractTraitList(itemStack);
List<ItemAttribute> atts = new ArrayList<>();
for (int i = 0; i < traits.size(); i++) {
atts.add(new AstralSorceryAmuletAttribute(
traits.getCompound(i).getString("ench"),
traits.getCompound(i).getInt("type")));
}
return atts;
}
@Override
public String getTranslationKey() {
return "astralsorcery_amulet";
}
@Override
public Object[] getTranslationParameters() {
ResourceLocation traitResource = new ResourceLocation(enchName);
String something = "";
Enchantment enchant = ForgeRegistries.ENCHANTMENTS.getValue(ResourceLocation.tryCreate(enchName));
if(enchant != null) {
something = new TranslationTextComponent(enchant.getName()).getString();
}
if(enchType == 1) something = "existing " + something;
return new Object[] { something };
}
@Override
public void writeNBT(CompoundNBT nbt) {
nbt.putString("enchName", this.enchName);
nbt.putInt("enchType", this.enchType);
}
@Override
public ItemAttribute readNBT(CompoundNBT nbt) {
return new AstralSorceryAmuletAttribute(nbt.getString("enchName"), nbt.getInt("enchType"));
}
private ListNBT extractTraitList(ItemStack stack) {
return stack.getTag() != null ? stack.getTag().getCompound("astralsorcery").getList("amuletEnchantments", 10) : new ListNBT();
}
}

View File

@ -0,0 +1,76 @@
package com.simibubi.create.content.logistics.item.filter.attribute.astralsorcery;
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.TranslationTextComponent;
import java.util.ArrayList;
import java.util.List;
public class AstralSorceryAttunementAttribute implements ItemAttribute {
String constellationName;
public AstralSorceryAttunementAttribute(String constellationName) {
this.constellationName = constellationName;
}
@Override
public boolean appliesTo(ItemStack itemStack) {
CompoundNBT nbt = extractAstralNBT(itemStack);
String constellation = nbt.contains("constellation") ? nbt.getString("constellation") : nbt.getString("constellationName");
// Special handling for shifting stars
ResourceLocation itemResource = itemStack.getItem().getRegistryName();
if(itemResource != null && itemResource.toString().contains("shifting_star_")) {
constellation = itemResource.toString().replace("shifting_star_", "");
}
return constellation.equals(constellationName);
}
@Override
public List<ItemAttribute> listAttributesOf(ItemStack itemStack) {
CompoundNBT nbt = extractAstralNBT(itemStack);
String constellation = nbt.contains("constellation") ? nbt.getString("constellation") : nbt.getString("constellationName");
// Special handling for shifting stars
ResourceLocation itemResource = itemStack.getItem().getRegistryName();
if(itemResource != null && itemResource.toString().contains("shifting_star_")) {
constellation = itemResource.toString().replace("shifting_star_", "");
}
List<ItemAttribute> atts = new ArrayList<>();
if(constellation.length() > 0) {
atts.add(new AstralSorceryAttunementAttribute(constellation));
}
return atts;
}
@Override
public String getTranslationKey() {
return "astralsorcery_constellation";
}
@Override
public Object[] getTranslationParameters() {
ResourceLocation constResource = new ResourceLocation(constellationName);
String something = new TranslationTextComponent(String.format("%s.constellation.%s", constResource.getNamespace(), constResource.getPath())).getString();
return new Object[] { something };
}
@Override
public void writeNBT(CompoundNBT nbt) {
nbt.putString("constellation", this.constellationName);
}
@Override
public ItemAttribute readNBT(CompoundNBT nbt) {
return new AstralSorceryAttunementAttribute(nbt.getString("constellation"));
}
private CompoundNBT extractAstralNBT(ItemStack stack) {
return stack.getTag() != null ? stack.getTag().getCompound("astralsorcery") : new CompoundNBT();
}
}

View File

@ -0,0 +1,65 @@
package com.simibubi.create.content.logistics.item.filter.attribute.astralsorcery;
import com.simibubi.create.content.logistics.item.filter.ItemAttribute;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TranslationTextComponent;
import java.util.ArrayList;
import java.util.List;
public class AstralSorceryCrystalAttribute implements ItemAttribute {
String traitName;
public AstralSorceryCrystalAttribute(String traitName) {
this.traitName = traitName;
}
@Override
public boolean appliesTo(ItemStack itemStack) {
for (INBT trait : extractTraitList(itemStack)) {
if(((CompoundNBT) trait).getString("property").equals(this.traitName))
return true;
}
return false;
}
@Override
public List<ItemAttribute> listAttributesOf(ItemStack itemStack) {
ListNBT traits = extractTraitList(itemStack);
List<ItemAttribute> atts = new ArrayList<>();
for (int i = 0; i < traits.size(); i++) {
atts.add(new AstralSorceryCrystalAttribute(traits.getCompound(i).getString("property")));
}
return atts;
}
@Override
public String getTranslationKey() {
return "astralsorcery_crystal";
}
@Override
public Object[] getTranslationParameters() {
ResourceLocation traitResource = new ResourceLocation(traitName);
String something = new TranslationTextComponent(String.format("crystal.property.%s.%s.name", traitResource.getNamespace(), traitResource.getPath())).getString();
return new Object[] { something };
}
@Override
public void writeNBT(CompoundNBT nbt) {
nbt.putString("property", this.traitName);
}
@Override
public ItemAttribute readNBT(CompoundNBT nbt) {
return new AstralSorceryCrystalAttribute(nbt.getString("property"));
}
private ListNBT extractTraitList(ItemStack stack) {
return stack.getTag() != null ? stack.getTag().getCompound("astralsorcery").getCompound("crystalProperties").getList("attributes", 10) : new ListNBT();
}
}

View File

@ -0,0 +1,65 @@
package com.simibubi.create.content.logistics.item.filter.attribute.astralsorcery;
import com.simibubi.create.content.logistics.item.filter.ItemAttribute;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TranslationTextComponent;
import java.util.ArrayList;
import java.util.List;
public class AstralSorceryPerkGemAttribute implements ItemAttribute {
String traitName;
public AstralSorceryPerkGemAttribute(String traitName) {
this.traitName = traitName;
}
@Override
public boolean appliesTo(ItemStack itemStack) {
for (INBT trait : extractTraitList(itemStack)) {
if(((CompoundNBT) trait).getString("type").equals(this.traitName))
return true;
}
return false;
}
@Override
public List<ItemAttribute> listAttributesOf(ItemStack itemStack) {
ListNBT traits = extractTraitList(itemStack);
List<ItemAttribute> atts = new ArrayList<>();
for (int i = 0; i < traits.size(); i++) {
atts.add(new AstralSorceryPerkGemAttribute(traits.getCompound(i).getString("type")));
}
return atts;
}
@Override
public String getTranslationKey() {
return "astralsorcery_perk_gem";
}
@Override
public Object[] getTranslationParameters() {
ResourceLocation traitResource = new ResourceLocation(traitName);
String something = new TranslationTextComponent(String.format("perk.attribute.%s.%s.name", traitResource.getNamespace(), traitResource.getPath())).getString();
return new Object[] { something };
}
@Override
public void writeNBT(CompoundNBT nbt) {
nbt.putString("type", this.traitName);
}
@Override
public ItemAttribute readNBT(CompoundNBT nbt) {
return new AstralSorceryPerkGemAttribute(nbt.getString("type"));
}
private ListNBT extractTraitList(ItemStack stack) {
return stack.getTag() != null ? stack.getTag().getCompound("astralsorcery").getList("attribute_modifiers", 10) : new ListNBT();
}
}