From ef1492a2a286876eeff767e72a88854646c36532 Mon Sep 17 00:00:00 2001 From: KHobbits Date: Sat, 12 Jan 2013 19:30:06 +0000 Subject: Clean up userdata saving, to prevent CMI Also update config section code to use newer bukkit methods --- .../com/earth2me/essentials/EssentialsConf.java | 163 ++++++++++----------- .../src/com/earth2me/essentials/UserData.java | 27 ++-- 2 files changed, 92 insertions(+), 98 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/EssentialsConf.java b/Essentials/src/com/earth2me/essentials/EssentialsConf.java index 70e683da4..175afb110 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsConf.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsConf.java @@ -221,6 +221,69 @@ public class EssentialsConf extends YamlConfiguration this.resourceClass = resClass; } + public void save() + { + try + { + save(configFile); + } + catch (IOException ex) + { + LOGGER.log(Level.SEVERE, ex.getMessage(), ex); + } + } + + public void saveWithError() throws IOException + { + save(configFile); + } + + @Override + public synchronized void save(final File file) throws IOException + { + if (file == null) + { + throw new IllegalArgumentException("File cannot be null"); + } + + Files.createParentDirs(file); + + final String data = saveToString(); + + if (data.length() == 0) + { + return; + } + + if (!configFile.exists()) + { + try + { + LOGGER.log(Level.INFO, _("creatingEmptyConfig", configFile.toString())); + if (!configFile.createNewFile()) + { + LOGGER.log(Level.SEVERE, _("failedToCreateConfig", configFile.toString())); + } + } + catch (IOException ex) + { + LOGGER.log(Level.SEVERE, _("failedToCreateConfig", configFile.toString()), ex); + } + } + + + final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), UTF8); + + try + { + writer.write(data); + } + finally + { + writer.close(); + } + } + public boolean hasProperty(final String path) { return isSet(path); @@ -305,94 +368,14 @@ public class EssentialsConf extends YamlConfiguration set(path, map); } - public long getLong(final String path, final long def) + public void setProperty(String path, List object) { - try - { - final Number num = (Number)get(path); - return num == null ? def : num.longValue(); - } - catch (ClassCastException ex) - { - return def; - } + set(path, new ArrayList(object)); } - @Override - public double getDouble(final String path, final double def) + public void setProperty(String path, Map object) { - try - { - Number num = (Number)get(path); - return num == null ? def : num.doubleValue(); - } - catch (ClassCastException ex) - { - return def; - } - } - - public void save() - { - try - { - save(configFile); - } - catch (IOException ex) - { - LOGGER.log(Level.SEVERE, ex.getMessage(), ex); - } - } - - public void saveWithError() throws IOException - { - save(configFile); - } - - @Override - public synchronized void save(final File file) throws IOException - { - if (file == null) - { - throw new IllegalArgumentException("File cannot be null"); - } - - Files.createParentDirs(file); - - final String data = saveToString(); - - if (data.length() == 0) - { - return; - } - - if (!configFile.exists()) - { - try - { - LOGGER.log(Level.INFO, _("creatingEmptyConfig", configFile.toString())); - if (!configFile.createNewFile()) - { - LOGGER.log(Level.SEVERE, _("failedToCreateConfig", configFile.toString())); - } - } - catch (IOException ex) - { - LOGGER.log(Level.SEVERE, _("failedToCreateConfig", configFile.toString()), ex); - } - } - - - final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), UTF8); - - try - { - writer.write(data); - } - finally - { - writer.close(); - } + set(path, new LinkedHashMap(object)); } public Object getProperty(String path) @@ -464,6 +447,12 @@ public class EssentialsConf extends YamlConfiguration return super.getDouble(path); } + @Override + public synchronized double getDouble(final String path, final double def) + { + return super.getDouble(path, def); + } + @Override public synchronized List getDoubleList(String path) { @@ -524,6 +513,12 @@ public class EssentialsConf extends YamlConfiguration return super.getLong(path); } + @Override + public synchronized long getLong(final String path, final long def) + { + return super.getLong(path, def); + } + @Override public synchronized List getLongList(String path) { diff --git a/Essentials/src/com/earth2me/essentials/UserData.java b/Essentials/src/com/earth2me/essentials/UserData.java index 94b1631f5..930a9c913 100644 --- a/Essentials/src/com/earth2me/essentials/UserData.java +++ b/Essentials/src/com/earth2me/essentials/UserData.java @@ -5,7 +5,6 @@ import java.io.File; import java.util.*; import org.bukkit.Location; import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.MemoryConfiguration; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -794,44 +793,44 @@ public abstract class UserData extends PlayerExtension implements IConf { return config.getBoolean("powertoolsenabled", true); } - private ConfigurationSection kitTimestamps; - - private ConfigurationSection _getKitTimestamps() + private Map kitTimestamps; + + private Map _getKitTimestamps() { - + if (config.isConfigurationSection("timestamps.kits")) { final ConfigurationSection section = config.getConfigurationSection("timestamps.kits"); - final ConfigurationSection newSection = new MemoryConfiguration(); + final Map timestamps = new HashMap(); for (String command : section.getKeys(false)) { if (section.isLong(command)) { - newSection.set(command.toLowerCase(Locale.ENGLISH), section.getLong(command)); + timestamps.put(command.toLowerCase(Locale.ENGLISH), section.getLong(command)); } else if (section.isInt(command)) { - newSection.set(command.toLowerCase(Locale.ENGLISH), (long)section.getInt(command)); + timestamps.put(command.toLowerCase(Locale.ENGLISH), (long)section.getInt(command)); } } - return newSection; + return timestamps; } - return new MemoryConfiguration(); + return new HashMap(); } - + public long getKitTimestamp(String name) { name = name.replace('.', '_').replace('/', '_'); - if (kitTimestamps != null) + if (kitTimestamps != null && kitTimestamps.containsKey(name)) { - return kitTimestamps.getLong(name, 0l); + return kitTimestamps.get(name); } return 0l; } public void setKitTimestamp(final String name, final long time) { - kitTimestamps.set(name.toLowerCase(Locale.ENGLISH), time); + kitTimestamps.put(name.toLowerCase(Locale.ENGLISH), time); config.setProperty("timestamps.kits", kitTimestamps); config.save(); } -- cgit v1.2.3