PageRenderTime 68ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/Essentials/src/com/earth2me/essentials/EssentialsConf.java

https://github.com/BodyshotzVG/Essentials
Java | 277 lines | 255 code | 19 blank | 3 comment | 42 complexity | 154686e42785af9ec6909c030f394521 MD5 | raw file
  1. package com.earth2me.essentials;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import org.bukkit.Location;
  14. import org.bukkit.Material;
  15. import org.bukkit.Server;
  16. import org.bukkit.World;
  17. import org.bukkit.inventory.ItemStack;
  18. import org.bukkit.util.config.Configuration;
  19. public class EssentialsConf extends Configuration
  20. {
  21. private static final Logger LOGGER = Logger.getLogger("Minecraft");
  22. private transient File configFile;
  23. private transient String templateName = null;
  24. private transient Class<?> resourceClass = EssentialsConf.class;
  25. public EssentialsConf(final File configFile)
  26. {
  27. super(configFile);
  28. this.configFile = configFile;
  29. if (this.root == null)
  30. {
  31. this.root = new HashMap<String, Object>();
  32. }
  33. }
  34. @Override
  35. public void load()
  36. {
  37. configFile = configFile.getAbsoluteFile();
  38. if (!configFile.getParentFile().exists())
  39. {
  40. if (!configFile.getParentFile().mkdirs())
  41. {
  42. LOGGER.log(Level.SEVERE, Util.format("failedToCreateConfig", configFile.toString()));
  43. }
  44. }
  45. // This will delete files where the first character is 0. In most cases they are broken.
  46. if (configFile.exists() && configFile.length() != 0)
  47. {
  48. try
  49. {
  50. final InputStream input = new FileInputStream(configFile);
  51. try
  52. {
  53. if (input.read() == 0)
  54. {
  55. input.close();
  56. configFile.delete();
  57. }
  58. }
  59. catch (IOException ex)
  60. {
  61. LOGGER.log(Level.SEVERE, null, ex);
  62. }
  63. finally
  64. {
  65. try
  66. {
  67. input.close();
  68. }
  69. catch (IOException ex)
  70. {
  71. LOGGER.log(Level.SEVERE, null, ex);
  72. }
  73. }
  74. }
  75. catch (FileNotFoundException ex)
  76. {
  77. LOGGER.log(Level.SEVERE, null, ex);
  78. }
  79. }
  80. if (!configFile.exists())
  81. {
  82. if (templateName != null)
  83. {
  84. LOGGER.log(Level.INFO, Util.format("creatingConfigFromTemplate", configFile.toString()));
  85. createFromTemplate();
  86. }
  87. else
  88. {
  89. try
  90. {
  91. LOGGER.log(Level.INFO, Util.format("creatingEmptyConfig", configFile.toString()));
  92. if (!configFile.createNewFile())
  93. {
  94. LOGGER.log(Level.SEVERE, Util.format("failedToCreateConfig", configFile.toString()));
  95. }
  96. }
  97. catch (IOException ex)
  98. {
  99. LOGGER.log(Level.SEVERE, Util.format("failedToCreateConfig", configFile.toString()), ex);
  100. }
  101. }
  102. }
  103. try
  104. {
  105. super.load();
  106. }
  107. catch (RuntimeException e)
  108. {
  109. LOGGER.log(Level.INFO, "File: " + configFile.toString());
  110. throw e;
  111. }
  112. if (this.root == null)
  113. {
  114. this.root = new HashMap<String, Object>();
  115. }
  116. }
  117. private void createFromTemplate()
  118. {
  119. InputStream istr = null;
  120. OutputStream ostr = null;
  121. try
  122. {
  123. istr = resourceClass.getResourceAsStream(templateName);
  124. if (istr == null)
  125. {
  126. LOGGER.log(Level.SEVERE, Util.format("couldNotFindTemplate", templateName));
  127. return;
  128. }
  129. ostr = new FileOutputStream(configFile);
  130. byte[] buffer = new byte[1024];
  131. int length = 0;
  132. length = istr.read(buffer);
  133. while (length > 0)
  134. {
  135. ostr.write(buffer, 0, length);
  136. length = istr.read(buffer);
  137. }
  138. }
  139. catch (IOException ex)
  140. {
  141. LOGGER.log(Level.SEVERE, Util.format("failedToWriteConfig", configFile.toString()), ex);
  142. return;
  143. }
  144. finally
  145. {
  146. try
  147. {
  148. if (istr != null)
  149. {
  150. istr.close();
  151. }
  152. }
  153. catch (IOException ex)
  154. {
  155. Logger.getLogger(EssentialsConf.class.getName()).log(Level.SEVERE, null, ex);
  156. }
  157. try
  158. {
  159. if (ostr != null)
  160. {
  161. ostr.close();
  162. }
  163. }
  164. catch (IOException ex)
  165. {
  166. LOGGER.log(Level.SEVERE, Util.format("failedToCloseConfig", configFile.toString()), ex);
  167. }
  168. }
  169. }
  170. public void setTemplateName(final String templateName)
  171. {
  172. this.templateName = templateName;
  173. }
  174. public File getFile()
  175. {
  176. return configFile;
  177. }
  178. public void setTemplateName(final String templateName, final Class<?> resClass)
  179. {
  180. this.templateName = templateName;
  181. this.resourceClass = resClass;
  182. }
  183. public boolean hasProperty(final String path)
  184. {
  185. return getProperty(path) != null;
  186. }
  187. public Location getLocation(final String path, final Server server) throws Exception
  188. {
  189. final String worldName = getString((path == null ? "" : path + ".") + "world");
  190. if (worldName == null || worldName.isEmpty())
  191. {
  192. return null;
  193. }
  194. final World world = server.getWorld(worldName);
  195. if (world == null)
  196. {
  197. throw new Exception(Util.i18n("invalidWorld"));
  198. }
  199. return new Location(world,
  200. getDouble((path == null ? "" : path + ".") + "x", 0),
  201. getDouble((path == null ? "" : path + ".") + "y", 0),
  202. getDouble((path == null ? "" : path + ".") + "z", 0),
  203. (float)getDouble((path == null ? "" : path + ".") + "yaw", 0),
  204. (float)getDouble((path == null ? "" : path + ".") + "pitch", 0));
  205. }
  206. public void setProperty(final String path, final Location loc)
  207. {
  208. setProperty((path == null ? "" : path + ".") + "world", loc.getWorld().getName());
  209. setProperty((path == null ? "" : path + ".") + "x", loc.getX());
  210. setProperty((path == null ? "" : path + ".") + "y", loc.getY());
  211. setProperty((path == null ? "" : path + ".") + "z", loc.getZ());
  212. setProperty((path == null ? "" : path + ".") + "yaw", loc.getYaw());
  213. setProperty((path == null ? "" : path + ".") + "pitch", loc.getPitch());
  214. }
  215. public ItemStack getItemStack(final String path)
  216. {
  217. return new ItemStack(
  218. Material.valueOf(getString(path + ".type", "AIR")),
  219. getInt(path + ".amount", 1),
  220. (short)getInt(path + ".damage", 0)/*,
  221. (byte)getInt(path + ".data", 0)*/);
  222. }
  223. public void setProperty(final String path, final ItemStack stack)
  224. {
  225. final Map<String, Object> map = new HashMap<String, Object>();
  226. map.put("type", stack.getType().toString());
  227. map.put("amount", stack.getAmount());
  228. map.put("damage", stack.getDurability());
  229. // getData().getData() is broken
  230. //map.put("data", stack.getDurability());
  231. setProperty(path, map);
  232. }
  233. public long getLong(final String path, final long def)
  234. {
  235. try
  236. {
  237. final Number num = (Number)getProperty(path);
  238. return num == null ? def : num.longValue();
  239. }
  240. catch (ClassCastException ex)
  241. {
  242. return def;
  243. }
  244. }
  245. @Override
  246. public double getDouble(final String path, final double def)
  247. {
  248. try
  249. {
  250. Number num = (Number)getProperty(path);
  251. return num == null ? def : num.doubleValue();
  252. }
  253. catch (ClassCastException ex)
  254. {
  255. return def;
  256. }
  257. }
  258. }