Search in sources :

Example 1 with NoteData

use of io.github.nucleuspowered.nucleus.modules.note.data.NoteData in project Nucleus by NucleusPowered.

the class NoteArgument 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.note.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.note.nouser", player));
    }
    User user = optUser.get();
    Optional<String> optIndex = args.nextIfPresent();
    if (!optIndex.isPresent()) {
        throw args.createError(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("args.note.noindex", user.getName()));
    }
    List<NoteData> noteData = handler.getNotesInternal(user);
    int index;
    try {
        index = Integer.parseInt(optIndex.get()) - 1;
        if (index >= noteData.size() || index < 0) {
            throw args.createError(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("args.note.nonotedata", optIndex.get(), user.getName()));
        }
    } catch (NumberFormatException ex) {
        throw args.createError(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("args.note.indexnotnumber"));
    }
    if (!noteData.isEmpty()) {
        return new Result(user, noteData.get(index));
    }
    throw args.createError(NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("args.note.nousernotes", user.getName()));
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) User(org.spongepowered.api.entity.living.player.User) NoteData(io.github.nucleuspowered.nucleus.modules.note.data.NoteData) Nullable(javax.annotation.Nullable)

Example 2 with NoteData

use of io.github.nucleuspowered.nucleus.modules.note.data.NoteData in project Nucleus by NucleusPowered.

the class CheckNotesCommand method createMessage.

private Text createMessage(NoteData note, User user) {
    String name;
    if (note.getNoterInternal().equals(Util.consoleFakeUUID)) {
        name = Sponge.getServer().getConsole().getName();
    } else {
        Optional<User> ou = Sponge.getServiceManager().provideUnchecked(UserStorageService.class).get(note.getNoterInternal());
        name = ou.map(User::getName).orElseGet(() -> plugin.getMessageProvider().getMessageWithFormat("standard.unknown"));
    }
    // Get the ID of the note, its index in the users List<NoteData>. Add one to start with an ID of 1.
    int id = handler.getNotesInternal(user).indexOf(note) + 1;
    // Action buttons, this should look like 'Action > [Delete] - [Return] <'
    Text.Builder actions = plugin.getMessageProvider().getTextMessageWithFormat("command.checknotes.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.checknotes.hover.delete"))).onClick(TextActions.runCommand("/removenote " + 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.checknotes.hover.return"))).onClick(TextActions.runCommand("/checknotes " + 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(note.getDate());
    // Create a clickable name providing more information about the warning
    Text.Builder information = Text.builder(name).onHover(TextActions.showText(plugin.getMessageProvider().getTextMessageWithFormat("command.checknotes.hover.check"))).onClick(TextActions.executeCallback(commandSource -> {
        commandSource.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checknotes.id", String.valueOf(id)));
        commandSource.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checknotes.date", date));
        commandSource.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checknotes.noter", name));
        commandSource.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checknotes.note", note.getNote()));
        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, note.getNote()));
    return message.build();
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) GenericArguments(org.spongepowered.api.command.args.GenericArguments) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) UserStorageService(org.spongepowered.api.service.user.UserStorageService) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) NoteData(io.github.nucleuspowered.nucleus.modules.note.data.NoteData) Util(io.github.nucleuspowered.nucleus.Util) NoteHandler(io.github.nucleuspowered.nucleus.modules.note.handlers.NoteHandler) 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) PaginationService(org.spongepowered.api.service.pagination.PaginationService) 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) 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) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 3 with NoteData

use of io.github.nucleuspowered.nucleus.modules.note.data.NoteData in project Nucleus by NucleusPowered.

the class NoteCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    User user = args.<User>getOne(playerKey).get();
    String note = args.<String>getOne(noteKey).get();
    UUID noter = Util.consoleFakeUUID;
    if (src instanceof Player) {
        noter = ((Player) src).getUniqueId();
    }
    NoteData noteData = new NoteData(Instant.now(), noter, note);
    if (noteHandler.addNote(user, noteData)) {
        MutableMessageChannel messageChannel = new PermissionMessageChannel(notifyPermission).asMutable();
        messageChannel.addMember(src);
        messageChannel.send(plugin.getMessageProvider().getTextMessageWithFormat("command.note.success", src.getName(), noteData.getNote(), user.getName()));
        return CommandResult.success();
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warn.fail", user.getName()));
    return CommandResult.empty();
}
Also used : PermissionMessageChannel(io.github.nucleuspowered.nucleus.util.PermissionMessageChannel) Player(org.spongepowered.api.entity.living.player.Player) User(org.spongepowered.api.entity.living.player.User) UUID(java.util.UUID) NoteData(io.github.nucleuspowered.nucleus.modules.note.data.NoteData) MutableMessageChannel(org.spongepowered.api.text.channel.MutableMessageChannel)

Example 4 with NoteData

use of io.github.nucleuspowered.nucleus.modules.note.data.NoteData in project Nucleus by NucleusPowered.

the class RemoveNoteCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    NoteArgument.Result result = args.<NoteArgument.Result>getOne(noteKey).get();
    User user = result.user;
    List<NoteData> notes = handler.getNotesInternal(user);
    if (notes.isEmpty()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checkwarnings.none", user.getName()));
        return CommandResult.success();
    }
    if (handler.removeNote(user, result.noteData)) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.removenote.success", user.getName()));
        return CommandResult.success();
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.removenote.failure", user.getName()));
    return CommandResult.empty();
}
Also used : User(org.spongepowered.api.entity.living.player.User) NoteArgument(io.github.nucleuspowered.nucleus.argumentparsers.NoteArgument) NoteData(io.github.nucleuspowered.nucleus.modules.note.data.NoteData)

Example 5 with NoteData

use of io.github.nucleuspowered.nucleus.modules.note.data.NoteData in project Nucleus by NucleusPowered.

the class CheckNotesCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    User user = args.<User>getOne(playerKey).get();
    List<NoteData> notes = handler.getNotesInternal(user);
    if (notes.isEmpty()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.checknotes.none", user.getName()));
        return CommandResult.success();
    }
    List<Text> messages = notes.stream().sorted(Comparator.comparing(NoteData::getDate)).map(x -> createMessage(x, user)).collect(Collectors.toList());
    messages.add(0, plugin.getMessageProvider().getTextMessageWithFormat("command.checknotes.info"));
    PaginationService paginationService = Sponge.getGame().getServiceManager().provideUnchecked(PaginationService.class);
    paginationService.builder().title(Text.builder().color(TextColors.GOLD).append(Text.of(plugin.getMessageProvider().getMessageWithFormat("command.checknotes.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) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) GenericArguments(org.spongepowered.api.command.args.GenericArguments) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) UserStorageService(org.spongepowered.api.service.user.UserStorageService) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) NoteData(io.github.nucleuspowered.nucleus.modules.note.data.NoteData) Util(io.github.nucleuspowered.nucleus.Util) NoteHandler(io.github.nucleuspowered.nucleus.modules.note.handlers.NoteHandler) 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) PaginationService(org.spongepowered.api.service.pagination.PaginationService) 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) 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) PaginationService(org.spongepowered.api.service.pagination.PaginationService) NoteData(io.github.nucleuspowered.nucleus.modules.note.data.NoteData)

Aggregations

NoteData (io.github.nucleuspowered.nucleus.modules.note.data.NoteData)6 User (org.spongepowered.api.entity.living.player.User)6 UserStorageService (org.spongepowered.api.service.user.UserStorageService)3 Util (io.github.nucleuspowered.nucleus.Util)2 RunAsync (io.github.nucleuspowered.nucleus.internal.annotations.RunAsync)2 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)2 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)2 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)2 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)2 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)2 NoteHandler (io.github.nucleuspowered.nucleus.modules.note.handlers.NoteHandler)2 ZoneId (java.time.ZoneId)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Sponge (org.spongepowered.api.Sponge)2 CommandResult (org.spongepowered.api.command.CommandResult)2 CommandSource (org.spongepowered.api.command.CommandSource)2