Search in sources :

Example 6 with WarnData

use of io.github.nucleuspowered.nucleus.modules.warn.data.WarnData 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

WarnData (io.github.nucleuspowered.nucleus.modules.warn.data.WarnData)6 User (org.spongepowered.api.entity.living.player.User)6 Text (org.spongepowered.api.text.Text)4 Util (io.github.nucleuspowered.nucleus.Util)3 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)3 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)3 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)3 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)3 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)3 WarnHandler (io.github.nucleuspowered.nucleus.modules.warn.handlers.WarnHandler)3 Instant (java.time.Instant)3 Optional (java.util.Optional)3 UUID (java.util.UUID)3 Sponge (org.spongepowered.api.Sponge)3 CommandResult (org.spongepowered.api.command.CommandResult)3 CommandSource (org.spongepowered.api.command.CommandSource)3 CommandContext (org.spongepowered.api.command.args.CommandContext)3 CommandElement (org.spongepowered.api.command.args.CommandElement)3 GenericArguments (org.spongepowered.api.command.args.GenericArguments)3 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)3