use of org.spongepowered.api.command.CommandSource in project Nucleus by NucleusPowered.
the class TextParsingUtils method createTextTemplateFragmentWithLinks.
public Tuples.NullableTuple<List<TextRepresentable>, Map<String, Function<CommandSource, Text>>> createTextTemplateFragmentWithLinks(String message) {
Preconditions.checkNotNull(message, "message");
if (message.isEmpty()) {
return new Tuples.NullableTuple<>(Lists.newArrayList(Text.EMPTY), null);
}
Matcher m = enhancedUrlParser.matcher(message);
if (!m.find()) {
return new Tuples.NullableTuple<>(Lists.newArrayList(oldLegacy(message)), null);
}
Map<String, Function<CommandSource, Text>> args = Maps.newHashMap();
List<TextRepresentable> texts = Lists.newArrayList();
String remaining = message;
StyleTuple st = TextParsingUtils.EMPTY;
do {
// We found a URL. We split on the URL that we have.
String[] textArray = remaining.split(enhancedUrlParser.pattern(), 2);
TextRepresentable first = Text.builder().color(st.colour).style(st.style).append(oldLegacy(textArray[0])).build();
// Add this text to the list regardless.
texts.add(first);
// If we have more to do, shove it into the "remaining" variable.
if (textArray.length == 2) {
remaining = textArray[1];
} else {
remaining = null;
}
// Get the last colour & styles
String colourMatch = m.group("colour");
if (colourMatch != null && !colourMatch.isEmpty()) {
// If there is a reset, explicitly do it.
TextStyle reset = TextStyles.NONE;
if (m.group("reset") != null) {
reset = TextStyles.RESET;
}
first = Text.of(reset, oldLegacy(m.group("colour")));
}
st = getLastColourAndStyle(first, st);
// Build the URL
String whiteSpace = m.group("first");
if (m.group("url") != null) {
String url = m.group("url");
texts.add(getTextForUrl(url, url, whiteSpace, st, m.group("options")));
} else if (m.group("specialUrl") != null) {
String url = m.group("sUrl");
String msg = m.group("msg");
texts.add(getTextForUrl(url, msg, whiteSpace, st, m.group("optionssurl")));
} else {
// Must be commands.
String cmd = m.group("sCmd");
String msg = m.group("sMsg");
String optionList = m.group("optionsscmd");
if (cmd.contains("{{subject}}")) {
String arg = UUID.randomUUID().toString();
args.put(arg, cs -> {
String command = cmd.replace("{{subject}}", cs.getName());
return getCmd(msg, command, optionList, whiteSpace);
});
texts.add(TextTemplate.arg(arg).color(st.colour).style(st.style).build());
} else {
texts.add(Text.of(st.colour, st.style, getCmd(msg, cmd, optionList, whiteSpace)));
}
}
} while (remaining != null && m.find());
// Add the last bit.
if (remaining != null) {
Text.Builder tb = Text.builder().color(st.colour).style(st.style).append(TextSerializers.FORMATTING_CODE.deserialize(remaining));
if (remaining.matches("^\\s+&r.*")) {
tb.style(TextStyles.RESET);
}
texts.add(tb.build());
}
// Return the list.
return new Tuples.NullableTuple<>(texts, args);
}
use of org.spongepowered.api.command.CommandSource in project Nucleus by NucleusPowered.
the class BroadcastCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
String m = args.<String>getOne(message).get();
NucleusTextTemplate textTemplate = NucleusTextTemplateFactory.createFromAmpersandString(m);
Text p = bc.getPrefix().getForCommandSource(src);
Text s = bc.getSuffix().getForCommandSource(src);
new NucleusTextTemplateMessageSender(textTemplate, src, t -> TextParsingUtils.joinTextsWithColoursFlowing(p, t, s)).send();
return CommandResult.success();
}
use of org.spongepowered.api.command.CommandSource in project Nucleus by NucleusPowered.
the class CheckJailedCommand method executeCommand.
@Override
protected CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
// Using the cache, tell us who is jailed.
MessageProvider provider = plugin.getMessageProvider();
Optional<NamedLocation> jail = args.getOne(jailNameKey);
List<UUID> usersInJail = jail.map(x -> plugin.getUserCacheService().getJailedIn(x.getName())).orElseGet(() -> plugin.getUserCacheService().getJailed());
String jailName = jail.map(NamedLocation::getName).orElseGet(() -> provider.getMessageWithFormat("standard.alljails"));
if (usersInJail.isEmpty()) {
src.sendMessage(provider.getTextMessageWithFormat("command.checkjailed.none", jailName));
return CommandResult.success();
}
// Get the users in this jail, or all jails
Util.getPaginationBuilder(src).title(provider.getTextMessageWithFormat("command.checkjailed.header", jailName)).contents(usersInJail.stream().map(x -> {
Text name = plugin.getNameUtil().getName(x).orElseGet(() -> Text.of("unknown: ", x.toString()));
return name.toBuilder().onHover(TextActions.showText(provider.getTextMessageWithFormat("command.checkjailed.hover"))).onClick(TextActions.runCommand("/nucleus:checkjail " + x.toString())).build();
}).collect(Collectors.toList())).sendTo(src);
return CommandResult.success();
}
use of org.spongepowered.api.command.CommandSource in project Nucleus by NucleusPowered.
the class StaffChatMessageChannel method send.
@Override
public void send(@Nullable Object sender, Text original, ChatType type) {
CommandSource source;
if (sender == null || !(sender instanceof CommandSource)) {
if (sender instanceof String) {
source = new NamedSource((String) sender);
} else {
source = Sponge.getServer().getConsole();
}
} else {
source = (CommandSource) sender;
}
Text prefix = template.getForCommandSource(source);
NucleusChatChannel.StaffChat.super.send(sender, Text.of(prefix, colour, original), type);
}
use of org.spongepowered.api.command.CommandSource 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();
}
Aggregations