summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/TileEntityCommand.java
blob: 4ff401b0571ca8df772469d2ea185e164c90b185 (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
package net.minecraft.server;

// CraftBukkit start
import java.util.ArrayList;
import java.util.Arrays;
import com.google.common.base.Joiner;
// CraftBukkit end

public class TileEntityCommand extends TileEntity implements ICommandListener {

    private String a = "";
    private final org.bukkit.command.BlockCommandSender sender;

    public TileEntityCommand() {
        sender = new org.bukkit.craftbukkit.command.CraftBlockCommandSender(this);
    }

    public void b(String s) {
        this.a = s;
        this.update();
    }

    public void a(World world) {
        if (!world.isStatic) {
            MinecraftServer minecraftserver = MinecraftServer.getServer();

            if (minecraftserver != null && minecraftserver.getEnableCommandBlock()) {
                // CraftBukkit start - handle command block as console
                org.bukkit.command.SimpleCommandMap commandMap = minecraftserver.server.getCommandMap();
                Joiner joiner = Joiner.on(" ");
                String command = this.a;
                if (this.a.startsWith("/")) {
                    command = this.a.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")) {
                    return;
                }

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

                // if the world has no players don't run
                if (this.world.players.isEmpty()) {
                    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();
                    }
                }

                // now dispatch all of the commands we ended up with
                for (int i = 0; i < commands.size(); i++) {
                    commandMap.dispatch(sender, joiner.join(Arrays.asList(commands.get(i))));
                }
                // CraftBukkit end
            }
        }
    }

    // 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.world) {
                    continue;
                }
                String[] command = args.clone();
                command[pos] = player.getLocalizedName();
                commands.add(command);
            }
        }

        return commands;
    }
    // CraftBukkit end

    public String getName() {
        return "@";
    }

    public void sendMessage(String s) {}

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

    public String a(String s, Object... aobject) {
        return s;
    }

    public void b(NBTTagCompound nbttagcompound) {
        super.b(nbttagcompound);
        nbttagcompound.setString("Command", this.a);
    }

    public void a(NBTTagCompound nbttagcompound) {
        super.a(nbttagcompound);
        this.a = nbttagcompound.getString("Command");
    }

    public ChunkCoordinates b() {
        return new ChunkCoordinates(this.x, this.y, this.z);
    }

    public Packet getUpdatePacket() {
        NBTTagCompound nbttagcompound = new NBTTagCompound();

        this.b(nbttagcompound);
        return new Packet132TileEntityData(this.x, this.y, this.z, 2, nbttagcompound);
    }
}