SimpleConfig.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package me.km.api;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.charset.Charset;
  5. import java.nio.charset.MalformedInputException;
  6. import java.nio.file.Files;
  7. import java.nio.file.Paths;
  8. import java.util.Arrays;
  9. import java.util.List;
  10. import java.util.TreeMap;
  11. import java.util.stream.Collectors;
  12. import me.km.dimensions.ModDimensions;
  13. import net.minecraft.util.math.Vec3d;
  14. public class SimpleConfig
  15. {
  16. private Module m;
  17. private final TreeMap<String, String> conf;
  18. private File file;
  19. public SimpleConfig(Module m, String path, boolean load)
  20. {
  21. this.m = m;
  22. file = new File("./" + path + ".snuvi");
  23. conf = new TreeMap<>();
  24. if(load && file.exists())
  25. {
  26. try
  27. {
  28. Files.readAllLines(file.toPath()).stream().forEach(s ->
  29. {
  30. int b = s.indexOf("=");
  31. if(b == -1)
  32. {
  33. m.sendWarningToConsole("Wrong syntax in '" + file.getPath() + "' - " + s);
  34. return;
  35. }
  36. conf.put(s.substring(0, b).trim(), s.substring(b + 1).trim());
  37. });
  38. }
  39. catch (MalformedInputException ex)
  40. {
  41. m.sendWarningToConsole("'" + file.getPath() + "' contains an illegal character, change file encoding");
  42. }
  43. catch (IOException ex)
  44. {
  45. m.sendWarningToConsole("File '" + file.getPath() + "' cannot be read");
  46. }
  47. }
  48. }
  49. public static List<File> getFiles(String path)
  50. {
  51. return Arrays.asList(new File("./" + path).listFiles());
  52. }
  53. public boolean exists()
  54. {
  55. return file.exists();
  56. }
  57. public boolean delete()
  58. {
  59. return file.delete();
  60. }
  61. public void save()
  62. {
  63. try
  64. {
  65. Files.write(Paths.get(file.toURI()), conf.entrySet().stream()
  66. .map(e -> e.getKey() + "=" + e.getValue())
  67. .collect(Collectors.toList()), Charset.forName("UTF-8"));
  68. }
  69. catch(IOException ex)
  70. {
  71. m.sendWarningToConsole("Can't write to '" + file.getPath() + "'");
  72. }
  73. }
  74. // -----------------------------------------------------------------------------------
  75. // Get Data
  76. // -----------------------------------------------------------------------------------
  77. public String getString(String key)
  78. {
  79. return conf.get(key);
  80. }
  81. public String getString(String key, String error)
  82. {
  83. String s = conf.get(key);
  84. if(s == null)
  85. {
  86. return error;
  87. }
  88. return s;
  89. }
  90. public float getFloat(String key, float error)
  91. {
  92. String s = conf.get(key);
  93. if(s == null)
  94. {
  95. return error;
  96. }
  97. try
  98. {
  99. return Float.parseFloat(s);
  100. }
  101. catch(NumberFormatException ex)
  102. {
  103. return error;
  104. }
  105. }
  106. public double getDouble(String key, double error)
  107. {
  108. String s = conf.get(key);
  109. if(s == null)
  110. {
  111. return error;
  112. }
  113. try
  114. {
  115. return Double.parseDouble(s);
  116. }
  117. catch(NumberFormatException ex)
  118. {
  119. return error;
  120. }
  121. }
  122. public Location getLocation(String key)
  123. {
  124. return new Location(ModDimensions.getWorldFromName(getString(key + ".world")),
  125. new Vec3d(getDouble(key + ".x", 0), getDouble(key + ".y", 0), getDouble(key + ".z", 0)),
  126. getFloat(key + ".yaw", 0), getFloat(key + ".pitch", 0));
  127. }
  128. // -----------------------------------------------------------------------------------
  129. // Add Data
  130. // -----------------------------------------------------------------------------------
  131. public void setString(String key, String value)
  132. {
  133. conf.put(key, value);
  134. }
  135. public void setDouble(String key, double d)
  136. {
  137. setString(key, String.valueOf(d));
  138. }
  139. public void setDouble(String key, float d)
  140. {
  141. setString(key, String.valueOf(d));
  142. }
  143. public void setLocation(String key, Location l)
  144. {
  145. setString(key + ".world", l.getWorld().getWorldInfo().getWorldName());
  146. setDouble(key + ".x", l.getPos().xCoord);
  147. setDouble(key + ".y", l.getPos().yCoord);
  148. setDouble(key + ".z", l.getPos().zCoord);
  149. setDouble(key + ".yaw", l.getYaw());
  150. setDouble(key + ".pitch", l.getPitch());
  151. }
  152. }