summaryrefslogtreewikicommitdiff
path: root/src/main/java/net/tylermurphy/hideAndSeek/util/CommandHandler.java
blob: 3a05ee552f68511e25fe7073d4734803fb251410 (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
/*
 * This file is part of Kenshins Hide and Seek
 *
 * Copyright (c) 2021 Tyler Murphy.
 *
 * Kenshins Hide and Seek free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * he Free Software Foundation version 3.
 *
 * Kenshins Hide and Seek 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

package net.tylermurphy.hideAndSeek.util;

import net.tylermurphy.hideAndSeek.command.*;
import net.tylermurphy.hideAndSeek.command.location.SetExitLocation;
import net.tylermurphy.hideAndSeek.command.location.SetLobbyLocation;
import net.tylermurphy.hideAndSeek.command.location.SetSpawnLocation;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

import static net.tylermurphy.hideAndSeek.configuration.Config.errorPrefix;
import static net.tylermurphy.hideAndSeek.configuration.Config.permissionsRequired;
import static net.tylermurphy.hideAndSeek.configuration.Localization.LOCAL;
import static net.tylermurphy.hideAndSeek.configuration.Localization.message;

public class CommandHandler {

	public static final Map<String,ICommand> COMMAND_REGISTER = new LinkedHashMap<>();
	
	private static void registerCommand(ICommand command) {
		if (!COMMAND_REGISTER.containsKey(command.getLabel())) {
			COMMAND_REGISTER.put(command.getLabel().toLowerCase(), command);
		}
	}
	
	public static void registerCommands() {
		registerCommand(new About());
		registerCommand(new Help());
		registerCommand(new Setup());
		registerCommand(new Start());
		registerCommand(new Stop());
		registerCommand(new SetSpawnLocation());
		registerCommand(new SetLobbyLocation());
		registerCommand(new SetExitLocation());
		registerCommand(new SetBorder());
		registerCommand(new Reload());
		registerCommand(new SaveMap());
		registerCommand(new SetBounds());
		registerCommand(new Join());
		registerCommand(new Leave());
		registerCommand(new Top());
		registerCommand(new Wins());
	}
	
	public static boolean handleCommand(CommandSender sender, String[] args) {
		if (!(sender instanceof Player)) {
			sender.sendMessage(errorPrefix + message("COMMAND_PLAYER_ONLY"));
		} else if (args.length < 1 || !COMMAND_REGISTER.containsKey(args[0].toLowerCase()) ) {
			if (permissionsRequired && !sender.hasPermission("hideandseek.about")) {
				sender.sendMessage(errorPrefix + LOCAL.get(""));
			} else {
				COMMAND_REGISTER.get("about").execute(sender, null);
			}
		} else {
			if (!args[0].equalsIgnoreCase("about") && !args[0].equalsIgnoreCase("help") && SaveMap.runningBackup) {
				sender.sendMessage(errorPrefix + message("MAPSAVE_INPROGRESS"));
			} else if (permissionsRequired && !sender.hasPermission("hideandseek."+args[0].toLowerCase())) {
				sender.sendMessage(errorPrefix + message("COMMAND_NOT_ALLOWED"));
			} else {
				try {
					COMMAND_REGISTER.get(args[0].toLowerCase()).execute(sender,Arrays.copyOfRange(args, 1, args.length));
				} catch (Exception e) {
					sender.sendMessage(errorPrefix + "An error has occured.");
					e.printStackTrace();
				}
			}
		}
		return true;
	}

}