FastAsyncWorldedit/core/src/main/java/com/boydti/fawe/config/Commands.java

140 lines
4.0 KiB
Java
Raw Normal View History

package com.boydti.fawe.config;
import com.boydti.fawe.configuration.ConfigurationSection;
import com.boydti.fawe.configuration.file.YamlConfiguration;
import com.sk89q.minecraft.util.commands.Command;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
2017-03-26 20:14:28 +02:00
import java.util.List;
import java.util.Map;
public class Commands {
private static YamlConfiguration cmdConfig;
private static File cmdFile;
public static void load(File file) {
cmdFile = file;
try {
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
cmdConfig = YamlConfiguration.loadConfiguration(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Command translate(final Command command) {
2016-08-29 06:29:36 +02:00
if (cmdConfig == null || command instanceof TranslatedCommand) {
return command;
}
2016-08-29 06:29:36 +02:00
return new TranslatedCommand(command);
}
2017-03-26 20:14:28 +02:00
public static String getAlias(String command) {
if (cmdConfig == null) {
return command;
}
ConfigurationSection commands = cmdConfig.getConfigurationSection(command);
List<String> aliases = commands.getStringList("aliases");
return (aliases == null || aliases.isEmpty()) ? command : aliases.get(0);
}
2016-08-29 06:29:36 +02:00
public static class TranslatedCommand implements Command {
private final String[] aliases;
private final String usage;
private final String desc;
private final String help;
private final Command command;
public TranslatedCommand(Command command) {
String id = command.aliases()[0];
ConfigurationSection commands = cmdConfig.getConfigurationSection(id);
boolean set = false;
if (commands == null) {
set = (commands = cmdConfig.createSection(id)) != null;
}
2016-08-29 06:29:36 +02:00
HashMap<String, Object> options = new HashMap<>();
options.put("aliases", new ArrayList<String>(Arrays.asList(command.aliases())));
options.put("usage", command.usage());
options.put("desc", command.desc());
options.put("help", command.help());
for (Map.Entry<String, Object> entry : options.entrySet()) {
String key = entry.getKey();
if (!commands.contains(key)) {
commands.set(key, entry.getValue());
set = true;
}
}
2016-08-29 06:29:36 +02:00
if (set) {
try {
cmdConfig.save(cmdFile);
} catch (IOException e) {
e.printStackTrace();
}
}
2016-08-29 06:29:36 +02:00
this.aliases = commands.getStringList("aliases").toArray(new String[0]);
this.usage = commands.getString("usage");
this.desc = commands.getString("desc");
this.help = commands.getString("help");
this.command = command;
}
2016-08-29 06:29:36 +02:00
@Override
public Class<? extends Annotation> annotationType() {
return command.annotationType();
}
2016-08-29 06:29:36 +02:00
@Override
public String[] aliases() {
return aliases;
}
2016-08-29 06:29:36 +02:00
@Override
public String usage() {
return usage;
}
2016-08-29 06:29:36 +02:00
@Override
public String desc() {
return desc;
}
2016-08-29 06:29:36 +02:00
@Override
public int min() {
return command.min();
}
2016-08-29 06:29:36 +02:00
@Override
public int max() {
return command.max();
}
2016-08-29 06:29:36 +02:00
@Override
public String flags() {
return command.flags();
}
2016-08-29 06:29:36 +02:00
@Override
public String help() {
return help;
}
@Override
public boolean anyFlags() {
return command.anyFlags();
}
}
2016-12-26 08:42:33 +01:00
public static Class<Commands> inject() {
return Commands.class;
}
}