123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package me.km.api;
- import java.io.File;
- import java.io.IOException;
- import java.nio.charset.Charset;
- import java.nio.charset.MalformedInputException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.Arrays;
- import java.util.List;
- import java.util.TreeMap;
- import java.util.stream.Collectors;
- import me.km.dimensions.ModDimensions;
- import net.minecraft.util.math.Vec3d;
- public class SimpleConfig
- {
- private Module m;
-
- private final TreeMap<String, String> conf;
- private File file;
-
- public SimpleConfig(Module m, String path, boolean load)
- {
- this.m = m;
- file = new File("./" + path + ".snuvi");
- conf = new TreeMap<>();
- if(load && file.exists())
- {
- try
- {
- Files.readAllLines(file.toPath()).stream().forEach(s ->
- {
- int b = s.indexOf("=");
- if(b == -1)
- {
- m.sendWarningToConsole("Wrong syntax in '" + file.getPath() + "' - " + s);
- return;
- }
- conf.put(s.substring(0, b).trim(), s.substring(b + 1).trim());
- });
- }
- catch (MalformedInputException ex)
- {
- m.sendWarningToConsole("'" + file.getPath() + "' contains an illegal character, change file encoding");
- }
- catch (IOException ex)
- {
- m.sendWarningToConsole("File '" + file.getPath() + "' cannot be read");
- }
- }
- }
-
- public static List<File> getFiles(String path)
- {
- return Arrays.asList(new File("./" + path).listFiles());
- }
-
- public boolean exists()
- {
- return file.exists();
- }
-
- public boolean delete()
- {
- return file.delete();
- }
-
- public void save()
- {
- try
- {
- Files.write(Paths.get(file.toURI()), conf.entrySet().stream()
- .map(e -> e.getKey() + "=" + e.getValue())
- .collect(Collectors.toList()), Charset.forName("UTF-8"));
- }
- catch(IOException ex)
- {
- m.sendWarningToConsole("Can't write to '" + file.getPath() + "'");
- }
- }
-
- // -----------------------------------------------------------------------------------
- // Get Data
- // -----------------------------------------------------------------------------------
-
- public String getString(String key)
- {
- return conf.get(key);
- }
-
- public String getString(String key, String error)
- {
- String s = conf.get(key);
- if(s == null)
- {
- return error;
- }
- return s;
- }
-
- public float getFloat(String key, float error)
- {
- String s = conf.get(key);
- if(s == null)
- {
- return error;
- }
- try
- {
- return Float.parseFloat(s);
- }
- catch(NumberFormatException ex)
- {
- return error;
- }
- }
-
- public double getDouble(String key, double error)
- {
- String s = conf.get(key);
- if(s == null)
- {
- return error;
- }
- try
- {
- return Double.parseDouble(s);
- }
- catch(NumberFormatException ex)
- {
- return error;
- }
- }
-
- public Location getLocation(String key)
- {
- return new Location(ModDimensions.getWorldFromName(getString(key + ".world")),
- new Vec3d(getDouble(key + ".x", 0), getDouble(key + ".y", 0), getDouble(key + ".z", 0)),
- getFloat(key + ".yaw", 0), getFloat(key + ".pitch", 0));
- }
-
- // -----------------------------------------------------------------------------------
- // Add Data
- // -----------------------------------------------------------------------------------
-
- public void setString(String key, String value)
- {
- conf.put(key, value);
- }
-
- public void setDouble(String key, double d)
- {
- setString(key, String.valueOf(d));
- }
-
- public void setDouble(String key, float d)
- {
- setString(key, String.valueOf(d));
- }
-
- public void setLocation(String key, Location l)
- {
- setString(key + ".world", l.getWorld().getWorldInfo().getWorldName());
- setDouble(key + ".x", l.getPos().xCoord);
- setDouble(key + ".y", l.getPos().yCoord);
- setDouble(key + ".z", l.getPos().zCoord);
- setDouble(key + ".yaw", l.getYaw());
- setDouble(key + ".pitch", l.getPitch());
- }
- }
|