Search in sources :

Example 1 with NicknameUserDataModule

use of io.github.nucleuspowered.nucleus.modules.nickname.datamodules.NicknameUserDataModule 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());
    }
}
Also used : User(org.spongepowered.api.entity.living.player.User) ChangeNicknameEvent(io.github.nucleuspowered.nucleus.modules.nickname.events.ChangeNicknameEvent) Text(org.spongepowered.api.text.Text) ModularUserService(io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService) Subject(org.spongepowered.api.service.permission.Subject) UserStorageService(org.spongepowered.api.service.user.UserStorageService) NicknameException(io.github.nucleuspowered.nucleus.api.exceptions.NicknameException) NicknameUserDataModule(io.github.nucleuspowered.nucleus.modules.nickname.datamodules.NicknameUserDataModule)

Aggregations

NicknameException (io.github.nucleuspowered.nucleus.api.exceptions.NicknameException)1 ModularUserService (io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService)1 NicknameUserDataModule (io.github.nucleuspowered.nucleus.modules.nickname.datamodules.NicknameUserDataModule)1 ChangeNicknameEvent (io.github.nucleuspowered.nucleus.modules.nickname.events.ChangeNicknameEvent)1 User (org.spongepowered.api.entity.living.player.User)1 Subject (org.spongepowered.api.service.permission.Subject)1 UserStorageService (org.spongepowered.api.service.user.UserStorageService)1 Text (org.spongepowered.api.text.Text)1