summaryrefslogtreecommitdiff
path: root/src/main/java/net/benjidial/nswp/commands/PlayerCommand.java
blob: 567069578265c58e9b5d4a4ee861c2317c64890c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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;
    }

  }

}