001package net.minecraft.tileentity;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import net.minecraft.command.ICommandManager;
006import net.minecraft.command.ICommandSender;
007import net.minecraft.nbt.NBTTagCompound;
008import net.minecraft.network.packet.Packet;
009import net.minecraft.network.packet.Packet132TileEntityData;
010import net.minecraft.server.MinecraftServer;
011import net.minecraft.util.ChunkCoordinates;
012import net.minecraft.world.World;
013
014public class TileEntityCommandBlock extends TileEntity implements ICommandSender
015{
016    /** The command this block will execute when powered. */
017    private String command = "";
018
019    /**
020     * Sets the command this block will execute when powered.
021     */
022    public void setCommand(String par1Str)
023    {
024        this.command = par1Str;
025        this.onInventoryChanged();
026    }
027
028    @SideOnly(Side.CLIENT)
029
030    /**
031     * Return the command this command block is set to execute.
032     */
033    public String getCommand()
034    {
035        return this.command;
036    }
037
038    /**
039     * Execute the command, called when the command block is powered.
040     */
041    public void executeCommandOnPowered(World par1World)
042    {
043        if (!par1World.isRemote)
044        {
045            MinecraftServer var2 = MinecraftServer.getServer();
046
047            if (var2 != null && var2.isCommandBlockEnabled())
048            {
049                ICommandManager var3 = var2.getCommandManager();
050                var3.executeCommand(this, this.command);
051            }
052        }
053    }
054
055    /**
056     * Gets the name of this command sender (usually username, but possibly "Rcon")
057     */
058    public String getCommandSenderName()
059    {
060        return "@";
061    }
062
063    public void sendChatToPlayer(String par1Str) {}
064
065    /**
066     * Returns true if the command sender is allowed to use the given command.
067     */
068    public boolean canCommandSenderUseCommand(int par1, String par2Str)
069    {
070        return par1 <= 2;
071    }
072
073    /**
074     * Translates and formats the given string key with the given arguments.
075     */
076    public String translateString(String par1Str, Object ... par2ArrayOfObj)
077    {
078        return par1Str;
079    }
080
081    /**
082     * Writes a tile entity to NBT.
083     */
084    public void writeToNBT(NBTTagCompound par1NBTTagCompound)
085    {
086        super.writeToNBT(par1NBTTagCompound);
087        par1NBTTagCompound.setString("Command", this.command);
088    }
089
090    /**
091     * Reads a tile entity from NBT.
092     */
093    public void readFromNBT(NBTTagCompound par1NBTTagCompound)
094    {
095        super.readFromNBT(par1NBTTagCompound);
096        this.command = par1NBTTagCompound.getString("Command");
097    }
098
099    /**
100     * Return the position for this command sender.
101     */
102    public ChunkCoordinates getPlayerCoordinates()
103    {
104        return new ChunkCoordinates(this.xCoord, this.yCoord, this.zCoord);
105    }
106
107    /**
108     * Overriden in a sign to provide the text.
109     */
110    public Packet getDescriptionPacket()
111    {
112        NBTTagCompound var1 = new NBTTagCompound();
113        this.writeToNBT(var1);
114        return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 2, var1);
115    }
116}