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 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)); } }