Custom Name Attributes

This commit is contained in:
Colman Davenport 2020-10-17 00:58:49 -04:00
parent 6ff57ca65c
commit c1bb1e64c9
2 changed files with 74 additions and 0 deletions

View file

@ -11,6 +11,7 @@ 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 net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import org.apache.commons.lang3.StringUtils;
@ -48,6 +49,7 @@ public interface ItemAttribute {
static ItemAttribute inItemGroup = register(new InItemGroup(ItemGroup.MISC));
static ItemAttribute hasEnchant = register(new EnchantAttribute("dummy"));
static ItemAttribute hasFluid = register(new FluidContentsAttribute("dummy"));
static ItemAttribute hasName = register(new ItemNameAttribute("dummy"));
static ItemAttribute addedBy = register(new AddedBy("dummy"));
static ItemAttribute register(ItemAttribute attributeType) {
@ -113,6 +115,7 @@ public interface ItemAttribute {
CONSUMABLE(ItemStack::isFood),
FLUID_CONTAINER(s -> s.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY).isPresent()),
ENCHANTED(ItemStack::isEnchanted),
RENAMED(ItemStack::hasDisplayName),
DAMAGED(ItemStack::isDamaged),
BADLY_DAMAGED(s -> s.isDamaged() && s.getDamage() / s.getMaxDamage() > 3 / 4f),
NOT_STACKABLE(Predicates.not(ItemStack::isStackable)),

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