Refactor EnchantAttribute

This commit is contained in:
Colman Davenport 2020-10-17 11:23:38 -04:00
parent 6d51a6c730
commit 42ef3796a2
2 changed files with 22 additions and 31 deletions

View File

@ -49,7 +49,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 hasEnchant = register(EnchantAttribute.EMPTY);
static ItemAttribute hasFluid = register(new FluidContentsAttribute("dummy")); static ItemAttribute hasFluid = register(new FluidContentsAttribute("dummy"));
static ItemAttribute hasName = register(new ItemNameAttribute("dummy")); static ItemAttribute hasName = register(new ItemNameAttribute("dummy"));
static ItemAttribute astralAmulet = register(new AstralSorceryAmuletAttribute("dummy", -1)); static ItemAttribute astralAmulet = register(new AstralSorceryAmuletAttribute("dummy", -1));

View File

@ -2,41 +2,34 @@ package com.simibubi.create.content.logistics.item.filter.attribute;
import com.simibubi.create.content.logistics.item.filter.ItemAttribute; import com.simibubi.create.content.logistics.item.filter.ItemAttribute;
import net.minecraft.enchantment.Enchantment; import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.ForgeRegistries;
import java.util.ArrayList; import javax.annotation.Nullable;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class EnchantAttribute implements ItemAttribute { public class EnchantAttribute implements ItemAttribute {
String enchantName; public static final EnchantAttribute EMPTY = new EnchantAttribute(null);
public EnchantAttribute(String enchantName) { private final Enchantment enchantment;
this.enchantName = enchantName;
public EnchantAttribute(@Nullable Enchantment enchantment) {
this.enchantment = enchantment;
} }
@Override @Override
public boolean appliesTo(ItemStack itemStack) { public boolean appliesTo(ItemStack itemStack) {
ListNBT enchants = extractEnchants(itemStack); return EnchantmentHelper.getEnchantments(itemStack).containsKey(enchantment);
for (int i = 0; i < enchants.size(); i++) {
if(enchants.getCompound(i).getString("id").equals(this.enchantName))
return true;
}
return false;
} }
@Override @Override
public List<ItemAttribute> listAttributesOf(ItemStack itemStack) { public List<ItemAttribute> listAttributesOf(ItemStack itemStack) {
ListNBT enchants = extractEnchants(itemStack); return EnchantmentHelper.getEnchantments(itemStack).keySet().stream().map(EnchantAttribute::new).collect(Collectors.toList());
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 @Override
@ -46,26 +39,24 @@ public class EnchantAttribute implements ItemAttribute {
@Override @Override
public Object[] getTranslationParameters() { public Object[] getTranslationParameters() {
String something = ""; String parameter = "";
Enchantment enchant = ForgeRegistries.ENCHANTMENTS.getValue(ResourceLocation.tryCreate(enchantName)); if(enchantment != null)
if(enchant != null) { parameter = new TranslationTextComponent(enchantment.getName()).getString();
something = new TranslationTextComponent(enchant.getName()).getString(); return new Object[] { parameter };
}
return new Object[] { something };
} }
@Override @Override
public void writeNBT(CompoundNBT nbt) { public void writeNBT(CompoundNBT nbt) {
nbt.putString("id", this.enchantName); if (enchantment == null)
return;
ResourceLocation id = ForgeRegistries.ENCHANTMENTS.getKey(enchantment);
if (id == null)
return;
nbt.putString("id", id.toString());
} }
@Override @Override
public ItemAttribute readNBT(CompoundNBT nbt) { public ItemAttribute readNBT(CompoundNBT nbt) {
return new EnchantAttribute(nbt.getString("id")); return nbt.contains("id") ? new EnchantAttribute(ForgeRegistries.ENCHANTMENTS.getValue(ResourceLocation.tryCreate(nbt.getString("id")))) : EMPTY;
}
private ListNBT extractEnchants(ItemStack stack) {
CompoundNBT tag = stack.getTag() != null ? stack.getTag() : new CompoundNBT();
return tag.contains("Enchantments") ? stack.getEnchantmentTagList() : tag.getList("StoredEnchantments", 10);
} }
} }