Search in sources :

Example 1 with Permissions

use of io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions in project Nucleus by NucleusPowered.

the class SetupPermissionsCommand method setupPerms.

private void setupPerms(CommandSource src, Subject group, SuggestedLevel level, boolean reset, boolean inherit) throws Exception {
    if (inherit && level.getLowerLevel() != null) {
        setupPerms(src, group, level.getLowerLevel(), reset, inherit);
    }
    Set<Context> globalContext = Sets.newHashSet();
    SubjectData data = group.getSubjectData();
    Set<String> definedPermissions = data.getPermissions(ImmutableSet.of()).keySet();
    Logger logger = Nucleus.getNucleus().getLogger();
    MessageProvider messageProvider = Nucleus.getNucleus().getMessageProvider();
    // Register all the permissions, but only those that have yet to be assigned.
    permissionRegistry.getPermissions().entrySet().stream().filter(x -> x.getValue().level == level).filter(x -> reset || !definedPermissions.contains(x.getKey())).forEach(x -> {
        logger.info(messageProvider.getMessageWithFormat("command.nucleus.permission.added", x.getKey(), group.getIdentifier()));
        data.setPermission(globalContext, x.getKey(), Tristate.TRUE);
    });
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.permission.complete", level.toString().toLowerCase(), group.getIdentifier()));
}
Also used : CommandContext(org.spongepowered.api.command.args.CommandContext) Context(org.spongepowered.api.service.context.Context) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) GenericArguments(org.spongepowered.api.command.args.GenericArguments) PermissionService(org.spongepowered.api.service.permission.PermissionService) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) PermissionRegistry(io.github.nucleuspowered.nucleus.internal.PermissionRegistry) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) ServiceChangeListener(io.github.nucleuspowered.nucleus.internal.permissions.ServiceChangeListener) Subject(org.spongepowered.api.service.permission.Subject) Nullable(javax.annotation.Nullable) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) SubjectData(org.spongepowered.api.service.permission.SubjectData) CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandSource(org.spongepowered.api.command.CommandSource) Context(org.spongepowered.api.service.context.Context) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Tristate(org.spongepowered.api.util.Tristate) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) Optional(java.util.Optional) Collections(java.util.Collections) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) SubjectData(org.spongepowered.api.service.permission.SubjectData) Logger(org.slf4j.Logger)

Example 2 with Permissions

use of io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions in project Nucleus by NucleusPowered.

the class DocGenCache method addCommand.

public void addCommand(final String moduleID, final AbstractCommand<?> abstractCommand) {
    if (abstractCommand.getClass().isAnnotationPresent(NoDocumentation.class)) {
        return;
    }
    CommandDoc cmd = new CommandDoc();
    String cmdPath = abstractCommand.getCommandPath().replaceAll("\\.", " ");
    cmd.setCommandName(cmdPath);
    cmd.setAliases(String.join(", ", Lists.newArrayList(abstractCommand.getAliases())));
    if (abstractCommand.getRootCommandAliases().length > 0) {
        cmd.setRootAliases(String.join(", ", Arrays.asList(abstractCommand.getRootCommandAliases())));
    }
    cmd.setPermissionbase(abstractCommand.getPermissionHandler().getBase());
    Class<? extends AbstractCommand> cac = abstractCommand.getClass();
    Permissions s = cac.getAnnotation(Permissions.class);
    if (s == null) {
        cmd.setDefaultLevel(cac.isAnnotationPresent(NoPermissions.class) ? SuggestedLevel.USER.name() : SuggestedLevel.ADMIN.name());
    } else {
        cmd.setDefaultLevel(s.suggestedLevel().name());
    }
    cmd.setModule(moduleID);
    if (!cac.isAnnotationPresent(NoModifiers.class)) {
        cmd.setCooldown(!cac.isAnnotationPresent(NoCooldown.class));
        cmd.setCost(!cac.isAnnotationPresent(NoCost.class));
        cmd.setWarmup(!cac.isAnnotationPresent(NoWarmup.class));
    }
    cmd.setSince(cac.getAnnotation(Since.class));
    String desc = abstractCommand.getDescription();
    if (!desc.contains(" ")) {
        logger.warn("Cannot generate description for: " + abstractCommand.getCommandPath() + ": " + desc);
    }
    cmd.setOneLineDescription(desc);
    String extendedDescription = abstractCommand.getExtendedDescription().replace("\n", "|br|").replace("\"", "&quot;");
    if (!extendedDescription.isEmpty()) {
        cmd.setExtendedDescription(extendedDescription);
    }
    List<PermissionDoc> lp = new ArrayList<>();
    abstractCommand.getPermissionHandler().getSuggestedPermissions().forEach((k, v) -> lp.add(addPermissionDocs(moduleID, k, v)));
    cmd.setPermissions(lp);
    cmd.setUsageString(abstractCommand.getUsageString(Sponge.getServer().getConsole()));
    cmd.setSubcommands(abstractCommand.getChildrenUsage(Sponge.getServer().getConsole()).map(Text::toPlain).orElse(""));
    cmd.setSimpleUsage(abstractCommand.getSimpleUsage(Sponge.getServer().getConsole()));
    // Essentials
    EssentialsEquivalent ee = abstractCommand.getClass().getAnnotation(EssentialsEquivalent.class);
    if (ee != null) {
        List<String> ss = Arrays.asList(ee.value());
        cmd.setEssentialsEquivalents(ss);
        cmd.setEssNotes(ee.notes());
        cmd.setExactEssEquiv(ee.isExact());
        EssentialsDoc doc = new EssentialsDoc();
        doc.setEssentialsCommands(ss);
        int i = cmdPath.lastIndexOf(" ");
        String c;
        if (i > -1) {
            c = cmdPath.substring(0, i) + " ";
        } else {
            c = "";
        }
        List<String> a = Lists.newArrayList(abstractCommand.getAliases()).stream().map(x -> c + x).collect(Collectors.toList());
        if (abstractCommand.getRootCommandAliases().length > 0) {
            a.addAll(Arrays.asList(abstractCommand.getRootCommandAliases()));
        }
        doc.setNucleusEquiv(a);
        doc.setExact(ee.isExact());
        doc.setNotes(ee.notes());
        essentialsDocs.add(doc);
    }
    commandDocs.add(cmd);
}
Also used : ConfigurationOptions(ninja.leaping.configurate.ConfigurationOptions) Arrays(java.util.Arrays) NoCost(io.github.nucleuspowered.nucleus.internal.annotations.command.NoCost) NoPermissions(io.github.nucleuspowered.nucleus.internal.annotations.command.NoPermissions) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) Since(io.github.nucleuspowered.nucleus.internal.annotations.Since) TypeToken(com.google.common.reflect.TypeToken) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) NucleusConfigAdapter(io.github.nucleuspowered.nucleus.internal.qsml.NucleusConfigAdapter) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) AbstractConfigAdapter(uk.co.drnaylor.quickstart.config.AbstractConfigAdapter) NoCooldown(io.github.nucleuspowered.nucleus.internal.annotations.command.NoCooldown) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) HoconConfigurationLoader(ninja.leaping.configurate.hocon.HoconConfigurationLoader) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) Collection(java.util.Collection) NoDocumentation(io.github.nucleuspowered.nucleus.internal.annotations.command.NoDocumentation) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) NoWarmup(io.github.nucleuspowered.nucleus.internal.annotations.command.NoWarmup) ConfigurableModule(io.github.nucleuspowered.nucleus.internal.qsml.module.ConfigurableModule) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) ConfigurateHelper(io.github.nucleuspowered.nucleus.configurate.ConfigurateHelper) ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) NoPermissions(io.github.nucleuspowered.nucleus.internal.annotations.command.NoPermissions) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) Since(io.github.nucleuspowered.nucleus.internal.annotations.Since)

Aggregations

Nucleus (io.github.nucleuspowered.nucleus.Nucleus)2 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)2 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)2 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)2 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Sponge (org.spongepowered.api.Sponge)2 Text (org.spongepowered.api.text.Text)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 TypeToken (com.google.common.reflect.TypeToken)1 ConfigurateHelper (io.github.nucleuspowered.nucleus.configurate.ConfigurateHelper)1 PermissionRegistry (io.github.nucleuspowered.nucleus.internal.PermissionRegistry)1 RunAsync (io.github.nucleuspowered.nucleus.internal.annotations.RunAsync)1 Since (io.github.nucleuspowered.nucleus.internal.annotations.Since)1 NoCooldown (io.github.nucleuspowered.nucleus.internal.annotations.command.NoCooldown)1