Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import dan200.computer.core.Computer;
- import dan200.computer.core.IMountedFileNormal;
- import dan200.computer.shared.TileEntityComputer;
- import reghzy.api.utils.text.RZFormats;
- import reghzy.ccapi.BaseAPI;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- public class SettingsAPI extends BaseAPI {
- private final HashMap<String, MappedValue> values = new HashMap<String, MappedValue>();
- public SettingsAPI(TileEntityComputer tile, Computer computer) {
- super(tile, computer, "settings");
- }
- @Override
- public void startup() {
- }
- @Override
- public void shutdown() {
- }
- public void load(String[] lines) {
- }
- public static class MappedValue {
- public final String key;
- public final String value;
- public final ArrayList<String> description;
- public MappedValue(String key, String value, ArrayList<String> description) {
- this.key = key;
- this.value = value;
- this.description = description;
- }
- }
- static {
- registerCommand(SettingsAPI.class, "load", (api, tile, computer, context, args, rawArgs) -> {
- api.values.clear();
- String path = args.getString(0, "settings");
- if (!computer.getAPIEnvironment().getFileSystem().exists(path)) {
- try {
- computer.getAPIEnvironment().getFileSystem().openForWrite(path, false).close();
- }
- catch (Throwable e) {
- return new Object[] {false};
- }
- }
- IMountedFileNormal file = computer.getAPIEnvironment().getFileSystem().openForRead(path);
- ArrayList<String> description = new ArrayList<String>();
- try {
- for (String line = file.readLine(); line != null; line = file.readLine()) {
- line = line.trim();
- if (line.startsWith("#")) {
- description.add(line.substring(1).trim());
- }
- else {
- int split = line.indexOf(':');
- if (split != -1) {
- String key = line.substring(0, split).trim();
- if (key.isEmpty()) {
- continue;
- }
- String value = line.substring(split + 1).trim();
- MappedValue mappedValue = new MappedValue(key, value, description);
- description = new ArrayList<String>();
- api.values.put(key, mappedValue);
- }
- }
- }
- return new Object[]{true};
- }
- catch (Throwable e) {
- return new Object[]{false};
- }
- finally {
- file.close();
- }
- });
- registerCommand(SettingsAPI.class, "save", (api, tile, computer, context, args, rawArgs) -> {
- String path = args.getString(0, "settings");
- IMountedFileNormal file = computer.getAPIEnvironment().getFileSystem().openForWrite(path, false);
- try {
- for(MappedValue value : api.values.values()) {
- for(String line : value.description) {
- writeLine(file, RZFormats.format("# {1}", line), true);
- }
- writeLine(file, RZFormats.format("{0}: {1}", value.key, value.value), true);
- }
- return new Object[]{true};
- }
- catch (Throwable e) {
- return new Object[] {false};
- }
- finally {
- file.close();
- }
- });
- registerCommand(SettingsAPI.class, "set", (api, tile, computer, context, args, rawArgs) -> {
- String key = args.assertGetString(0, "Arg0 must be the property's key/name");
- String value = args.assertGetString(1, "Arg1 must be the property's value");
- ArrayList<String> description = new ArrayList<String>();
- for(int i = 2; i < rawArgs.length; i++) {
- if (rawArgs[i] != null) {
- description.add(rawArgs[i].toString());
- }
- }
- MappedValue mappedValue = new MappedValue(key, value, description);
- api.values.put(key, mappedValue);
- return new Object[0];
- });
- registerCommand(SettingsAPI.class, "unset", (api, tile, computer, context, args, rawArgs) -> {
- return new Object[] {api.values.remove(args.assertGetString(0, "Arg0 must be the property's key/name"))};
- });
- registerCommand(SettingsAPI.class, "get", (api, tile, computer, context, args, rawArgs) -> {
- String key = args.assertGetString(0, "Arg0 must be the property's key/name");
- MappedValue mappedValue = api.values.get(key);
- return new Object[]{mappedValue == null ? null : mappedValue.value};
- });
- registerCommand(SettingsAPI.class, "getDetails", (api, tile, computer, context, args, rawArgs) -> {
- String key = args.assertGetString(0, "Arg0 must be the property's key/name");
- MappedValue mappedValue = api.values.get(key);
- if (mappedValue == null) {
- return null;
- }
- else {
- return mappedValue.description.toArray();
- }
- });
- registerCommand(SettingsAPI.class, "clear", (api, tile, computer, context, args, rawArgs) -> {
- int size = api.values.size();
- api.values.clear();
- return new Object[] {size};
- });
- registerCommand(SettingsAPI.class, "getNames", (api, tile, computer, context, args, rawArgs) -> api.values.keySet().toArray());
- registerCommand(SettingsAPI.class, "getValues", (api, tile, computer, context, args, rawArgs) -> api.values.values().toArray());
- registerCommand(SettingsAPI.class, "getEntries", (api, tile, computer, context, args, rawArgs) -> {
- ArrayList<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
- for(Map.Entry<String, MappedValue> entry : api.values.entrySet()) {
- HashMap<String, String> map = new HashMap<String, String>();
- map.put(entry.getKey(), entry.getValue().value);
- mapList.add(map);
- }
- return mapList.toArray();
- });
- }
- public static void writeLine(IMountedFileNormal mount, String value, boolean newLine) throws IOException {
- mount.write(value, 0, value.length(), newLine);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement