Advertisement
bobmarley12345

thingy api settings computercraft

Jun 23rd, 2022
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.80 KB | None | 0 0
  1. import dan200.computer.core.Computer;
  2. import dan200.computer.core.IMountedFileNormal;
  3. import dan200.computer.shared.TileEntityComputer;
  4. import reghzy.api.utils.text.RZFormats;
  5. import reghzy.ccapi.BaseAPI;
  6.  
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11.  
  12. public class SettingsAPI extends BaseAPI {
  13.     private final HashMap<String, MappedValue> values = new HashMap<String, MappedValue>();
  14.  
  15.     public SettingsAPI(TileEntityComputer tile, Computer computer) {
  16.         super(tile, computer, "settings");
  17.     }
  18.  
  19.     @Override
  20.     public void startup() {
  21.  
  22.     }
  23.  
  24.     @Override
  25.     public void shutdown() {
  26.  
  27.     }
  28.  
  29.     public void load(String[] lines) {
  30.  
  31.     }
  32.  
  33.     public static class MappedValue {
  34.         public final String key;
  35.         public final String value;
  36.         public final ArrayList<String> description;
  37.  
  38.         public MappedValue(String key, String value, ArrayList<String> description) {
  39.             this.key = key;
  40.             this.value = value;
  41.             this.description = description;
  42.         }
  43.     }
  44.  
  45.     static {
  46.         registerCommand(SettingsAPI.class, "load", (api, tile, computer, context, args, rawArgs) -> {
  47.             api.values.clear();
  48.             String path = args.getString(0, "settings");
  49.             if (!computer.getAPIEnvironment().getFileSystem().exists(path)) {
  50.                 try {
  51.                     computer.getAPIEnvironment().getFileSystem().openForWrite(path, false).close();
  52.                 }
  53.                 catch (Throwable e) {
  54.                     return new Object[] {false};
  55.                 }
  56.             }
  57.  
  58.             IMountedFileNormal file = computer.getAPIEnvironment().getFileSystem().openForRead(path);
  59.             ArrayList<String> description = new ArrayList<String>();
  60.             try {
  61.                 for (String line = file.readLine(); line != null; line = file.readLine()) {
  62.                     line = line.trim();
  63.                     if (line.startsWith("#")) {
  64.                         description.add(line.substring(1).trim());
  65.                     }
  66.                     else {
  67.                         int split = line.indexOf(':');
  68.                         if (split != -1) {
  69.                             String key = line.substring(0, split).trim();
  70.                             if (key.isEmpty()) {
  71.                                 continue;
  72.                             }
  73.  
  74.                             String value = line.substring(split + 1).trim();
  75.                             MappedValue mappedValue = new MappedValue(key, value, description);
  76.                             description = new ArrayList<String>();
  77.                             api.values.put(key, mappedValue);
  78.                         }
  79.                     }
  80.                 }
  81.  
  82.                 return new Object[]{true};
  83.             }
  84.             catch (Throwable e) {
  85.                 return new Object[]{false};
  86.             }
  87.             finally {
  88.                 file.close();
  89.             }
  90.         });
  91.  
  92.         registerCommand(SettingsAPI.class, "save", (api, tile, computer, context, args, rawArgs) -> {
  93.             String path = args.getString(0, "settings");
  94.             IMountedFileNormal file = computer.getAPIEnvironment().getFileSystem().openForWrite(path, false);
  95.             try {
  96.                 for(MappedValue value : api.values.values()) {
  97.                     for(String line : value.description) {
  98.                         writeLine(file, RZFormats.format("# {1}", line), true);
  99.                     }
  100.  
  101.                     writeLine(file, RZFormats.format("{0}: {1}", value.key, value.value), true);
  102.                 }
  103.  
  104.                 return new Object[]{true};
  105.             }
  106.             catch (Throwable e) {
  107.                 return new Object[] {false};
  108.             }
  109.             finally {
  110.                 file.close();
  111.             }
  112.         });
  113.  
  114.         registerCommand(SettingsAPI.class, "set", (api, tile, computer, context, args, rawArgs) -> {
  115.             String key = args.assertGetString(0, "Arg0 must be the property's key/name");
  116.             String value = args.assertGetString(1, "Arg1 must be the property's value");
  117.             ArrayList<String> description = new ArrayList<String>();
  118.             for(int i = 2; i < rawArgs.length; i++) {
  119.                 if (rawArgs[i] != null) {
  120.                     description.add(rawArgs[i].toString());
  121.                 }
  122.             }
  123.  
  124.             MappedValue mappedValue = new MappedValue(key, value, description);
  125.             api.values.put(key, mappedValue);
  126.             return new Object[0];
  127.         });
  128.  
  129.         registerCommand(SettingsAPI.class, "unset", (api, tile, computer, context, args, rawArgs) -> {
  130.             return new Object[] {api.values.remove(args.assertGetString(0, "Arg0 must be the property's key/name"))};
  131.         });
  132.  
  133.         registerCommand(SettingsAPI.class, "get", (api, tile, computer, context, args, rawArgs) -> {
  134.             String key = args.assertGetString(0, "Arg0 must be the property's key/name");
  135.             MappedValue mappedValue = api.values.get(key);
  136.             return new Object[]{mappedValue == null ? null : mappedValue.value};
  137.         });
  138.  
  139.         registerCommand(SettingsAPI.class, "getDetails", (api, tile, computer, context, args, rawArgs) -> {
  140.             String key = args.assertGetString(0, "Arg0 must be the property's key/name");
  141.             MappedValue mappedValue = api.values.get(key);
  142.             if (mappedValue == null) {
  143.                 return null;
  144.             }
  145.             else {
  146.                 return mappedValue.description.toArray();
  147.             }
  148.         });
  149.  
  150.         registerCommand(SettingsAPI.class, "clear", (api, tile, computer, context, args, rawArgs) -> {
  151.             int size = api.values.size();
  152.             api.values.clear();
  153.             return new Object[] {size};
  154.         });
  155.  
  156.         registerCommand(SettingsAPI.class, "getNames", (api, tile, computer, context, args, rawArgs) -> api.values.keySet().toArray());
  157.         registerCommand(SettingsAPI.class, "getValues", (api, tile, computer, context, args, rawArgs) -> api.values.values().toArray());
  158.         registerCommand(SettingsAPI.class, "getEntries", (api, tile, computer, context, args, rawArgs) -> {
  159.             ArrayList<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
  160.             for(Map.Entry<String, MappedValue> entry : api.values.entrySet()) {
  161.                 HashMap<String, String> map = new HashMap<String, String>();
  162.                 map.put(entry.getKey(), entry.getValue().value);
  163.                 mapList.add(map);
  164.             }
  165.  
  166.             return mapList.toArray();
  167.         });
  168.     }
  169.  
  170.     public static void writeLine(IMountedFileNormal mount, String value, boolean newLine) throws IOException {
  171.         mount.write(value, 0, value.length(), newLine);
  172.     }
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement