Search in sources :

Example 46 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)

Example 47 with CommandResult

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

the class PowertoolCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    ItemStack itemStack = src.getItemInHand(HandTypes.MAIN_HAND).orElseThrow(() -> ReturnMessageException.fromKey("command.powertool.noitem"));
    Optional<String> command = args.getOne(commandKey);
    PowertoolUserDataModule inu = Nucleus.getNucleus().getUserDataManager().getUnchecked(src).get(PowertoolUserDataModule.class);
    return command.map(s -> setPowertool(src, inu, itemStack.getType(), s)).orElseGet(() -> viewPowertool(src, inu, itemStack));
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule) GenericArguments(org.spongepowered.api.command.args.GenericArguments) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Lists(com.google.common.collect.Lists) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Player(org.spongepowered.api.entity.living.player.Player) ItemType(org.spongepowered.api.item.ItemType) ItemStack(org.spongepowered.api.item.inventory.ItemStack) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule)

Example 48 with CommandResult

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

the class GameruleCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    WorldProperties worldProperties = getWorldFromUserOrArgs(src, worldKey, args);
    Map<String, String> gameRules = worldProperties.getGameRules();
    String message = plugin.getMessageProvider().getMessageWithFormat("command.world.gamerule.key");
    List<Text> text = gameRules.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).map(x -> Text.of(TextActions.suggestCommand(String.format("/world gamerule set %s %s ", worldProperties.getWorldName(), x.getKey())), TextSerializers.FORMATTING_CODE.deserialize(MessageFormat.format(message, x.getKey(), x.getValue())))).collect(Collectors.toList());
    Util.getPaginationBuilder(src).title(plugin.getMessageProvider().getTextMessageWithFormat("command.world.gamerule.header", worldProperties.getWorldName())).contents(text).sendTo(src);
    return CommandResult.success();
}
Also used : NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) CommandSource(org.spongepowered.api.command.CommandSource) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) CommandElement(org.spongepowered.api.command.args.CommandElement) GenericArguments(org.spongepowered.api.command.args.GenericArguments) Collectors(java.util.stream.Collectors) MessageFormat(java.text.MessageFormat) TextSerializers(org.spongepowered.api.text.serializer.TextSerializers) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Util(io.github.nucleuspowered.nucleus.Util) Comparator(java.util.Comparator) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) Text(org.spongepowered.api.text.Text) Map(java.util.Map) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 49 with CommandResult

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

the class CheckWarningsCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    User user = args.<User>getOne(playerKey).get();
    handler.updateWarnings(user);
    List<WarnData> warnings;
    final List<WarnData> allWarnings = handler.getWarningsInternal(user);
    if (args.hasAny("all")) {
        warnings = allWarnings;
    } else if (args.hasAny("expired")) {
        warnings = allWarnings.stream().filter(WarnData::isExpired).collect(Collectors.toList());
    } else {
        warnings = allWarnings.stream().filter(x -> !x.isExpired()).collect(Collectors.toList());
    }
    if (warnings.isEmpty()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.none", user.getName()));
        return CommandResult.success();
    }
    List<Text> messages = warnings.stream().sorted(Comparator.comparing(WarnData::getDate)).map(x -> createMessage(allWarnings, x, user)).collect(Collectors.toList());
    messages.add(0, plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.info"));
    PaginationService paginationService = Sponge.getGame().getServiceManager().provideUnchecked(PaginationService.class);
    paginationService.builder().title(Text.builder().color(TextColors.GOLD).append(Text.of(plugin.getMessageProvider().getMessageWithFormat("command.checkwarnings.header", user.getName()))).build()).padding(Text.builder().color(TextColors.YELLOW).append(Text.of("=")).build()).contents(messages).sendTo(src);
    return CommandResult.success();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) WarnData(io.github.nucleuspowered.nucleus.modules.warn.data.WarnData) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) GenericArguments(org.spongepowered.api.command.args.GenericArguments) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) WarnHandler(io.github.nucleuspowered.nucleus.modules.warn.handlers.WarnHandler) 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) TextColors(org.spongepowered.api.text.format.TextColors) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) User(org.spongepowered.api.entity.living.player.User) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Instant(java.time.Instant) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) ChronoUnit(java.time.temporal.ChronoUnit) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) Comparator(java.util.Comparator) User(org.spongepowered.api.entity.living.player.User) WarnData(io.github.nucleuspowered.nucleus.modules.warn.data.WarnData) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService)

Example 50 with CommandResult

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

the class WarnCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    final User user = args.<User>getOne(playerKey).get();
    Optional<Long> optDuration = args.getOne(durationKey);
    String reason = args.<String>getOne(reasonKey).get();
    if (permissions.testSuffix(user, "exempt.target", src, false)) {
        throw ReturnMessageException.fromKey("command.warn.exempt", user.getName());
    }
    // Set default duration if no duration given
    if (warnConfig.getDefaultLength() != -1 && !optDuration.isPresent()) {
        optDuration = Optional.of(warnConfig.getDefaultLength());
    }
    UUID warner = Util.getUUID(src);
    WarnData warnData = optDuration.map(aLong -> new WarnData(Instant.now(), warner, reason, Duration.ofSeconds(aLong))).orElseGet(() -> new WarnData(Instant.now(), warner, reason));
    // Check if too long (No duration provided, it is infinite)
    if (!optDuration.isPresent() && warnConfig.getMaximumWarnLength() != -1 && !permissions.testSuffix(src, "exempt.length")) {
        throw ReturnMessageException.fromKey("command.warn.length.toolong", Util.getTimeStringFromSeconds(warnConfig.getMaximumWarnLength()));
    }
    // Check if too long
    if (optDuration.orElse(Long.MAX_VALUE) > warnConfig.getMaximumWarnLength() && warnConfig.getMaximumWarnLength() != -1 && !permissions.testSuffix(src, "exempt.length")) {
        throw ReturnMessageException.fromKey("command.warn.length.toolong", Util.getTimeStringFromSeconds(warnConfig.getMaximumWarnLength()));
    }
    // Check if too short
    if (optDuration.orElse(Long.MAX_VALUE) < warnConfig.getMinimumWarnLength() && warnConfig.getMinimumWarnLength() != -1 && !permissions.testSuffix(src, "exempt.length")) {
        throw ReturnMessageException.fromKey("command.warn.length.tooshort", Util.getTimeStringFromSeconds(warnConfig.getMinimumWarnLength()));
    }
    if (handler.addWarning(user, warnData)) {
        MutableMessageChannel messageChannel = new PermissionMessageChannel(permissions.getPermissionWithSuffix("notify")).asMutable();
        messageChannel.addMember(src);
        if (optDuration.isPresent()) {
            String time = Util.getTimeStringFromSeconds(optDuration.get());
            messageChannel.send(plugin.getMessageProvider().getTextMessageWithFormat("command.warn.success.time", user.getName(), src.getName(), warnData.getReason(), time));
            if (user.isOnline()) {
                user.getPlayer().get().sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("warn.playernotify.time", warnData.getReason(), time));
            }
        } else {
            messageChannel.send(plugin.getMessageProvider().getTextMessageWithFormat("command.warn.success.norm", user.getName(), src.getName(), warnData.getReason()));
            if (user.isOnline()) {
                user.getPlayer().get().sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("warn.playernotify.standard", warnData.getReason()));
            }
        }
        // Check if the subject has action command should be executed
        if (warnConfig.getWarningsBeforeAction() != -1) {
            if (handler.getWarningsInternal(user, true, false).size() < warnConfig.getWarningsBeforeAction()) {
                return CommandResult.success();
            }
            // Expire all active warnings
            // The cause is the plugin, as this isn't directly the warning user.
            CauseStackHelper.createFrameWithCausesWithConsumer(c -> handler.clearWarnings(user, false, false, c), src);
            // Get and run the action command
            String command = warnConfig.getActionCommand().replaceAll("\\{\\{name}}", user.getName());
            Sponge.getCommandManager().process(Sponge.getServer().getConsole(), command);
        }
        return CommandResult.success();
    }
    throw ReturnMessageException.fromKey("command.warn.fail", user.getName());
}
Also used : WarnConfig(io.github.nucleuspowered.nucleus.modules.warn.config.WarnConfig) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) WarnData(io.github.nucleuspowered.nucleus.modules.warn.data.WarnData) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) HashMap(java.util.HashMap) GenericArguments(org.spongepowered.api.command.args.GenericArguments) WarnHandler(io.github.nucleuspowered.nucleus.modules.warn.handlers.WarnHandler) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) TimespanArgument(io.github.nucleuspowered.nucleus.argumentparsers.TimespanArgument) Duration(java.time.Duration) Map(java.util.Map) 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) MutableMessageChannel(org.spongepowered.api.text.channel.MutableMessageChannel) WarnConfigAdapter(io.github.nucleuspowered.nucleus.modules.warn.config.WarnConfigAdapter) CommandResult(org.spongepowered.api.command.CommandResult) User(org.spongepowered.api.entity.living.player.User) CommandSource(org.spongepowered.api.command.CommandSource) PermissionMessageChannel(io.github.nucleuspowered.nucleus.util.PermissionMessageChannel) CauseStackHelper(io.github.nucleuspowered.nucleus.util.CauseStackHelper) Sponge(org.spongepowered.api.Sponge) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) UUID(java.util.UUID) Instant(java.time.Instant) CommandElement(org.spongepowered.api.command.args.CommandElement) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) Optional(java.util.Optional) PermissionMessageChannel(io.github.nucleuspowered.nucleus.util.PermissionMessageChannel) User(org.spongepowered.api.entity.living.player.User) WarnData(io.github.nucleuspowered.nucleus.modules.warn.data.WarnData) UUID(java.util.UUID) MutableMessageChannel(org.spongepowered.api.text.channel.MutableMessageChannel)

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