Search in sources :

Example 31 with CommandSource

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);
}
Also used : TextRepresentable(org.spongepowered.api.text.TextRepresentable) TextStyle(org.spongepowered.api.text.format.TextStyle) Arrays(java.util.Arrays) TextRepresentable(org.spongepowered.api.text.TextRepresentable) NucleusPlugin(io.github.nucleuspowered.nucleus.NucleusPlugin) URL(java.net.URL) TextTemplate(org.spongepowered.api.text.TextTemplate) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) Function(java.util.function.Function) CoreModule(io.github.nucleuspowered.nucleus.modules.core.CoreModule) CoreConfigAdapter(io.github.nucleuspowered.nucleus.modules.core.config.CoreConfigAdapter) Lists(com.google.common.collect.Lists) Matcher(java.util.regex.Matcher) Text(org.spongepowered.api.text.Text) TextColor(org.spongepowered.api.text.format.TextColor) Map(java.util.Map) HoverAction(org.spongepowered.api.text.action.HoverAction) TextColors(org.spongepowered.api.text.format.TextColors) Nullable(javax.annotation.Nullable) TextActions(org.spongepowered.api.text.action.TextActions) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) User(org.spongepowered.api.entity.living.player.User) Iterator(java.util.Iterator) CommandSource(org.spongepowered.api.command.CommandSource) MalformedURLException(java.net.MalformedURLException) TextStyles(org.spongepowered.api.text.format.TextStyles) UUID(java.util.UUID) Maps(com.google.common.collect.Maps) Consumer(java.util.function.Consumer) TextSerializers(org.spongepowered.api.text.serializer.TextSerializers) List(java.util.List) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Tuples(io.github.nucleuspowered.nucleus.util.Tuples) Player(org.spongepowered.api.entity.living.player.Player) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Text(org.spongepowered.api.text.Text) Function(java.util.function.Function) TextStyle(org.spongepowered.api.text.format.TextStyle)

Example 32 with CommandSource

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();
}
Also used : NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) NucleusTextTemplateFactory(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateFactory) CommandResult(org.spongepowered.api.command.CommandResult) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) RemainingStringsArgument(io.github.nucleuspowered.nucleus.argumentparsers.RemainingStringsArgument) AdminConfig(io.github.nucleuspowered.nucleus.modules.admin.config.AdminConfig) CommandSource(org.spongepowered.api.command.CommandSource) AdminConfigAdapter(io.github.nucleuspowered.nucleus.modules.admin.config.AdminConfigAdapter) TextParsingUtils(io.github.nucleuspowered.nucleus.internal.text.TextParsingUtils) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) AdminModule(io.github.nucleuspowered.nucleus.modules.admin.AdminModule) CommandElement(org.spongepowered.api.command.args.CommandElement) GenericArguments(org.spongepowered.api.command.args.GenericArguments) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) NucleusTextTemplateMessageSender(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateMessageSender) BroadcastConfig(io.github.nucleuspowered.nucleus.modules.admin.config.BroadcastConfig) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) NucleusTextTemplateMessageSender(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateMessageSender) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) Text(org.spongepowered.api.text.Text)

Example 33 with CommandSource

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();
}
Also used : NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) CommandSource(org.spongepowered.api.command.CommandSource) NamedLocation(io.github.nucleuspowered.nucleus.api.nucleusdata.NamedLocation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) UUID(java.util.UUID) CommandElement(org.spongepowered.api.command.args.CommandElement) GenericArguments(org.spongepowered.api.command.args.GenericArguments) Collectors(java.util.stream.Collectors) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) JailArgument(io.github.nucleuspowered.nucleus.argumentparsers.JailArgument) Optional(java.util.Optional) Util(io.github.nucleuspowered.nucleus.Util) JailHandler(io.github.nucleuspowered.nucleus.modules.jail.handlers.JailHandler) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) NamedLocation(io.github.nucleuspowered.nucleus.api.nucleusdata.NamedLocation) Text(org.spongepowered.api.text.Text) UUID(java.util.UUID)

Example 34 with CommandSource

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);
}
Also used : Text(org.spongepowered.api.text.Text) CommandSource(org.spongepowered.api.command.CommandSource)

Example 35 with CommandSource

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

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