001    package net.minecraft.client.gui.inventory;
002    
003    import cpw.mods.fml.relauncher.Side;
004    import cpw.mods.fml.relauncher.SideOnly;
005    import java.io.ByteArrayOutputStream;
006    import java.io.DataOutputStream;
007    import java.util.Iterator;
008    import net.minecraft.client.gui.GuiButton;
009    import net.minecraft.client.gui.GuiScreen;
010    import net.minecraft.client.renderer.RenderHelper;
011    import net.minecraft.entity.player.InventoryPlayer;
012    import net.minecraft.inventory.ContainerBeacon;
013    import net.minecraft.item.Item;
014    import net.minecraft.item.ItemStack;
015    import net.minecraft.network.packet.Packet250CustomPayload;
016    import net.minecraft.tileentity.TileEntityBeacon;
017    import net.minecraft.util.StatCollector;
018    import org.lwjgl.opengl.GL11;
019    
020    @SideOnly(Side.CLIENT)
021    public class GuiBeacon extends GuiContainer
022    {
023        private TileEntityBeacon beacon;
024        private GuiBeaconButtonConfirm beaconConfirmButton;
025        private boolean buttonsNotDrawn;
026    
027        public GuiBeacon(InventoryPlayer par1, TileEntityBeacon par2)
028        {
029            super(new ContainerBeacon(par1, par2));
030            this.beacon = par2;
031            this.xSize = 230;
032            this.ySize = 219;
033        }
034    
035        /**
036         * Adds the buttons (and other controls) to the screen in question.
037         */
038        public void initGui()
039        {
040            super.initGui();
041            this.controlList.add(this.beaconConfirmButton = new GuiBeaconButtonConfirm(this, -1, this.guiLeft + 164, this.guiTop + 107));
042            this.controlList.add(new GuiBeaconButtonCancel(this, -2, this.guiLeft + 190, this.guiTop + 107));
043            this.buttonsNotDrawn = true;
044            this.beaconConfirmButton.enabled = false;
045        }
046    
047        /**
048         * Called from the main game loop to update the screen.
049         */
050        public void updateScreen()
051        {
052            super.updateScreen();
053    
054            if (this.buttonsNotDrawn && this.beacon.getLevels() >= 0)
055            {
056                this.buttonsNotDrawn = false;
057                int var2;
058                int var3;
059                int var4;
060                int var5;
061                GuiBeaconButtonPower var6;
062    
063                for (int var1 = 0; var1 <= 2; ++var1)
064                {
065                    var2 = TileEntityBeacon.effectsList[var1].length;
066                    var3 = var2 * 22 + (var2 - 1) * 2;
067    
068                    for (var4 = 0; var4 < var2; ++var4)
069                    {
070                        var5 = TileEntityBeacon.effectsList[var1][var4].id;
071                        var6 = new GuiBeaconButtonPower(this, var1 << 8 | var5, this.guiLeft + 76 + var4 * 24 - var3 / 2, this.guiTop + 22 + var1 * 25, var5, var1);
072                        this.controlList.add(var6);
073    
074                        if (var1 >= this.beacon.getLevels())
075                        {
076                            var6.enabled = false;
077                        }
078                        else if (var5 == this.beacon.getPrimaryEffect())
079                        {
080                            var6.func_82254_b(true);
081                        }
082                    }
083                }
084    
085                byte var7 = 3;
086                var2 = TileEntityBeacon.effectsList[var7].length + 1;
087                var3 = var2 * 22 + (var2 - 1) * 2;
088    
089                for (var4 = 0; var4 < var2 - 1; ++var4)
090                {
091                    var5 = TileEntityBeacon.effectsList[var7][var4].id;
092                    var6 = new GuiBeaconButtonPower(this, var7 << 8 | var5, this.guiLeft + 167 + var4 * 24 - var3 / 2, this.guiTop + 47, var5, var7);
093                    this.controlList.add(var6);
094    
095                    if (var7 >= this.beacon.getLevels())
096                    {
097                        var6.enabled = false;
098                    }
099                    else if (var5 == this.beacon.getSecondaryEffect())
100                    {
101                        var6.func_82254_b(true);
102                    }
103                }
104    
105                if (this.beacon.getPrimaryEffect() > 0)
106                {
107                    GuiBeaconButtonPower var8 = new GuiBeaconButtonPower(this, var7 << 8 | this.beacon.getPrimaryEffect(), this.guiLeft + 167 + (var2 - 1) * 24 - var3 / 2, this.guiTop + 47, this.beacon.getPrimaryEffect(), var7);
108                    this.controlList.add(var8);
109    
110                    if (var7 >= this.beacon.getLevels())
111                    {
112                        var8.enabled = false;
113                    }
114                    else if (this.beacon.getPrimaryEffect() == this.beacon.getSecondaryEffect())
115                    {
116                        var8.func_82254_b(true);
117                    }
118                }
119            }
120    
121            this.beaconConfirmButton.enabled = this.beacon.getStackInSlot(0) != null && this.beacon.getPrimaryEffect() > 0;
122        }
123    
124        /**
125         * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
126         */
127        protected void actionPerformed(GuiButton par1GuiButton)
128        {
129            if (par1GuiButton.id == -2)
130            {
131                this.mc.displayGuiScreen((GuiScreen)null);
132            }
133            else if (par1GuiButton.id == -1)
134            {
135                String var2 = "MC|Beacon";
136                ByteArrayOutputStream var3 = new ByteArrayOutputStream();
137                DataOutputStream var4 = new DataOutputStream(var3);
138    
139                try
140                {
141                    var4.writeInt(this.beacon.getPrimaryEffect());
142                    var4.writeInt(this.beacon.getSecondaryEffect());
143                    this.mc.getSendQueue().addToSendQueue(new Packet250CustomPayload(var2, var3.toByteArray()));
144                }
145                catch (Exception var6)
146                {
147                    var6.printStackTrace();
148                }
149    
150                this.mc.displayGuiScreen((GuiScreen)null);
151            }
152            else if (par1GuiButton instanceof GuiBeaconButtonPower)
153            {
154                if (((GuiBeaconButtonPower)par1GuiButton).func_82255_b())
155                {
156                    return;
157                }
158    
159                int var7 = par1GuiButton.id;
160                int var8 = var7 & 255;
161                int var9 = var7 >> 8;
162    
163                if (var9 < 3)
164                {
165                    this.beacon.func_82128_d(var8);
166                }
167                else
168                {
169                    this.beacon.func_82127_e(var8);
170                }
171    
172                this.controlList.clear();
173                this.initGui();
174                this.updateScreen();
175            }
176        }
177    
178        /**
179         * Draw the foreground layer for the GuiContainer (everything in front of the items)
180         */
181        protected void drawGuiContainerForegroundLayer(int par1, int par2)
182        {
183            RenderHelper.disableStandardItemLighting();
184            this.drawCenteredString(this.fontRenderer, StatCollector.translateToLocal("tile.beacon.primary"), 62, 10, 14737632);
185            this.drawCenteredString(this.fontRenderer, StatCollector.translateToLocal("tile.beacon.secondary"), 169, 10, 14737632);
186            Iterator var3 = this.controlList.iterator();
187    
188            while (var3.hasNext())
189            {
190                GuiButton var4 = (GuiButton)var3.next();
191    
192                if (var4.func_82252_a())
193                {
194                    var4.func_82251_b(par1 - this.guiLeft, par2 - this.guiTop);
195                    break;
196                }
197            }
198    
199            RenderHelper.enableGUIStandardItemLighting();
200        }
201    
202        /**
203         * Draw the background layer for the GuiContainer (everything behind the items)
204         */
205        protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
206        {
207            int var4 = this.mc.renderEngine.getTexture("/gui/beacon.png");
208            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
209            this.mc.renderEngine.bindTexture(var4);
210            int var5 = (this.width - this.xSize) / 2;
211            int var6 = (this.height - this.ySize) / 2;
212            this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, this.ySize);
213            itemRenderer.zLevel = 100.0F;
214            itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, new ItemStack(Item.emerald), var5 + 42, var6 + 109);
215            itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, new ItemStack(Item.diamond), var5 + 42 + 22, var6 + 109);
216            itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, new ItemStack(Item.ingotGold), var5 + 42 + 44, var6 + 109);
217            itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, new ItemStack(Item.ingotIron), var5 + 42 + 66, var6 + 109);
218            itemRenderer.zLevel = 0.0F;
219        }
220    }