Search in sources :

Example 1 with CommandSpec

use of me.lucko.luckperms.common.locale.command.CommandSpec 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)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 BufferedReader (java.io.BufferedReader)1 Map (java.util.Map)1 CommandSpec (me.lucko.luckperms.common.locale.command.CommandSpec)1 CommandSpecData (me.lucko.luckperms.common.locale.command.CommandSpecData)1 Message (me.lucko.luckperms.common.locale.message.Message)1 Yaml (org.yaml.snakeyaml.Yaml)1