use of org.spongepowered.common.data.manipulator.mutable.SpongeDisplayNameData in project SpongeCommon by SpongePowered.
the class DisplayNameDataProcessor method from.
@Override
public Optional<DisplayNameData> from(DataHolder holder) {
if (holder instanceof Entity) {
@Nullable Text displayName = ((IMixinEntity) holder).getDisplayNameText();
if (displayName != null) {
return Optional.of(new SpongeDisplayNameData(displayName));
}
return Optional.empty();
} else if (holder instanceof ItemStack) {
ItemStack stack = (ItemStack) holder;
if (!stack.hasDisplayName()) {
return Optional.empty();
}
if (stack.getItem() == Items.WRITTEN_BOOK) {
final NBTTagCompound compound = stack.getTagCompound();
if (compound == null) {
// The book wasn't initialized.
return Optional.empty();
}
return Optional.of(new SpongeDisplayNameData(SpongeTexts.fromLegacy(compound.getString(NbtDataUtil.ITEM_BOOK_TITLE))));
}
final NBTTagCompound compound = ((ItemStack) holder).getSubCompound(NbtDataUtil.ITEM_DISPLAY);
if (compound != null && compound.hasKey(NbtDataUtil.ITEM_DISPLAY_NAME, NbtDataUtil.TAG_STRING)) {
return Optional.of(new SpongeDisplayNameData(SpongeTexts.fromLegacy(compound.getString(NbtDataUtil.ITEM_DISPLAY_NAME))));
}
return Optional.empty();
} else if (holder instanceof IWorldNameable) {
if (((IWorldNameable) holder).hasCustomName()) {
final String customName = ((IWorldNameable) holder).getName();
final DisplayNameData data = new SpongeDisplayNameData(SpongeTexts.fromLegacy(customName));
return Optional.of(data);
}
return Optional.empty();
}
return Optional.empty();
}
Aggregations