summaryrefslogtreecommitdiff
path: root/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java
blob: 618ab7478fe3d59f1a9ec7a38b4520ae70affc91 (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
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;
    }
  }
}