summaryrefslogtreecommitdiff
path: root/src/com/redstoner/modules/blockplacemods/BlockPlaceMods.java
blob: 6befbae97a23aef02bc956745422adae42ab6f26 (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
package com.redstoner.modules.blockplacemods;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;

import com.nemez.cmdmgr.Command;
import com.redstoner.annotations.AutoRegisterListener;
import com.redstoner.annotations.Commands;
import com.redstoner.annotations.Version;
import com.redstoner.misc.CommandHolderType;
import com.redstoner.misc.Main;
import com.redstoner.modules.Module;
import com.redstoner.modules.blockplacemods.mods.Mod;
import com.redstoner.modules.blockplacemods.mods.ModAbstract;
import com.redstoner.modules.blockplacemods.mods.ModToggledAbstract;
import com.redstoner.utils.CommandException;
import com.redstoner.utils.CommandMap;

@Commands(CommandHolderType.None)
@AutoRegisterListener
@Version(major = 4, minor = 1, revision = 1, compatible = 4)
public final class BlockPlaceMods implements Module, Listener
{
	@Override
	public boolean onEnable()
	{
		ModAbstract.registerAll(getLogger());
		for (Mod mod : new ArrayList<>(ModAbstract.getMods().values()))
		{
			mod.registerListeners();
		}
		try
		{
			Map<String, org.bukkit.command.Command> commandMap = CommandMap.getCommandMap();
			org.bukkit.command.Command command = new BlockPlaceModsCommand();
			for (String alias : getCommandAliases())
			{
				commandMap.put(alias, command);
			}
		}
		catch (ReflectiveOperationException ex)
		{
			ex.printStackTrace();
			return false;
		}
		return true;
	}
	
	@Override
	public void postEnable()
	{
		setPrefix("BPM");
	}
	
	@Override
	public void onDisable()
	{
		for (Mod mod : ModAbstract.getMods().values())
		{
			mod.unregisterListeners();
		}
		try
		{
			Map<String, org.bukkit.command.Command> commandMap = CommandMap.getCommandMap();
			for (String alias : getCommandAliases())
			{
				org.bukkit.command.Command command = commandMap.get(alias);
				if (command != null && command.getClass() == BlockPlaceModsCommand.class)
				{
					commandMap.remove(alias);
				}
			}
		}
		catch (Exception ignored)
		{}
	}
	
	private static String[] getCommandAliases()
	{
		String pluginName = Main.plugin.getName().toLowerCase();
		// @noformat
		return new String[]{"mod", 		pluginName + ":mod",
				"set", 		pluginName + ":set",
				"toggle",	pluginName + ":toggle"
		};
		// @format
	}
	
	@Command(hook = "mod_empty")
	public void onModEmptyCommand(CommandSender sender)
	{
		onModCommand(sender, "");
	}
	
	@Command(hook = "mod")
	public void onModCommand(CommandSender sender, String input)
	{
		String[] args = new ArrayList<>(Arrays.asList(input.split(" "))).stream()
				.filter(x -> x != null && !x.trim().isEmpty()).toArray(String[]::new);
		String prefix = "";
		String message;
		try
		{
			if (args.length > 0)
			{
				Mod target = ModAbstract.getMod(args[0].toLowerCase());
				if (target != null)
				{
					prefix += "&7[&2" + capitalize(target.getName()) + "&7]:&a ";
					if (!(sender instanceof Player))
					{
						message = "&cYou must be a player to use any block place mod";
					}
					else
					{
						message = target.runCommand((Player) sender, Arrays.copyOfRange(args, 1, args.length));
					}
				}
				else if (args[0].equalsIgnoreCase("help"))
				{
					message = commandHelp(sender, args);
				}
				else
				{
					message = "&cThat argument could not be recognized";
				}
			}
			else
			{
				message = commandHelp(sender, args);
			}
		}
		catch (CommandException ex)
		{
			message = " &c" + ex.getMessage();
		}
		catch (Throwable t)
		{
			message = " &cAn unexpected error occurred while executing this command.";
			t.printStackTrace();
		}
		getLogger().message(sender, prefix + message);
	}
	
	private String commandHelp(CommandSender sender, String[] args)
	{
		StringBuilder result = new StringBuilder("§7BlockPlaceMods adds some redstone-centric utilities");
		result.append("\n").append(ChatColor.GRAY.toString()).append("Available mods:");
		List<Mod> mods = new ArrayList<>(new HashSet<>(ModAbstract.getMods().values()));
		mods.sort(Comparator.<Mod> comparingInt(m -> ModToggledAbstract.class.isInstance(m) ? 1 : -1)
				.thenComparing(Mod::getName));
		for (Mod mod : mods)
		{
			result.append("\n").append(ChatColor.AQUA.toString()).append("/mod ").append(ChatColor.ITALIC.toString())
					.append(mod.getName());
			for (String alias : mod.getAliases())
			{
				result.append('|').append(alias);
			}
			result.append(ChatColor.GRAY.toString()).append(" - ").append(mod.getDescription());
		}
		return result.toString();
	}
	
	private static String capitalize(String modName)
	{
		if (modName.isEmpty())
		{
			return modName;
		}
		char first = modName.charAt(0);
		if (first != (first = Character.toUpperCase(first)))
		{
			char[] result = modName.toCharArray();
			result[0] = first;
			return String.valueOf(result);
		}
		return modName;
	}
	
	private class BlockPlaceModsCommand extends org.bukkit.command.Command
	{
		public BlockPlaceModsCommand()
		{
			super("mod");
			String[] aliases = getCommandAliases();
			setAliases(Arrays.asList(Arrays.copyOfRange(aliases, 1, aliases.length)));
		}
		
		@Override
		public boolean execute(CommandSender sender, String label, String[] args)
		{
			onModCommand(sender, String.join(" ", args));
			return true;
		}
	}
}