summaryrefslogtreecommitdiff
path: root/src/main/java/net/benjidial/nswp/commands
diff options
context:
space:
mode:
authorBenji Dial <benji@benjidial.net>2023-12-30 14:24:46 -0500
committerBenji Dial <benji@benjidial.net>2023-12-30 14:24:46 -0500
commite06ee55259b3549a59358a13c095a274bacf6a05 (patch)
treea6e8b1b33434ee2c7de7028d4abad77b7fbe8296 /src/main/java/net/benjidial/nswp/commands
parentef65431d405a713b138a55054e78497883f27e54 (diff)
downloadnew-simple-waypoints-e06ee55259b3549a59358a13c095a274bacf6a05.tar.gz
testing new version
Diffstat (limited to 'src/main/java/net/benjidial/nswp/commands')
-rw-r--r--src/main/java/net/benjidial/nswp/commands/AllowTPA.java39
-rw-r--r--src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java26
-rw-r--r--src/main/java/net/benjidial/nswp/commands/DenyTPA.java32
-rw-r--r--src/main/java/net/benjidial/nswp/commands/ListWaypoints.java42
-rw-r--r--src/main/java/net/benjidial/nswp/commands/PlayerCommand.java88
-rw-r--r--src/main/java/net/benjidial/nswp/commands/RenameWaypoint.java53
-rw-r--r--src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java25
-rw-r--r--src/main/java/net/benjidial/nswp/commands/TPA.java40
-rw-r--r--src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java29
-rw-r--r--src/main/java/net/benjidial/nswp/commands/WaypointBack.java25
-rw-r--r--src/main/java/net/benjidial/nswp/commands/WaypointCommand.java71
11 files changed, 327 insertions, 143 deletions
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;
- }
- }
-}