PageRenderTime 119ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/worldedit-core/src/main/java/com/sk89q/worldedit/util/PropertiesConfiguration.java

https://gitlab.com/Skull3x/WorldEdit
Java | 260 lines | 172 code | 22 blank | 66 comment | 25 complexity | 34bbf8fe6b24a97257f2084933523b5c MD5 | raw file
  1. /*
  2. * WorldEdit, a Minecraft world manipulation toolkit
  3. * Copyright (C) sk89q <http://www.sk89q.com>
  4. * Copyright (C) WorldEdit team and contributors
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. // $Id$
  20. package com.sk89q.worldedit.util;
  21. import com.sk89q.util.StringUtil;
  22. import com.sk89q.worldedit.LocalConfiguration;
  23. import com.sk89q.worldedit.LocalSession;
  24. import com.sk89q.worldedit.world.snapshot.SnapshotRepository;
  25. import java.io.File;
  26. import java.io.FileInputStream;
  27. import java.io.FileNotFoundException;
  28. import java.io.FileOutputStream;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.io.OutputStream;
  32. import java.util.HashSet;
  33. import java.util.Properties;
  34. import java.util.Set;
  35. import java.util.logging.Level;
  36. import java.util.logging.Logger;
  37. /**
  38. * Simple LocalConfiguration that loads settings using
  39. * {@code java.util.Properties}.
  40. */
  41. public class PropertiesConfiguration extends LocalConfiguration {
  42. private static final Logger log = Logger.getLogger(PropertiesConfiguration.class.getCanonicalName());
  43. protected Properties properties;
  44. protected File path;
  45. /**
  46. * Construct the object. The configuration isn't loaded yet.
  47. *
  48. * @param path the path tot he configuration
  49. */
  50. public PropertiesConfiguration(File path) {
  51. this.path = path;
  52. properties = new Properties();
  53. }
  54. @Override
  55. public void load() {
  56. InputStream stream = null;
  57. try {
  58. stream = new FileInputStream(path);
  59. properties.load(stream);
  60. } catch (FileNotFoundException ignored) {
  61. } catch (IOException e) {
  62. log.log(Level.WARNING, "Failed to read configuration", e);
  63. } finally {
  64. if (stream != null) {
  65. try {
  66. stream.close();
  67. } catch (IOException ignored) {
  68. }
  69. }
  70. }
  71. loadExtra();
  72. profile = getBool("profile", profile);
  73. disallowedBlocks = getIntSet("disallowed-blocks", defaultDisallowedBlocks);
  74. defaultChangeLimit = getInt("default-max-changed-blocks", defaultChangeLimit);
  75. maxChangeLimit = getInt("max-changed-blocks", maxChangeLimit);
  76. defaultMaxPolygonalPoints = getInt("default-max-polygon-points", defaultMaxPolygonalPoints);
  77. maxPolygonalPoints = getInt("max-polygon-points", maxPolygonalPoints);
  78. defaultMaxPolyhedronPoints = getInt("default-max-polyhedron-points", defaultMaxPolyhedronPoints);
  79. maxPolyhedronPoints = getInt("max-polyhedron-points", maxPolyhedronPoints);
  80. shellSaveType = getString("shell-save-type", shellSaveType);
  81. maxRadius = getInt("max-radius", maxRadius);
  82. maxSuperPickaxeSize = getInt("max-super-pickaxe-size", maxSuperPickaxeSize);
  83. maxBrushRadius = getInt("max-brush-radius", maxBrushRadius);
  84. logCommands = getBool("log-commands", logCommands);
  85. logFile = getString("log-file", logFile);
  86. registerHelp = getBool("register-help", registerHelp);
  87. wandItem = getInt("wand-item", wandItem);
  88. superPickaxeDrop = getBool("super-pickaxe-drop-items", superPickaxeDrop);
  89. superPickaxeManyDrop = getBool("super-pickaxe-many-drop-items", superPickaxeManyDrop);
  90. noDoubleSlash = getBool("no-double-slash", noDoubleSlash);
  91. useInventory = getBool("use-inventory", useInventory);
  92. useInventoryOverride = getBool("use-inventory-override", useInventoryOverride);
  93. useInventoryCreativeOverride = getBool("use-inventory-creative-override", useInventoryCreativeOverride);
  94. navigationWand = getInt("nav-wand-item", navigationWand);
  95. navigationWandMaxDistance = getInt("nav-wand-distance", navigationWandMaxDistance);
  96. navigationUseGlass = getBool("nav-use-glass", navigationUseGlass);
  97. scriptTimeout = getInt("scripting-timeout", scriptTimeout);
  98. saveDir = getString("schematic-save-dir", saveDir);
  99. scriptsDir = getString("craftscript-dir", scriptsDir);
  100. butcherDefaultRadius = getInt("butcher-default-radius", butcherDefaultRadius);
  101. butcherMaxRadius = getInt("butcher-max-radius", butcherMaxRadius);
  102. allowSymlinks = getBool("allow-symbolic-links", allowSymlinks);
  103. LocalSession.MAX_HISTORY_SIZE = Math.max(15, getInt("history-size", 15));
  104. String snapshotsDir = getString("snapshots-dir", "");
  105. if (!snapshotsDir.isEmpty()) {
  106. snapshotRepo = new SnapshotRepository(snapshotsDir);
  107. }
  108. OutputStream output = null;
  109. path.getParentFile().mkdirs();
  110. try {
  111. output = new FileOutputStream(path);
  112. properties.store(output, "Don't put comments; they get removed");
  113. } catch (FileNotFoundException e) {
  114. log.log(Level.WARNING, "Failed to write configuration", e);
  115. } catch (IOException e) {
  116. log.log(Level.WARNING, "Failed to write configuration", e);
  117. } finally {
  118. if (output != null) {
  119. try {
  120. output.close();
  121. } catch (IOException ignored) {
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * Called to load extra configuration.
  128. */
  129. protected void loadExtra() {
  130. }
  131. /**
  132. * Get a string value.
  133. *
  134. * @param key the key
  135. * @param def the default value
  136. * @return the value
  137. */
  138. protected String getString(String key, String def) {
  139. if (def == null) {
  140. def = "";
  141. }
  142. String val = properties.getProperty(key);
  143. if (val == null) {
  144. properties.setProperty(key, def);
  145. return def;
  146. } else {
  147. return val;
  148. }
  149. }
  150. /**
  151. * Get a boolean value.
  152. *
  153. * @param key the key
  154. * @param def the default value
  155. * @return the value
  156. */
  157. protected boolean getBool(String key, boolean def) {
  158. String val = properties.getProperty(key);
  159. if (val == null) {
  160. properties.setProperty(key, def ? "true" : "false");
  161. return def;
  162. } else {
  163. return val.equalsIgnoreCase("true")
  164. || val.equals("1");
  165. }
  166. }
  167. /**
  168. * Get an integer value.
  169. *
  170. * @param key the key
  171. * @param def the default value
  172. * @return the value
  173. */
  174. protected int getInt(String key, int def) {
  175. String val = properties.getProperty(key);
  176. if (val == null) {
  177. properties.setProperty(key, String.valueOf(def));
  178. return def;
  179. } else {
  180. try {
  181. return Integer.parseInt(val);
  182. } catch (NumberFormatException e) {
  183. properties.setProperty(key, String.valueOf(def));
  184. return def;
  185. }
  186. }
  187. }
  188. /**
  189. * Get a double value.
  190. *
  191. * @param key the key
  192. * @param def the default value
  193. * @return the value
  194. */
  195. protected double getDouble(String key, double def) {
  196. String val = properties.getProperty(key);
  197. if (val == null) {
  198. properties.setProperty(key, String.valueOf(def));
  199. return def;
  200. } else {
  201. try {
  202. return Double.parseDouble(val);
  203. } catch (NumberFormatException e) {
  204. properties.setProperty(key, String.valueOf(def));
  205. return def;
  206. }
  207. }
  208. }
  209. /**
  210. * Get a double value.
  211. *
  212. * @param key the key
  213. * @param def the default value
  214. * @return the value
  215. */
  216. protected Set<Integer> getIntSet(String key, int[] def) {
  217. String val = properties.getProperty(key);
  218. if (val == null) {
  219. properties.setProperty(key, StringUtil.joinString(def, ",", 0));
  220. Set<Integer> set = new HashSet<Integer>();
  221. for (int i : def) {
  222. set.add(i);
  223. }
  224. return set;
  225. } else {
  226. Set<Integer> set = new HashSet<Integer>();
  227. String[] parts = val.split(",");
  228. for (String part : parts) {
  229. try {
  230. int v = Integer.parseInt(part.trim());
  231. set.add(v);
  232. } catch (NumberFormatException ignored) {
  233. }
  234. }
  235. return set;
  236. }
  237. }
  238. }