SimpleConfig.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.TreeMap;
  9. import java.util.stream.Collectors;
  10. import me.km.dimensions.ModDimensions;
  11. import me.km.exception.IllegalItemStackStringException;
  12. import me.km.utils.ItemStackUtils;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.util.math.Vec3d;
  15. public class SimpleConfig
  16. {
  17. private Module m;
  18. private final TreeMap<String, String> conf;
  19. private File file;
  20. public SimpleConfig(Module m, String path, boolean load)
  21. {
  22. this.m = m;
  23. file = new File("./" + path + ".snuvi");
  24. conf = new TreeMap<>();
  25. if(load && file.exists())
  26. {
  27. try
  28. {
  29. Files.readAllLines(file.toPath()).stream().forEach(s ->
  30. {
  31. int b = s.indexOf("=");
  32. if(b == -1)
  33. {
  34. m.sendWarningToConsole("Wrong syntax in '" + file.getPath() + "' - " + s);
  35. return;
  36. }
  37. conf.put(s.substring(0, b).trim(), s.substring(b + 1).trim());
  38. });
  39. }
  40. catch (MalformedInputException ex)
  41. {
  42. m.sendWarningToConsole("'" + file.getPath() + "' contains an illegal character, change file encoding");
  43. }
  44. catch (IOException ex)
  45. {
  46. m.sendWarningToConsole("File '" + file.getPath() + "' cannot be read");
  47. }
  48. }
  49. //System.out.println("WUSI");
  50. //conf.forEach((k, v) -> System.out.println(k + " " + v));
  51. }
  52. public static File[] getFiles(String path)
  53. {
  54. return new File("./" + path).listFiles();
  55. }
  56. public boolean exists()
  57. {
  58. return file.exists();
  59. }
  60. public boolean delete()
  61. {
  62. return file.delete();
  63. }
  64. public boolean save()
  65. {
  66. try
  67. {
  68. if(!file.getParentFile().exists())
  69. {
  70. file.getParentFile().mkdirs();
  71. }
  72. if(!file.exists())
  73. {
  74. file.createNewFile();
  75. }
  76. Files.write(Paths.get(file.toURI()), conf.entrySet().stream()
  77. .map(e -> e.getKey() + "=" + e.getValue())
  78. .collect(Collectors.toList()), Charset.forName("UTF-8"));
  79. return true;
  80. }
  81. catch(IOException ex)
  82. {
  83. m.sendWarningToConsole("Can't write to '" + file.getPath() + "'");
  84. ex.printStackTrace();
  85. return false;
  86. }
  87. }
  88. // -----------------------------------------------------------------------------------
  89. // Get Data
  90. // -----------------------------------------------------------------------------------
  91. public String getString(String key)
  92. {
  93. return conf.get(key);
  94. }
  95. public String getString(String key, String error)
  96. {
  97. String s = conf.get(key);
  98. if(s == null)
  99. {
  100. return error;
  101. }
  102. return s;
  103. }
  104. public float getFloat(String key, float error)
  105. {
  106. String s = conf.get(key);
  107. if(s == null)
  108. {
  109. return error;
  110. }
  111. try
  112. {
  113. return Float.parseFloat(s);
  114. }
  115. catch(NumberFormatException ex)
  116. {
  117. return error;
  118. }
  119. }
  120. public double getDouble(String key, double error)
  121. {
  122. String s = conf.get(key);
  123. if(s == null)
  124. {
  125. return error;
  126. }
  127. try
  128. {
  129. return Double.parseDouble(s);
  130. }
  131. catch(NumberFormatException ex)
  132. {
  133. return error;
  134. }
  135. }
  136. public int getInt(String key, int error)
  137. {
  138. String s = conf.get(key);
  139. if(s == null)
  140. {
  141. return error;
  142. }
  143. try
  144. {
  145. return Integer.parseInt(s);
  146. }
  147. catch(NumberFormatException ex)
  148. {
  149. return error;
  150. }
  151. }
  152. public boolean getBoolean(String key, boolean error)
  153. {
  154. String s = conf.get(key);
  155. if(s == null)
  156. {
  157. return error;
  158. }
  159. try
  160. {
  161. return Boolean.valueOf(s);
  162. }
  163. catch(NumberFormatException ex)
  164. {
  165. return error;
  166. }
  167. }
  168. public Location getLocation(String key)
  169. {
  170. return new Location(ModDimensions.getWorldFromName(getString(key + ".world")),
  171. new Vec3d(getDouble(key + ".x", 0), getDouble(key + ".y", 0), getDouble(key + ".z", 0)),
  172. getFloat(key + ".yaw", 0), getFloat(key + ".pitch", 0));
  173. }
  174. public ItemStack getItemStack(String key)
  175. {
  176. String s = conf.get(key);
  177. if(s == null)
  178. {
  179. return ItemStack.EMPTY;
  180. }
  181. try
  182. {
  183. return ItemStackUtils.getStackFromNbtString(s);
  184. }
  185. catch(IllegalItemStackStringException ex)
  186. {
  187. return ItemStack.EMPTY;
  188. }
  189. }
  190. // -----------------------------------------------------------------------------------
  191. // Add Data
  192. // -----------------------------------------------------------------------------------
  193. public void setString(String key, String value)
  194. {
  195. conf.put(key, value);
  196. }
  197. public void setDouble(String key, double d)
  198. {
  199. setString(key, String.valueOf(d));
  200. }
  201. public void setFloat(String key, float d)
  202. {
  203. setString(key, String.valueOf(d));
  204. }
  205. public void setInt(String key, int i)
  206. {
  207. setString(key, String.valueOf(i));
  208. }
  209. public void setBoolean(String key, boolean b)
  210. {
  211. setString(key, String.valueOf(b));
  212. }
  213. public void setLocation(String key, Location l)
  214. {
  215. setString(key + ".world", l.getWorld().getWorldInfo().getWorldName());
  216. setDouble(key + ".x", l.getPos().xCoord);
  217. setDouble(key + ".y", l.getPos().yCoord);
  218. setDouble(key + ".z", l.getPos().zCoord);
  219. setDouble(key + ".yaw", l.getYaw());
  220. setDouble(key + ".pitch", l.getPitch());
  221. }
  222. public void setItemStack(String key, ItemStack stack)
  223. {
  224. setString(key, ItemStackUtils.getNbtString(stack));
  225. }
  226. }