From 019b49ef11b453026e63b9e683d7827b85c81ab9 Mon Sep 17 00:00:00 2001 From: snowleo Date: Tue, 6 Dec 2011 13:41:29 +0100 Subject: Updated EssentialsSpawn to use the new config code /spawn and /home now call the PlayerRespawnEvent to make it more compatible with other plugins. --- .../src/com/earth2me/essentials/Essentials.java | 14 +- .../com/earth2me/essentials/EssentialsUpgrade.java | 50 +++++++ .../src/com/earth2me/essentials/IEssentials.java | 8 +- .../com/earth2me/essentials/IEssentialsModule.java | 6 + .../src/com/earth2me/essentials/Teleport.java | 10 +- .../earth2me/essentials/commands/Commandhome.java | 16 +-- .../essentials/commands/EssentialsCommand.java | 8 ++ .../essentials/commands/IEssentialsCommand.java | 3 + .../com/earth2me/essentials/settings/Spawns.java | 18 +++ .../storage/AbstractDelayedYamlFileReader.java | 61 +++++++++ .../essentials/storage/BukkitConstructor.java | 32 ++++- .../essentials/storage/YamlStorageReader.java | 15 ++- .../src/com/earth2me/essentials/user/User.java | 6 - .../test/com/earth2me/essentials/StorageTest.java | 14 +- EssentialsSpawn/nbproject/pmd.settings | 1 + .../earth2me/essentials/spawn/Commandsetspawn.java | 4 +- .../earth2me/essentials/spawn/Commandspawn.java | 6 +- .../earth2me/essentials/spawn/EssentialsSpawn.java | 18 ++- .../spawn/EssentialsSpawnPlayerListener.java | 47 ++++--- .../earth2me/essentials/spawn/SpawnStorage.java | 145 +++++++++++++++++++++ .../earth2me/essentials/xmpp/EssentialsXMPP.java | 3 +- 21 files changed, 406 insertions(+), 79 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/IEssentialsModule.java create mode 100644 Essentials/src/com/earth2me/essentials/settings/Spawns.java create mode 100644 Essentials/src/com/earth2me/essentials/storage/AbstractDelayedYamlFileReader.java create mode 100644 EssentialsSpawn/nbproject/pmd.settings create mode 100644 EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index 98d919150..677caa941 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -65,7 +65,6 @@ public class Essentials extends JavaPlugin implements IEssentials private static final Logger LOGGER = Logger.getLogger("Minecraft"); private transient ISettings settings; private final transient TNTExplodeListener tntListener = new TNTExplodeListener(this); - private transient Spawn spawn; private transient Jail jail; private transient Warps warps; private transient Worth worth; @@ -159,8 +158,6 @@ public class Essentials extends JavaPlugin implements IEssentials userMap = new UserMap(this); confList.add(userMap); execTimer.mark("Init(Usermap)"); - spawn = new Spawn(getServer(), this.getDataFolder()); - confList.add(spawn); warps = new Warps(getServer(), this.getDataFolder()); confList.add(warps); execTimer.mark("Init(Spawn/Warp)"); @@ -297,11 +294,11 @@ public class Essentials extends JavaPlugin implements IEssentials @Override public boolean onCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args) { - return onCommandEssentials(sender, command, commandLabel, args, Essentials.class.getClassLoader(), "com.earth2me.essentials.commands.Command", "essentials."); + return onCommandEssentials(sender, command, commandLabel, args, Essentials.class.getClassLoader(), "com.earth2me.essentials.commands.Command", "essentials.", null); } @Override - public boolean onCommandEssentials(final CommandSender sender, final Command command, final String commandLabel, final String[] args, final ClassLoader classLoader, final String commandPath, final String permissionPrefix) + public boolean onCommandEssentials(final CommandSender sender, final Command command, final String commandLabel, final String[] args, final ClassLoader classLoader, final String commandPath, final String permissionPrefix, final IEssentialsModule module) { // Allow plugins to override the command via onCommand if (!getSettings().isCommandOverridden(command.getName()) && !commandLabel.startsWith("e")) @@ -344,6 +341,7 @@ public class Essentials extends JavaPlugin implements IEssentials { cmd = (IEssentialsCommand)classLoader.loadClass(commandPath + command.getName()).newInstance(); cmd.setEssentials(this); + cmd.setEssentialsModule(module); } catch (Exception ex) { @@ -442,12 +440,6 @@ public class Essentials extends JavaPlugin implements IEssentials return backup; } - @Override - public Spawn getSpawn() - { - return spawn; - } - @Override public User getUser(final Object base) { diff --git a/Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java b/Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java index 9070e25ac..29658ae4d 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java @@ -1,5 +1,7 @@ package com.earth2me.essentials; +import com.earth2me.essentials.settings.Spawns; +import com.earth2me.essentials.storage.YamlStorageWriter; import static com.earth2me.essentials.I18n._; import java.io.*; import java.math.BigInteger; @@ -681,6 +683,53 @@ public class EssentialsUpgrade } } + private void updateSpawnsToNewSpawnsConfig() + { + if (doneFile.getBoolean("updateSpawnsToNewSpawnsConfig", false)) + { + return; + } + final File configFile = new File(ess.getDataFolder(), "spawn.yml"); + if (configFile.exists()) + { + + final EssentialsConf config = new EssentialsConf(configFile); + try + { + config.load(); + if (!config.hasProperty("spawns")) + { + final Spawns spawns = new Spawns(); + List keys = config.getKeys(); + for (String group : keys) + { + Location loc = getFakeLocation(config, group); + spawns.getSpawns().put(group.toLowerCase(Locale.ENGLISH), loc); + } + if (!configFile.renameTo(new File(ess.getDataFolder(), "spawn.yml.old"))) + { + throw new Exception(_("fileRenameError", "spawn.yml")); + } + PrintWriter writer = new PrintWriter(configFile); + try + { + new YamlStorageWriter(writer).save(spawns); + } + finally + { + writer.close(); + } + } + } + catch (Exception ex) + { + Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex); + } + } + doneFile.setProperty("updateSpawnsToNewSpawnsConfig", true); + doneFile.save(); + } + public void beforeSettings() { if (!ess.getDataFolder().exists()) @@ -701,5 +750,6 @@ public class EssentialsUpgrade updateUsersPowerToolsFormat(); updateUsersHomesFormat(); deleteOldItemsCsv(); + updateSpawnsToNewSpawnsConfig(); } } diff --git a/Essentials/src/com/earth2me/essentials/IEssentials.java b/Essentials/src/com/earth2me/essentials/IEssentials.java index a56fc790c..9b5c5457b 100644 --- a/Essentials/src/com/earth2me/essentials/IEssentials.java +++ b/Essentials/src/com/earth2me/essentials/IEssentials.java @@ -15,10 +15,10 @@ public interface IEssentials extends Plugin void reload(); - boolean onCommandEssentials(CommandSender sender, Command command, String commandLabel, String[] args, ClassLoader classLoader, String commandPath, String permissionPrefix); + boolean onCommandEssentials(CommandSender sender, Command command, String commandLabel, String[] args, ClassLoader classLoader, String commandPath, String permissionPrefix, IEssentialsModule module); User getUser(Object base); - + I18n getI18n(); User getOfflineUser(String name); @@ -39,8 +39,6 @@ public interface IEssentials extends Plugin Backup getBackup(); - Spawn getSpawn(); - Methods getPaymentMethod(); int scheduleAsyncDelayedTask(Runnable run); @@ -54,7 +52,7 @@ public interface IEssentials extends Plugin TNTExplodeListener getTNTListener(); PermissionsHandler getPermissionsHandler(); - + AlternativeCommandsHandler getAlternativeCommandsHandler(); void showError(final CommandSender sender, final Throwable exception, final String commandLabel); diff --git a/Essentials/src/com/earth2me/essentials/IEssentialsModule.java b/Essentials/src/com/earth2me/essentials/IEssentialsModule.java new file mode 100644 index 000000000..69a1a494c --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/IEssentialsModule.java @@ -0,0 +1,6 @@ +package com.earth2me.essentials; + + +public interface IEssentialsModule +{ +} diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java index 92a4b77f5..654232012 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -7,6 +7,8 @@ import java.util.GregorianCalendar; import java.util.logging.Logger; import org.bukkit.Location; import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerRespawnEvent; public class Teleport implements Runnable @@ -122,9 +124,13 @@ public class Teleport implements Runnable this.ess = ess; } - public void respawn(Spawn spawn, Trade chargeFor) throws Exception + public void respawn(final Trade chargeFor) throws Exception { - teleport(new Target(spawn.getSpawn(user.getGroup())), chargeFor); + final Player player = user.getBase(); + final Location bed = player.getBedSpawnLocation(); + final PlayerRespawnEvent pre = new PlayerRespawnEvent(player, bed == null ? player.getWorld().getSpawnLocation() : bed, bed != null); + ess.getServer().getPluginManager().callEvent(pre); + teleport(new Target(pre.getRespawnLocation()), chargeFor); } public void warp(String warp, Trade chargeFor) throws Exception diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java index 8b684e3af..ab8355ead 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java @@ -43,7 +43,8 @@ public class Commandhome extends EssentialsCommand } try { - if ("bed".equalsIgnoreCase(homeName)) { + if ("bed".equalsIgnoreCase(homeName)) + { final Location bed = player.getBedSpawnLocation(); if (bed != null) { @@ -58,18 +59,7 @@ public class Commandhome extends EssentialsCommand final List homes = player.getHomes(); if (homes.isEmpty() && player.equals(user)) { - final Location loc = player.getBedSpawnLocation(); - if (loc == null) - { - if (ess.getSettings().spawnIfNoHome()) - { - user.getTeleport().respawn(ess.getSpawn(), charge); - } - } - else - { - user.getTeleport().teleport(loc, charge); - } + user.getTeleport().respawn(charge); } else if (homes.isEmpty()) { diff --git a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java index 6b9d17b26..13328e1b5 100644 --- a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java +++ b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java @@ -2,6 +2,7 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.IEssentialsModule; import com.earth2me.essentials.OfflinePlayer; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; @@ -17,6 +18,7 @@ public abstract class EssentialsCommand implements IEssentialsCommand { private final transient String name; protected transient IEssentials ess; + protected transient IEssentialsModule module; protected final static Logger logger = Logger.getLogger("Minecraft"); protected EssentialsCommand(final String name) @@ -29,6 +31,12 @@ public abstract class EssentialsCommand implements IEssentialsCommand { this.ess = ess; } + + @Override + public void setEssentialsModule(final IEssentialsModule module) + { + this.module = module; + } @Override public String getName() diff --git a/Essentials/src/com/earth2me/essentials/commands/IEssentialsCommand.java b/Essentials/src/com/earth2me/essentials/commands/IEssentialsCommand.java index f16d3059c..439c14d70 100644 --- a/Essentials/src/com/earth2me/essentials/commands/IEssentialsCommand.java +++ b/Essentials/src/com/earth2me/essentials/commands/IEssentialsCommand.java @@ -1,6 +1,7 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.IEssentialsModule; import com.earth2me.essentials.User; import org.bukkit.Server; import org.bukkit.command.Command; @@ -18,4 +19,6 @@ public interface IEssentialsCommand throws Exception; void setEssentials(IEssentials ess); + + void setEssentialsModule(IEssentialsModule module); } diff --git a/Essentials/src/com/earth2me/essentials/settings/Spawns.java b/Essentials/src/com/earth2me/essentials/settings/Spawns.java new file mode 100644 index 000000000..a0afa450c --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/settings/Spawns.java @@ -0,0 +1,18 @@ +package com.earth2me.essentials.settings; + +import com.earth2me.essentials.storage.MapValueType; +import com.earth2me.essentials.storage.StorageObject; +import java.util.HashMap; +import java.util.Map; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.bukkit.Location; + + +@Data +@EqualsAndHashCode(callSuper = false) +public class Spawns implements StorageObject +{ + @MapValueType(Location.class) + private Map spawns = new HashMap(); +} diff --git a/Essentials/src/com/earth2me/essentials/storage/AbstractDelayedYamlFileReader.java b/Essentials/src/com/earth2me/essentials/storage/AbstractDelayedYamlFileReader.java new file mode 100644 index 000000000..0bac39bdb --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/storage/AbstractDelayedYamlFileReader.java @@ -0,0 +1,61 @@ +package com.earth2me.essentials.storage; + +import com.earth2me.essentials.IEssentials; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.logging.Level; +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; + + +public abstract class AbstractDelayedYamlFileReader implements Runnable +{ + private final transient File file; + private final transient Class clazz; + private final transient Plugin plugin; + + public AbstractDelayedYamlFileReader(final IEssentials ess, final File file, final Class clazz) + { + this.file = file; + this.clazz = clazz; + this.plugin = ess; + ess.scheduleAsyncDelayedTask(this); + } + + public abstract void onStart(); + + @Override + public void run() + { + FileReader reader = null; + try + { + onStart(); + reader = new FileReader(file); + final T object = new YamlStorageReader(reader, plugin).load(clazz); + onFinish(object); + } + catch (FileNotFoundException ex) + { + Bukkit.getLogger().log(Level.SEVERE, file.toString(), ex); + } + finally + { + try + { + if (reader != null) + { + reader.close(); + } + } + catch (IOException ex) + { + Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex); + } + } + } + + public abstract void onFinish(T object); +} diff --git a/Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java b/Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java index c3cd9a022..77b542144 100644 --- a/Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java +++ b/Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java @@ -9,17 +9,22 @@ import org.bukkit.World; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.bukkit.material.MaterialData; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPluginLoader; import org.yaml.snakeyaml.constructor.Constructor; +import org.yaml.snakeyaml.error.YAMLException; import org.yaml.snakeyaml.nodes.*; public class BukkitConstructor extends Constructor { private final transient Pattern NUMPATTERN = Pattern.compile("\\d+"); + private final transient Plugin plugin; - public BukkitConstructor(Class clazz) + public BukkitConstructor(final Class clazz, final Plugin plugin) { super(clazz); + this.plugin = plugin; yamlClassConstructors.put(NodeId.scalar, new ConstructBukkitScalar()); yamlClassConstructors.put(NodeId.mapping, new ConstructBukkitMapping()); } @@ -266,4 +271,29 @@ public class BukkitConstructor extends Constructor return super.construct(node); } } + + @Override + protected Class getClassForNode(final Node node) + { + Class clazz; + final String name = node.getTag().getClassName(); + if (plugin == null) + { + clazz = super.getClassForNode(node); + } + else + { + final JavaPluginLoader jpl = (JavaPluginLoader)plugin.getPluginLoader(); + clazz = jpl.getClassByName(name); + } + + if (clazz == null) + { + throw new YAMLException("Class not found: " + name); + } + else + { + return clazz; + } + } } diff --git a/Essentials/src/com/earth2me/essentials/storage/YamlStorageReader.java b/Essentials/src/com/earth2me/essentials/storage/YamlStorageReader.java index 321f5fc3c..3aa483e5c 100644 --- a/Essentials/src/com/earth2me/essentials/storage/YamlStorageReader.java +++ b/Essentials/src/com/earth2me/essentials/storage/YamlStorageReader.java @@ -6,6 +6,7 @@ import java.util.*; import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Level; import java.util.logging.Logger; +import org.bukkit.plugin.Plugin; import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; @@ -16,10 +17,12 @@ public class YamlStorageReader implements IStorageReader private transient static Map preparedYamls = Collections.synchronizedMap(new HashMap()); private transient static Map locks = new HashMap(); private transient final Reader reader; + private transient final Plugin plugin; - public YamlStorageReader(final Reader reader) + public YamlStorageReader(final Reader reader, final Plugin plugin) { this.reader = reader; + this.plugin = plugin; } @Override @@ -68,16 +71,16 @@ public class YamlStorageReader implements IStorageReader return ret; } - private static Constructor prepareConstructor(final Class clazz) + private Constructor prepareConstructor(final Class clazz) { - final Constructor constructor = new BukkitConstructor(clazz); + final Constructor constructor = new BukkitConstructor(clazz, plugin); final Set classes = new HashSet(); prepareConstructor(constructor, classes, clazz); return constructor; } - private static void prepareConstructor(final Constructor constructor, final Set classes, final Class clazz) + private void prepareConstructor(final Constructor constructor, final Set classes, final Class clazz) { classes.add(clazz); final TypeDescription description = new TypeDescription(clazz); @@ -94,7 +97,7 @@ public class YamlStorageReader implements IStorageReader constructor.addTypeDescription(description); } - private static void prepareList(final Field field, final TypeDescription description, final Set classes, final Constructor constructor) + private void prepareList(final Field field, final TypeDescription description, final Set classes, final Constructor constructor) { final ListType listType = field.getAnnotation(ListType.class); if (listType != null) @@ -108,7 +111,7 @@ public class YamlStorageReader implements IStorageReader } } - private static void prepareMap(final Field field, final TypeDescription description, final Set classes, final Constructor constructor) + private void prepareMap(final Field field, final TypeDescription description, final Set classes, final Constructor constructor) { final MapValueType mapType = field.getAnnotation(MapValueType.class); if (mapType != null) diff --git a/Essentials/src/com/earth2me/essentials/user/User.java b/Essentials/src/com/earth2me/essentials/user/User.java index 6ad35bfae..70f2c7f3c 100644 --- a/Essentials/src/com/earth2me/essentials/user/User.java +++ b/Essentials/src/com/earth2me/essentials/user/User.java @@ -3,7 +3,6 @@ package com.earth2me.essentials.user; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.storage.AbstractDelayedYamlFileWriter; import com.earth2me.essentials.storage.StorageObject; -import com.earth2me.essentials.storage.YamlStorageReader; import java.util.concurrent.locks.ReentrantReadWriteLock; import lombok.Cleanup; import org.bukkit.OfflinePlayer; @@ -25,11 +24,6 @@ public class User extends UserBase implements IOfflineUser super(offlinePlayer, ess); } - public void loadUserData() - { - data = new YamlStorageReader(null).load(UserData.class); - } - @Override public UserData getData() { diff --git a/Essentials/test/com/earth2me/essentials/StorageTest.java b/Essentials/test/com/earth2me/essentials/StorageTest.java index d57d88542..d4dc1be70 100644 --- a/Essentials/test/com/earth2me/essentials/StorageTest.java +++ b/Essentials/test/com/earth2me/essentials/StorageTest.java @@ -47,11 +47,11 @@ public class StorageTest extends TestCase ext.start(); final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); final Reader reader = new InputStreamReader(bais); - final Settings settings = new YamlStorageReader(reader).load(Settings.class); + final Settings settings = new YamlStorageReader(reader, null).load(Settings.class); ext.mark("load empty settings"); final ByteArrayInputStream bais3 = new ByteArrayInputStream(new byte[0]); final Reader reader3 = new InputStreamReader(bais3); - final Settings settings3 = new YamlStorageReader(reader3).load(Settings.class); + final Settings settings3 = new YamlStorageReader(reader3, null).load(Settings.class); ext.mark("load empty settings (class cached)"); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final PrintWriter writer = new PrintWriter(baos); @@ -62,7 +62,7 @@ public class StorageTest extends TestCase System.out.println(new String(written)); final ByteArrayInputStream bais2 = new ByteArrayInputStream(written); final Reader reader2 = new InputStreamReader(bais2); - final Settings settings2 = new YamlStorageReader(reader2).load(Settings.class); + final Settings settings2 = new YamlStorageReader(reader2, null).load(Settings.class); System.out.println(settings.toString()); System.out.println(settings2.toString()); ext.mark("reload settings"); @@ -80,11 +80,11 @@ public class StorageTest extends TestCase ext.start(); final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); final Reader reader = new InputStreamReader(bais); - final com.earth2me.essentials.user.UserData userdata = new YamlStorageReader(reader).load(com.earth2me.essentials.user.UserData.class); + final com.earth2me.essentials.user.UserData userdata = new YamlStorageReader(reader, null).load(com.earth2me.essentials.user.UserData.class); ext.mark("load empty user"); final ByteArrayInputStream bais3 = new ByteArrayInputStream(new byte[0]); final Reader reader3 = new InputStreamReader(bais3); - final com.earth2me.essentials.user.UserData userdata3 = new YamlStorageReader(reader3).load(com.earth2me.essentials.user.UserData.class); + final com.earth2me.essentials.user.UserData userdata3 = new YamlStorageReader(reader3, null).load(com.earth2me.essentials.user.UserData.class); ext.mark("load empty user (class cached)"); for (int j = 0; j < 10000; j++) @@ -107,11 +107,11 @@ public class StorageTest extends TestCase ext.mark("debug output"); final ByteArrayInputStream bais2 = new ByteArrayInputStream(written); final Reader reader2 = new InputStreamReader(bais2); - final com.earth2me.essentials.user.UserData userdata2 = new YamlStorageReader(reader2).load(com.earth2me.essentials.user.UserData.class); + final com.earth2me.essentials.user.UserData userdata2 = new YamlStorageReader(reader2, null).load(com.earth2me.essentials.user.UserData.class); ext.mark("reload file"); final ByteArrayInputStream bais4 = new ByteArrayInputStream(written); final Reader reader4 = new InputStreamReader(bais4); - final com.earth2me.essentials.user.UserData userdata4 = new YamlStorageReader(reader4).load(com.earth2me.essentials.user.UserData.class); + final com.earth2me.essentials.user.UserData userdata4 = new YamlStorageReader(reader4, null).load(com.earth2me.essentials.user.UserData.class); ext.mark("reload file (cached)"); System.out.println(userdata.toString()); System.out.println(userdata2.toString()); diff --git a/EssentialsSpawn/nbproject/pmd.settings b/EssentialsSpawn/nbproject/pmd.settings new file mode 100644 index 000000000..6a34e356c --- /dev/null +++ b/EssentialsSpawn/nbproject/pmd.settings @@ -0,0 +1 @@ +DoNotUseThreads diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandsetspawn.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandsetspawn.java index 33dff8241..c6c89a20d 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandsetspawn.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandsetspawn.java @@ -14,10 +14,10 @@ public class Commandsetspawn extends EssentialsCommand } @Override - public void run(Server server, User user, String commandLabel, String[] args) throws Exception + public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { final String group = args.length > 0 ? getFinalArg(args, 0) : "default"; - ess.getSpawn().setSpawn(user.getLocation(), group); + ((SpawnStorage)module).setSpawn(user.getLocation(), group); user.sendMessage(_("spawnSet", group)); } } diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java index 635d782f2..fe1630b4d 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java @@ -24,7 +24,7 @@ public class Commandspawn extends EssentialsCommand if (args.length > 0 && user.isAuthorized("essentials.spawn.others")) { final User otherUser = getPlayer(server, args, 0); - otherUser.getTeleport().respawn(ess.getSpawn(), charge); + otherUser.getTeleport().respawn(charge); if (!otherUser.equals(user)) { otherUser.sendMessage(_("teleportAtoB", user.getDisplayName(), "spawn")); @@ -33,7 +33,7 @@ public class Commandspawn extends EssentialsCommand } else { - user.getTeleport().respawn(ess.getSpawn(), charge); + user.getTeleport().respawn(charge); } } @@ -45,7 +45,7 @@ public class Commandspawn extends EssentialsCommand throw new NotEnoughArgumentsException(); } final User user = getPlayer(server, args, 0); - user.getTeleport().respawn(ess.getSpawn(), null); + user.getTeleport().respawn(null); user.sendMessage(_("teleportAtoB", user.getDisplayName(), "spawn")); sender.sendMessage(_("teleporting")); } diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawn.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawn.java index 256faf6c7..6f6244f25 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawn.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawn.java @@ -2,8 +2,10 @@ package com.earth2me.essentials.spawn; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.IEssentialsModule; import java.util.logging.Level; import java.util.logging.Logger; +import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.event.Event.Priority; @@ -14,8 +16,9 @@ import org.bukkit.plugin.java.JavaPlugin; public class EssentialsSpawn extends JavaPlugin { - private static final Logger LOGGER = Logger.getLogger("Minecraft"); + private static final Logger LOGGER = Bukkit.getLogger(); private transient IEssentials ess; + private transient SpawnStorage spawns; public void onEnable() { @@ -25,11 +28,15 @@ public class EssentialsSpawn extends JavaPlugin { LOGGER.log(Level.WARNING, _("versionMismatchAll")); } - if (!ess.isEnabled()) { + if (!ess.isEnabled()) + { this.setEnabled(false); return; } - final EssentialsSpawnPlayerListener playerListener = new EssentialsSpawnPlayerListener(ess); + + spawns = new SpawnStorage(ess); + + final EssentialsSpawnPlayerListener playerListener = new EssentialsSpawnPlayerListener(ess, spawns); pluginManager.registerEvent(Type.PLAYER_RESPAWN, playerListener, Priority.Low, this); pluginManager.registerEvent(Type.PLAYER_JOIN, playerListener, Priority.Low, this); @@ -41,8 +48,9 @@ public class EssentialsSpawn extends JavaPlugin } @Override - public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) + public boolean onCommand(final CommandSender sender, final Command command, + final String commandLabel, final String[] args) { - return ess.onCommandEssentials(sender, command, commandLabel, args, EssentialsSpawn.class.getClassLoader(), "com.earth2me.essentials.spawn.Command", "essentials."); + return ess.onCommandEssentials(sender, command, commandLabel, args, EssentialsSpawn.class.getClassLoader(), "com.earth2me.essentials.spawn.Command", "essentials.", spawns); } } diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java index bca748296..6bb453f12 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java @@ -4,7 +4,7 @@ import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; import java.util.logging.Level; -import java.util.logging.Logger; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerListener; @@ -14,11 +14,13 @@ import org.bukkit.event.player.PlayerRespawnEvent; public class EssentialsSpawnPlayerListener extends PlayerListener { private final transient IEssentials ess; + private final transient SpawnStorage spawns; - public EssentialsSpawnPlayerListener(final IEssentials ess) + public EssentialsSpawnPlayerListener(final IEssentials ess, final SpawnStorage spawns) { super(); this.ess = ess; + this.spawns = spawns; } @Override @@ -39,7 +41,7 @@ public class EssentialsSpawnPlayerListener extends PlayerListener return; } } - final Location spawn = ess.getSpawn().getSpawn(user.getGroup()); + final Location spawn = spawns.getSpawn(user.getGroup()); if (spawn != null) { event.setRespawnLocation(spawn); @@ -58,20 +60,7 @@ public class EssentialsSpawnPlayerListener extends PlayerListener user.setNew(false); if (!"none".equalsIgnoreCase(ess.getSettings().getNewbieSpawn())) { - ess.scheduleSyncDelayedTask(new Runnable() - { - public void run() - { - try - { - user.getTeleport().now(ess.getSpawn().getSpawn(ess.getSettings().getNewbieSpawn()), false); - } - catch (Exception ex) - { - Logger.getLogger("Minecraft").log(Level.WARNING, _("teleportNewPlayerError"), ex); - } - } - }); + ess.scheduleSyncDelayedTask(new NewPlayerTeleport(user)); } if (ess.getSettings().getAnnounceNewPlayers()) @@ -79,4 +68,28 @@ public class EssentialsSpawnPlayerListener extends PlayerListener ess.broadcastMessage(user, ess.getSettings().getAnnounceNewPlayerFormat(user)); } } + + + private class NewPlayerTeleport implements Runnable + { + private final transient User user; + + public NewPlayerTeleport(final User user) + { + this.user = user; + } + + @Override + public void run() + { + try + { + user.getTeleport().now(spawns.getSpawn(ess.getSettings().getNewbieSpawn()), false); + } + catch (Exception ex) + { + Bukkit.getLogger().log(Level.WARNING, _("teleportNewPlayerError"), ex); + } + } + } } diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java new file mode 100644 index 000000000..ac98b5921 --- /dev/null +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java @@ -0,0 +1,145 @@ +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.StorageObject; +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.Location; +import org.bukkit.World; + + +public class SpawnStorage implements IConf, 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(); + } + + public void setSpawn(final Location loc, final String group) + { + rwl.writeLock().lock(); + try + { + if (spawns.getSpawns() == null) + { + spawns.setSpawns(new HashMap()); + } + spawns.getSpawns().put(group.toLowerCase(Locale.ENGLISH), loc); + } + finally + { + rwl.writeLock().unlock(); + } + new SpawnWriter(); + + if ("default".equalsIgnoreCase(group)) + { + loc.getWorld().setSpawnLocation(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); + } + } + + public Location getSpawn(final String group) + { + rwl.readLock().lock(); + try + { + if (spawns == null || spawns.getSpawns() == null || group == null) + { + return getWorldSpawn(); + } + final Map spawnMap = spawns.getSpawns(); + String groupName = group.toLowerCase(Locale.ENGLISH); + if (!spawnMap.containsKey(groupName)) + { + groupName = "default"; + } + if (!spawnMap.containsKey(groupName)) + { + return getWorldSpawn(); + } + return spawnMap.get(groupName); + } + finally + { + rwl.readLock().unlock(); + } + } + + private Location getWorldSpawn() + { + for (World world : ess.getServer().getWorlds()) + { + if (world.getEnvironment() != World.Environment.NORMAL) + { + continue; + } + return world.getSpawnLocation(); + } + 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 onFinish(final Spawns object) + { + spawns = object; + rwl.writeLock().unlock(); + } + } +} diff --git a/EssentialsXMPP/src/com/earth2me/essentials/xmpp/EssentialsXMPP.java b/EssentialsXMPP/src/com/earth2me/essentials/xmpp/EssentialsXMPP.java index 56126e23b..bcbbfa17d 100644 --- a/EssentialsXMPP/src/com/earth2me/essentials/xmpp/EssentialsXMPP.java +++ b/EssentialsXMPP/src/com/earth2me/essentials/xmpp/EssentialsXMPP.java @@ -67,12 +67,13 @@ public class EssentialsXMPP extends JavaPlugin implements IEssentialsXMPP { xmpp.disconnect(); } + instance = null; } @Override public boolean onCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args) { - return ess.onCommandEssentials(sender, command, commandLabel, args, EssentialsXMPP.class.getClassLoader(), "com.earth2me.essentials.xmpp.Command", "essentials."); + return ess.onCommandEssentials(sender, command, commandLabel, args, EssentialsXMPP.class.getClassLoader(), "com.earth2me.essentials.xmpp.Command", "essentials.", null); } @Override -- cgit v1.2.3