123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- 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.TreeMap;
- import java.util.stream.Collectors;
- import me.km.dimensions.ModDimensions;
- import me.km.exception.IllegalItemStackStringException;
- import me.km.utils.ItemStackUtils;
- import net.minecraft.item.ItemStack;
- 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");
- }
- }
-
- //System.out.println("WUSI");
- //conf.forEach((k, v) -> System.out.println(k + " " + v));
- }
-
- public static File[] getFiles(String path)
- {
- return new File("./" + path).listFiles();
- }
-
- public boolean exists()
- {
- return file.exists();
- }
-
- public boolean delete()
- {
- return file.delete();
- }
-
- public boolean save()
- {
- try
- {
- if(!file.getParentFile().exists())
- {
- file.getParentFile().mkdirs();
- }
- if(!file.exists())
- {
- file.createNewFile();
- }
- Files.write(Paths.get(file.toURI()), conf.entrySet().stream()
- .map(e -> e.getKey() + "=" + e.getValue())
- .collect(Collectors.toList()), Charset.forName("UTF-8"));
- return true;
- }
- catch(IOException ex)
- {
- m.sendWarningToConsole("Can't write to '" + file.getPath() + "'");
- ex.printStackTrace();
- return false;
- }
- }
-
- // -----------------------------------------------------------------------------------
- // 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 int getInt(String key, int error)
- {
- String s = conf.get(key);
- if(s == null)
- {
- return error;
- }
- try
- {
- return Integer.parseInt(s);
- }
- catch(NumberFormatException ex)
- {
- return error;
- }
- }
-
- public boolean getBoolean(String key, boolean error)
- {
- String s = conf.get(key);
- if(s == null)
- {
- return error;
- }
- try
- {
- return Boolean.valueOf(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));
- }
-
- public ItemStack getItemStack(String key)
- {
- String s = conf.get(key);
- if(s == null)
- {
- return ItemStack.EMPTY;
- }
- try
- {
- return ItemStackUtils.getStackFromNbtString(s);
- }
- catch(IllegalItemStackStringException ex)
- {
- return ItemStack.EMPTY;
- }
- }
-
- // -----------------------------------------------------------------------------------
- // 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 setFloat(String key, float d)
- {
- setString(key, String.valueOf(d));
- }
-
- public void setInt(String key, int i)
- {
- setString(key, String.valueOf(i));
- }
-
- public void setBoolean(String key, boolean b)
- {
- setString(key, String.valueOf(b));
- }
-
- 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());
- }
-
- public void setItemStack(String key, ItemStack stack)
- {
- setString(key, ItemStackUtils.getNbtString(stack));
- }
- }
|