mirror of
https://github.com/Creators-of-Create/Create.git
synced 2024-11-08 19:12:14 +01:00
Custom Name Attributes
This commit is contained in:
parent
6ff57ca65c
commit
c1bb1e64c9
2 changed files with 74 additions and 0 deletions
|
@ -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)),
|
||||
|
|
|
@ -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 "";
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue