Search in sources :

Example 1 with WarnData

use of io.github.nucleuspowered.nucleus.modules.warn.data.WarnData in project Nucleus by NucleusPowered.

the class CheckWarningsCommand method createMessage.

private Text createMessage(List<WarnData> allData, WarnData warning, User user) {
    Optional<UUID> warner = warning.getWarner();
    final String name;
    name = warner.map(uuid -> Util.getUserFromUUID(warning.getWarner().get()).map(User::getName).orElse(Sponge.getServer().getConsole().getName())).orElseGet(() -> Sponge.getServer().getConsole().getName());
    // Get the remaining length of the warning
    String time;
    if (warning.getEndTimestamp().isPresent()) {
        time = Util.getTimeStringFromSeconds(Instant.now().until(warning.getEndTimestamp().get(), ChronoUnit.SECONDS));
    } else if (warning.getTimeFromNextLogin().isPresent()) {
        time = Util.getTimeStringFromSeconds(warning.getTimeFromNextLogin().get().getSeconds());
    } else {
        time = plugin.getMessageProvider().getMessageWithFormat("standard.restoftime");
    }
    // Get the ID of the warning, its index in the users List<WarnData>
    int id = allData.indexOf(warning) + 1;
    // Action buttons, for a non expired warning this should look like 'Action > [Delete] - [Expire] - [Return] <'
    Text.Builder actions = plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.action").toBuilder();
    // Add separation between the word 'Action' and action buttons
    actions.append(Text.of(TextColors.GOLD, " > "));
    // Add the delete button [Delete]
    actions.append(Text.builder().append(Text.of(TextColors.RED, plugin.getMessageProvider().getMessageWithFormat("standard.action.delete"))).onHover(TextActions.showText(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.hover.delete"))).onClick(TextActions.runCommand("/removewarning --remove " + user.getName() + " " + id)).build());
    // Add a - to separate it from the next action button
    actions.append(Text.of(TextColors.GOLD, " - "));
    // Add the expire button if the warning isn't expired [Expire]
    if (!warning.isExpired()) {
        actions.append(Text.builder().append(Text.of(TextColors.YELLOW, plugin.getMessageProvider().getMessageWithFormat("standard.action.expire"))).onHover(TextActions.showText(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.hover.expire"))).onClick(TextActions.runCommand("/removewarning " + user.getName() + " " + id)).build());
        // Add a - to separate it from the next action button
        actions.append(Text.of(TextColors.GOLD, " - "));
    }
    // Add the return button [Return]
    actions.append(Text.builder().append(Text.of(TextColors.GREEN, plugin.getMessageProvider().getMessageWithFormat("standard.action.return"))).onHover(TextActions.showText(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.hover.return"))).onClick(TextActions.runCommand("/checkwarnings " + user.getName())).build());
    // Add a < to end the actions button list
    actions.append(Text.of(TextColors.GOLD, " < "));
    // Get and format the date of the warning
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM dd, yyyy").withZone(ZoneId.systemDefault());
    String date = dtf.format(warning.getDate());
    // Create a clickable name providing more information about the warning
    Text.Builder information = Text.builder(name).onHover(TextActions.showText(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.hover.check"))).onClick(TextActions.executeCallback(commandSource -> {
        commandSource.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.id", String.valueOf(id)));
        commandSource.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.date", date));
        commandSource.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.remaining", time));
        commandSource.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.warner", name));
        commandSource.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.warning", warning.getReason()));
        commandSource.sendMessage(actions.build());
    }));
    // Create the warning message
    Text.Builder message = Text.builder().append(Text.of(TextColors.GREEN, information.build())).append(Text.of(": ")).append(Text.of(TextColors.YELLOW, warning.getReason()));
    // Add the remaining length of the warning
    if (warning.isExpired()) {
        message.append(Text.of(TextColors.GRAY, " " + plugin.getMessageProvider().getMessageWithFormat("standard.status.expired")));
    } else {
        message.append(Text.of(TextColors.GREEN, " " + plugin.getMessageProvider().getMessageWithFormat("standard.for") + " "));
        if (Character.isLetter(time.charAt(0))) {
            message.append(Text.of(TextColors.YELLOW, time.substring(0, 1).toLowerCase() + time.substring(1)));
        } else {
            message.append(Text.of(TextColors.YELLOW, time));
        }
    }
    return message.build();
}
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) Text(org.spongepowered.api.text.Text) UUID(java.util.UUID) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 2 with WarnData

use of io.github.nucleuspowered.nucleus.modules.warn.data.WarnData in project Nucleus by NucleusPowered.

the class WarningArgument method parseValue.

@Nullable
@Override
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
    Optional<String> optPlayer = args.nextIfPresent();
    if (!optPlayer.isPresent()) {
        throw args.createError(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("args.warning.nouserarg"));
    }
    String player = optPlayer.get();
    Optional<User> optUser = Sponge.getServiceManager().provideUnchecked(UserStorageService.class).get(player);
    if (!optUser.isPresent()) {
        throw args.createError(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("args.warning.nouser", player));
    }
    User user = optUser.get();
    Optional<String> optIndex = args.nextIfPresent();
    if (!optIndex.isPresent()) {
        throw args.createError(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("args.warning.noindex", user.getName()));
    }
    List<WarnData> warnData = handler.getWarningsInternal(user);
    int index;
    try {
        index = Integer.parseInt(optIndex.get()) - 1;
        if (index >= warnData.size() || index < 0) {
            throw args.createError(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("args.warning.nowarndata", optIndex.get(), user.getName()));
        }
    } catch (NumberFormatException ex) {
        throw args.createError(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("args.warning.indexnotnumber"));
    }
    if (!warnData.isEmpty()) {
        return new Result(user, warnData.get(index));
    }
    throw args.createError(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("args.warning.nouserwarnings", user.getName()));
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) User(org.spongepowered.api.entity.living.player.User) WarnData(io.github.nucleuspowered.nucleus.modules.warn.data.WarnData) Nullable(javax.annotation.Nullable)

Example 3 with WarnData

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

use of io.github.nucleuspowered.nucleus.modules.warn.data.WarnData in project Nucleus by NucleusPowered.

the class ClearWarningsCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    User user = args.<User>getOne(playerKey).get();
    List<WarnData> warnings = handler.getWarningsInternal(user);
    if (warnings.isEmpty()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.none", user.getName()));
        return CommandResult.success();
    }
    // By default expire all active warnings.
    // If the flag --all is used then remove all warnings
    // If the flag --expired is used then remove all expired warnings.
    // If the flag --remove is used then remove all active warnings.
    boolean removeActive = false;
    boolean removeExpired = false;
    Text message = plugin.getMessageProvider().getTextMessageWithFormat("command.clearwarnings.success", user.getName());
    if (args.hasAny("all")) {
        removeActive = true;
        removeExpired = true;
        message = plugin.getMessageProvider().getTextMessageWithFormat("command.clearwarnings.all", user.getName());
    } else if (args.hasAny("remove")) {
        removeActive = true;
        message = plugin.getMessageProvider().getTextMessageWithFormat("command.clearwarnings.remove", user.getName());
    } else if (args.hasAny("expired")) {
        removeExpired = true;
        message = plugin.getMessageProvider().getTextMessageWithFormat("command.clearwarnings.expired", user.getName());
    }
    if (handler.clearWarnings(user, removeActive, removeExpired, CauseStackHelper.createCause(src))) {
        src.sendMessage(message);
        return CommandResult.success();
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.clearwarnings.failure", user.getName()));
    return CommandResult.empty();
}
Also used : User(org.spongepowered.api.entity.living.player.User) WarnData(io.github.nucleuspowered.nucleus.modules.warn.data.WarnData) Text(org.spongepowered.api.text.Text)

Example 5 with WarnData

use of io.github.nucleuspowered.nucleus.modules.warn.data.WarnData in project Nucleus by NucleusPowered.

the class RemoveWarningCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    WarningArgument.Result result = args.<WarningArgument.Result>getOne(warningKey).get();
    User user = result.user;
    boolean removePermanently = false;
    if (args.hasAny("remove")) {
        removePermanently = true;
    }
    List<WarnData> warnings = handler.getWarningsInternal(user);
    if (warnings.isEmpty()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.none", user.getName()));
        return CommandResult.success();
    }
    if (handler.removeWarning(user, result.warnData, removePermanently, CauseStackHelper.createCause(src))) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.removewarning.success", user.getName()));
        return CommandResult.success();
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.removewarning.failure", user.getName()));
    return CommandResult.empty();
}
Also used : User(org.spongepowered.api.entity.living.player.User) WarnData(io.github.nucleuspowered.nucleus.modules.warn.data.WarnData) WarningArgument(io.github.nucleuspowered.nucleus.argumentparsers.WarningArgument)

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