    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    private GuiButton doneBtn;
    022    private GuiButton cancelBtn;
    023
    024    public GuiCommandBlock(TileEntityCommandBlock par1)
    025    {
    026        this.commandBlock = par1;
    027    }
    028
    029    /**
    030     * Called from the main game loop to update the screen.
    031     */
    032    public void updateScreen()
    033    {
    034        this.commandTextField.updateCursorCounter();
    035    }
    036
    037    /**
    038     * Adds the buttons (and other controls) to the screen in question.
    039     */
    040    public void initGui()
    041    {
    042        StringTranslate stringtranslate = StringTranslate.getInstance();
    043        Keyboard.enableRepeatEvents(true);
    044        this.buttonList.clear();
    045        this.buttonList.add(this.doneBtn = new GuiButton(0, this.width / 2 - 100, this.height / 4 + 96 + 12, stringtranslate.translateKey("gui.done")));
    046        this.buttonList.add(this.cancelBtn = new GuiButton(1, this.width / 2 - 100, this.height / 4 + 120 + 12, stringtranslate.translateKey("gui.cancel")));
    047        this.commandTextField = new GuiTextField(this.fontRenderer, this.width / 2 - 150, 60, 300, 20);
    048        this.commandTextField.setMaxStringLength(32767);
    049        this.commandTextField.setFocused(true);
    050        this.commandTextField.setText(this.commandBlock.getCommand());
    051        this.doneBtn.enabled = this.commandTextField.getText().trim().length() > 0;
    052    }
    053
    054    /**
    055     * Called when the screen is unloaded. Used to disable keyboard repeat events
    056     */
    057    public void onGuiClosed()
    058    {
    059        Keyboard.enableRepeatEvents(false);
    060    }
    061
    062    /**
    063     * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
    064     */
    065    protected void actionPerformed(GuiButton par1GuiButton)
    066    {
    067        if (par1GuiButton.enabled)
    068        {
    069            if (par1GuiButton.id == 1)
    070            {
    071                this.mc.displayGuiScreen((GuiScreen)null);
    072            }
    073            else if (par1GuiButton.id == 0)
    074            {
    075                String s = "MC|AdvCdm";
    076                ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
    077                DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
    078
    079                try
    080                {
    081                    dataoutputstream.writeInt(this.commandBlock.xCoord);
    082                    dataoutputstream.writeInt(this.commandBlock.yCoord);
    083                    dataoutputstream.writeInt(this.commandBlock.zCoord);
    084                    Packet.writeString(this.commandTextField.getText(), dataoutputstream);
    085                    this.mc.getNetHandler().addToSendQueue(new Packet250CustomPayload(s, bytearrayoutputstream.toByteArray()));
    086                }
    087                catch (Exception exception)
    088                {
    089                    exception.printStackTrace();
    090                }
    091
    092                this.mc.displayGuiScreen((GuiScreen)null);
    093            }
    094        }
    095    }
    096
    097    /**
    098     * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
    099     */
    100    protected void keyTyped(char par1, int par2)
    101    {
    102        this.commandTextField.textboxKeyTyped(par1, par2);
    103        this.doneBtn.enabled = this.commandTextField.getText().trim().length() > 0;
    104
    105        if (par2 != 28 && par1 != 13)
    106        {
    107            if (par2 == 1)
    108            {
    109                this.actionPerformed(this.cancelBtn);
    110            }
    111        }
    112        else
    113        {
    114            this.actionPerformed(this.doneBtn);
    115        }
    116    }
    117
    118    /**
    119     * Called when the mouse is clicked.
    120     */
    121    protected void mouseClicked(int par1, int par2, int par3)
    122    {
    123        super.mouseClicked(par1, par2, par3);
    124        this.commandTextField.mouseClicked(par1, par2, par3);
    125    }
    126
    127    /**
    128     * Draws the screen and all the components in it.
    129     */
    130    public void drawScreen(int par1, int par2, float par3)
    131    {
    132        StringTranslate stringtranslate = StringTranslate.getInstance();
    133        this.drawDefaultBackground();
    134        this.drawCenteredString(this.fontRenderer, stringtranslate.translateKey("advMode.setCommand"), this.width / 2, this.height / 4 - 60 + 20, 16777215);
    135        this.drawString(this.fontRenderer, stringtranslate.translateKey("advMode.command"), this.width / 2 - 150, 47, 10526880);
    136        this.drawString(this.fontRenderer, stringtranslate.translateKey("advMode.nearestPlayer"), this.width / 2 - 150, 97, 10526880);
    137        this.drawString(this.fontRenderer, stringtranslate.translateKey("advMode.randomPlayer"), this.width / 2 - 150, 108, 10526880);
    138        this.drawString(this.fontRenderer, stringtranslate.translateKey("advMode.allPlayers"), this.width / 2 - 150, 119, 10526880);
    139        this.commandTextField.drawTextBox();
    140        super.drawScreen(par1, par2, par3);
    141    }
    142}