-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTpSubCommand.java
More file actions
68 lines (60 loc) · 2.49 KB
/
TpSubCommand.java
File metadata and controls
68 lines (60 loc) · 2.49 KB
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
package de.themoep.entitydetection.commands;
import de.themoep.entitydetection.EntityDetection;
import de.themoep.entitydetection.searcher.SearchResult;
import de.themoep.entitydetection.searcher.SearchResultEntry;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
* Copyright 2016 Max Lee (https://github.com/Phoenix616/)
* <p/>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License as published by
* the Mozilla Foundation, version 2.
* <p/>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Mozilla Public License v2.0 for more details.
* <p/>
* You should have received a copy of the Mozilla Public License v2.0
* along with this program. If not, see <http://mozilla.org/MPL/2.0/>.
*/
public class TpSubCommand extends SubCommand {
public TpSubCommand(EntityDetection plugin) {
super(plugin, plugin.getName().toLowerCase(), "tp",
"<#result>"
);
}
@Override
public boolean execute(CommandSender sender, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "This sub command can only be run by a player!");
return true;
}
if (args.length == 0) {
return false;
}
return teleport((Player)sender, args[0], getPlugin().getResult(sender));
}
private <T> boolean teleport(Player sender, String page, SearchResult<T> lastResult) {
if (lastResult == null) {
sender.sendMessage(ChatColor.RED + "You have to view a search result before teleporting to an entry! Use /detect search or /detect list [<type>]");
return true;
}
int i;
try {
i = Integer.parseInt(page);
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.YELLOW + page + ChatColor.RED + " is not a proper number input!");
return false;
}
if (i == 0 || lastResult.getSortedEntries().size() < i) {
sender.sendMessage(ChatColor.RED + "Result " + ChatColor.YELLOW + page + ChatColor.RED + " is not in the list!");
return true;
}
SearchResultEntry<T> entry = lastResult.getSortedEntries().get(i - 1);
lastResult.teleport(sender, entry, i);
return true;
}
}