From a7097df231597751d8cb27032ca07afa0ca3598e Mon Sep 17 00:00:00 2001 From: snowleo Date: Tue, 6 Dec 2011 15:38:14 +0100 Subject: Refactoring to create less redundant code --- .../earth2me/essentials/spawn/SpawnStorage.java | 111 ++++----------------- 1 file changed, 19 insertions(+), 92 deletions(-) (limited to 'EssentialsSpawn') diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java index 02fb26c9b..088ee9052 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java @@ -1,57 +1,46 @@ package com.earth2me.essentials.spawn; -import com.earth2me.essentials.IConf; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentialsModule; import com.earth2me.essentials.settings.Spawns; -import com.earth2me.essentials.storage.AbstractDelayedYamlFileReader; -import com.earth2me.essentials.storage.AbstractDelayedYamlFileWriter; -import com.earth2me.essentials.storage.ObjectLoadException; -import com.earth2me.essentials.storage.StorageObject; +import com.earth2me.essentials.storage.AsyncStorageObjectHolder; import java.io.File; import java.util.HashMap; import java.util.Locale; import java.util.Map; -import java.util.concurrent.locks.ReentrantReadWriteLock; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; -public class SpawnStorage implements IConf, IEssentialsModule +public class SpawnStorage extends AsyncStorageObjectHolder implements IEssentialsModule { - private transient Spawns spawns; - private final transient IEssentials ess; - private final transient File spawnfile; - private final transient ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); - public SpawnStorage(final IEssentials ess) { - this.ess = ess; - spawnfile = new File(ess.getDataFolder(), "spawn.yml"); - new SpawnReader(); + super(ess, Spawns.class); + reloadConfig(); + } + + @Override + public File getStorageFile() + { + return new File(ess.getDataFolder(), "spawn.yml"); } public void setSpawn(final Location loc, final String group) { - rwl.writeLock().lock(); + acquireWriteLock(); try { - if (spawns == null) + if (getData().getSpawns() == null) { - spawns = new Spawns(); + getData().setSpawns(new HashMap()); } - if (spawns.getSpawns() == null) - { - spawns.setSpawns(new HashMap()); - } - spawns.getSpawns().put(group.toLowerCase(Locale.ENGLISH), loc); + getData().getSpawns().put(group.toLowerCase(Locale.ENGLISH), loc); } finally { - rwl.writeLock().unlock(); + unlock(); } - new SpawnWriter(); if ("default".equalsIgnoreCase(group)) { @@ -61,14 +50,14 @@ public class SpawnStorage implements IConf, IEssentialsModule public Location getSpawn(final String group) { - rwl.readLock().lock(); + acquireReadLock(); try { - if (spawns == null || spawns.getSpawns() == null || group == null) + if (getData().getSpawns() == null || group == null) { return getWorldSpawn(); } - final Map spawnMap = spawns.getSpawns(); + final Map spawnMap = getData().getSpawns(); String groupName = group.toLowerCase(Locale.ENGLISH); if (!spawnMap.containsKey(groupName)) { @@ -82,7 +71,7 @@ public class SpawnStorage implements IConf, IEssentialsModule } finally { - rwl.readLock().unlock(); + unlock(); } } @@ -98,66 +87,4 @@ public class SpawnStorage implements IConf, IEssentialsModule } return ess.getServer().getWorlds().get(0).getSpawnLocation(); } - - @Override - public void reloadConfig() - { - new SpawnReader(); - } - - - private class SpawnWriter extends AbstractDelayedYamlFileWriter - { - public SpawnWriter() - { - super(ess, spawnfile); - } - - @Override - public StorageObject getObject() - { - rwl.readLock().lock(); - return spawns; - } - - @Override - public void onFinish() - { - rwl.readLock().unlock(); - } - } - - - private class SpawnReader extends AbstractDelayedYamlFileReader - { - public SpawnReader() - { - super(ess, spawnfile, Spawns.class); - } - - @Override - public void onStart() - { - rwl.writeLock().lock(); - } - - @Override - public void onSuccess(final Spawns object) - { - if (object != null) - { - spawns = object; - } - rwl.writeLock().unlock(); - } - - @Override - public void onException() - { - if (spawns == null) { - spawns = new Spawns(); - } - rwl.writeLock().unlock(); - } - } } -- cgit v1.2.3