Search in sources :

Example 36 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class PrintPermsCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    Map<String, PermissionInformation> l = plugin.getPermissionRegistry().getPermissions();
    List<String> notsuggested = l.entrySet().stream().filter(x -> x.getValue().level == SuggestedLevel.NONE).map(Map.Entry::getKey).collect(Collectors.toList());
    List<String> owner = l.entrySet().stream().filter(x -> x.getValue().level == SuggestedLevel.NONE).map(Map.Entry::getKey).collect(Collectors.toList());
    List<String> admin = l.entrySet().stream().filter(x -> x.getValue().level == SuggestedLevel.ADMIN).map(Map.Entry::getKey).collect(Collectors.toList());
    List<String> mod = l.entrySet().stream().filter(x -> x.getValue().level == SuggestedLevel.MOD).map(Map.Entry::getKey).collect(Collectors.toList());
    List<String> user = l.entrySet().stream().filter(x -> x.getValue().level == SuggestedLevel.USER).map(Map.Entry::getKey).collect(Collectors.toList());
    String file = "plugin-perms.txt";
    BufferedWriter f = new BufferedWriter(new FileWriter(file));
    Consumer<String> permWriter = x -> {
        try {
            f.write(x);
            f.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    };
    f.write("Not Suggested");
    f.write("-----");
    f.newLine();
    notsuggested.stream().sorted().forEach(permWriter);
    f.newLine();
    f.write("Owner");
    f.write("-----");
    f.newLine();
    owner.stream().sorted().forEach(permWriter);
    f.newLine();
    f.write("Admin");
    f.write("-----");
    f.newLine();
    admin.stream().sorted().forEach(permWriter);
    f.newLine();
    f.write("Mod");
    f.write("-----");
    f.newLine();
    mod.stream().sorted().forEach(permWriter);
    f.newLine();
    f.write("User");
    f.write("-----");
    f.newLine();
    user.stream().sorted().forEach(permWriter);
    f.flush();
    f.close();
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.printperms", file));
    return CommandResult.success();
}
Also used : NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) CommandSource(org.spongepowered.api.command.CommandSource) BufferedWriter(java.io.BufferedWriter) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) FileWriter(java.io.FileWriter) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) Consumer(java.util.function.Consumer) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) CommandContext(org.spongepowered.api.command.args.CommandContext) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) FileWriter(java.io.FileWriter) IOException(java.io.IOException) Map(java.util.Map) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) BufferedWriter(java.io.BufferedWriter)

Example 37 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class KitListCommand method executeCommand.

@Override
public CommandResult executeCommand(final CommandSource src, CommandContext args) throws Exception {
    Set<String> kits = KIT_HANDLER.getKitNames();
    if (kits.isEmpty()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.list.empty"));
        return CommandResult.empty();
    }
    PaginationService paginationService = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
    ArrayList<Text> kitText = Lists.newArrayList();
    final KitUserDataModule user = src instanceof Player ? Nucleus.getNucleus().getUserDataManager().getUnchecked(((Player) src).getUniqueId()).get(KitUserDataModule.class) : null;
    final boolean showHidden = kitPermissionHandler.testSuffix(src, "showhidden");
    Stream<String> kitStream = KIT_HANDLER.getKitNames(showHidden).stream();
    if (this.isSeparatePermissions) {
        kitStream = kitStream.filter(kit -> src.hasPermission(KitHandler.getPermissionForKit(kit.toLowerCase())));
    }
    kitStream.forEach(kit -> kitText.add(createKit(src, user, kit, KIT_HANDLER.getKit(kit).get())));
    PaginationList.Builder paginationBuilder = paginationService.builder().contents(kitText).title(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.list.kits")).padding(Text.of(TextColors.GREEN, "-"));
    paginationBuilder.sendTo(src);
    return CommandResult.success();
}
Also used : KitUserDataModule(io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) KitFallbackBase(io.github.nucleuspowered.nucleus.modules.kit.commands.KitFallbackBase) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) ArrayList(java.util.ArrayList) PaginationList(org.spongepowered.api.service.pagination.PaginationList) Lists(com.google.common.collect.Lists) KitUserDataModule(io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Duration(java.time.Duration) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) Nullable(javax.annotation.Nullable) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandPermissionHandler(io.github.nucleuspowered.nucleus.internal.CommandPermissionHandler) CommandResult(org.spongepowered.api.command.CommandResult) KitHandler(io.github.nucleuspowered.nucleus.modules.kit.handlers.KitHandler) TextActions(org.spongepowered.api.text.action.TextActions) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandSource(org.spongepowered.api.command.CommandSource) TextStyles(org.spongepowered.api.text.format.TextStyles) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Instant(java.time.Instant) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) Stream(java.util.stream.Stream) Player(org.spongepowered.api.entity.living.player.Player) KitConfigAdapter(io.github.nucleuspowered.nucleus.modules.kit.config.KitConfigAdapter) Player(org.spongepowered.api.entity.living.player.Player) PaginationList(org.spongepowered.api.service.pagination.PaginationList) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService)

Example 38 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class KitViewCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    final Kit kitInfo = args.<Kit>getOne(KIT_PARAMETER).get();
    Inventory inventory = Util.getKitInventoryBuilder().property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.view.title", kitInfo.getName()))).build(this.plugin);
    List<ItemStack> lis = kitInfo.getStacks().stream().filter(x -> !x.getType().equals(ItemTypes.NONE)).map(ItemStackSnapshot::createStack).collect(Collectors.toList());
    if (this.processTokens) {
        KIT_HANDLER.processTokensInItemStacks(src, lis);
    }
    lis.forEach(inventory::offer);
    return src.openInventory(inventory).map(x -> {
        KIT_HANDLER.addViewer(x);
        return CommandResult.success();
    }).orElseThrow(() -> ReturnMessageException.fromKey("command.kit.view.cantopen", kitInfo.getName()));
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) ItemTypes(org.spongepowered.api.item.ItemTypes) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) Since(io.github.nucleuspowered.nucleus.internal.annotations.Since) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) KitFallbackBase(io.github.nucleuspowered.nucleus.modules.kit.commands.KitFallbackBase) GenericArguments(org.spongepowered.api.command.args.GenericArguments) ItemStack(org.spongepowered.api.item.inventory.ItemStack) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) KitHandler(io.github.nucleuspowered.nucleus.modules.kit.handlers.KitHandler) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) InventoryTitle(org.spongepowered.api.item.inventory.property.InventoryTitle) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) Player(org.spongepowered.api.entity.living.player.Player) KitArgument(io.github.nucleuspowered.nucleus.argumentparsers.KitArgument) KitConfigAdapter(io.github.nucleuspowered.nucleus.modules.kit.config.KitConfigAdapter) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 39 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class StaffChatCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    Optional<String> toSend = args.getOne(message);
    if (toSend.isPresent()) {
        try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            frame.addContext(EventContexts.SHOULD_FORMAT_CHANNEL, StaffChatMessageChannel.getInstance().formatMessages());
            if (src instanceof Player) {
                Player pl = (Player) src;
                frame.pushCause(pl);
                frame.addContext(EventContextKeys.PLAYER_SIMULATED, pl.getProfile());
                MessageChannel mc = pl.getMessageChannel();
                pl.setMessageChannel(StaffChatMessageChannel.getInstance());
                pl.simulateChat(TextParsingUtils.addUrls(toSend.get()), Sponge.getCauseStackManager().getCurrentCause());
                pl.setMessageChannel(mc);
            } else {
                StaffChatMessageChannel.getInstance().send(src, TextParsingUtils.addUrls(toSend.get()), ChatTypes.CHAT);
            }
            return CommandResult.success();
        }
    }
    if (!(src instanceof Player)) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat.consoletoggle"));
    }
    Player player = (Player) src;
    StaffChatTransientModule s = plugin.getUserDataManager().get(player).map(y -> y.getTransient(StaffChatTransientModule.class)).orElseGet(StaffChatTransientModule::new);
    boolean result = !(src.getMessageChannel() instanceof StaffChatMessageChannel);
    if (result) {
        s.setPreviousMessageChannel(player.getMessageChannel());
        src.setMessageChannel(StaffChatMessageChannel.getInstance());
    } else {
        src.setMessageChannel(s.getPreviousMessageChannel().orElse(MessageChannel.TO_ALL));
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat." + (result ? "on" : "off")));
    return CommandResult.success();
}
Also used : ChatTypes(org.spongepowered.api.text.chat.ChatTypes) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) RemainingStringsArgument(io.github.nucleuspowered.nucleus.argumentparsers.RemainingStringsArgument) TextParsingUtils(io.github.nucleuspowered.nucleus.internal.text.TextParsingUtils) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) EventContexts(io.github.nucleuspowered.nucleus.api.EventContexts) CommandSource(org.spongepowered.api.command.CommandSource) StaffChatMessageChannel(io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel) Sponge(org.spongepowered.api.Sponge) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) StaffChatTransientModule(io.github.nucleuspowered.nucleus.modules.staffchat.datamodules.StaffChatTransientModule) Player(org.spongepowered.api.entity.living.player.Player) StaffChatMessageChannel(io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) StaffChatTransientModule(io.github.nucleuspowered.nucleus.modules.staffchat.datamodules.StaffChatTransientModule) StaffChatMessageChannel(io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel)

Example 40 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class TemporaryMessageCommand method executeCommand.

@Override
protected CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    // Get the temporary message item.
    ServerListGeneralDataModule mod = plugin.getGeneralService().get(ServerListGeneralDataModule.class);
    if (args.hasAny("r")) {
        if (mod.getMessage().isPresent()) {
            // Remove
            mod.remove();
            // Send message.
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.removed"));
            return CommandResult.success();
        }
        throw ReturnMessageException.fromKey("command.serverlist.message.noremoved");
    }
    // Which line?
    boolean linetwo = args.<Integer>getOne(line).map(x -> x == 2).orElse(false);
    Optional<String> onMessage = args.getOne(this.message);
    if (!onMessage.isPresent()) {
        boolean isValid = mod.getExpiry().map(x -> x.isAfter(Instant.now())).orElse(false);
        if (!isValid) {
            throw ReturnMessageException.fromKey("command.serverlist.message.isempty");
        }
        if (linetwo) {
            mod.setLineTwo(null);
        } else {
            mod.setLineOne(null);
        }
        Optional<Text> newMessage = mod.getMessage();
        if (newMessage.isPresent()) {
            // Send message
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.set"));
            src.sendMessage(newMessage.get());
        } else {
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.empty"));
        }
        return CommandResult.success();
    }
    String nMessage = onMessage.get();
    // If the expiry is null or before now, and there is no timespan, then it's an hour.
    Instant endTime = args.<Long>getOne(timespan).map(x -> Instant.now().plus(x, ChronoUnit.SECONDS)).orElseGet(() -> mod.getExpiry().map(x -> x.isBefore(Instant.now()) ? x.plusSeconds(3600) : x).orElseGet(() -> Instant.now().plusSeconds(3600)));
    // Set the expiry.
    mod.setExpiry(endTime);
    if (linetwo) {
        mod.setLineTwo(nMessage);
    } else {
        mod.setLineOne(nMessage);
    }
    Optional<Text> newMessage = mod.getMessage();
    if (newMessage.isPresent()) {
        // Send message
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.set"));
        src.sendMessage(newMessage.get());
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.expiry", Util.getTimeToNow(endTime)));
        return CommandResult.success();
    }
    throw ReturnMessageException.fromKey("command.serverlist.message.notset");
}
Also used : BoundedIntegerArgument(io.github.nucleuspowered.nucleus.argumentparsers.BoundedIntegerArgument) CommandResult(org.spongepowered.api.command.CommandResult) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) CommandSource(org.spongepowered.api.command.CommandSource) ServerListGeneralDataModule(io.github.nucleuspowered.nucleus.modules.serverlist.datamodules.ServerListGeneralDataModule) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) Instant(java.time.Instant) CommandElement(org.spongepowered.api.command.args.CommandElement) GenericArguments(org.spongepowered.api.command.args.GenericArguments) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) ChronoUnit(java.time.temporal.ChronoUnit) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) TimespanArgument(io.github.nucleuspowered.nucleus.argumentparsers.TimespanArgument) Optional(java.util.Optional) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) Instant(java.time.Instant) ServerListGeneralDataModule(io.github.nucleuspowered.nucleus.modules.serverlist.datamodules.ServerListGeneralDataModule) Text(org.spongepowered.api.text.Text)

Aggregations

CommandResult (org.spongepowered.api.command.CommandResult)62 Text (org.spongepowered.api.text.Text)46 CommandContext (org.spongepowered.api.command.args.CommandContext)40 List (java.util.List)39 CommandSource (org.spongepowered.api.command.CommandSource)37 Player (org.spongepowered.api.entity.living.player.Player)36 Sponge (org.spongepowered.api.Sponge)34 Optional (java.util.Optional)33 Collectors (java.util.stream.Collectors)33 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)26 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)26 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)26 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)26 TextColors (org.spongepowered.api.text.format.TextColors)25 CommandElement (org.spongepowered.api.command.args.CommandElement)21 Util (io.github.nucleuspowered.nucleus.Util)20 GenericArguments (org.spongepowered.api.command.args.GenericArguments)20 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)18 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)18 CommandException (org.spongepowered.api.command.CommandException)18