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; } } }