Updated Jails to use the new config classes

This commit is contained in:
snowleo 2011-12-06 17:28:48 +01:00
parent 2851a4634c
commit 72e187cd5c
19 changed files with 380 additions and 207 deletions

View File

@ -19,6 +19,7 @@ package com.earth2me.essentials;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.api.Economy;
import com.earth2me.essentials.api.IJails;
import com.earth2me.essentials.commands.EssentialsCommand;
import com.earth2me.essentials.commands.IEssentialsCommand;
import com.earth2me.essentials.commands.NoChargeException;
@ -65,7 +66,7 @@ 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 Jail jail;
private transient Jails jails;
private transient Warps warps;
private transient Worth worth;
private transient List<IConf> confList;
@ -244,16 +245,8 @@ public class Essentials extends JavaPlugin implements IEssentials
pm.registerEvent(Type.FOOD_LEVEL_CHANGE, entityListener, Priority.Lowest, this);
//TODO: Check if this should be here, and not above before reload()
jail = new Jail(this);
final JailPlayerListener jailPlayerListener = new JailPlayerListener(this);
confList.add(jail);
pm.registerEvent(Type.BLOCK_BREAK, jail, Priority.Low, this);
pm.registerEvent(Type.BLOCK_DAMAGE, jail, Priority.Low, this);
pm.registerEvent(Type.BLOCK_PLACE, jail, Priority.Low, this);
pm.registerEvent(Type.PLAYER_INTERACT, jailPlayerListener, Priority.Low, this);
pm.registerEvent(Type.PLAYER_RESPAWN, jailPlayerListener, Priority.High, this);
pm.registerEvent(Type.PLAYER_TELEPORT, jailPlayerListener, Priority.High, this);
pm.registerEvent(Type.PLAYER_JOIN, jailPlayerListener, Priority.High, this);
jails = new Jails(this);
confList.add(jails);
pm.registerEvent(Type.ENTITY_EXPLODE, tntListener, Priority.High, this);
@ -415,9 +408,9 @@ public class Essentials extends JavaPlugin implements IEssentials
}
@Override
public Jail getJail()
public IJails getJails()
{
return jail;
return jails;
}
@Override

View File

@ -729,6 +729,53 @@ public class EssentialsUpgrade
doneFile.setProperty("updateSpawnsToNewSpawnsConfig", true);
doneFile.save();
}
private void updateJailsToNewJailsConfig()
{
if (doneFile.getBoolean("updateJailsToNewJailsConfig", false))
{
return;
}
final File configFile = new File(ess.getDataFolder(), "jail.yml");
if (configFile.exists())
{
final EssentialsConf config = new EssentialsConf(configFile);
try
{
config.load();
if (!config.hasProperty("jails"))
{
final com.earth2me.essentials.settings.Jails jails = new com.earth2me.essentials.settings.Jails();
List<String> keys = config.getKeys();
for (String jailName : keys)
{
Location loc = getFakeLocation(config, jailName);
jails.getJails().put(jailName.toLowerCase(Locale.ENGLISH), loc);
}
if (!configFile.renameTo(new File(ess.getDataFolder(), "jail.yml.old")))
{
throw new Exception(_("fileRenameError", "jail.yml"));
}
PrintWriter writer = new PrintWriter(configFile);
try
{
new YamlStorageWriter(writer).save(jails);
}
finally
{
writer.close();
}
}
}
catch (Exception ex)
{
Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
}
}
doneFile.setProperty("updateJailsToNewJailsConfig", true);
doneFile.save();
}
public void beforeSettings()
{
@ -751,5 +798,6 @@ public class EssentialsUpgrade
updateUsersHomesFormat();
deleteOldItemsCsv();
updateSpawnsToNewSpawnsConfig();
updateJailsToNewJailsConfig();
}
}

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials;
import com.earth2me.essentials.api.IJails;
import com.earth2me.essentials.perm.PermissionsHandler;
import com.earth2me.essentials.register.payment.Methods;
import org.bukkit.World;
@ -34,7 +35,7 @@ public interface IEssentials extends Plugin
BukkitScheduler getScheduler();
Jail getJail();
IJails getJails();
Warps getWarps();

View File

@ -5,6 +5,8 @@ import java.net.InetSocketAddress;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.inventory.PlayerInventory;
/**
* @deprecated This will be moved to the api package soon
*/
@ -58,4 +60,8 @@ public interface IUser
String getDisplayName();
boolean isHidden();
Teleport getTeleport();
void setJail(String jail);
}

View File

@ -1,100 +0,0 @@
package com.earth2me.essentials;
import static com.earth2me.essentials.I18n._;
import java.io.File;
import java.util.List;
import java.util.Locale;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.BlockPlaceEvent;
public class Jail extends BlockListener implements IConf
{
private static final Logger logger = Logger.getLogger("Minecraft");
private final EssentialsConf config;
private final IEssentials ess;
public Jail(IEssentials ess)
{
this.ess = ess;
config = new EssentialsConf(new File(ess.getDataFolder(), "jail.yml"));
config.load();
}
public void setJail(Location loc, String jailName) throws Exception
{
config.setProperty(jailName.toLowerCase(Locale.ENGLISH), loc);
config.save();
}
public Location getJail(String jailName) throws Exception
{
if (jailName == null || config.getProperty(jailName.toLowerCase(Locale.ENGLISH)) == null)
{
throw new Exception(_("jailNotExist"));
}
Location loc = config.getLocation(jailName.toLowerCase(Locale.ENGLISH), ess.getServer());
return loc;
}
public void sendToJail(User user, String jail) throws Exception
{
if (!(user.getBase() instanceof OfflinePlayer))
{
user.getTeleport().now(getJail(jail), false);
}
user.setJail(jail);
}
public void delJail(String jail) throws Exception
{
config.removeProperty(jail.toLowerCase(Locale.ENGLISH));
config.save();
}
public List<String> getJails() throws Exception
{
return config.getKeys(null);
}
@Override
public void reloadConfig()
{
config.load();
}
@Override
public void onBlockBreak(BlockBreakEvent event)
{
User user = ess.getUser(event.getPlayer());
if (user.isJailed())
{
event.setCancelled(true);
}
}
@Override
public void onBlockPlace(BlockPlaceEvent event)
{
User user = ess.getUser(event.getPlayer());
if (user.isJailed())
{
event.setCancelled(true);
}
}
@Override
public void onBlockDamage(BlockDamageEvent event)
{
User user = ess.getUser(event.getPlayer());
if (user.isJailed())
{
event.setCancelled(true);
}
}
}

View File

@ -1,81 +0,0 @@
package com.earth2me.essentials;
import static com.earth2me.essentials.I18n._;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.event.player.*;
public class JailPlayerListener extends PlayerListener
{
private static final Logger LOGGER = Logger.getLogger("Minecraft");
private final IEssentials ess;
public JailPlayerListener(IEssentials parent)
{
this.ess = parent;
}
@Override
public void onPlayerInteract(PlayerInteractEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (user.isJailed())
{
event.setCancelled(true);
}
}
@Override
public void onPlayerRespawn(PlayerRespawnEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (user.isJailed() && user.getJail() != null && !user.getJail().isEmpty())
{
try
{
event.setRespawnLocation(ess.getJail().getJail(user.getJail()));
}
catch (Exception ex)
{
}
}
}
@Override
public void onPlayerTeleport(PlayerTeleportEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (!user.isJailed() || user.getJail() == null || user.getJail().isEmpty())
{
return;
}
try
{
event.setTo(ess.getJail().getJail(user.getJail()));
}
catch (Exception ex)
{
LOGGER.log(Level.WARNING, _("returnPlayerToJailError"), ex);
}
user.sendMessage(_("jailMessage"));
}
@Override
public void onPlayerJoin(final PlayerJoinEvent event)
{
User u = ess.getUser(event.getPlayer());
if (u.isJailed())
{
try
{
ess.getJail().sendToJail(u, u.getJail());
}
catch (Exception ex)
{
LOGGER.log(Level.WARNING, _("returnPlayerToJailError"), ex);
}
u.sendMessage(_("jailMessage"));
}
}
}

View File

@ -0,0 +1,246 @@
package com.earth2me.essentials;
import com.earth2me.essentials.api.IJails;
import com.earth2me.essentials.storage.AsyncStorageObjectHolder;
import static com.earth2me.essentials.I18n._;
import java.io.File;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event.Type;
import org.bukkit.event.block.*;
import org.bukkit.event.player.*;
import org.bukkit.plugin.PluginManager;
public class Jails extends AsyncStorageObjectHolder<com.earth2me.essentials.settings.Jails> implements IJails
{
private static final transient Logger LOGGER = Bukkit.getLogger();
public Jails(final IEssentials ess)
{
super(ess, com.earth2me.essentials.settings.Jails.class);
registerListeners();
}
private void registerListeners()
{
final PluginManager pluginManager = ess.getServer().getPluginManager();
final JailBlockListener blockListener = new JailBlockListener();
final JailPlayerListener playerListener = new JailPlayerListener();
pluginManager.registerEvent(Type.BLOCK_BREAK, blockListener, Priority.Low, ess);
pluginManager.registerEvent(Type.BLOCK_DAMAGE, blockListener, Priority.Low, ess);
pluginManager.registerEvent(Type.BLOCK_PLACE, blockListener, Priority.Low, ess);
pluginManager.registerEvent(Type.PLAYER_INTERACT, playerListener, Priority.Low, ess);
pluginManager.registerEvent(Type.PLAYER_RESPAWN, playerListener, Priority.High, ess);
pluginManager.registerEvent(Type.PLAYER_TELEPORT, playerListener, Priority.High, ess);
pluginManager.registerEvent(Type.PLAYER_JOIN, playerListener, Priority.High, ess);
}
@Override
public File getStorageFile()
{
return new File(ess.getDataFolder(), "jail.yml");
}
@Override
public Location getJail(final String jailName) throws Exception
{
acquireReadLock();
try
{
if (getData().getJails() == null || jailName == null
|| !getData().getJails().containsKey(jailName.toLowerCase(Locale.ENGLISH)))
{
throw new Exception(_("jailNotExist"));
}
return getData().getJails().get(jailName.toLowerCase(Locale.ENGLISH));
}
finally
{
unlock();
}
}
@Override
public Collection<String> getList() throws Exception
{
acquireReadLock();
try
{
if (getData().getJails() == null)
{
return Collections.emptyList();
}
return new ArrayList<String>(getData().getJails().keySet());
}
finally
{
unlock();
}
}
@Override
public void removeJail(final String jail) throws Exception
{
acquireWriteLock();
try
{
if (getData().getJails() == null)
{
return;
}
getData().getJails().remove(jail.toLowerCase(Locale.ENGLISH));
}
finally
{
unlock();
}
}
@Override
public void sendToJail(final IUser user, final String jail) throws Exception
{
acquireReadLock();
try
{
if (!(user.getBase() instanceof OfflinePlayer))
{
user.getTeleport().now(getJail(jail), false);
}
user.setJail(jail);
}
finally
{
unlock();
}
}
@Override
public void setJail(final String jailName, final Location loc) throws Exception
{
acquireWriteLock();
try
{
if (getData().getJails() == null)
{
getData().setJails(new HashMap<String, Location>());
}
getData().getJails().put(jailName.toLowerCase(Locale.ENGLISH), loc);
}
finally
{
unlock();
}
}
private class JailBlockListener extends BlockListener
{
@Override
public void onBlockBreak(final BlockBreakEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (user.isJailed())
{
event.setCancelled(true);
}
}
@Override
public void onBlockPlace(final BlockPlaceEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (user.isJailed())
{
event.setCancelled(true);
}
}
@Override
public void onBlockDamage(final BlockDamageEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (user.isJailed())
{
event.setCancelled(true);
}
}
}
private class JailPlayerListener extends PlayerListener
{
@Override
public void onPlayerInteract(final PlayerInteractEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (user.isJailed())
{
event.setCancelled(true);
}
}
@Override
public void onPlayerRespawn(final PlayerRespawnEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (!user.isJailed() || user.getJail() == null || user.getJail().isEmpty())
{
return;
}
try
{
event.setRespawnLocation(getJail(user.getJail()));
}
catch (Exception ex)
{
LOGGER.log(Level.WARNING, _("returnPlayerToJailError"), ex);
}
}
@Override
public void onPlayerTeleport(final PlayerTeleportEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (!user.isJailed() || user.getJail() == null || user.getJail().isEmpty())
{
return;
}
try
{
event.setTo(getJail(user.getJail()));
}
catch (Exception ex)
{
LOGGER.log(Level.WARNING, _("returnPlayerToJailError"), ex);
}
user.sendMessage(_("jailMessage"));
}
@Override
public void onPlayerJoin(final PlayerJoinEvent event)
{
final User user = ess.getUser(event.getPlayer());
if (!user.isJailed() || user.getJail() == null || user.getJail().isEmpty())
{
return;
}
try
{
sendToJail(user, user.getJail());
}
catch (Exception ex)
{
LOGGER.log(Level.WARNING, _("returnPlayerToJailError"), ex);
}
user.sendMessage(_("jailMessage"));
}
}
}

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials;
import com.earth2me.essentials.api.ITeleport;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.commands.NotEnoughArgumentsException;
import java.util.Calendar;
@ -11,7 +12,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerRespawnEvent;
public class Teleport implements Runnable
public class Teleport implements Runnable, ITeleport
{
private static final double MOVE_CONSTANT = 0.3;

View File

@ -19,7 +19,7 @@ public interface IEssentials extends Plugin, IReload
ISettings getSettings();
IJail getJail();
IJails getJail();
IWarps getWarps();

View File

@ -4,15 +4,15 @@ import java.util.Collection;
import org.bukkit.Location;
public interface IJail extends IReload
public interface IJails extends IReload
{
Location getJail(String jailName) throws Exception;
Collection<String> getJails() throws Exception;
Collection<String> getList() throws Exception;
void removeJail(String jail) throws Exception;
void sendToJail(IUser user, String jail) throws Exception;
void sendToJail(com.earth2me.essentials.IUser user, String jail) throws Exception;
void setJail(String jailName, Location loc) throws Exception;
}

View File

@ -0,0 +1,9 @@
package com.earth2me.essentials.api;
import org.bukkit.Location;
public interface ITeleport
{
void now(Location loc, boolean cooldown) throws Exception;
}

View File

@ -36,4 +36,8 @@ public interface IUser extends Player, IReload
Location getHome(Location loc) throws Exception;
boolean isHidden();
ITeleport getTeleport();
void setJail(String jail);
}

View File

@ -19,7 +19,7 @@ public class Commanddeljail extends EssentialsCommand
{
throw new NotEnoughArgumentsException();
}
ess.getJail().delJail(args[0]);
ess.getJails().removeJail(args[0]);
sender.sendMessage(_("deleteJail", args[0]));
}
}

View File

@ -15,6 +15,6 @@ public class Commandjails extends EssentialsCommand
@Override
protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
{
sender.sendMessage("§7" + Util.joinList(" ", ess.getJail().getJails()));
sender.sendMessage("§7" + Util.joinList(" ", ess.getJails().getList()));
}
}

View File

@ -19,7 +19,7 @@ public class Commandsetjail extends EssentialsCommand
{
throw new NotEnoughArgumentsException();
}
ess.getJail().setJail(user.getLocation(), args[0]);
ess.getJails().setJail(args[0], user.getLocation());
user.sendMessage(_("jailSet", args[0]));
}

View File

@ -47,12 +47,12 @@ public class Commandtogglejail extends EssentialsCommand
}
if (!(player.getBase() instanceof OfflinePlayer))
{
ess.getJail().sendToJail(player, args[1]);
ess.getJails().sendToJail(player, args[1]);
}
else
{
// Check if jail exists
ess.getJail().getJail(args[1]);
ess.getJails().getJail(args[1]);
}
player.setJailed(true);
player.sendMessage(_("userJailed"));

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 Jails implements StorageObject
{
@MapValueType(Location.class)
private Map<String, Location> jails = new HashMap<String, Location>();
}

View File

@ -2,13 +2,14 @@ package com.earth2me.essentials.storage;
import com.earth2me.essentials.IConf;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.api.IReload;
import java.io.File;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import org.bukkit.Bukkit;
public abstract class AsyncStorageObjectHolder<T extends StorageObject> implements IConf, IStorageObjectHolder<T>
public abstract class AsyncStorageObjectHolder<T extends StorageObject> implements IConf, IStorageObjectHolder<T>, IReload
{
private transient T data;
private final transient ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
@ -53,7 +54,7 @@ public abstract class AsyncStorageObjectHolder<T extends StorageObject> implemen
{
unlock();
}
public void unlock()
{
if (rwl.isWriteLockedByCurrentThread())
@ -73,6 +74,12 @@ public abstract class AsyncStorageObjectHolder<T extends StorageObject> implemen
new StorageObjectDataReader();
}
@Override
public void onReload()
{
new StorageObjectDataReader();
}
public abstract File getStorageFile();

View File

@ -2,6 +2,7 @@ package com.earth2me.essentials.user;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.IUser;
import com.earth2me.essentials.Teleport;
import com.earth2me.essentials.commands.IEssentialsCommand;
import lombok.Cleanup;
import org.bukkit.Location;
@ -175,4 +176,24 @@ public class User extends UserBase implements IUser
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Teleport getTeleport()
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void setJail(final String jail)
{
acquireWriteLock();
try
{
getData().setJail(jail);
}
finally
{
unlock();
}
}
}