summaryrefslogtreecommitdiff
path: root/src/main/java/net/benjidial/nswp/commands
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/net/benjidial/nswp/commands')
-rw-r--r--src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java20
-rw-r--r--src/main/java/net/benjidial/nswp/commands/ListWaypoints.java39
-rw-r--r--src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java22
-rw-r--r--src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java27
-rw-r--r--src/main/java/net/benjidial/nswp/commands/WaypointBack.java27
-rw-r--r--src/main/java/net/benjidial/nswp/commands/WaypointCommand.java71
6 files changed, 206 insertions, 0 deletions
diff --git a/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java b/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java
new file mode 100644
index 0000000..d24d133
--- /dev/null
+++ b/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java
@@ -0,0 +1,20 @@
+package net.benjidial.nswp.commands;
+
+import net.benjidial.nswp.Database;
+
+import org.bukkit.entity.Player;
+
+import java.sql.SQLException;
+
+public class DeleteWaypoint extends WaypointCommand {
+ public CompletionType getCompletionType() {
+ return CompletionType.Waypoint;
+ }
+
+ public boolean body(Player player, String[] args) throws SQLException {
+ if (args.length != 1)
+ return false;
+ Database.deleteWaypoint(player, args[0]);
+ return true;
+ }
+}
diff --git a/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java b/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java
new file mode 100644
index 0000000..b6b61a3
--- /dev/null
+++ b/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java
@@ -0,0 +1,39 @@
+package net.benjidial.nswp.commands;
+
+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;
+
+public class ListWaypoints extends WaypointCommand {
+ public CompletionType getCompletionType() {
+ return CompletionType.Waypoint;
+ }
+
+ public boolean body(Player player, 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.");
+ return true;
+ }
+ if (waypoints.size() == 1)
+ player.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);
+ }
+ return true;
+ }
+}
diff --git a/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java b/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java
new file mode 100644
index 0000000..f0b9dcd
--- /dev/null
+++ b/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java
@@ -0,0 +1,22 @@
+package net.benjidial.nswp.commands;
+
+import net.benjidial.nswp.Database;
+import net.benjidial.nswp.Waypoint;
+
+import org.bukkit.entity.Player;
+
+import java.sql.SQLException;
+
+public class SaveWaypoint extends WaypointCommand {
+ public CompletionType getCompletionType() {
+ return CompletionType.None;
+ }
+
+ 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);
+ return true;
+ }
+}
diff --git a/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java b/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java
new file mode 100644
index 0000000..4e84e3c
--- /dev/null
+++ b/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java
@@ -0,0 +1,27 @@
+package net.benjidial.nswp.commands;
+
+import net.benjidial.nswp.Database;
+import net.benjidial.nswp.Waypoint;
+
+import org.bukkit.entity.Player;
+
+import java.sql.SQLException;
+
+public class TeleportToWaypoint extends WaypointCommand {
+ public CompletionType getCompletionType() {
+ return CompletionType.Waypoint;
+ }
+
+ public boolean body(Player player, 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);
+ }
+ return true;
+ }
+}
diff --git a/src/main/java/net/benjidial/nswp/commands/WaypointBack.java b/src/main/java/net/benjidial/nswp/commands/WaypointBack.java
new file mode 100644
index 0000000..3fa0de3
--- /dev/null
+++ b/src/main/java/net/benjidial/nswp/commands/WaypointBack.java
@@ -0,0 +1,27 @@
+package net.benjidial.nswp.commands;
+
+import net.benjidial.nswp.Database;
+
+import org.bukkit.entity.Player;
+import org.bukkit.Location;
+
+import java.sql.SQLException;
+
+public class WaypointBack extends WaypointCommand {
+ public CompletionType getCompletionType() {
+ return CompletionType.None;
+ }
+
+ 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);
+ }
+ return true;
+ }
+}
diff --git a/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java b/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java
new file mode 100644
index 0000000..618ab74
--- /dev/null
+++ b/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java
@@ -0,0 +1,71 @@
+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;
+ }
+ }
+}