From e06ee55259b3549a59358a13c095a274bacf6a05 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sat, 30 Dec 2023 14:24:46 -0500 Subject: testing new version --- .../net/benjidial/nswp/commands/PlayerCommand.java | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/main/java/net/benjidial/nswp/commands/PlayerCommand.java (limited to 'src/main/java/net/benjidial/nswp/commands/PlayerCommand.java') 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 filterByArg(List list, String[] args, int index) { + + ArrayList 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 searchWaypointsByArg(Player player, String[] args, int index) throws SQLException { + + Database.createWaypointTable(player); + + ArrayList 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 getTabCompletions(Player sender, String[] args) throws SQLException { + return new ArrayList<>(); + } + + public List 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; + } + + } + +} -- cgit v1.2.3