Search in sources :

Example 21 with CommandSource

use of org.spongepowered.api.command.CommandSource in project SpongeAPI by SpongePowered.

the class GenericArgumentsTest method testGettingBothPlayers.

@Test
public void testGettingBothPlayers() throws Exception {
    CommandArgs args = new CommandArgs("test", Lists.newArrayList(new SingleArg("test", 0, 5)));
    CommandContext context = new CommandContext();
    CommandSource source = Mockito.mock(CommandSource.class);
    getPlayerElement().parse(source, args, context);
    Collection<Player> cpl = context.getAll(Text.of("player"));
    assertEquals(2, cpl.size());
    Collection<String> s = cpl.stream().map(User::getName).collect(Collectors.toList());
    assertTrue(s.contains("test1"));
    assertTrue(s.contains("test2"));
}
Also used : SingleArg(org.spongepowered.api.command.args.parsing.SingleArg) Player(org.spongepowered.api.entity.living.player.Player) CommandSource(org.spongepowered.api.command.CommandSource) Test(org.junit.Test)

Example 22 with CommandSource

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

the class PlayerConsoleArgumentTest method mockSource.

private CommandSource mockSource() {
    CommandSource mock = Mockito.mock(CommandSource.class);
    Mockito.when(mock.hasPermission(Mockito.any())).thenReturn(true);
    return mock;
}
Also used : CommandSource(org.spongepowered.api.command.CommandSource)

Example 23 with CommandSource

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

the class CommandBaseTests method testThatCommandSourcesCannotExecutePlayerCommands.

/**
 * Tests that if a {@link CommandSource} that is not a {@link Player} is provided, the command fails with
 * {@link CommandResult#empty()}.
 *
 * @throws CommandException
 */
@Test(expected = NucleusCommandException.class)
public void testThatCommandSourcesCannotExecutePlayerCommands() throws CommandException {
    PlayerCommand cmd = new PlayerCommand();
    getInjector().injectMembers(cmd);
    cmd.postInit();
    CommandSource mock = getMockCommandSource();
    CommandResult result = cmd.process(mock, "");
}
Also used : CommandSource(org.spongepowered.api.command.CommandSource) CommandResult(org.spongepowered.api.command.CommandResult) Test(org.junit.Test)

Example 24 with CommandSource

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

the class CommandBaseTests method getMockCommandSource.

private CommandSource getMockCommandSource() {
    CommandSource pl = Mockito.mock(CommandSource.class);
    Mockito.when(pl.hasPermission(Matchers.any())).thenReturn(true);
    Mockito.when(pl.hasPermission(Matchers.any(), Matchers.any())).thenReturn(true);
    Mockito.when(pl.getPermissionValue(Matchers.any(), Matchers.any())).thenReturn(Tristate.TRUE);
    return pl;
}
Also used : CommandSource(org.spongepowered.api.command.CommandSource)

Example 25 with CommandSource

use of org.spongepowered.api.command.CommandSource 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)

Aggregations

CommandSource (org.spongepowered.api.command.CommandSource)93 Text (org.spongepowered.api.text.Text)61 CommandResult (org.spongepowered.api.command.CommandResult)49 List (java.util.List)47 CommandContext (org.spongepowered.api.command.args.CommandContext)45 Collectors (java.util.stream.Collectors)39 Optional (java.util.Optional)37 Sponge (org.spongepowered.api.Sponge)37 Player (org.spongepowered.api.entity.living.player.Player)36 TextColors (org.spongepowered.api.text.format.TextColors)30 CommandElement (org.spongepowered.api.command.args.CommandElement)27 TextActions (org.spongepowered.api.text.action.TextActions)27 GenericArguments (org.spongepowered.api.command.args.GenericArguments)25 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)25 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)24 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)23 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)23 Util (io.github.nucleuspowered.nucleus.Util)20 Nullable (javax.annotation.Nullable)20 World (org.spongepowered.api.world.World)20