001package net.minecraft.client.gui;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.io.ByteArrayOutputStream;
006import java.io.DataOutputStream;
007import net.minecraft.network.packet.Packet;
008import net.minecraft.network.packet.Packet250CustomPayload;
009import net.minecraft.tileentity.TileEntityCommandBlock;
010import net.minecraft.util.StringTranslate;
011import org.lwjgl.input.Keyboard;
012
013@SideOnly(Side.CLIENT)
014public class GuiCommandBlock extends GuiScreen
015{
016    /** Text field containing the command block's command. */
017    private GuiTextField commandTextField;
018
019    /** Command block being edited. */
020    private final TileEntityCommandBlock commandBlock;
021
022    public GuiCommandBlock(TileEntityCommandBlock par1)
023    {
024        this.commandBlock = par1;
025    }
026
027    /**
028     * Called from the main game loop to update the screen.
029     */
030    public void updateScreen()
031    {
032        this.commandTextField.updateCursorCounter();
033    }
034
035    /**
036     * Adds the buttons (and other controls) to the screen in question.
037     */
038    public void initGui()
039    {
040        StringTranslate var1 = StringTranslate.getInstance();
041        Keyboard.enableRepeatEvents(true);
042        this.controlList.clear();
043        this.controlList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 96 + 12, var1.translateKey("gui.done")));
044        this.controlList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 120 + 12, var1.translateKey("gui.cancel")));
045        this.commandTextField = new GuiTextField(this.fontRenderer, this.width / 2 - 150, 60, 300, 20);
046        this.commandTextField.setMaxStringLength(32767);
047        this.commandTextField.setFocused(true);
048        this.commandTextField.setText(this.commandBlock.getCommand());
049    }
050
051    /**
052     * Called when the screen is unloaded. Used to disable keyboard repeat events
053     */
054    public void onGuiClosed()
055    {
056        Keyboard.enableRepeatEvents(false);
057    }
058
059    /**
060     * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
061     */
062    protected void actionPerformed(GuiButton par1GuiButton)
063    {
064        if (par1GuiButton.enabled)
065        {
066            if (par1GuiButton.id == 1)
067            {
068                this.mc.displayGuiScreen((GuiScreen)null);
069            }
070            else if (par1GuiButton.id == 0)
071            {
072                String var2 = "MC|AdvCdm";
073                ByteArrayOutputStream var3 = new ByteArrayOutputStream();
074                DataOutputStream var4 = new DataOutputStream(var3);
075
076                try
077                {
078                    var4.writeInt(this.commandBlock.xCoord);
079                    var4.writeInt(this.commandBlock.yCoord);
080                    var4.writeInt(this.commandBlock.zCoord);
081                    Packet.writeString(this.commandTextField.getText(), var4);
082                    this.mc.getSendQueue().addToSendQueue(new Packet250CustomPayload(var2, var3.toByteArray()));
083                }
084                catch (Exception var6)
085                {
086                    var6.printStackTrace();
087                }
088
089                this.mc.displayGuiScreen((GuiScreen)null);
090            }
091        }
092    }
093
094    /**
095     * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
096     */
097    protected void keyTyped(char par1, int par2)
098    {
099        this.commandTextField.textboxKeyTyped(par1, par2);
100        ((GuiButton)this.controlList.get(0)).enabled = this.commandTextField.getText().trim().length() > 0;
101
102        if (par1 == 13)
103        {
104            this.actionPerformed((GuiButton)this.controlList.get(0));
105        }
106    }
107
108    /**
109     * Called when the mouse is clicked.
110     */
111    protected void mouseClicked(int par1, int par2, int par3)
112    {
113        super.mouseClicked(par1, par2, par3);
114        this.commandTextField.mouseClicked(par1, par2, par3);
115    }
116
117    /**
118     * Draws the screen and all the components in it.
119     */
120    public void drawScreen(int par1, int par2, float par3)
121    {
122        StringTranslate var4 = StringTranslate.getInstance();
123        this.drawDefaultBackground();
124        this.drawCenteredString(this.fontRenderer, var4.translateKey("advMode.setCommand"), this.width / 2, this.height / 4 - 60 + 20, 16777215);
125        this.drawString(this.fontRenderer, var4.translateKey("advMode.command"), this.width / 2 - 150, 47, 10526880);
126        this.drawString(this.fontRenderer, var4.translateKey("advMode.nearestPlayer"), this.width / 2 - 150, 97, 10526880);
127        this.drawString(this.fontRenderer, var4.translateKey("advMode.randomPlayer"), this.width / 2 - 150, 108, 10526880);
128        this.drawString(this.fontRenderer, var4.translateKey("advMode.allPlayers"), this.width / 2 - 150, 119, 10526880);
129        this.commandTextField.drawTextBox();
130        super.drawScreen(par1, par2, par3);
131    }
132}