Search in sources :

Example 31 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class ListGroupTests method testTwoGroupsWithWeights.

@Test
public void testTwoGroupsWithWeights() {
    Subject admin = createSubjectWithWeight("admin", 1);
    Subject ace = createSubjectWithWeight("ace", 0);
    Assert.assertEquals(-1, ListPlayerCommand.groupComparison(ListPlayerCommand.weightingFunction, admin, ace));
}
Also used : Subject(org.spongepowered.api.service.permission.Subject) Test(org.junit.Test)

Example 32 with Subject

use of org.spongepowered.api.service.permission.Subject 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

Subject (org.spongepowered.api.service.permission.Subject)32 SubjectReference (org.spongepowered.api.service.permission.SubjectReference)12 List (java.util.List)10 Test (org.junit.Test)10 Player (org.spongepowered.api.entity.living.player.Player)9 PermissionService (org.spongepowered.api.service.permission.PermissionService)9 Optional (java.util.Optional)6 Context (org.spongepowered.api.service.context.Context)6 Inject (org.spongepowered.asm.mixin.injection.Inject)6 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5 Lists (com.google.common.collect.Lists)4 Maps (com.google.common.collect.Maps)4 Util (io.github.nucleuspowered.nucleus.Util)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 CommandSource (org.spongepowered.api.command.CommandSource)4 Text (org.spongepowered.api.text.Text)4 Tristate (org.spongepowered.api.util.Tristate)4 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)3 ListPlayerCommand (io.github.nucleuspowered.nucleus.modules.playerinfo.commands.ListPlayerCommand)3