mirror of
https://github.com/Creators-of-Create/Create.git
synced 2024-11-13 21:41:34 +01:00
Specific Enchantment Attribute
This commit is contained in:
parent
d585fc0e21
commit
a7e2d842af
2 changed files with 73 additions and 0 deletions
|
@ -9,6 +9,7 @@ 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.EnchantAttribute;
|
||||||
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 +44,7 @@ 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(new EnchantAttribute("dummy"));
|
||||||
static ItemAttribute addedBy = register(new AddedBy("dummy"));
|
static ItemAttribute addedBy = register(new AddedBy("dummy"));
|
||||||
|
|
||||||
static ItemAttribute register(ItemAttribute attributeType) {
|
static ItemAttribute register(ItemAttribute attributeType) {
|
||||||
|
|
|
@ -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<ItemAttribute> listAttributesOf(ItemStack itemStack) {
|
||||||
|
ListNBT enchants = extractEnchants(itemStack);
|
||||||
|
List<ItemAttribute> 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue