[API] Add a warp api, with future 3.x support :: minor cleanup

This commit is contained in:
Iaccidentally 2013-04-28 16:23:45 -04:00
parent 02df03a4b1
commit 0a03696983
6 changed files with 130 additions and 3 deletions

View File

@ -43,6 +43,7 @@ public class I18n implements II18n
instance = null;
}
@Override
public Locale getCurrentLocale()
{
return currentLocale;

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials;
public interface IConf {
public interface IConf
{
public void reloadConfig();
}

View File

@ -2,8 +2,17 @@ package com.earth2me.essentials;
import org.bukkit.command.CommandSender;
public interface IReplyTo {
public interface IReplyTo
{
/**
* Sets the user to reply to
* @param user
*/
public void setReplyTo(CommandSender user);
/**
* Gets the user the sender should reply to
* @return
*/
public CommandSender getReplyTo();
}

View File

@ -1,6 +1,8 @@
package com.earth2me.essentials;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.api.IWarps;
import com.earth2me.essentials.api.InvalidNameException;
import com.earth2me.essentials.commands.WarpNotFoundException;
import java.io.File;
import java.io.IOException;
@ -11,7 +13,7 @@ import org.bukkit.Location;
import org.bukkit.Server;
public class Warps implements IConf
public class Warps implements IConf, IWarps
{
private static final Logger logger = Logger.getLogger("Minecraft");
private final Map<StringIgnoreCase, EssentialsConf> warpPoints = new HashMap<StringIgnoreCase, EssentialsConf>();
@ -29,6 +31,7 @@ public class Warps implements IConf
reloadConfig();
}
@Override
public boolean isEmpty()
{
return warpPoints.isEmpty();
@ -45,6 +48,7 @@ public class Warps implements IConf
return keys;
}
@Override
public Location getWarp(String warp) throws Exception
{
EssentialsConf conf = warpPoints.get(new StringIgnoreCase(warp));
@ -55,6 +59,7 @@ public class Warps implements IConf
return conf.getLocation(null, server);
}
@Override
public void setWarp(String name, Location loc) throws Exception
{
String filename = Util.sanitizeFileName(name);
@ -126,6 +131,41 @@ public class Warps implements IConf
}
}
// This is for api support, and so 3.x will not break this api
@Override
public Collection<String> getList()
{
final List<String> keys = new ArrayList<String>();
for (StringIgnoreCase stringIgnoreCase : warpPoints.keySet())
{
keys.add(stringIgnoreCase.getString());
}
Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);
return keys;
}
// This is for api support, and so 3.x will not break this api
@Override
public void removeWarp(String name) throws Exception
{
EssentialsConf conf = warpPoints.get(new StringIgnoreCase(name));
if (conf == null)
{
throw new Exception(_("warpNotExist"));
}
if (!conf.getFile().delete())
{
throw new Exception(_("warpDeleteError"));
}
warpPoints.remove(new StringIgnoreCase(name));
}
//This is here for future 3.x api support. Not implemented here becasue storage is handled differently
@Override
public File getWarpFile(String name) throws InvalidNameException
{
throw new UnsupportedOperationException("Not supported yet.");
}
private static class StringIgnoreCase
{

View File

@ -0,0 +1,60 @@
package com.earth2me.essentials.api;
import java.io.File;
import java.util.Collection;
import org.bukkit.Location;
public interface IWarps
{
/**
* Get a warp by name
*
* @param warp - Warp name
* @return - Location the warp is set to
* @throws Exception
*/
Location getWarp(String warp) throws Exception;
/**
* Gets a list of warps
*
* @return - A {@link Collection} of warps
*/
Collection<String> getList();
/**
* Delete a warp from the warp DB
*
* @param name - Name of warp
* @throws Exception
*/
void removeWarp(String name) throws Exception;
/**
* Set a warp
*
* @param name - Name of warp
* @param loc - Location of warp
* @throws Exception
*/
void setWarp(String name, Location loc) throws Exception;
/**
* Check to see if the file is empty
*
* @return
*/
boolean isEmpty();
/**
* Get a warp file
* note: this is not yet implemented, as 3.x uses different storage methods
*
* @param name - name of file
* @return - an instance of the file
* @throws InvalidNameException - When the file is not found
*/
File getWarpFile(String name) throws InvalidNameException;
}

View File

@ -0,0 +1,16 @@
package com.earth2me.essentials.api;
public class InvalidNameException extends Exception
{
/**
* NOTE: This is not implemented yet, just here for future 3.x api support
* Allow serialization of the InvalidNameException exception
*/
private static final long serialVersionUID = 1485321420293663139L;
public InvalidNameException(Throwable thrwbl)
{
super(thrwbl);
}
}