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+ }
0 commit comments