summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/bukkit/craftbukkit/command/VanillaCommandWrapper.java
blob: 0319bf3b7183ad2cbe36e391083047864df219ff (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
package org.bukkit.craftbukkit.command;

import java.util.Iterator;
import java.util.List;

import net.minecraft.server.*;

import org.apache.commons.lang.Validate;
import org.apache.logging.log4j.Level;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.RemoteConsoleCommandSender;
import org.bukkit.command.defaults.*;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.entity.CraftMinecartCommand;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.entity.minecart.CommandMinecart;

public final class VanillaCommandWrapper extends VanillaCommand {
    protected final CommandAbstract vanillaCommand;

    public VanillaCommandWrapper(CommandAbstract vanillaCommand) {
        super(vanillaCommand.getCommand());
        this.vanillaCommand = vanillaCommand;
    }

    public VanillaCommandWrapper(CommandAbstract vanillaCommand, String usage) {
        super(vanillaCommand.getCommand());
        this.vanillaCommand = vanillaCommand;
        this.description = "A Mojang provided command.";
        this.usageMessage = usage;
        this.setPermission("minecraft.command." + vanillaCommand.getCommand());
    }

    @Override
    public boolean execute(CommandSender sender, String commandLabel, String[] args) {
        if (!testPermission(sender)) return true;

        ICommandListener icommandlistener = getListener(sender);
        // Some commands use the worldserver variable but we leave it full of null values,
        // so we must temporarily populate it with the world of the commandsender
        WorldServer[] prev = MinecraftServer.getServer().worldServer;
        MinecraftServer.getServer().worldServer = new WorldServer[]{(WorldServer) icommandlistener.getWorld()};
        try {
            vanillaCommand.execute(icommandlistener, args);
            // PAIL fake throws
            if (false) throw new ExceptionUsage(null, null);
            if (false) throw new CommandException(null, null);
        } catch (ExceptionUsage exceptionusage) {
            ChatMessage chatmessage = new ChatMessage("commands.generic.usage", new Object[] {new ChatMessage(exceptionusage.getMessage(), exceptionusage.getArgs())});
            chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
            icommandlistener.sendMessage(chatmessage);
        } catch (CommandException commandexception) {
            ChatMessage chatmessage = new ChatMessage(commandexception.getMessage(), commandexception.getArgs());
            chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
            icommandlistener.sendMessage(chatmessage);
        } finally {
            MinecraftServer.getServer().worldServer = prev;
        }
        return true;
    }

    @Override
    public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
        Validate.notNull(sender, "Sender cannot be null");
        Validate.notNull(args, "Arguments cannot be null");
        Validate.notNull(alias, "Alias cannot be null");
        return (List<String>) vanillaCommand.tabComplete(getListener(sender), args, new BlockPosition(0, 0, 0));
    }

    public final int dispatchVanillaCommandBlock(ICommandListener icommandlistener, String s) {
        // Copied from net.minecraft.server.CommandHandler
        s = s.trim();
        if (s.startsWith("/")) {
            s = s.substring(1);
        }
        String as[] = s.split(" ");
        as = dropFirstArgument(as);
        int i = getPlayerListSize(as);
        int j = 0;
        // Some commands use the worldserver variable but we leave it full of null values,
        // so we must temporarily populate it with the world of the commandsender
        WorldServer[] prev = MinecraftServer.getServer().worldServer;
        MinecraftServer.getServer().worldServer = new WorldServer[]{(WorldServer) icommandlistener.getWorld()};
        try {
            if (vanillaCommand.canUse(icommandlistener)) {
                if (i > -1) {
                    List<Entity> list = ((List<Entity>)PlayerSelector.getPlayers(icommandlistener, as[i], Entity.class));
                    String s2 = as[i];
                    
                    icommandlistener.a(EnumCommandResult.AFFECTED_ENTITIES, list.size());
                    Iterator<Entity> iterator = list.iterator();

                    while (iterator.hasNext()) {
                        Entity entity = iterator.next();

                        try {
                            as[i] = entity.getUniqueID().toString();
                            vanillaCommand.execute(icommandlistener, as);
                            j++;
                            // PAIL fake throws
                            if (false) throw new ExceptionUsage(null, null);
                            if (false) throw new CommandException(null, null);
                        } catch (ExceptionUsage exceptionusage) {
                            ChatMessage chatmessage = new ChatMessage("commands.generic.usage", new Object[] { new ChatMessage(exceptionusage.getMessage(), exceptionusage.getArgs())});
                            chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
                            icommandlistener.sendMessage(chatmessage);
                        } catch (CommandException commandexception) {
                            ChatMessage chatmessage = new ChatMessage(commandexception.getMessage(), commandexception.getArgs());
                            chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
                            icommandlistener.sendMessage(chatmessage);
                        } 
                    }
                    as[i] = s2;
                } else {
                    icommandlistener.a(EnumCommandResult.AFFECTED_ENTITIES, 1);
                    vanillaCommand.execute(icommandlistener, as);
                    j++;
                }
            } else {
                ChatMessage chatmessage = new ChatMessage("commands.generic.permission", new Object[0]);
                chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
                icommandlistener.sendMessage(chatmessage);
            }
            // PAIL start: fix compile error
            if (false) throw new ExceptionUsage(null, null);
            if (false) throw new CommandException(null, null);
            // PAIL end
        } catch (ExceptionUsage exceptionusage) {
            ChatMessage chatmessage1 = new ChatMessage("commands.generic.usage", new Object[] { new ChatMessage(exceptionusage.getMessage(), exceptionusage.getArgs()) });
            chatmessage1.getChatModifier().setColor(EnumChatFormat.RED);
            icommandlistener.sendMessage(chatmessage1);
        } catch (CommandException commandexception) {
            ChatMessage chatmessage2 = new ChatMessage(commandexception.getMessage(), commandexception.getArgs());
            chatmessage2.getChatModifier().setColor(EnumChatFormat.RED);
            icommandlistener.sendMessage(chatmessage2);
        } catch (Throwable throwable) {
            ChatMessage chatmessage3 = new ChatMessage("commands.generic.exception", new Object[0]);
            chatmessage3.getChatModifier().setColor(EnumChatFormat.RED);
            icommandlistener.sendMessage(chatmessage3);
            if(icommandlistener instanceof TileEntityCommandListener) {
                TileEntityCommandListener listener = (TileEntityCommandListener) icommandlistener;
                MinecraftServer.getLogger().log(Level.WARN, String.format("CommandBlock at (%d,%d,%d) failed to handle command", listener.getChunkCoordinates().getX(), listener.getChunkCoordinates().getY(), listener.getChunkCoordinates().getZ()), throwable);
            } else if (icommandlistener instanceof EntityMinecartCommandBlockListener) {
                EntityMinecartCommandBlockListener listener = (EntityMinecartCommandBlockListener) icommandlistener;
                MinecraftServer.getLogger().log(Level.WARN, String.format("MinecartCommandBlock at (%d,%d,%d) failed to handle command", listener.getChunkCoordinates().getX(), listener.getChunkCoordinates().getY(), listener.getChunkCoordinates().getZ()), throwable);
            } else {
                MinecraftServer.getLogger().log(Level.WARN, String.format("Unknown CommandBlock failed to handle command"), throwable);
            }
        } finally {
            MinecraftServer.getServer().worldServer = prev;
        }
        icommandlistener.a(EnumCommandResult.SUCCESS_COUNT, j);
        return j;
    }

    private ICommandListener getListener(CommandSender sender) {
        if (sender instanceof Player) {
            return ((CraftPlayer) sender).getHandle();
        }
        if (sender instanceof BlockCommandSender) {
            return ((CraftBlockCommandSender) sender).getTileEntity();
        }
        if (sender instanceof CommandMinecart) {
            return ((EntityMinecartCommandBlock) ((CraftMinecartCommand) sender).getHandle()).getCommandBlock();
        }
        if (sender instanceof RemoteConsoleCommandSender) {
            return RemoteControlCommandListener.getInstance();
        }
        if (sender instanceof ConsoleCommandSender) {
            return ((CraftServer) sender.getServer()).getServer();
        }
        return null;
    }

    private int getPlayerListSize(String as[]) {
        for (int i = 0; i < as.length; i++) {
            if (vanillaCommand.isListStart(as, i) && PlayerSelector.isList(as[i])) {
                return i;
            }
        }
        return -1;
    }

    private String[] dropFirstArgument(String as[]) {
        String as1[] = new String[as.length - 1];
        for (int i = 1; i < as.length; i++) {
            as1[i - 1] = as[i];
        }

        return as1;
    }
}