summaryrefslogtreecommitdiff
path: root/com/nemez/cmdmgr/util/Executable.java
blob: df396fc9b0fbcfcaca3dc7cd1f9fd8d8b1b5fa00 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package com.nemez.cmdmgr.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;

import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;

import com.nemez.cmdmgr.Command;
import com.nemez.cmdmgr.CommandManager;
import com.nemez.cmdmgr.component.BooleanComponent;
import com.nemez.cmdmgr.component.ByteComponent;
import com.nemez.cmdmgr.component.ConstantComponent;
import com.nemez.cmdmgr.component.DoubleComponent;
import com.nemez.cmdmgr.component.FloatComponent;
import com.nemez.cmdmgr.component.ICommandComponent;
import com.nemez.cmdmgr.component.IntegerComponent;
import com.nemez.cmdmgr.component.LongComponent;
import com.nemez.cmdmgr.component.ShortComponent;
import com.nemez.cmdmgr.component.StringComponent;

public class Executable implements CommandExecutor {

	private ArrayList<ExecutableDefinition> commands;
	private ArrayList<HelpPageCommand[]> help;
	private String name;
	private JavaPlugin plugin;
	
	public Executable(String name, ArrayList<HelpPageCommand[]> help) {
		this.help = help;
		this.name = name;
		this.commands = new ArrayList<ExecutableDefinition>();
	}
	
	public void register(ArrayList<Method> methods, JavaPlugin plugin, Object methodContainer) {
		for (HelpPageCommand[] page : help) {
			for (HelpPageCommand cmd : page) {
				if (cmd != null) {
					processLine(cmd.usage.split("\\ "), cmd.permission, cmd.method, methods, methodContainer, plugin);
				}
			}
		}
		
		this.plugin = plugin;
		plugin.getCommand(name).setExecutor(this);
		
		if (CommandManager.errors) {
			plugin.getLogger().log(Level.WARNING, "There were parser errors, some commands may not function properly!");
			CommandManager.errors = false;
		}
	}
	
	private void processLine(String[] line, String permission, String method, ArrayList<Method> methods, Object methodContainer, JavaPlugin plugin) {
		ArrayList<ICommandComponent> command = new ArrayList<ICommandComponent>();
		if (method == null && line[1].equals("help")) {
			command.add(new ConstantComponent("help"));
			IntegerComponent pageID = new IntegerComponent();
			pageID.argName = "page";
			command.add(pageID);
			ExecutableDefinition def = new ExecutableDefinition(command, permission, null, methodContainer);
			commands.add(def);
			return;
		}
		HashMap<Integer, ICommandComponent> methodParams = new HashMap<Integer, ICommandComponent>();
		method = method.trim() + " ";
		String[] methodArray = method.split(" ");
		Method target = null;
		
		for (String s : line) {
			if (s.contains("/")) {
				continue;
			}
			if (s.contains(":")) {
				String[] type = s.split(":");
				String paramName = "";
				switch (type[1].substring(0, type[1].length() - 1)) {
				case "i8":
					ByteComponent comp1 = new ByteComponent();
					comp1.argName = type[0].substring(1);
					paramName = comp1.argName;
					command.add(comp1);
					break;
				case "i16":
					ShortComponent comp2 = new ShortComponent();
					comp2.argName = type[0].substring(1);
					paramName = comp2.argName;
					command.add(comp2);
					break;
				case "i32":
					IntegerComponent comp3 = new IntegerComponent();
					comp3.argName = type[0].substring(1);
					paramName = comp3.argName;
					command.add(comp3);
					break;
				case "i64":
					LongComponent comp4 = new LongComponent();
					comp4.argName = type[0].substring(1);
					paramName = comp4.argName;
					command.add(comp4);
					break;
				case "fp32":
					FloatComponent comp5 = new FloatComponent();
					comp5.argName = type[0].substring(1);
					paramName = comp5.argName;
					command.add(comp5);
					break;
				case "fp64":
					DoubleComponent comp6 = new DoubleComponent();
					comp6.argName = type[0].substring(1);
					paramName = comp6.argName;
					command.add(comp6);
					break;
				case "str":
					StringComponent comp7 = new StringComponent();
					comp7.argName = type[0].substring(1);
					paramName = comp7.argName;
					command.add(comp7);
					break;
				case "bool":
					BooleanComponent comp8 = new BooleanComponent();
					comp8.argName = type[0].substring(1);
					paramName = comp8.argName;
					command.add(comp8);
				default:
					return;
				}
				int index = 0;
				for (int i = 1; i < methodArray.length; i++) {
					if (methodArray[i] != null && !methodArray[i].trim().equals("")) {
						if (methodArray[i].trim().equals(paramName)) {
							methodParams.put(index, command.get(command.size() - 1));
						}
						index++;
					}
				}
			}else{
				command.add(new ConstantComponent(s));
			}
		}
		
		for (Method m : methods) {
			Command[] annotations = m.getAnnotationsByType(Command.class);
			if (annotations == null || annotations.length != 1) {
				plugin.getLogger().log(Level.WARNING, "Invalid method (" + methodArray[0] + ")");
				CommandManager.errors = true;
				System.err.println("Method not found! (" + methodArray[0] + ")");
				return;
			}else{
				if (annotations[0].hook().equals(methodArray[0])) {
					Class<?>[] params = m.getParameterTypes();
					if (params.length -1 != methodParams.size()) {
						System.err.println("error again! :D");
						return;
					}else{
						for (int i = 0; i < params.length; i++) {
							if (i == 0) {
								if (params[0] != CommandSender.class) {
									plugin.getLogger().log(Level.WARNING, "Invalid method (" + methodArray[0] + "): First argument is not CommandSender");
									CommandManager.errors = true;
									return;
								}
							}else{
								ICommandComponent comp = methodParams.get(i - 1);
								if (comp instanceof ByteComponent && params[i] == byte.class) {
									
								}else if (comp instanceof ShortComponent && params[i] == short.class) {
									
								}else if (comp instanceof IntegerComponent && params[i] == int.class) {
									
								}else if (comp instanceof LongComponent && params[i] == long.class) {
									
								}else if (comp instanceof FloatComponent && params[i] == float.class) {
									
								}else if (comp instanceof DoubleComponent && params[i] == double.class) {
									
								}else if (comp instanceof StringComponent && params[i] == String.class) {
									
								}else if (comp instanceof BooleanComponent && params[i] == boolean.class) {
									
								}else{
									plugin.getLogger().log(Level.WARNING, "Invalid method (" + methodArray[0] + "): Invalid method arguments");
									CommandManager.errors = true;
									return;
								}
							}
						}
						target = m;
						break;
					}
				}
			}
		}
		if (target == null) {
			plugin.getLogger().log(Level.WARNING, "Invalid method (" + methodArray[0] + "): Method not found");
			CommandManager.errors = true;
			return;
		}
		ExecutableDefinition def = new ExecutableDefinition(command, permission, target, methodContainer);
		commands.add(def);
	}

	@Override
	public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String name, String[] args) {
		ArrayList<ExecutableDefinition> defs = new ArrayList<ExecutableDefinition>();
		defs.addAll(commands);
		for (int i = 0; i < args.length; i++) {
			for (int j = 0; j < defs.size(); j++) {
				if (!defs.get(j).valid(i, args[i])) {
					defs.remove(j);
					j--;
				}
			}
		}
		if (args.length == 0 || defs.size() == 0) {
			printPage(sender, 1);
		}else{
			ExecutableDefinition def = defs.get(0);
			if (!sender.hasPermission(def.getPermission())) {
				sender.sendMessage(CommandManager.noPermissionFormatting + "You do not have permission to execute this command.");
				return true;
			}
			if (def.getLength() != args.length) {
				printPage(sender, 1);
				return true;
			}
			ArrayList<Object> arguments = new ArrayList<Object>();
			for (int i = 0; i < args.length; i++) {
				if (def.isArgument(i)) {
					arguments.add(def.get(i, args[i]));
				}
			}
			if (def.isHelp() || args[0].equals("help")) {
				try {
					int page = Integer.parseInt(args[1]);
					printPage(sender, page);
				} catch (Exception e) {
					printPage(sender, 1);
				}
			}else if (!def.invoke(arguments, sender, plugin)) {
				printPage(sender, 1);
			}
		}
		return true;
	}
	
	private void printPage(CommandSender sender, int page) {
		page--;
		if (page < 0 || page >= help.size()) {
			sender.sendMessage(CommandManager.helpInvalidPageFormatting + "Non-existant page (" + (page + 1) + ").\nThere are " + help.size() + " pages.");
		}else{
			HelpPageCommand[] pageData = help.get(page);
			sender.sendMessage(CommandManager.helpPageHeaderFormatting + "### Help Page " + (page + 1) + "/" + (help.size()) + " ###");
			for (HelpPageCommand c : pageData) {
				if (c != null) {
					sender.sendMessage(CommandManager.helpUsageFormatting + c.usage);
					sender.sendMessage(CommandManager.helpDescriptionFormatting + c.description);
				}
			}
		}
	}
}