From dec6894e5d70544b90e92a72fea8d88bf981d689 Mon Sep 17 00:00:00 2001 From: Iaccidentally Date: Sun, 28 Apr 2013 16:23:45 -0400 Subject: [PATCH] [API] Add a warp api, with future 3.x support :: minor cleanup --- .../src/com/earth2me/essentials/I18n.java | 1 + .../src/com/earth2me/essentials/IConf.java | 3 +- .../src/com/earth2me/essentials/IReplyTo.java | 11 +++- .../src/com/earth2me/essentials/Warps.java | 42 ++++++++++++- .../com/earth2me/essentials/api/IWarps.java | 60 +++++++++++++++++++ .../essentials/api/InvalidNameException.java | 16 +++++ 6 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/api/IWarps.java create mode 100644 Essentials/src/com/earth2me/essentials/api/InvalidNameException.java diff --git a/Essentials/src/com/earth2me/essentials/I18n.java b/Essentials/src/com/earth2me/essentials/I18n.java index 93f743140..b2ae6a73a 100644 --- a/Essentials/src/com/earth2me/essentials/I18n.java +++ b/Essentials/src/com/earth2me/essentials/I18n.java @@ -43,6 +43,7 @@ public class I18n implements II18n instance = null; } + @Override public Locale getCurrentLocale() { return currentLocale; diff --git a/Essentials/src/com/earth2me/essentials/IConf.java b/Essentials/src/com/earth2me/essentials/IConf.java index a523f8638..600c7ed48 100644 --- a/Essentials/src/com/earth2me/essentials/IConf.java +++ b/Essentials/src/com/earth2me/essentials/IConf.java @@ -1,5 +1,6 @@ package com.earth2me.essentials; -public interface IConf { +public interface IConf +{ public void reloadConfig(); } diff --git a/Essentials/src/com/earth2me/essentials/IReplyTo.java b/Essentials/src/com/earth2me/essentials/IReplyTo.java index 5bef5fced..429fd7584 100644 --- a/Essentials/src/com/earth2me/essentials/IReplyTo.java +++ b/Essentials/src/com/earth2me/essentials/IReplyTo.java @@ -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(); } diff --git a/Essentials/src/com/earth2me/essentials/Warps.java b/Essentials/src/com/earth2me/essentials/Warps.java index 6c3448f4e..ce1eff83a 100644 --- a/Essentials/src/com/earth2me/essentials/Warps.java +++ b/Essentials/src/com/earth2me/essentials/Warps.java @@ -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 warpPoints = new HashMap(); @@ -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 getList() + { + final List keys = new ArrayList(); + 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 { diff --git a/Essentials/src/com/earth2me/essentials/api/IWarps.java b/Essentials/src/com/earth2me/essentials/api/IWarps.java new file mode 100644 index 000000000..8bab07397 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/api/IWarps.java @@ -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 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; + +} diff --git a/Essentials/src/com/earth2me/essentials/api/InvalidNameException.java b/Essentials/src/com/earth2me/essentials/api/InvalidNameException.java new file mode 100644 index 000000000..b62a74ce3 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/api/InvalidNameException.java @@ -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); + } +}