-
Notifications
You must be signed in to change notification settings - Fork 497
/
CommandHelpers.java
139 lines (124 loc) · 5.03 KB
/
CommandHelpers.java
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
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
* <p/>
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.lib.commands;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
public final class CommandHelpers {
private CommandHelpers() {
}
public static World getWorld(ICommandSender sender, IModCommand command, String[] args, int worldArgIndex) {
// Handle passed in world argument
if (worldArgIndex < args.length) {
try {
int dim = Integer.parseInt(args[worldArgIndex]);
World world = MinecraftServer.getServer().worldServerForDimension(dim);
if (world != null) {
return world;
}
} catch (Exception ex) {
throwWrongUsage(sender, command);
}
}
return getWorld(sender, command);
}
public static World getWorld(ICommandSender sender, IModCommand command) {
if (sender instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) sender;
return player.worldObj;
}
return MinecraftServer.getServer().worldServerForDimension(0);
}
public static String[] getPlayers() {
return MinecraftServer.getServer().getAllUsernames();
}
public static void sendLocalizedChatMessage(ICommandSender sender, String locTag, Object... args) {
sender.addChatMessage(new ChatComponentTranslation(locTag, args));
}
public static void sendLocalizedChatMessage(ICommandSender sender, ChatStyle chatStyle, String locTag, Object... args) {
ChatComponentTranslation chat = new ChatComponentTranslation(locTag, args);
chat.setChatStyle(chatStyle);
sender.addChatMessage(chat);
}
/**
Avoid using this function if at all possible. Commands are processed on the server,
which has no localization information.
@param sender
@param message
*/
public static void sendChatMessage(ICommandSender sender, String message) {
sender.addChatMessage(new ChatComponentText(message));
}
public static void throwWrongUsage(ICommandSender sender, IModCommand command) throws WrongUsageException {
throw new WrongUsageException(String.format(StatCollector.translateToLocal("command.buildcraft.help"), command.getCommandUsage(sender)));
}
public static void processChildCommand(ICommandSender sender, SubCommand child, String[] args) {
if (!sender.canCommandSenderUseCommand(child.getRequiredPermissionLevel(), child.getFullCommandString())) {
throw new WrongUsageException(StatCollector.translateToLocal("command.buildcraft.noperms"));
}
String[] newargs = new String[args.length - 1];
System.arraycopy(args, 1, newargs, 0, newargs.length);
child.processCommand(sender, newargs);
}
public static void printHelp(ICommandSender sender, IModCommand command) {
ChatStyle header = new ChatStyle();
header.setColor(EnumChatFormatting.GRAY);
header.setBold(true);
sendLocalizedChatMessage(sender, header, "command.buildcraft." + command.getFullCommandString().replace(" ", ".") + ".format", command.getFullCommandString());
ChatStyle body = new ChatStyle();
body.setColor(EnumChatFormatting.GRAY);
if (command.getCommandAliases().size() > 0) {
sendLocalizedChatMessage(sender, body, "command.buildcraft.aliases", command.getCommandAliases().toString().replace("[", "").replace("]", ""));
}
if (command.getRequiredPermissionLevel() > 0) {
sendLocalizedChatMessage(sender, body, "command.buildcraft.permlevel", command.getRequiredPermissionLevel());
}
sendLocalizedChatMessage(sender, body, "command.buildcraft." + command.getFullCommandString().replace(" ", ".") + ".help");
if (!command.getChildren().isEmpty()) {
sendLocalizedChatMessage(sender, "command.buildcraft.list");
for (SubCommand child : command.getChildren()) {
sendLocalizedChatMessage(sender, "command.buildcraft." + child.getFullCommandString().replace(" ", ".") + ".desc", child.getCommandName());
}
}
}
public static boolean processStandardCommands(ICommandSender sender, IModCommand command, String[] args) {
if (args.length >= 1) {
if ("help".equals(args[0])) {
command.printHelp(sender);
return true;
}
for (SubCommand child : command.getChildren()) {
if (matches(args[0], child)) {
processChildCommand(sender, child, args);
return true;
}
}
}
return false;
}
public static boolean matches(String commandName, IModCommand command) {
if (commandName.equals(command.getCommandName())) {
return true;
} else if (command.getCommandAliases() != null) {
for (String alias : command.getCommandAliases()) {
if (commandName.equals(alias)) {
return true;
}
}
}
return false;
}
}