summaryrefslogtreecommitdiff
path: root/src/main/java/net/tylermurphy/Minecraft/Command/CommandHandler.java
blob: c289604e87a0eaabf2c0a9a1926a19eeda4b44d0 (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
package net.tylermurphy.Minecraft.Command;

import net.tylermurphy.Minecraft.Command.Commands.Teleport;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;


public class CommandHandler {

	public final static Map<String, ICommand> REGISTER = new HashMap<>();
	
	public static void registerCommands() {
		register(new Teleport());
	}
	
	private static void register(ICommand command) {
        if (!REGISTER.containsKey(command.getInvoke())) {
        	REGISTER.put(command.getInvoke().toLowerCase(), command);
        }
	}
	
	public static void handleCommand(String text) {
		final String[] split = text.replaceFirst(
                "(?i)" + Pattern.quote("/"), "").split("\\s+");
        final String invoke = split[0].toLowerCase();
        if(REGISTER.containsKey(invoke)) {
        	ICommand command = REGISTER.get(invoke);
        	final List<String> args = Arrays.asList(split).subList(1, split.length);
        	command.invoke(args);
        }
	}
	
}