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();
}
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()));
}
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();
}
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();
}
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();
}
Aggregations