Updated EssentialsSpawn to use the new config code

/spawn and /home now call the PlayerRespawnEvent to make it more compatible with other plugins.
This commit is contained in:
snowleo 2011-12-06 13:41:29 +01:00
parent f3b278eac2
commit 019b49ef11
21 changed files with 406 additions and 79 deletions

View File

@ -65,7 +65,6 @@ public class Essentials extends JavaPlugin implements IEssentials
private static final Logger LOGGER = Logger.getLogger("Minecraft"); private static final Logger LOGGER = Logger.getLogger("Minecraft");
private transient ISettings settings; private transient ISettings settings;
private final transient TNTExplodeListener tntListener = new TNTExplodeListener(this); private final transient TNTExplodeListener tntListener = new TNTExplodeListener(this);
private transient Spawn spawn;
private transient Jail jail; private transient Jail jail;
private transient Warps warps; private transient Warps warps;
private transient Worth worth; private transient Worth worth;
@ -159,8 +158,6 @@ public class Essentials extends JavaPlugin implements IEssentials
userMap = new UserMap(this); userMap = new UserMap(this);
confList.add(userMap); confList.add(userMap);
execTimer.mark("Init(Usermap)"); execTimer.mark("Init(Usermap)");
spawn = new Spawn(getServer(), this.getDataFolder());
confList.add(spawn);
warps = new Warps(getServer(), this.getDataFolder()); warps = new Warps(getServer(), this.getDataFolder());
confList.add(warps); confList.add(warps);
execTimer.mark("Init(Spawn/Warp)"); execTimer.mark("Init(Spawn/Warp)");
@ -297,11 +294,11 @@ public class Essentials extends JavaPlugin implements IEssentials
@Override @Override
public boolean onCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args) 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 @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 // Allow plugins to override the command via onCommand
if (!getSettings().isCommandOverridden(command.getName()) && !commandLabel.startsWith("e")) 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 = (IEssentialsCommand)classLoader.loadClass(commandPath + command.getName()).newInstance();
cmd.setEssentials(this); cmd.setEssentials(this);
cmd.setEssentialsModule(module);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -442,12 +440,6 @@ public class Essentials extends JavaPlugin implements IEssentials
return backup; return backup;
} }
@Override
public Spawn getSpawn()
{
return spawn;
}
@Override @Override
public User getUser(final Object base) public User getUser(final Object base)
{ {

View File

@ -1,5 +1,7 @@
package com.earth2me.essentials; package com.earth2me.essentials;
import com.earth2me.essentials.settings.Spawns;
import com.earth2me.essentials.storage.YamlStorageWriter;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import java.io.*; import java.io.*;
import java.math.BigInteger; 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<String> 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() public void beforeSettings()
{ {
if (!ess.getDataFolder().exists()) if (!ess.getDataFolder().exists())
@ -701,5 +750,6 @@ public class EssentialsUpgrade
updateUsersPowerToolsFormat(); updateUsersPowerToolsFormat();
updateUsersHomesFormat(); updateUsersHomesFormat();
deleteOldItemsCsv(); deleteOldItemsCsv();
updateSpawnsToNewSpawnsConfig();
} }
} }

View File

@ -15,10 +15,10 @@ public interface IEssentials extends Plugin
void reload(); 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); User getUser(Object base);
I18n getI18n(); I18n getI18n();
User getOfflineUser(String name); User getOfflineUser(String name);
@ -39,8 +39,6 @@ public interface IEssentials extends Plugin
Backup getBackup(); Backup getBackup();
Spawn getSpawn();
Methods getPaymentMethod(); Methods getPaymentMethod();
int scheduleAsyncDelayedTask(Runnable run); int scheduleAsyncDelayedTask(Runnable run);
@ -54,7 +52,7 @@ public interface IEssentials extends Plugin
TNTExplodeListener getTNTListener(); TNTExplodeListener getTNTListener();
PermissionsHandler getPermissionsHandler(); PermissionsHandler getPermissionsHandler();
AlternativeCommandsHandler getAlternativeCommandsHandler(); AlternativeCommandsHandler getAlternativeCommandsHandler();
void showError(final CommandSender sender, final Throwable exception, final String commandLabel); void showError(final CommandSender sender, final Throwable exception, final String commandLabel);

View File

@ -0,0 +1,6 @@
package com.earth2me.essentials;
public interface IEssentialsModule
{
}

View File

@ -7,6 +7,8 @@ import java.util.GregorianCalendar;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerRespawnEvent;
public class Teleport implements Runnable public class Teleport implements Runnable
@ -122,9 +124,13 @@ public class Teleport implements Runnable
this.ess = ess; 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 public void warp(String warp, Trade chargeFor) throws Exception

View File

@ -43,7 +43,8 @@ public class Commandhome extends EssentialsCommand
} }
try try
{ {
if ("bed".equalsIgnoreCase(homeName)) { if ("bed".equalsIgnoreCase(homeName))
{
final Location bed = player.getBedSpawnLocation(); final Location bed = player.getBedSpawnLocation();
if (bed != null) if (bed != null)
{ {
@ -58,18 +59,7 @@ public class Commandhome extends EssentialsCommand
final List<String> homes = player.getHomes(); final List<String> homes = player.getHomes();
if (homes.isEmpty() && player.equals(user)) if (homes.isEmpty() && player.equals(user))
{ {
final Location loc = player.getBedSpawnLocation(); user.getTeleport().respawn(charge);
if (loc == null)
{
if (ess.getSettings().spawnIfNoHome())
{
user.getTeleport().respawn(ess.getSpawn(), charge);
}
}
else
{
user.getTeleport().teleport(loc, charge);
}
} }
else if (homes.isEmpty()) else if (homes.isEmpty())
{ {

View File

@ -2,6 +2,7 @@ package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.IEssentialsModule;
import com.earth2me.essentials.OfflinePlayer; import com.earth2me.essentials.OfflinePlayer;
import com.earth2me.essentials.Trade; import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
@ -17,6 +18,7 @@ public abstract class EssentialsCommand implements IEssentialsCommand
{ {
private final transient String name; private final transient String name;
protected transient IEssentials ess; protected transient IEssentials ess;
protected transient IEssentialsModule module;
protected final static Logger logger = Logger.getLogger("Minecraft"); protected final static Logger logger = Logger.getLogger("Minecraft");
protected EssentialsCommand(final String name) protected EssentialsCommand(final String name)
@ -29,6 +31,12 @@ public abstract class EssentialsCommand implements IEssentialsCommand
{ {
this.ess = ess; this.ess = ess;
} }
@Override
public void setEssentialsModule(final IEssentialsModule module)
{
this.module = module;
}
@Override @Override
public String getName() public String getName()

View File

@ -1,6 +1,7 @@
package com.earth2me.essentials.commands; package com.earth2me.essentials.commands;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.IEssentialsModule;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -18,4 +19,6 @@ public interface IEssentialsCommand
throws Exception; throws Exception;
void setEssentials(IEssentials ess); void setEssentials(IEssentials ess);
void setEssentialsModule(IEssentialsModule module);
} }

View File

@ -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<String, Location> spawns = new HashMap<String, Location>();
}

View File

@ -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<T extends StorageObject> implements Runnable
{
private final transient File file;
private final transient Class<T> clazz;
private final transient Plugin plugin;
public AbstractDelayedYamlFileReader(final IEssentials ess, final File file, final Class<T> 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);
}

View File

@ -9,17 +9,22 @@ import org.bukkit.World;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData; 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.constructor.Constructor;
import org.yaml.snakeyaml.error.YAMLException;
import org.yaml.snakeyaml.nodes.*; import org.yaml.snakeyaml.nodes.*;
public class BukkitConstructor extends Constructor public class BukkitConstructor extends Constructor
{ {
private final transient Pattern NUMPATTERN = Pattern.compile("\\d+"); 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); super(clazz);
this.plugin = plugin;
yamlClassConstructors.put(NodeId.scalar, new ConstructBukkitScalar()); yamlClassConstructors.put(NodeId.scalar, new ConstructBukkitScalar());
yamlClassConstructors.put(NodeId.mapping, new ConstructBukkitMapping()); yamlClassConstructors.put(NodeId.mapping, new ConstructBukkitMapping());
} }
@ -266,4 +271,29 @@ public class BukkitConstructor extends Constructor
return super.construct(node); 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;
}
}
} }

View File

@ -6,6 +6,7 @@ import java.util.*;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.plugin.Plugin;
import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.constructor.Constructor;
@ -16,10 +17,12 @@ public class YamlStorageReader implements IStorageReader
private transient static Map<Class, Yaml> preparedYamls = Collections.synchronizedMap(new HashMap<Class, Yaml>()); private transient static Map<Class, Yaml> preparedYamls = Collections.synchronizedMap(new HashMap<Class, Yaml>());
private transient static Map<Class, ReentrantLock> locks = new HashMap<Class, ReentrantLock>(); private transient static Map<Class, ReentrantLock> locks = new HashMap<Class, ReentrantLock>();
private transient final Reader reader; 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.reader = reader;
this.plugin = plugin;
} }
@Override @Override
@ -68,16 +71,16 @@ public class YamlStorageReader implements IStorageReader
return ret; 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<Class> classes = new HashSet<Class>(); final Set<Class> classes = new HashSet<Class>();
prepareConstructor(constructor, classes, clazz); prepareConstructor(constructor, classes, clazz);
return constructor; return constructor;
} }
private static void prepareConstructor(final Constructor constructor, final Set<Class> classes, final Class clazz) private void prepareConstructor(final Constructor constructor, final Set<Class> classes, final Class clazz)
{ {
classes.add(clazz); classes.add(clazz);
final TypeDescription description = new TypeDescription(clazz); final TypeDescription description = new TypeDescription(clazz);
@ -94,7 +97,7 @@ public class YamlStorageReader implements IStorageReader
constructor.addTypeDescription(description); constructor.addTypeDescription(description);
} }
private static void prepareList(final Field field, final TypeDescription description, final Set<Class> classes, final Constructor constructor) private void prepareList(final Field field, final TypeDescription description, final Set<Class> classes, final Constructor constructor)
{ {
final ListType listType = field.getAnnotation(ListType.class); final ListType listType = field.getAnnotation(ListType.class);
if (listType != null) if (listType != null)
@ -108,7 +111,7 @@ public class YamlStorageReader implements IStorageReader
} }
} }
private static void prepareMap(final Field field, final TypeDescription description, final Set<Class> classes, final Constructor constructor) private void prepareMap(final Field field, final TypeDescription description, final Set<Class> classes, final Constructor constructor)
{ {
final MapValueType mapType = field.getAnnotation(MapValueType.class); final MapValueType mapType = field.getAnnotation(MapValueType.class);
if (mapType != null) if (mapType != null)

View File

@ -3,7 +3,6 @@ package com.earth2me.essentials.user;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.storage.AbstractDelayedYamlFileWriter; import com.earth2me.essentials.storage.AbstractDelayedYamlFileWriter;
import com.earth2me.essentials.storage.StorageObject; import com.earth2me.essentials.storage.StorageObject;
import com.earth2me.essentials.storage.YamlStorageReader;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import lombok.Cleanup; import lombok.Cleanup;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -25,11 +24,6 @@ public class User extends UserBase implements IOfflineUser
super(offlinePlayer, ess); super(offlinePlayer, ess);
} }
public void loadUserData()
{
data = new YamlStorageReader(null).load(UserData.class);
}
@Override @Override
public UserData getData() public UserData getData()
{ {

View File

@ -47,11 +47,11 @@ public class StorageTest extends TestCase
ext.start(); ext.start();
final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
final Reader reader = new InputStreamReader(bais); 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"); ext.mark("load empty settings");
final ByteArrayInputStream bais3 = new ByteArrayInputStream(new byte[0]); final ByteArrayInputStream bais3 = new ByteArrayInputStream(new byte[0]);
final Reader reader3 = new InputStreamReader(bais3); 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)"); ext.mark("load empty settings (class cached)");
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PrintWriter writer = new PrintWriter(baos); final PrintWriter writer = new PrintWriter(baos);
@ -62,7 +62,7 @@ public class StorageTest extends TestCase
System.out.println(new String(written)); System.out.println(new String(written));
final ByteArrayInputStream bais2 = new ByteArrayInputStream(written); final ByteArrayInputStream bais2 = new ByteArrayInputStream(written);
final Reader reader2 = new InputStreamReader(bais2); 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(settings.toString());
System.out.println(settings2.toString()); System.out.println(settings2.toString());
ext.mark("reload settings"); ext.mark("reload settings");
@ -80,11 +80,11 @@ public class StorageTest extends TestCase
ext.start(); ext.start();
final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
final Reader reader = new InputStreamReader(bais); 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"); ext.mark("load empty user");
final ByteArrayInputStream bais3 = new ByteArrayInputStream(new byte[0]); final ByteArrayInputStream bais3 = new ByteArrayInputStream(new byte[0]);
final Reader reader3 = new InputStreamReader(bais3); 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)"); ext.mark("load empty user (class cached)");
for (int j = 0; j < 10000; j++) for (int j = 0; j < 10000; j++)
@ -107,11 +107,11 @@ public class StorageTest extends TestCase
ext.mark("debug output"); ext.mark("debug output");
final ByteArrayInputStream bais2 = new ByteArrayInputStream(written); final ByteArrayInputStream bais2 = new ByteArrayInputStream(written);
final Reader reader2 = new InputStreamReader(bais2); 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"); ext.mark("reload file");
final ByteArrayInputStream bais4 = new ByteArrayInputStream(written); final ByteArrayInputStream bais4 = new ByteArrayInputStream(written);
final Reader reader4 = new InputStreamReader(bais4); 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)"); ext.mark("reload file (cached)");
System.out.println(userdata.toString()); System.out.println(userdata.toString());
System.out.println(userdata2.toString()); System.out.println(userdata2.toString());

View File

@ -0,0 +1 @@
DoNotUseThreads

View File

@ -14,10 +14,10 @@ public class Commandsetspawn extends EssentialsCommand
} }
@Override @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"; 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)); user.sendMessage(_("spawnSet", group));
} }
} }

View File

@ -24,7 +24,7 @@ public class Commandspawn extends EssentialsCommand
if (args.length > 0 && user.isAuthorized("essentials.spawn.others")) if (args.length > 0 && user.isAuthorized("essentials.spawn.others"))
{ {
final User otherUser = getPlayer(server, args, 0); final User otherUser = getPlayer(server, args, 0);
otherUser.getTeleport().respawn(ess.getSpawn(), charge); otherUser.getTeleport().respawn(charge);
if (!otherUser.equals(user)) if (!otherUser.equals(user))
{ {
otherUser.sendMessage(_("teleportAtoB", user.getDisplayName(), "spawn")); otherUser.sendMessage(_("teleportAtoB", user.getDisplayName(), "spawn"));
@ -33,7 +33,7 @@ public class Commandspawn extends EssentialsCommand
} }
else else
{ {
user.getTeleport().respawn(ess.getSpawn(), charge); user.getTeleport().respawn(charge);
} }
} }
@ -45,7 +45,7 @@ public class Commandspawn extends EssentialsCommand
throw new NotEnoughArgumentsException(); throw new NotEnoughArgumentsException();
} }
final User user = getPlayer(server, args, 0); final User user = getPlayer(server, args, 0);
user.getTeleport().respawn(ess.getSpawn(), null); user.getTeleport().respawn(null);
user.sendMessage(_("teleportAtoB", user.getDisplayName(), "spawn")); user.sendMessage(_("teleportAtoB", user.getDisplayName(), "spawn"));
sender.sendMessage(_("teleporting")); sender.sendMessage(_("teleporting"));
} }

View File

@ -2,8 +2,10 @@ package com.earth2me.essentials.spawn;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.IEssentialsModule;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.event.Event.Priority; import org.bukkit.event.Event.Priority;
@ -14,8 +16,9 @@ import org.bukkit.plugin.java.JavaPlugin;
public class EssentialsSpawn extends 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 IEssentials ess;
private transient SpawnStorage spawns;
public void onEnable() public void onEnable()
{ {
@ -25,11 +28,15 @@ public class EssentialsSpawn extends JavaPlugin
{ {
LOGGER.log(Level.WARNING, _("versionMismatchAll")); LOGGER.log(Level.WARNING, _("versionMismatchAll"));
} }
if (!ess.isEnabled()) { if (!ess.isEnabled())
{
this.setEnabled(false); this.setEnabled(false);
return; 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_RESPAWN, playerListener, Priority.Low, this);
pluginManager.registerEvent(Type.PLAYER_JOIN, playerListener, Priority.Low, this); pluginManager.registerEvent(Type.PLAYER_JOIN, playerListener, Priority.Low, this);
@ -41,8 +48,9 @@ public class EssentialsSpawn extends JavaPlugin
} }
@Override @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);
} }
} }

View File

@ -4,7 +4,7 @@ import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerListener; import org.bukkit.event.player.PlayerListener;
@ -14,11 +14,13 @@ import org.bukkit.event.player.PlayerRespawnEvent;
public class EssentialsSpawnPlayerListener extends PlayerListener public class EssentialsSpawnPlayerListener extends PlayerListener
{ {
private final transient IEssentials ess; private final transient IEssentials ess;
private final transient SpawnStorage spawns;
public EssentialsSpawnPlayerListener(final IEssentials ess) public EssentialsSpawnPlayerListener(final IEssentials ess, final SpawnStorage spawns)
{ {
super(); super();
this.ess = ess; this.ess = ess;
this.spawns = spawns;
} }
@Override @Override
@ -39,7 +41,7 @@ public class EssentialsSpawnPlayerListener extends PlayerListener
return; return;
} }
} }
final Location spawn = ess.getSpawn().getSpawn(user.getGroup()); final Location spawn = spawns.getSpawn(user.getGroup());
if (spawn != null) if (spawn != null)
{ {
event.setRespawnLocation(spawn); event.setRespawnLocation(spawn);
@ -58,20 +60,7 @@ public class EssentialsSpawnPlayerListener extends PlayerListener
user.setNew(false); user.setNew(false);
if (!"none".equalsIgnoreCase(ess.getSettings().getNewbieSpawn())) if (!"none".equalsIgnoreCase(ess.getSettings().getNewbieSpawn()))
{ {
ess.scheduleSyncDelayedTask(new Runnable() ess.scheduleSyncDelayedTask(new NewPlayerTeleport(user));
{
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);
}
}
});
} }
if (ess.getSettings().getAnnounceNewPlayers()) if (ess.getSettings().getAnnounceNewPlayers())
@ -79,4 +68,28 @@ public class EssentialsSpawnPlayerListener extends PlayerListener
ess.broadcastMessage(user, ess.getSettings().getAnnounceNewPlayerFormat(user)); 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);
}
}
}
} }

View File

@ -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<String, Location>());
}
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<String, Location> 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<Spawns>
{
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();
}
}
}

View File

@ -67,12 +67,13 @@ public class EssentialsXMPP extends JavaPlugin implements IEssentialsXMPP
{ {
xmpp.disconnect(); xmpp.disconnect();
} }
instance = null;
} }
@Override @Override
public boolean onCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args) 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 @Override