diff options
author | Benji Dial <benji@benjidial.net> | 2023-12-30 14:24:46 -0500 |
---|---|---|
committer | Benji Dial <benji@benjidial.net> | 2023-12-30 14:24:46 -0500 |
commit | e06ee55259b3549a59358a13c095a274bacf6a05 (patch) | |
tree | a6e8b1b33434ee2c7de7028d4abad77b7fbe8296 /src/main/java/net/benjidial/nswp/commands/PlayerCommand.java | |
parent | ef65431d405a713b138a55054e78497883f27e54 (diff) | |
download | new-simple-waypoints-e06ee55259b3549a59358a13c095a274bacf6a05.tar.gz |
testing new version
Diffstat (limited to 'src/main/java/net/benjidial/nswp/commands/PlayerCommand.java')
-rw-r--r-- | src/main/java/net/benjidial/nswp/commands/PlayerCommand.java | 88 |
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; + } + + } + +} |