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