diff options
Diffstat (limited to 'src/main/java/net')
14 files changed, 449 insertions, 154 deletions
diff --git a/src/main/java/net/benjidial/nswp/Database.java b/src/main/java/net/benjidial/nswp/Database.java index 21f52f5..f123bdb 100644 --- a/src/main/java/net/benjidial/nswp/Database.java +++ b/src/main/java/net/benjidial/nswp/Database.java @@ -10,6 +10,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.sql.Connection; import java.sql.ResultSet; +import java.util.UUID; public class Database { static Connection connection; @@ -57,7 +58,6 @@ public class Database { } public static Location getWBack(Player player) throws SQLException { - createWBacks(); PreparedStatement statement = connection.prepareStatement( "SELECT * FROM wbacks WHERE player_uuid = ?" ); @@ -69,7 +69,6 @@ public class Database { } public static void setWBack(Player player, Location location) throws SQLException { - createWBacks(); PreparedStatement statement = connection.prepareStatement( "REPLACE INTO wbacks(player_uuid, world, x, y, z, pitch, yaw) VALUES(?, ?, ?, ?, ?, ?, ?)" ); @@ -101,11 +100,11 @@ public class Database { ); } - public static ArrayList<Waypoint> searchWaypoints(Player player, String start) throws SQLException { + public static ArrayList<Waypoint> searchWaypoints(Player player, String searchString) throws SQLException { PreparedStatement statement = connection.prepareStatement( "SELECT * FROM " + waypointTableName(player) + " WHERE name LIKE ? ESCAPE '!' ORDER BY world, name ASC" ); - statement.setString(1, "%" + start.replace("!", "!!").replace("_", "!_") + "%"); + statement.setString(1, "%" + searchString.replace("!", "!!").replace("_", "!_") + "%"); ResultSet results = statement.executeQuery(); ArrayList<Waypoint> list = new ArrayList<Waypoint>(); while (results.next()) @@ -154,4 +153,104 @@ public class Database { else player.sendMessage("Waypoint deleted."); } + + public static void createTPATable() throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "CREATE TABLE IF NOT EXISTS tpa_allowed (" + + "from TEXT NOT NULL, " + + "to TEXT NOT NULL" + + ")" + ); + statement.executeUpdate(); + } + + public static boolean isTPAAllowed(Player from, Player to) throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM tpa_allowed WHERE from = ? AND to = ?" + ); + statement.setString(1, playerID(from)); + statement.setString(2, playerID(to)); + ResultSet results = statement.executeQuery(); + return results.next(); + } + + public static void allowTPA(Player from, Player to) throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "INSERT INTO tpa_allowed(from, to) VALUES(?, ?)" + ); + statement.setString(1, playerID(from)); + statement.setString(2, playerID(to)); + statement.executeUpdate(); + } + + public static int disallowTPA(String fromName, Player to) throws SQLException { + //this is kinda scrungled but i think it should work. + + PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM tpa_allowed WHERE to = ?" + ); + statement.setString(1, playerID(to)); + ResultSet table = statement.executeQuery(); + + int removed = 0; + + while (table.next()) { + String fromID = table.getString("from"); + + if (Bukkit.getOfflinePlayer(UUID.fromString(fromID)).getName() == fromName) { + PreparedStatement deleteStatement = connection.prepareStatement( + "DELETE FROM tpa_allowed WHERE from = ? AND to = ?" + ); + deleteStatement.setString(1, fromID); + deleteStatement.setString(2, playerID(to)); + deleteStatement.executeUpdate(); + ++removed; + } + } + + return removed; + } + + public static ArrayList<String> getOnlineTPADisallowedTo(Player to) throws SQLException { + + ArrayList<String> results = new ArrayList<>(); + for (Player player : Bukkit.getOnlinePlayers()) + if (!isTPAAllowed(player, to)) + results.add(player.getName()); + + return results; + + } + + public static ArrayList<String> getTPAAllowedTo(Player to) throws SQLException { + + PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM tpa_allowed WHERE to = ?" + ); + statement.setString(1, playerID(to)); + ResultSet table = statement.executeQuery(); + + ArrayList<String> results = new ArrayList<>(); + while (table.next()) { + String name = Bukkit.getOfflinePlayer(UUID.fromString(table.getString("from"))).getName(); + //there is no way to disallow a player whose name is not know. but, to allow a player, + //they have to be online, and so their name should be known. + if (name != null) + results.add(name); + } + + return results; + + } + + public static ArrayList<String> getOnlineTPAAllowedFrom(Player from) throws SQLException { + + ArrayList<String> results = new ArrayList<>(); + for (Player player : Bukkit.getOnlinePlayers()) + if (isTPAAllowed(from, player)) + results.add(player.getName()); + + return results; + + } } diff --git a/src/main/java/net/benjidial/nswp/Plugin.java b/src/main/java/net/benjidial/nswp/Plugin.java index 45509a7..94c652e 100644 --- a/src/main/java/net/benjidial/nswp/Plugin.java +++ b/src/main/java/net/benjidial/nswp/Plugin.java @@ -5,7 +5,6 @@ import net.benjidial.nswp.commands.*; import org.bukkit.plugin.java.JavaPlugin; import java.util.logging.Logger; -import java.util.logging.Level; import java.sql.SQLException; import java.io.File; @@ -13,23 +12,30 @@ public class Plugin extends JavaPlugin { public static Logger logger; public void onEnable() { + logger = getLogger(); getDataFolder().mkdirs(); try { Database.connect(new File(getDataFolder(), "database.db").getPath()); + Database.createWBacks(); + Database.createTPATable(); } catch (SQLException ex) { - logger.log(Level.SEVERE, "Database error: (not enabling plugin)"); + logger.severe("Database error (not enabling plugin):"); ex.printStackTrace(); return; } - getCommand("wlist" ).setExecutor(new ListWaypoints()); - getCommand("wsave" ).setExecutor(new SaveWaypoint()); - getCommand("wtp" ).setExecutor(new TeleportToWaypoint()); - getCommand("wdel" ).setExecutor(new DeleteWaypoint()); - getCommand("wback" ).setExecutor(new WaypointBack()); + getCommand("wtpallow").setExecutor(new AllowTPA()); + getCommand("wdel").setExecutor(new DeleteWaypoint()); + getCommand("wtpdeny").setExecutor(new DenyTPA()); + getCommand("wlist").setExecutor(new ListWaypoints()); getCommand("wrename").setExecutor(new RenameWaypoint()); + getCommand("wsave").setExecutor(new SaveWaypoint()); + getCommand("wtp").setExecutor(new TeleportToWaypoint()); + getCommand("wtpa").setExecutor(new TPA()); + getCommand("wback").setExecutor(new WaypointBack()); + } } diff --git a/src/main/java/net/benjidial/nswp/Waypoint.java b/src/main/java/net/benjidial/nswp/Waypoint.java index ec3daf6..7b3beff 100644 --- a/src/main/java/net/benjidial/nswp/Waypoint.java +++ b/src/main/java/net/benjidial/nswp/Waypoint.java @@ -3,6 +3,7 @@ package net.benjidial.nswp; import org.bukkit.Location; public class Waypoint { + public Waypoint(String name, Location location) { this.name = name; this.location = location; @@ -10,4 +11,9 @@ public class Waypoint { public String name; public Location location; + + public String toString() { + return name + " (" + location.getBlockX() + " " + location.getBlockY() + " " + location.getBlockZ() + " in " + location.getWorld().getName() + ")"; + } + } diff --git a/src/main/java/net/benjidial/nswp/commands/AllowTPA.java b/src/main/java/net/benjidial/nswp/commands/AllowTPA.java new file mode 100644 index 0000000..80ecd2d --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/AllowTPA.java @@ -0,0 +1,39 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; + +import org.bukkit.entity.Player; +import org.bukkit.Bukkit; + +import java.sql.SQLException; +import java.util.List; + +public class AllowTPA extends PlayerCommand { + + public List<String> getTabCompletions(Player sender, String[] args) throws SQLException { + return filterByArg(Database.getOnlineTPADisallowedTo(sender), args, 0); + } + + public boolean doCommand(Player sender, String[] args) throws SQLException { + + if (args.length != 1) + return false; + + Player other = Bukkit.getPlayer(args[0]); + if (other == null) { + sender.sendMessage("Could not find " + args[0] + ". Are they online?"); + return true; + } + + if (Database.isTPAAllowed(other, sender)) { + sender.sendMessage(other.getName() + " is already allowed to teleport to you."); + return true; + } + + Database.allowTPA(other, sender); + sender.sendMessage(other.getName() + " is now allowed to teleport to you."); + return true; + + } + +} diff --git a/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java b/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java index d24d133..785d31f 100644 --- a/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java +++ b/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java @@ -1,20 +1,36 @@ package net.benjidial.nswp.commands; import net.benjidial.nswp.Database; +import net.benjidial.nswp.Waypoint; import org.bukkit.entity.Player; import java.sql.SQLException; +import java.util.List; -public class DeleteWaypoint extends WaypointCommand { - public CompletionType getCompletionType() { - return CompletionType.Waypoint; +public class DeleteWaypoint extends PlayerCommand { + + public List<String> getTabCompletions(Player sender, String[] args) throws SQLException { + return searchWaypointsByArg(sender, args, 0); } - public boolean body(Player player, String[] args) throws SQLException { + public boolean doCommand(Player sender, String[] args) throws SQLException { + if (args.length != 1) return false; - Database.deleteWaypoint(player, args[0]); + + Database.createWaypointTable(sender); + + Waypoint wp = Database.lookupWaypoint(sender, args[0]); + if (wp != null) { + Database.deleteWaypoint(sender, args[0]); + sender.sendMessage("Deleted " + wp.toString() + "."); + } + else + sender.sendMessage("You do not have a waypoint " + args[0] + "."); + return true; + } + } diff --git a/src/main/java/net/benjidial/nswp/commands/DenyTPA.java b/src/main/java/net/benjidial/nswp/commands/DenyTPA.java new file mode 100644 index 0000000..43077ab --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/DenyTPA.java @@ -0,0 +1,32 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; + +import org.bukkit.entity.Player; + +import java.sql.SQLException; +import java.util.List; + +public class DenyTPA extends PlayerCommand { + + public List<String> getTabCompletions(Player sender, String[] args) throws SQLException { + return filterByArg(Database.getTPAAllowedTo(sender), args, 0); + } + + public boolean doCommand(Player sender, String[] args) throws SQLException { + + if (args.length != 1) + return false; + + int removed = Database.disallowTPA(args[0], sender); + + if (removed == 0) + sender.sendMessage("Did not find anyone allowed to teleport to you with the name " + args[0] + "."); + else + sender.sendMessage(args[0] + " is no longer allowed to teleport to you."); + + return true; + + } + +} diff --git a/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java b/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java index b6b61a3..336a07b 100644 --- a/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java +++ b/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java @@ -4,36 +4,40 @@ import net.benjidial.nswp.Database; import net.benjidial.nswp.Waypoint; import org.bukkit.entity.Player; -import org.bukkit.World; import java.sql.SQLException; import java.util.ArrayList; +import java.util.List; -public class ListWaypoints extends WaypointCommand { - public CompletionType getCompletionType() { - return CompletionType.Waypoint; +public class ListWaypoints extends PlayerCommand { + + public List<String> getTabCompletions(Player sender, String[] args) throws SQLException { + return searchWaypointsByArg(sender, args, 0); } - public boolean body(Player player, String[] args) throws SQLException { + public boolean doCommand(Player sender, String[] args) throws SQLException { + if (args.length > 1) return false; - ArrayList<Waypoint> waypoints = Database.searchWaypoints(player, args.length == 0 ? "" : args[0]); - if (waypoints.size() == 0) { - player.sendMessage("No waypoints found."); + + Database.createWaypointTable(sender); + ArrayList<Waypoint> wps = Database.searchWaypoints(sender, args.length == 0 ? "" : args[0]); + + if (wps.size() == 0) { + sender.sendMessage("0 waypoints found."); return true; } - if (waypoints.size() == 1) - player.sendMessage("1 waypoint found:"); + + if (wps.size() == 1) + sender.sendMessage("1 waypoint found:"); else - player.sendMessage(waypoints.size() + " waypoints found:"); - World lastWorld = null; - for (Waypoint waypoint : waypoints) { - if (waypoint.location.getWorld() != lastWorld) { - lastWorld = waypoint.location.getWorld(); - player.sendMessage(" " + lastWorld.getName() + ": "); - } - player.sendMessage(" " + waypoint.name); - } + sender.sendMessage(wps.size() + " waypoints found:"); + + for (Waypoint wp : wps) + sender.sendMessage(" " + wp.toString()); + return true; + } + } 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; + } + + } + +} diff --git a/src/main/java/net/benjidial/nswp/commands/RenameWaypoint.java b/src/main/java/net/benjidial/nswp/commands/RenameWaypoint.java index 44af06c..07f255a 100644 --- a/src/main/java/net/benjidial/nswp/commands/RenameWaypoint.java +++ b/src/main/java/net/benjidial/nswp/commands/RenameWaypoint.java @@ -6,32 +6,45 @@ import net.benjidial.nswp.Waypoint; import org.bukkit.entity.Player; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; -public class RenameWaypoint extends WaypointCommand { - public CompletionType getCompletionType() { - return CompletionType.Waypoint; - } +public class RenameWaypoint extends PlayerCommand { + + public List<String> getTabCompletions(Player sender, String[] args) throws SQLException { + if (args.length > 1) + return new ArrayList<String>(); + return searchWaypointsByArg(sender, args, 0); + } - @Override - public boolean body(Player player, String[] args) throws SQLException { - if (args.length != 2) return false; + public boolean doCommand(Player sender, String[] args) throws SQLException { - if (Database.lookupWaypoint(player, args[1]) != null) { - player.sendMessage("Waypoint with that name already exists!"); - return true; - } + if (args.length != 2) + return false; - Waypoint waypoint = Database.lookupWaypoint(player, args[0]); + Database.createWaypointTable(sender); + Waypoint wp = Database.lookupWaypoint(sender, args[0]); - if (waypoint == null) { - player.sendMessage("No waypoint with that name."); - } else { - Database.deleteWaypoint(player, args[0]); - waypoint.name = args[1]; - Database.addWaypoint(player, waypoint); - } + if (wp == null) { + sender.sendMessage("You do not have a waypoint " + args[0] + "."); + return true; + } - return true; + Waypoint otherWP = Database.lookupWaypoint(sender, args[1]); + if (otherWP != null) { + sender.sendMessage("You already have a waypoint " + otherWP.toString() + "."); + return true; } + String oldToString = wp.toString(); + wp.name = args[1]; + + Database.deleteWaypoint(sender, args[0]); + Database.addWaypoint(sender, wp); + + sender.sendMessage("Renamed " + oldToString + " to " + args[1] + "."); + return true; + + } + } diff --git a/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java b/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java index f0b9dcd..e77d6ff 100644 --- a/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java +++ b/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java @@ -7,16 +7,27 @@ import org.bukkit.entity.Player; import java.sql.SQLException; -public class SaveWaypoint extends WaypointCommand { - public CompletionType getCompletionType() { - return CompletionType.None; - } +public class SaveWaypoint extends PlayerCommand { + + public boolean doCommand(Player sender, String[] args) throws SQLException { - public boolean body(Player player, String[] args) throws SQLException { if (args.length != 1) return false; - Waypoint waypoint = new Waypoint(args[0], player.getLocation()); - Database.addWaypoint(player, waypoint); + + Database.createWaypointTable(sender); + + Waypoint wp = Database.lookupWaypoint(sender, args[0]); + if (wp != null) { + sender.sendMessage("You already have a waypoint " + wp.toString() + "."); + return true; + } + + wp = new Waypoint(args[0], sender.getLocation()); + Database.addWaypoint(sender, wp); + + sender.sendMessage("Added " + wp.toString() + "."); return true; + } + } diff --git a/src/main/java/net/benjidial/nswp/commands/TPA.java b/src/main/java/net/benjidial/nswp/commands/TPA.java new file mode 100644 index 0000000..e160fc0 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/TPA.java @@ -0,0 +1,40 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; + +import org.bukkit.entity.Player; +import org.bukkit.Bukkit; + +import java.sql.SQLException; +import java.util.List; + +public class TPA extends PlayerCommand { + + public List<String> getTabCompletions(Player sender, String[] args) throws SQLException { + return filterByArg(Database.getOnlineTPAAllowedFrom(sender), args, 0); + } + + public boolean doCommand(Player sender, String[] args) throws SQLException { + + if (args.length != 1) + return false; + + Player other = Bukkit.getPlayer(args[0]); + if (other == null) { + sender.sendMessage("Could not find " + args[0] + ". Are they online?"); + return true; + } + + if (!Database.isTPAAllowed(sender, other)) { + sender.sendMessage("You are not allowed to teleport to " + other.getName() + "."); + sender.sendMessage("They must first run /wtpallow " + sender.getName()); + return true; + } + + Database.setWBack(sender, sender.getLocation()); + sender.teleport(other); + return true; + + } + +} diff --git a/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java b/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java index 4e84e3c..71b2ad3 100644 --- a/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java +++ b/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java @@ -6,22 +6,31 @@ import net.benjidial.nswp.Waypoint; import org.bukkit.entity.Player; import java.sql.SQLException; +import java.util.List; -public class TeleportToWaypoint extends WaypointCommand { - public CompletionType getCompletionType() { - return CompletionType.Waypoint; +public class TeleportToWaypoint extends PlayerCommand { + + public List<String> getTabCompletions(Player sender, String[] args) throws SQLException { + return searchWaypointsByArg(sender, args, 0); } - public boolean body(Player player, String[] args) throws SQLException { + public boolean doCommand(Player sender, String[] args) throws SQLException { + if (args.length != 1) return false; - Waypoint waypoint = Database.lookupWaypoint(player, args[0]); - if (waypoint == null) - player.sendMessage("No waypoint with that name."); - else { - Database.setWBack(player, player.getLocation()); - player.teleport(waypoint.location); + + Database.createWaypointTable(sender); + + Waypoint wp = Database.lookupWaypoint(sender, args[0]); + if (wp == null) { + sender.sendMessage("You do not have a waypoint " + args[0] + "."); + return true; } + + Database.setWBack(sender, sender.getLocation()); + sender.teleport(wp.location); return true; + } + } diff --git a/src/main/java/net/benjidial/nswp/commands/WaypointBack.java b/src/main/java/net/benjidial/nswp/commands/WaypointBack.java index 3fa0de3..2082ae7 100644 --- a/src/main/java/net/benjidial/nswp/commands/WaypointBack.java +++ b/src/main/java/net/benjidial/nswp/commands/WaypointBack.java @@ -7,21 +7,24 @@ import org.bukkit.Location; import java.sql.SQLException; -public class WaypointBack extends WaypointCommand { - public CompletionType getCompletionType() { - return CompletionType.None; - } +public class WaypointBack extends PlayerCommand { + + public boolean doCommand(Player sender, String[] args) throws SQLException { - public boolean body(Player player, String[] args) throws SQLException { if (args.length != 0) return false; - Location wback = Database.getWBack(player); - if (wback == null) - player.sendMessage("You have not teleported to any waypoints."); - else { - Database.setWBack(player, player.getLocation()); - player.teleport(wback); + + Location wback = Database.getWBack(sender); + + if (wback == null) { + sender.sendMessage("You have never teleported anywhere with NewSimpleWaypoints."); + return true; } + + Database.setWBack(sender, sender.getLocation()); + sender.teleport(wback); return true; + } + } diff --git a/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java b/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java deleted file mode 100644 index 618ab74..0000000 --- a/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java +++ /dev/null @@ -1,71 +0,0 @@ -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.util.logging.Level; -import java.sql.SQLException; -import java.util.ArrayList; - -public abstract class WaypointCommand implements TabExecutor { - enum CompletionType { - None, Waypoint - } - - public abstract CompletionType getCompletionType(); - - public ArrayList<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) { - if (!(sender instanceof Player)) { - sender.sendMessage("Only players may use waypoints."); - return new ArrayList<>(); - } - Player player = (Player)sender; - - switch (getCompletionType()) { - case Waypoint: - if (args.length > 1) - return new ArrayList<>(); - try { - Database.createWaypointTable(player); - ArrayList<String> results = new ArrayList<>(); - for (Waypoint waypoint : Database.searchWaypoints(player, args.length == 0 ? "" : args[0])) - results.add(waypoint.name); - return results; - } - catch (SQLException ex) { - Plugin.logger.log(Level.SEVERE, "Database error:"); - ex.printStackTrace(); - sender.sendMessage("Database error."); - return new ArrayList<>(); - } - default: - return new ArrayList<>(); - } - } - - public abstract boolean body(Player player, String[] args) throws SQLException; - - public boolean onCommand(CommandSender sender, Command command, String name, String[] args) { - if (!(sender instanceof Player)) { - sender.sendMessage("Only players may use waypoints."); - return true; - } - try { - Player player = (Player)sender; - Database.createWaypointTable(player); - return body(player, args); - } - catch (SQLException ex) { - Plugin.logger.log(Level.SEVERE, "Database error:"); - ex.printStackTrace(); - sender.sendMessage("Database error."); - return true; - } - } -} |