Clean up userdata saving, to prevent CMI

Also update config section code to use newer bukkit methods
This commit is contained in:
KHobbits 2013-01-12 19:30:06 +00:00
parent 298ab846c1
commit ef1492a2a2
2 changed files with 92 additions and 98 deletions

View File

@ -221,6 +221,69 @@ public class EssentialsConf extends YamlConfiguration
this.resourceClass = resClass; 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) public boolean hasProperty(final String path)
{ {
return isSet(path); return isSet(path);
@ -305,94 +368,14 @@ public class EssentialsConf extends YamlConfiguration
set(path, map); set(path, map);
} }
public long getLong(final String path, final long def) public void setProperty(String path, List object)
{ {
try set(path, new ArrayList(object));
{
final Number num = (Number)get(path);
return num == null ? def : num.longValue();
}
catch (ClassCastException ex)
{
return def;
}
} }
@Override public void setProperty(String path, Map object)
public double getDouble(final String path, final double def)
{ {
try set(path, new LinkedHashMap(object));
{
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();
}
} }
public Object getProperty(String path) public Object getProperty(String path)
@ -464,6 +447,12 @@ public class EssentialsConf extends YamlConfiguration
return super.getDouble(path); return super.getDouble(path);
} }
@Override
public synchronized double getDouble(final String path, final double def)
{
return super.getDouble(path, def);
}
@Override @Override
public synchronized List<Double> getDoubleList(String path) public synchronized List<Double> getDoubleList(String path)
{ {
@ -524,6 +513,12 @@ public class EssentialsConf extends YamlConfiguration
return super.getLong(path); return super.getLong(path);
} }
@Override
public synchronized long getLong(final String path, final long def)
{
return super.getLong(path, def);
}
@Override @Override
public synchronized List<Long> getLongList(String path) public synchronized List<Long> getLongList(String path)
{ {

View File

@ -5,7 +5,6 @@ import java.io.File;
import java.util.*; import java.util.*;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -794,44 +793,44 @@ public abstract class UserData extends PlayerExtension implements IConf
{ {
return config.getBoolean("powertoolsenabled", true); return config.getBoolean("powertoolsenabled", true);
} }
private ConfigurationSection kitTimestamps; private Map<String, Long> kitTimestamps;
private ConfigurationSection _getKitTimestamps() private Map<String, Long> _getKitTimestamps()
{ {
if (config.isConfigurationSection("timestamps.kits")) if (config.isConfigurationSection("timestamps.kits"))
{ {
final ConfigurationSection section = config.getConfigurationSection("timestamps.kits"); final ConfigurationSection section = config.getConfigurationSection("timestamps.kits");
final ConfigurationSection newSection = new MemoryConfiguration(); final Map<String, Long> timestamps = new HashMap<String, Long>();
for (String command : section.getKeys(false)) for (String command : section.getKeys(false))
{ {
if (section.isLong(command)) 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)) 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<String, Long>();
} }
public long getKitTimestamp(String name) public long getKitTimestamp(String name)
{ {
name = name.replace('.', '_').replace('/', '_'); name = name.replace('.', '_').replace('/', '_');
if (kitTimestamps != null) if (kitTimestamps != null && kitTimestamps.containsKey(name))
{ {
return kitTimestamps.getLong(name, 0l); return kitTimestamps.get(name);
} }
return 0l; return 0l;
} }
public void setKitTimestamp(final String name, final long time) 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.setProperty("timestamps.kits", kitTimestamps);
config.save(); config.save();
} }