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. --- .../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 +++++++++++++++++++++ 5 files changed, 193 insertions(+), 27 deletions(-) create mode 100644 EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java (limited to 'EssentialsSpawn/src/com') 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(); + } + } +} -- cgit v1.2.3