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.opengl.GL11;
008    import org.lwjgl.opengl.GL12;
009    
010    @SideOnly(Side.CLIENT)
011    public class GuiMerchant extends GuiContainer
012    {
013        /** Instance of IMerchant interface. */
014        private IMerchant theIMerchant;
015        private GuiButtonMerchant nextRecipeButtonIndex;
016        private GuiButtonMerchant previousRecipeButtonIndex;
017        private int currentRecipeIndex = 0;
018    
019        public GuiMerchant(InventoryPlayer par1InventoryPlayer, IMerchant par2IMerchant, World par3World)
020        {
021            super(new ContainerMerchant(par1InventoryPlayer, par2IMerchant, par3World));
022            this.theIMerchant = par2IMerchant;
023        }
024    
025        /**
026         * Adds the buttons (and other controls) to the screen in question.
027         */
028        public void initGui()
029        {
030            super.initGui();
031            int var1 = (this.width - this.xSize) / 2;
032            int var2 = (this.height - this.ySize) / 2;
033            this.controlList.add(this.nextRecipeButtonIndex = new GuiButtonMerchant(1, var1 + 120 + 27, var2 + 24 - 1, true));
034            this.controlList.add(this.previousRecipeButtonIndex = new GuiButtonMerchant(2, var1 + 36 - 19, var2 + 24 - 1, false));
035            this.nextRecipeButtonIndex.enabled = false;
036            this.previousRecipeButtonIndex.enabled = false;
037        }
038    
039        /**
040         * Draw the foreground layer for the GuiContainer (everything in front of the items)
041         */
042        protected void drawGuiContainerForegroundLayer()
043        {
044            this.fontRenderer.drawString(StatCollector.translateToLocal("entity.Villager.name"), 56, 6, 4210752);
045            this.fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
046        }
047    
048        /**
049         * Called from the main game loop to update the screen.
050         */
051        public void updateScreen()
052        {
053            super.updateScreen();
054            MerchantRecipeList var1 = this.theIMerchant.getRecipes(this.mc.thePlayer);
055    
056            if (var1 != null)
057            {
058                this.nextRecipeButtonIndex.enabled = this.currentRecipeIndex < var1.size() - 1;
059                this.previousRecipeButtonIndex.enabled = this.currentRecipeIndex > 0;
060            }
061        }
062    
063        /**
064         * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
065         */
066        protected void actionPerformed(GuiButton par1GuiButton)
067        {
068            boolean var2 = false;
069    
070            if (par1GuiButton == this.nextRecipeButtonIndex)
071            {
072                ++this.currentRecipeIndex;
073                var2 = true;
074            }
075            else if (par1GuiButton == this.previousRecipeButtonIndex)
076            {
077                --this.currentRecipeIndex;
078                var2 = true;
079            }
080    
081            if (var2)
082            {
083                ((ContainerMerchant)this.inventorySlots).setCurrentRecipeIndex(this.currentRecipeIndex);
084                ByteArrayOutputStream var3 = new ByteArrayOutputStream();
085                DataOutputStream var4 = new DataOutputStream(var3);
086    
087                try
088                {
089                    var4.writeInt(this.currentRecipeIndex);
090                    this.mc.getSendQueue().addToSendQueue(new Packet250CustomPayload("MC|TrSel", var3.toByteArray()));
091                }
092                catch (Exception var6)
093                {
094                    var6.printStackTrace();
095                }
096            }
097        }
098    
099        /**
100         * Draw the background layer for the GuiContainer (everything behind the items)
101         */
102        protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
103        {
104            int var4 = this.mc.renderEngine.getTexture("/gui/trading.png");
105            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
106            this.mc.renderEngine.bindTexture(var4);
107            int var5 = (this.width - this.xSize) / 2;
108            int var6 = (this.height - this.ySize) / 2;
109            this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, this.ySize);
110        }
111    
112        /**
113         * Draws the screen and all the components in it.
114         */
115        public void drawScreen(int par1, int par2, float par3)
116        {
117            super.drawScreen(par1, par2, par3);
118            MerchantRecipeList var4 = this.theIMerchant.getRecipes(this.mc.thePlayer);
119    
120            if (var4 != null && !var4.isEmpty())
121            {
122                int var5 = (this.width - this.xSize) / 2;
123                int var6 = (this.height - this.ySize) / 2;
124                GL11.glPushMatrix();
125                int var7 = this.currentRecipeIndex;
126                MerchantRecipe var8 = (MerchantRecipe)var4.get(var7);
127                ItemStack var9 = var8.getItemToBuy();
128                ItemStack var10 = var8.getSecondItemToBuy();
129                ItemStack var11 = var8.getItemToSell();
130                RenderHelper.enableGUIStandardItemLighting();
131                GL11.glDisable(GL11.GL_LIGHTING);
132                GL11.glEnable(GL12.GL_RESCALE_NORMAL);
133                GL11.glEnable(GL11.GL_COLOR_MATERIAL);
134                GL11.glEnable(GL11.GL_LIGHTING);
135                itemRenderer.zLevel = 100.0F;
136                itemRenderer.renderItemIntoGUI(this.fontRenderer, this.mc.renderEngine, var9, var5 + 36, var6 + 24);
137                itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, var9, var5 + 36, var6 + 24);
138    
139                if (var10 != null)
140                {
141                    itemRenderer.renderItemIntoGUI(this.fontRenderer, this.mc.renderEngine, var10, var5 + 62, var6 + 24);
142                    itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, var10, var5 + 62, var6 + 24);
143                }
144    
145                itemRenderer.renderItemIntoGUI(this.fontRenderer, this.mc.renderEngine, var11, var5 + 120, var6 + 24);
146                itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, var11, var5 + 120, var6 + 24);
147                itemRenderer.zLevel = 0.0F;
148                GL11.glDisable(GL11.GL_LIGHTING);
149    
150                if (this.func_74188_c(36, 24, 16, 16, par1, par2))
151                {
152                    this.func_74184_a(var9, par1, par2);
153                }
154                else if (var10 != null && this.func_74188_c(62, 24, 16, 16, par1, par2))
155                {
156                    this.func_74184_a(var10, par1, par2);
157                }
158                else if (this.func_74188_c(120, 24, 16, 16, par1, par2))
159                {
160                    this.func_74184_a(var11, par1, par2);
161                }
162    
163                GL11.glPopMatrix();
164                GL11.glEnable(GL11.GL_LIGHTING);
165                GL11.glEnable(GL11.GL_DEPTH_TEST);
166                RenderHelper.enableStandardItemLighting();
167            }
168        }
169    
170        /**
171         * Gets the Instance of IMerchant interface.
172         */
173        public IMerchant getIMerchant()
174        {
175            return this.theIMerchant;
176        }
177    }