Skip to content

Commit a706bdb

Browse files
committed
make info-command-override actually work
fixes #4
1 parent 486e148 commit a706bdb

File tree

4 files changed

+131
-12
lines changed

4 files changed

+131
-12
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2013 Goblom.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package lol.hyper.anarchystats;
26+
27+
import java.lang.reflect.Field;
28+
import java.util.List;
29+
30+
import org.bukkit.Bukkit;
31+
import org.bukkit.command.Command;
32+
import org.bukkit.command.CommandExecutor;
33+
import org.bukkit.command.CommandMap;
34+
import org.bukkit.command.CommandSender;
35+
import org.bukkit.command.TabExecutor;
36+
37+
/**
38+
* For a How-To on how to use AbstractCommand see this post @ http://forums.bukkit.org/threads/195990/
39+
*
40+
* @author Goblom
41+
*/
42+
public abstract class AbstractCommand implements CommandExecutor, TabExecutor {
43+
44+
protected final String command;
45+
protected final String description;
46+
protected final List<String> alias;
47+
protected final String usage;
48+
protected final String permMessage;
49+
50+
protected static CommandMap cmap;
51+
52+
public AbstractCommand(String command) {
53+
this(command, null, null, null, null);
54+
}
55+
56+
public AbstractCommand(String command, String usage) {
57+
this(command, usage, null, null, null);
58+
}
59+
60+
public AbstractCommand(String command, String usage, String description) {
61+
this(command, usage, description, null, null);
62+
}
63+
64+
public AbstractCommand(String command, String usage, String description, String permissionMessage) {
65+
this(command, usage, description, permissionMessage, null);
66+
}
67+
68+
public AbstractCommand(String command, String usage, String description, List<String> aliases) {
69+
this(command, usage, description, null, aliases);
70+
}
71+
72+
public AbstractCommand(String command, String usage, String description, String permissionMessage, List<String> aliases) {
73+
this.command = command.toLowerCase();
74+
this.usage = usage;
75+
this.description = description;
76+
this.permMessage = permissionMessage;
77+
this.alias = aliases;
78+
}
79+
80+
public void register() {
81+
ReflectCommand cmd = new ReflectCommand(this.command);
82+
if (this.alias != null) cmd.setAliases(this.alias);
83+
if (this.description != null) cmd.setDescription(this.description);
84+
if (this.usage != null) cmd.setUsage(this.usage);
85+
if (this.permMessage != null) cmd.setPermissionMessage(this.permMessage);
86+
getCommandMap().register("", cmd);
87+
cmd.setExecutor(this);
88+
}
89+
90+
final CommandMap getCommandMap() {
91+
if (cmap == null) {
92+
try {
93+
final Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
94+
f.setAccessible(true);
95+
cmap = (CommandMap) f.get(Bukkit.getServer());
96+
return getCommandMap();
97+
} catch (Exception e) { e.printStackTrace(); }
98+
} else if (cmap != null) { return cmap; }
99+
return getCommandMap();
100+
}
101+
102+
private final class ReflectCommand extends Command {
103+
private AbstractCommand exe = null;
104+
protected ReflectCommand(String command) { super(command); }
105+
public void setExecutor(AbstractCommand exe) { this.exe = exe; }
106+
@Override public boolean execute(CommandSender sender, String commandLabel, String[] args) {
107+
if (exe != null) { return exe.onCommand(sender, this, commandLabel, args); }
108+
return false;
109+
}
110+
111+
@Override public List<String> tabComplete(CommandSender sender, String alais, String[] args) {
112+
if (exe != null) { return exe.onTabComplete(sender, this, alais, args); }
113+
return null;
114+
}
115+
}
116+
117+
public abstract boolean onCommand(CommandSender sender, Command cmd, String label, String[] args);
118+
119+
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
120+
return null;
121+
}
122+
}

src/main/java/lol/hyper/anarchystats/AnarchyStats.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import org.bstats.bukkit.Metrics;
66
import org.bukkit.Bukkit;
77
import org.bukkit.command.Command;
8+
import org.bukkit.command.PluginCommand;
89
import org.bukkit.configuration.file.FileConfiguration;
910
import org.bukkit.configuration.file.YamlConfiguration;
11+
import org.bukkit.plugin.Plugin;
1012
import org.bukkit.plugin.java.JavaPlugin;
1113

1214
import java.io.File;
@@ -23,26 +25,22 @@ public final class AnarchyStats extends JavaPlugin {
2325
public final Logger logger = this.getLogger();
2426
public final ArrayList<Path> worldPaths = new ArrayList<>();
2527

26-
public CommandInfo commandInfo;
2728
public CommandReload commandReload;
2829
public MessageParser messageParser;
2930

3031
@Override
3132
public void onEnable() {
3233
messageParser = new MessageParser(this);
33-
commandInfo = new CommandInfo(this, messageParser);
3434
commandReload = new CommandReload(this);
3535
if (!configFile.exists()) {
3636
this.saveResource("config.yml", true);
3737
logger.info("Copying default config!");
3838
}
3939
loadConfig();
40-
Command commandTest = Bukkit.getPluginCommand(config.getString("info-command-override"));
41-
if (commandTest != null) {
42-
logger.warning("We detected that /" + config.getString("info-command-override") + " is already taken by another plugin. There might be some issues with this. To change this, edit the info-command-override setting in the config.");
43-
}
4440

45-
this.getCommand(config.getString("info-command-override")).setExecutor(commandInfo);
41+
AbstractCommand infoCommand = new CommandInfo(config.getString("info-command-override"), this, messageParser);
42+
infoCommand.register();
43+
4644
this.getCommand("anarchystats").setExecutor(commandReload);
4745
Bukkit.getScheduler().runTaskAsynchronously(this, this::updateWorldSize);
4846

src/main/java/lol/hyper/anarchystats/commands/CommandInfo.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package lol.hyper.anarchystats.commands;
22

3+
import lol.hyper.anarchystats.AbstractCommand;
34
import lol.hyper.anarchystats.AnarchyStats;
45
import lol.hyper.anarchystats.MessageParser;
56
import org.bukkit.Bukkit;
67
import org.bukkit.command.Command;
7-
import org.bukkit.command.CommandExecutor;
88
import org.bukkit.command.CommandSender;
99

10-
public class CommandInfo implements CommandExecutor {
10+
public class CommandInfo extends AbstractCommand {
1111

1212
private final AnarchyStats anarchyStats;
1313
private final MessageParser messageParser;
1414

15-
public CommandInfo(AnarchyStats anarchyStats, MessageParser messageParser) {
15+
public CommandInfo(String command, AnarchyStats anarchyStats, MessageParser messageParser) {
16+
super(command);
1617
this.anarchyStats = anarchyStats;
1718
this.messageParser = messageParser;
1819
}

src/main/resources/plugin.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@ api-version: 1.15
55
load: STARTUP
66
author: hyperdefined
77
commands:
8-
info:
9-
description: See world information.
108
anarchystats:
119
description: Main command.

0 commit comments

Comments
 (0)