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