Search in sources :

Example 6 with Message

use of me.lucko.luckperms.common.locale.message.Message in project LuckPerms by lucko.

the class SimpleLocaleManager method loadFromFile.

@Override
@SuppressWarnings("unchecked")
public void loadFromFile(File file) throws Exception {
    try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
        ImmutableMap.Builder<Message, String> messages = ImmutableMap.builder();
        ImmutableMap.Builder<CommandSpec, CommandSpecData> commands = ImmutableMap.builder();
        Map<String, Object> data = (Map<String, Object>) new Yaml().load(reader);
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            if (entry.getKey() == null || entry.getKey().isEmpty() || entry.getValue() == null) {
                continue;
            }
            // might be a message
            if (entry.getValue() instanceof String) {
                String key = entry.getKey().toUpperCase().replace('-', '_');
                String value = (String) entry.getValue();
                try {
                    messages.put(Message.valueOf(key), value);
                } catch (IllegalArgumentException e) {
                // ignore
                }
            }
            // might be the entries for command specifications - take care for malformed entries of differing types.
            if (entry.getKey().equals("command-specs") && entry.getValue() instanceof Map) {
                Map<?, ?> commandKeys = (Map) entry.getValue();
                // key is the command id, value is a map of the commands attributes
                for (Map.Entry commandKey : commandKeys.entrySet()) {
                    // just try catch, can't be bothered with safe casting every single value.
                    try {
                        String id = (String) commandKey.getKey();
                        Map<String, Object> attributes = (Map<String, Object>) commandKey.getValue();
                        CommandSpec spec = CommandSpec.valueOf(id.toUpperCase().replace('-', '_'));
                        String description = (String) attributes.get("description");
                        String usage = (String) attributes.get("usage");
                        Map<String, String> args = (Map<String, String>) attributes.get("args");
                        if (args != null && args.isEmpty()) {
                            args = null;
                        }
                        CommandSpecData specData = new CommandSpecData(description, usage, args == null ? null : ImmutableMap.copyOf(args));
                        commands.put(spec, specData);
                    } catch (IllegalArgumentException e) {
                    // ignore
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        this.messages = messages.build();
        this.commands = commands.build();
    }
}
Also used : Message(me.lucko.luckperms.common.locale.message.Message) ImmutableMap(com.google.common.collect.ImmutableMap) CommandSpecData(me.lucko.luckperms.common.locale.command.CommandSpecData) Yaml(org.yaml.snakeyaml.Yaml) CommandSpec(me.lucko.luckperms.common.locale.command.CommandSpec) BufferedReader(java.io.BufferedReader) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 7 with Message

use of me.lucko.luckperms.common.locale.message.Message in project LuckPerms by lucko.

the class VerboseListener method sendNotification.

private void sendNotification(CheckData data) {
    String msg = "&a" + data.getCheckTarget() + "&7 - &a" + data.getPermission() + "&7 - " + getTristateColor(data.getResult()) + data.getResult().name().toLowerCase();
    if (this.notifiedSender.isConsole()) {
        // just send as a raw message
        Message.VERBOSE_LOG.send(this.notifiedSender, msg);
        return;
    }
    // form a hoverevent from the check trace
    TextComponent textComponent = TextUtils.fromLegacy(Message.VERBOSE_LOG.asString(this.notifiedSender.getPlatform().getLocaleManager(), msg));
    // build the text
    List<String> hover = new ArrayList<>();
    hover.add("&bOrigin: &2" + data.getCheckOrigin().name());
    hover.add("&bContext: &r" + MessageUtils.contextSetToString(data.getCheckContext()));
    hover.add("&bTrace: &r");
    Consumer<StackTraceElement> printer = StackTracePrinter.elementToString(str -> hover.add("&7" + str));
    int overflow;
    if (data.getCheckOrigin() == CheckOrigin.API || data.getCheckOrigin() == CheckOrigin.INTERNAL) {
        overflow = CHAT_UNFILTERED_PRINTER.process(data.getCheckTrace(), printer);
    } else {
        overflow = CHAT_FILTERED_PRINTER.process(data.getCheckTrace(), printer);
    }
    if (overflow != 0) {
        hover.add("&f... and " + overflow + " more");
    }
    // send the message
    HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextUtils.fromLegacy(TextUtils.joinNewline(hover.stream()), CommandManager.AMPERSAND_CHAR));
    TextComponent text = textComponent.toBuilder().applyDeep(comp -> comp.hoverEvent(hoverEvent)).build();
    this.notifiedSender.sendMessage(text);
}
Also used : TextComponent(net.kyori.text.TextComponent) JsonObject(com.google.gson.JsonObject) CommandManager(me.lucko.luckperms.common.command.CommandManager) Date(java.util.Date) TextUtils(me.lucko.luckperms.common.utils.TextUtils) Tristate(me.lucko.luckperms.api.Tristate) MessageUtils(me.lucko.luckperms.common.command.utils.MessageUtils) StackTracePrinter(me.lucko.luckperms.common.utils.StackTracePrinter) SimpleDateFormat(java.text.SimpleDateFormat) Sender(me.lucko.luckperms.common.sender.Sender) TextComponent(net.kyori.text.TextComponent) HoverEvent(net.kyori.text.event.HoverEvent) JArray(me.lucko.luckperms.common.utils.gson.JArray) StandardPastebin(me.lucko.luckperms.common.web.StandardPastebin) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) DateUtil(me.lucko.luckperms.common.utils.DateUtil) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Message(me.lucko.luckperms.common.locale.message.Message) JObject(me.lucko.luckperms.common.utils.gson.JObject) HoverEvent(net.kyori.text.event.HoverEvent) ArrayList(java.util.ArrayList)

Aggregations

Message (me.lucko.luckperms.common.locale.message.Message)7 List (java.util.List)5 CommandSpec (me.lucko.luckperms.common.locale.command.CommandSpec)5 Sender (me.lucko.luckperms.common.sender.Sender)5 TextComponent (net.kyori.text.TextComponent)5 HoverEvent (net.kyori.text.event.HoverEvent)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 CommandResult (me.lucko.luckperms.common.command.CommandResult)4 CommandPermission (me.lucko.luckperms.common.command.access.CommandPermission)4 LocaleManager (me.lucko.luckperms.common.locale.LocaleManager)4 LuckPermsPlugin (me.lucko.luckperms.common.plugin.LuckPermsPlugin)4 Predicates (me.lucko.luckperms.common.utils.Predicates)4 ClickEvent (net.kyori.text.event.ClickEvent)4 UUID (java.util.UUID)3 Consumer (java.util.function.Consumer)3 Collectors (java.util.stream.Collectors)3 Node (me.lucko.luckperms.api.Node)3 CommandManager (me.lucko.luckperms.common.command.CommandManager)3 SingleCommand (me.lucko.luckperms.common.command.abstraction.SingleCommand)3