use of io.github.nucleuspowered.nucleus.modules.nickname.events.ChangeNicknameEvent in project Nucleus by NucleusPowered.
the class NicknameService method removeNick.
private void removeNick(User user, Cause cause) throws NicknameException {
Text currentNickname = getNickname(user).orElse(null);
ChangeNicknameEvent cne = new ChangeNicknameEvent(cause, currentNickname, null, user);
if (Sponge.getEventManager().post(cne)) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.eventcancel", user.getName()), NicknameException.Type.EVENT_CANCELLED);
}
Nucleus.getNucleus().getUserDataManager().get(user).orElseThrow(() -> new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("standard.error.nouser"), NicknameException.Type.NO_USER)).get(NicknameUserDataModule.class).removeNickname();
if (user.isOnline()) {
user.getPlayer().ifPresent(x -> x.sendMessage(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.delnick.success.base")));
}
}
use of io.github.nucleuspowered.nucleus.modules.nickname.events.ChangeNicknameEvent in project Nucleus by NucleusPowered.
the class NicknameService method setNick.
private void setNick(User pl, Cause cause, Text nickname, boolean bypass) throws NicknameException {
String plain = nickname.toPlain().trim();
if (plain.isEmpty()) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.tooshort"), NicknameException.Type.TOO_SHORT);
}
// Does the user exist?
try {
Optional<User> match = Sponge.getServiceManager().provideUnchecked(UserStorageService.class).get(nickname.toPlain().trim());
// The only person who can use such a name is oneself.
if (match.isPresent() && !match.get().getUniqueId().equals(pl.getUniqueId())) {
// Fail - cannot use another's name.
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.nameinuse", plain), NicknameException.Type.NOT_OWN_IGN);
}
} catch (IllegalArgumentException ignored) {
// We allow some other nicknames too.
}
if (!bypass) {
// Giving subject must have the colour permissions and whatnot. Also,
// colour and color are the two spellings we support. (RULE BRITANNIA!)
Optional<Subject> os = cause.first(Subject.class);
if (os.isPresent()) {
stripPermissionless(os.get(), nickname);
}
if (!pattern.matcher(plain).matches()) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.nopattern", pattern.pattern()), NicknameException.Type.INVALID_PATTERN);
}
int strippedNameLength = plain.length();
// Do a regex remove to check minimum length requirements.
if (strippedNameLength < Math.max(min, 1)) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.tooshort"), NicknameException.Type.TOO_SHORT);
}
// Do a regex remove to check maximum length requirements. Will be at least the minimum length
if (strippedNameLength > Math.max(max, min)) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.toolong"), NicknameException.Type.TOO_SHORT);
}
}
// Send an event
Text currentNickname = getNickname(pl).orElse(null);
ChangeNicknameEvent cne = new ChangeNicknameEvent(cause, currentNickname, nickname, pl);
if (Sponge.getEventManager().post(cne)) {
throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.eventcancel", pl.getName()), NicknameException.Type.EVENT_CANCELLED);
}
ModularUserService mus = Nucleus.getNucleus().getUserDataManager().getUnchecked(pl);
NicknameUserDataModule nicknameUserDataModule = mus.get(NicknameUserDataModule.class);
nicknameUserDataModule.setNickname(nickname);
mus.set(nicknameUserDataModule);
mus.save();
Text set = nicknameUserDataModule.getNicknameAsText().get();
if (pl.isOnline()) {
pl.getPlayer().get().sendMessage(Text.builder().append(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.success.base")).append(Text.of(" - ", TextColors.RESET, set)).build());
}
}
Aggregations