Search in sources :

Example 1 with DisplayNameData

use of org.spongepowered.api.data.manipulator.mutable.DisplayNameData 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)

Example 2 with DisplayNameData

use of org.spongepowered.api.data.manipulator.mutable.DisplayNameData in project LanternServer by LanternPowered.

the class SelectorResolver method addNameFilters.

private void addNameFilters(List<Predicate<Entity>> filters) {
    Selector sel = this.selector;
    Optional<Argument.Invertible<String>> nameOpt = sel.getArgument(ArgumentTypes.NAME);
    if (nameOpt.isPresent()) {
        final String name = nameOpt.get().getValue();
        final boolean inverted = nameOpt.get().isInverted();
        filters.add(input -> {
            Optional<DisplayNameData> dispName = input.get(DisplayNameData.class);
            return inverted ^ (dispName.isPresent() && name.equals(dispName.get().displayName().get().toPlain()));
        });
    }
}
Also used : DisplayNameData(org.spongepowered.api.data.manipulator.mutable.DisplayNameData) Invertible(org.spongepowered.api.text.selector.Argument.Invertible) Selector(org.spongepowered.api.text.selector.Selector)

Example 3 with DisplayNameData

use of org.spongepowered.api.data.manipulator.mutable.DisplayNameData in project SpongeCommon by SpongePowered.

the class DisplayNameDataProcessor method set.

@Override
public DataTransactionResult set(DataHolder holder, DisplayNameData manipulator, MergeFunction function) {
    if (holder instanceof IMixinEntity && !(holder instanceof Player)) {
        final Optional<DisplayNameData> old = from(holder);
        final DisplayNameData merged = checkNotNull(function).merge(old.orElse(null), manipulator);
        final Text newValue = merged.displayName().get();
        final ImmutableValue<Text> immutableValue = merged.displayName().asImmutable();
        try {
            ((IMixinEntity) holder).setDisplayName(newValue);
            if (old.isPresent()) {
                return DataTransactionResult.successReplaceResult(old.get().displayName().asImmutable(), immutableValue);
            }
            return DataTransactionResult.successResult(immutableValue);
        } catch (Exception e) {
            SpongeImpl.getLogger().debug("An exception occurred when setting data: ", e);
            return DataTransactionResult.errorResult(immutableValue);
        }
    }
    if (holder instanceof ItemStack) {
        final Optional<DisplayNameData> prevValue = from(holder);
        final DisplayNameData merged = checkNotNull(function).merge(prevValue.orElse(null), manipulator);
        final Text newValue = merged.displayName().get();
        final ImmutableValue<Text> immutableValue = merged.displayName().asImmutable();
        ItemStack stack = (ItemStack) holder;
        if (stack.getItem() == Items.WRITTEN_BOOK) {
            NbtDataUtil.getOrCreateCompound(stack).setString(NbtDataUtil.ITEM_BOOK_TITLE, SpongeTexts.toLegacy(newValue));
        } else {
            stack.setStackDisplayName(SpongeTexts.toLegacy(newValue));
        }
        if (prevValue.isPresent()) {
            return DataTransactionResult.successReplaceResult(prevValue.get().displayName().asImmutable(), immutableValue);
        }
        return DataTransactionResult.successResult(immutableValue);
    }
    return DataTransactionResult.failResult(manipulator.getValues());
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) 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) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Text(org.spongepowered.api.text.Text) ItemStack(net.minecraft.item.ItemStack)

Example 4 with DisplayNameData

use of org.spongepowered.api.data.manipulator.mutable.DisplayNameData in project SpongeCommon by SpongePowered.

the class SelectorResolver method addNameFilters.

private void addNameFilters(List<Predicate<Entity>> filters) {
    Selector sel = this.selector;
    Optional<Argument.Invertible<String>> nameOpt = sel.getArgument(ArgumentTypes.NAME);
    if (nameOpt.isPresent()) {
        final String name = nameOpt.get().getValue();
        final boolean inverted = nameOpt.get().isInverted();
        filters.add(input -> {
            Optional<DisplayNameData> dispName = input.get(DisplayNameData.class);
            return inverted ^ (dispName.isPresent() && name.equals(dispName.get().displayName().get().toPlain()));
        });
    }
}
Also used : DisplayNameData(org.spongepowered.api.data.manipulator.mutable.DisplayNameData) Invertible(org.spongepowered.api.text.selector.Argument.Invertible) Selector(org.spongepowered.api.text.selector.Selector)

Example 5 with DisplayNameData

use of org.spongepowered.api.data.manipulator.mutable.DisplayNameData in project modules-extra by CubeEngine.

the class ChatCommands method nick.

@Command(desc = "Changes your display name")
public void nick(CommandSource context, @Label("<name>|-reset") String name, @Optional Player player) {
    // TODO this only works when ChatFormat uses {DISPLAYNAME}
    if (player == null) {
        if (!(context instanceof Player)) {
            // console has no data / displayname
            i18n.send(context, NEGATIVE, "You cannot change the consoles display name");
            return;
        }
        player = ((Player) context);
    }
    if (!context.equals(player) && !context.hasPermission(module.perms().COMMAND_NICK_OTHER.getId())) {
        i18n.send(context, NEGATIVE, "You are not allowed to change the nickname of another player!");
        return;
    }
    if ("-r".equalsIgnoreCase(name) || "-reset".equalsIgnoreCase(name)) {
        DisplayNameData display = player.getOrCreate(DisplayNameData.class).get();
        display.displayName().set(Text.of(context.getName()));
        player.offer(display);
        i18n.send(context, POSITIVE, "Display name reset to {user}", context);
        return;
    }
    if (name.length() >= 3 && name.length() <= 16 && Sponge.getServiceManager().provideUnchecked(UserStorageService.class).get(name).isPresent() && !context.hasPermission(module.perms().COMMAND_NICK_OFOTHER.getId())) {
        i18n.send(context, NEGATIVE, "This name has been taken by another player!");
        return;
    }
    i18n.send(context, POSITIVE, "Display name changed from {user} to {user}", context, name);
    DisplayNameData display = player.getOrCreate(DisplayNameData.class).get();
    display.displayName().set(ChatFormat.fromLegacy(name, '&'));
    player.offer(display);
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) DisplayNameData(org.spongepowered.api.data.manipulator.mutable.DisplayNameData) Command(org.cubeengine.butler.parametric.Command)

Aggregations

DisplayNameData (org.spongepowered.api.data.manipulator.mutable.DisplayNameData)5 ItemStack (net.minecraft.item.ItemStack)2 ImmutableDisplayNameData (org.spongepowered.api.data.manipulator.immutable.ImmutableDisplayNameData)2 Player (org.spongepowered.api.entity.living.player.Player)2 Text (org.spongepowered.api.text.Text)2 Invertible (org.spongepowered.api.text.selector.Argument.Invertible)2 Selector (org.spongepowered.api.text.selector.Selector)2 ImmutableSpongeDisplayNameData (org.spongepowered.common.data.manipulator.immutable.ImmutableSpongeDisplayNameData)2 SpongeDisplayNameData (org.spongepowered.common.data.manipulator.mutable.SpongeDisplayNameData)2 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)2 Nullable (javax.annotation.Nullable)1 Entity (net.minecraft.entity.Entity)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 IWorldNameable (net.minecraft.world.IWorldNameable)1 Command (org.cubeengine.butler.parametric.Command)1