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();
}
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()));
});
}
}
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());
}
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()));
});
}
}
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);
}
Aggregations