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