Search in sources :

Example 1 with SpongeDisplayNameData

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();
}
Also used : ImmutableSpongeDisplayNameData(org.spongepowered.common.data.manipulator.immutable.ImmutableSpongeDisplayNameData) SpongeDisplayNameData(org.spongepowered.common.data.manipulator.mutable.SpongeDisplayNameData) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Entity(net.minecraft.entity.Entity) ImmutableSpongeDisplayNameData(org.spongepowered.common.data.manipulator.immutable.ImmutableSpongeDisplayNameData) SpongeDisplayNameData(org.spongepowered.common.data.manipulator.mutable.SpongeDisplayNameData) DisplayNameData(org.spongepowered.api.data.manipulator.mutable.DisplayNameData) ImmutableDisplayNameData(org.spongepowered.api.data.manipulator.immutable.ImmutableDisplayNameData) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Text(org.spongepowered.api.text.Text) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) ItemStack(net.minecraft.item.ItemStack) Nullable(javax.annotation.Nullable) IWorldNameable(net.minecraft.world.IWorldNameable)

Aggregations

Nullable (javax.annotation.Nullable)1 Entity (net.minecraft.entity.Entity)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 IWorldNameable (net.minecraft.world.IWorldNameable)1 ImmutableDisplayNameData (org.spongepowered.api.data.manipulator.immutable.ImmutableDisplayNameData)1 DisplayNameData (org.spongepowered.api.data.manipulator.mutable.DisplayNameData)1 Text (org.spongepowered.api.text.Text)1 ImmutableSpongeDisplayNameData (org.spongepowered.common.data.manipulator.immutable.ImmutableSpongeDisplayNameData)1 SpongeDisplayNameData (org.spongepowered.common.data.manipulator.mutable.SpongeDisplayNameData)1 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)1