-
Notifications
You must be signed in to change notification settings - Fork 14
/
CommandOverride.java
108 lines (86 loc) · 3.45 KB
/
CommandOverride.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
package serverutils.utils.ranks;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.common.util.FakePlayerFactory;
import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.eventhandler.Event;
import serverutils.lib.lib.command.CommandTreeBase;
public class CommandOverride extends CommandBase {
public static ICommand create(ICommand command, String parent, @Nullable ModContainer container) {
if (command instanceof CommandTreeBase) {
return new CommandTreeOverride((CommandTreeBase) command, parent, container);
} else {
return new CommandOverride(command, parent, container);
}
}
public final ICommand mirrored;
public final String node;
public final IChatComponent usage;
public final ModContainer modContainer;
private CommandOverride(ICommand c, String parent, @Nullable ModContainer container) {
mirrored = c;
node = parent + '.' + mirrored.getCommandName();
Ranks.INSTANCE.commands.put(node, this);
String usageS = getCommandUsage(FakePlayerFactory.getMinecraft(Ranks.INSTANCE.universe.world));
if (usageS == null || usageS.isEmpty()
|| usageS.indexOf('/') != -1
|| usageS.indexOf('%') != -1
|| usageS.indexOf(' ') != -1) {
usage = new ChatComponentText(usageS);
} else {
usage = new ChatComponentTranslation(usageS);
}
modContainer = container;
}
@Override
public String getCommandName() {
return mirrored.getCommandName();
}
@Override
public String getCommandUsage(ICommandSender sender) {
return mirrored.getCommandUsage(sender);
}
@Override
public List<String> getCommandAliases() {
return mirrored.getCommandAliases();
}
@Override
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
mirrored.processCommand(sender, args);
}
@Override
public int getRequiredPermissionLevel() {
return mirrored instanceof CommandBase ? ((CommandBase) mirrored).getRequiredPermissionLevel() : 4;
}
@Override
public boolean canCommandSenderUseCommand(ICommandSender sender) {
if (sender instanceof EntityPlayerMP) {
Event.Result result = Ranks.INSTANCE.getPermissionResult((EntityPlayerMP) sender, node, true);
if (result != Event.Result.DEFAULT) {
return result == Event.Result.ALLOW;
}
}
return mirrored.canCommandSenderUseCommand(sender);
}
@Override
public List<String> addTabCompletionOptions(ICommandSender sender, String[] args) {
return mirrored.addTabCompletionOptions(sender, args);
}
@Override
public boolean isUsernameIndex(String[] args, int index) {
return mirrored.isUsernameIndex(args, index);
}
@Override
public int compareTo(ICommand o) {
return o instanceof CommandOverride ? node.compareTo(((CommandOverride) o).node)
: getCommandName().compareTo(o.getCommandName());
}
}