summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/CommandBlockListenerAbstract.java
blob: 0cd512d55ebdca1f14fcad3e08da332414c7a703 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package net.minecraft.server;

import java.text.SimpleDateFormat;
import java.util.Date;

// CraftBukkit start
import java.util.ArrayList;
import org.apache.logging.log4j.Level;
import org.bukkit.craftbukkit.command.VanillaCommandWrapper;
import com.google.common.base.Joiner;
// CraftBukkit end

public abstract class CommandBlockListenerAbstract implements ICommandListener {

    private static final SimpleDateFormat a = new SimpleDateFormat("HH:mm:ss");
    private int b;
    private boolean c = true;
    private IChatBaseComponent d = null;
    public String e = ""; // CraftBukkit - private -> public
    private String f = "@";
    protected org.bukkit.command.CommandSender sender; // CraftBukkit - add sender;

    public CommandBlockListenerAbstract() {}

    public int g() {
        return this.b;
    }

    public IChatBaseComponent h() {
        return this.d;
    }

    public void a(NBTTagCompound nbttagcompound) {
        nbttagcompound.setString("Command", this.e);
        nbttagcompound.setInt("SuccessCount", this.b);
        nbttagcompound.setString("CustomName", this.f);
        if (this.d != null) {
            nbttagcompound.setString("LastOutput", ChatSerializer.a(this.d));
        }

        nbttagcompound.setBoolean("TrackOutput", this.c);
    }

    public void b(NBTTagCompound nbttagcompound) {
        this.e = nbttagcompound.getString("Command");
        this.b = nbttagcompound.getInt("SuccessCount");
        if (nbttagcompound.hasKeyOfType("CustomName", 8)) {
            this.f = nbttagcompound.getString("CustomName");
        }

        if (nbttagcompound.hasKeyOfType("LastOutput", 8)) {
            this.d = ChatSerializer.a(nbttagcompound.getString("LastOutput"));
        }

        if (nbttagcompound.hasKeyOfType("TrackOutput", 1)) {
            this.c = nbttagcompound.getBoolean("TrackOutput");
        }
    }

    public boolean a(int i, String s) {
        return i <= 2;
    }

    public void setCommand(String s) {
        this.e = s;
    }

    public String getCommand() {
        return this.e;
    }

    public void a(World world) {
        if (world.isStatic) {
            this.b = 0;
        }

        MinecraftServer minecraftserver = MinecraftServer.getServer();

        if (minecraftserver != null && minecraftserver.getEnableCommandBlock()) {
            // CraftBukkit start - Handle command block commands using Bukkit dispatcher
            org.bukkit.command.SimpleCommandMap commandMap = minecraftserver.server.getCommandMap();
            Joiner joiner = Joiner.on(" ");
            String command = this.e;
            if (this.e.startsWith("/")) {
                command = this.e.substring(1);
            }
            String[] args = command.split(" ");
            ArrayList<String[]> commands = new ArrayList<String[]>();

            // Block disallowed commands
            if (args[0].equalsIgnoreCase("stop") || args[0].equalsIgnoreCase("kick") || args[0].equalsIgnoreCase("op") ||
                    args[0].equalsIgnoreCase("deop") || args[0].equalsIgnoreCase("ban") || args[0].equalsIgnoreCase("ban-ip") ||
                    args[0].equalsIgnoreCase("pardon") || args[0].equalsIgnoreCase("pardon-ip") || args[0].equalsIgnoreCase("reload")) {
                this.b = 0;
                return;
            }

            // If the world has no players don't run
            if (this.getWorld().players.isEmpty()) {
                this.b = 0;
                return;
            }

            // Handle vanilla commands;
            if (minecraftserver.server.getCommandBlockOverride(args[0])) {
                org.bukkit.command.Command commandBlockCommand = commandMap.getCommand("minecraft:" + args[0]);
                if (commandBlockCommand instanceof VanillaCommandWrapper) {
                    this.b = ((VanillaCommandWrapper) commandBlockCommand).dispatchVanillaCommandBlock(this, this.e);
                    return;
                }
            }

            // Make sure this is a valid command
            if (commandMap.getCommand(args[0]) == null) {
                this.b = 0;
                return;
            }

            // testfor command requires special handling
            if (args[0].equalsIgnoreCase("testfor")) {
                if (args.length < 2) {
                    this.b = 0;
                    return;
                }

                EntityPlayer[] players = PlayerSelector.getPlayers(this, args[1]);

                if (players != null && players.length > 0) {
                    this.b = players.length;
                    return;
                } else {
                    EntityPlayer player = MinecraftServer.getServer().getPlayerList().getPlayer(args[1]);
                    if (player == null) {
                        this.b = 0;
                        return;
                    } else {
                        this.b = 1;
                        return;
                    }
                }
            }

            commands.add(args);

            // Find positions of command block syntax, if any
            ArrayList<String[]> newCommands = new ArrayList<String[]>();
            for (int i = 0; i < args.length; i++) {
                if (PlayerSelector.isPattern(args[i])) {
                    for (int j = 0; j < commands.size(); j++) {
                        newCommands.addAll(this.buildCommands(commands.get(j), i));
                    }
                    ArrayList<String[]> temp = commands;
                    commands = newCommands;
                    newCommands = temp;
                    newCommands.clear();
                }
            }

            int completed = 0;

            // Now dispatch all of the commands we ended up with
            for (int i = 0; i < commands.size(); i++) {
                try {
                    if (commandMap.dispatch(sender, joiner.join(java.util.Arrays.asList(commands.get(i))))) {
                        completed++;
                    }
                } catch (Throwable exception) {
                    if(this instanceof TileEntityCommandListener) {
                        TileEntityCommandListener listener = (TileEntityCommandListener) this;
                        MinecraftServer.getLogger().log(Level.WARN, String.format("CommandBlock at (%d,%d,%d) failed to handle command", listener.getChunkCoordinates().x, listener.getChunkCoordinates().y, listener.getChunkCoordinates().z), exception);
                    } else if (this instanceof EntityMinecartCommandBlockListener) {
                        EntityMinecartCommandBlockListener listener = (EntityMinecartCommandBlockListener) this;
                        MinecraftServer.getLogger().log(Level.WARN, String.format("MinecartCommandBlock at (%d,%d,%d) failed to handle command", listener.getChunkCoordinates().x, listener.getChunkCoordinates().y, listener.getChunkCoordinates().z), exception);
                    } else {
                        MinecraftServer.getLogger().log(Level.WARN, String.format("Unknown CommandBlock failed to handle command"), exception);
                    }
                }
            }

            this.b = completed;
            // CraftBukkit end
        } else {
            this.b = 0;
        }
    }

    // CraftBukkit start
    private ArrayList<String[]> buildCommands(String[] args, int pos) {
        ArrayList<String[]> commands = new ArrayList<String[]>();
        EntityPlayer[] players = PlayerSelector.getPlayers(this, args[pos]);
        if (players != null) {
            for (EntityPlayer player : players) {
                if (player.world != this.getWorld()) {
                    continue;
                }
                String[] command = args.clone();
                command[pos] = player.getName();
                commands.add(command);
            }
        }

        return commands;
    }
    // CraftBukkit end

    public String getName() {
        return this.f;
    }

    public IChatBaseComponent getScoreboardDisplayName() {
        return new ChatComponentText(this.getName());
    }

    public void setName(String s) {
        this.f = s;
    }

    public void sendMessage(IChatBaseComponent ichatbasecomponent) {
        if (this.c && this.getWorld() != null && !this.getWorld().isStatic) {
            this.d = (new ChatComponentText("[" + a.format(new Date()) + "] ")).addSibling(ichatbasecomponent);
            this.e();
        }
    }

    public abstract void e();

    public void b(IChatBaseComponent ichatbasecomponent) {
        this.d = ichatbasecomponent;
    }
}