use of me.lucko.luckperms.common.locale.command.CommandSpecData 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();
}
}
Aggregations