summaryrefslogtreecommitdiff
path: root/src/main/java/net/benjidial/nswp/commands/PlayerCommand.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/net/benjidial/nswp/commands/PlayerCommand.java')
-rw-r--r--src/main/java/net/benjidial/nswp/commands/PlayerCommand.java88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/main/java/net/benjidial/nswp/commands/PlayerCommand.java b/src/main/java/net/benjidial/nswp/commands/PlayerCommand.java
new file mode 100644
index 0000000..5670695
--- /dev/null
+++ b/src/main/java/net/benjidial/nswp/commands/PlayerCommand.java
@@ -0,0 +1,88 @@
+package net.benjidial.nswp.commands;
+
+import net.benjidial.nswp.Database;
+import net.benjidial.nswp.Waypoint;
+import net.benjidial.nswp.Plugin;
+
+import org.bukkit.command.CommandSender;
+import org.bukkit.command.TabExecutor;
+import org.bukkit.command.Command;
+import org.bukkit.entity.Player;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+public abstract class PlayerCommand implements TabExecutor {
+
+ public static List<String> filterByArg(List<String> list, String[] args, int index) {
+
+ ArrayList<String> filtered = new ArrayList<>();
+ String searchString = args.length <= index ? "" : args[index];
+
+ for (String candidate : list)
+ if (candidate.contains(searchString))
+ filtered.add(candidate);
+
+ return filtered;
+
+ }
+
+ public static List<String> searchWaypointsByArg(Player player, String[] args, int index) throws SQLException {
+
+ Database.createWaypointTable(player);
+
+ ArrayList<String> filtered = new ArrayList<>();
+ String searchString = args.length <= index ? "" : args[index];
+
+ for (Waypoint wp : Database.searchWaypoints(player, searchString))
+ filtered.add(wp.name);
+
+ return filtered;
+
+ }
+
+ public List<String> getTabCompletions(Player sender, String[] args) throws SQLException {
+ return new ArrayList<>();
+ }
+
+ public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
+
+ if (!(sender instanceof Player))
+ return new ArrayList<>();
+
+ try {
+ return getTabCompletions((Player)sender, args);
+ }
+
+ catch (SQLException ex) {
+ Plugin.logger.severe("Database error:");
+ ex.printStackTrace();
+ return new ArrayList<>();
+ }
+
+ }
+
+ public abstract boolean doCommand(Player sender, String[] args) throws SQLException;
+
+ public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
+
+ if (!(sender instanceof Player)) {
+ sender.sendMessage("This command may only be run by a player.");
+ return true;
+ }
+
+ try {
+ return doCommand((Player)sender, args);
+ }
+
+ catch (SQLException ex) {
+ sender.sendMessage("Database error.");
+ Plugin.logger.severe("Database error:");
+ ex.printStackTrace();
+ return true;
+ }
+
+ }
+
+}