    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    private int field_96106_a = 0;
    017
    018    /** The command this block will execute when powered. */
    019    private String command = "";
    020    private String field_96105_c = "@";
    021
    022    /**
    023     * Sets the command this block will execute when powered.
    024     */
    025    public void setCommand(String par1Str)
    026    {
    027        this.command = par1Str;
    028        this.onInventoryChanged();
    029    }
    030
    031    @SideOnly(Side.CLIENT)
    032
    033    /**
    034     * Return the command this command block is set to execute.
    035     */
    036    public String getCommand()
    037    {
    038        return this.command;
    039    }
    040
    041    /**
    042     * Execute the command, called when the command block is powered.
    043     */
    044    public int executeCommandOnPowered(World par1World)
    045    {
    046        if (par1World.isRemote)
    047        {
    048            return 0;
    049        }
    050        else
    051        {
    052            MinecraftServer minecraftserver = MinecraftServer.getServer();
    053
    054            if (minecraftserver != null && minecraftserver.isCommandBlockEnabled())
    055            {
    056                ICommandManager icommandmanager = minecraftserver.getCommandManager();
    057                return icommandmanager.executeCommand(this, this.command);
    058            }
    059            else
    060            {
    061                return 0;
    062            }
    063        }
    064    }
    065
    066    /**
    067     * Gets the name of this command sender (usually username, but possibly "Rcon")
    068     */
    069    public String getCommandSenderName()
    070    {
    071        return this.field_96105_c;
    072    }
    073
    074    public void func_96104_c(String par1Str)
    075    {
    076        this.field_96105_c = par1Str;
    077    }
    078
    079    public void sendChatToPlayer(String par1Str) {}
    080
    081    /**
    082     * Returns true if the command sender is allowed to use the given command.
    083     */
    084    public boolean canCommandSenderUseCommand(int par1, String par2Str)
    085    {
    086        return par1 <= 2;
    087    }
    088
    089    /**
    090     * Translates and formats the given string key with the given arguments.
    091     */
    092    public String translateString(String par1Str, Object ... par2ArrayOfObj)
    093    {
    094        return par1Str;
    095    }
    096
    097    /**
    098     * Writes a tile entity to NBT.
    099     */
    100    public void writeToNBT(NBTTagCompound par1NBTTagCompound)
    101    {
    102        super.writeToNBT(par1NBTTagCompound);
    103        par1NBTTagCompound.setString("Command", this.command);
    104        par1NBTTagCompound.setInteger("SuccessCount", this.field_96106_a);
    105        par1NBTTagCompound.setString("CustomName", this.field_96105_c);
    106    }
    107
    108    /**
    109     * Reads a tile entity from NBT.
    110     */
    111    public void readFromNBT(NBTTagCompound par1NBTTagCompound)
    112    {
    113        super.readFromNBT(par1NBTTagCompound);
    114        this.command = par1NBTTagCompound.getString("Command");
    115        this.field_96106_a = par1NBTTagCompound.getInteger("SuccessCount");
    116
    117        if (par1NBTTagCompound.hasKey("CustomName"))
    118        {
    119            this.field_96105_c = par1NBTTagCompound.getString("CustomName");
    120        }
    121    }
    122
    123    /**
    124     * Return the position for this command sender.
    125     */
    126    public ChunkCoordinates getPlayerCoordinates()
    127    {
    128        return new ChunkCoordinates(this.xCoord, this.yCoord, this.zCoord);
    129    }
    130
    131    /**
    132     * Overriden in a sign to provide the text.
    133     */
    134    public Packet getDescriptionPacket()
    135    {
    136        NBTTagCompound nbttagcompound = new NBTTagCompound();
    137        this.writeToNBT(nbttagcompound);
    138        return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 2, nbttagcompound);
    139    }
    140
    141    public int func_96103_d()
    142    {
    143        return this.field_96106_a;
    144    }
    145
    146    public void func_96102_a(int par1)
    147    {
    148        this.field_96106_a = par1;
    149    }
    150}