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.client.gui.inventory.GuiContainer;
008import net.minecraft.client.renderer.RenderHelper;
009import net.minecraft.entity.IMerchant;
010import net.minecraft.entity.player.InventoryPlayer;
011import net.minecraft.inventory.ContainerMerchant;
012import net.minecraft.item.ItemStack;
013import net.minecraft.network.packet.Packet250CustomPayload;
014import net.minecraft.util.StatCollector;
015import net.minecraft.village.MerchantRecipe;
016import net.minecraft.village.MerchantRecipeList;
017import net.minecraft.world.World;
018import org.lwjgl.opengl.GL11;
019import org.lwjgl.opengl.GL12;
020
021@SideOnly(Side.CLIENT)
022public 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        MerchantRecipeList var7 = this.theIMerchant.getRecipes(this.mc.thePlayer);
122
123        if (var7 != null && !var7.isEmpty())
124        {
125            int var8 = this.currentRecipeIndex;
126            MerchantRecipe var9 = (MerchantRecipe)var7.get(var8);
127
128            if (var9.func_82784_g())
129            {
130                GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.mc.renderEngine.getTexture("/gui/trading.png"));
131                GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
132                GL11.glDisable(GL11.GL_LIGHTING);
133                this.drawTexturedModalRect(this.guiLeft + 83, this.guiTop + 21, 212, 0, 28, 21);
134                this.drawTexturedModalRect(this.guiLeft + 83, this.guiTop + 51, 212, 0, 28, 21);
135            }
136        }
137    }
138
139    /**
140     * Draws the screen and all the components in it.
141     */
142    public void drawScreen(int par1, int par2, float par3)
143    {
144        super.drawScreen(par1, par2, par3);
145        MerchantRecipeList var4 = this.theIMerchant.getRecipes(this.mc.thePlayer);
146
147        if (var4 != null && !var4.isEmpty())
148        {
149            int var5 = (this.width - this.xSize) / 2;
150            int var6 = (this.height - this.ySize) / 2;
151            int var7 = this.currentRecipeIndex;
152            MerchantRecipe var8 = (MerchantRecipe)var4.get(var7);
153            GL11.glPushMatrix();
154            ItemStack var9 = var8.getItemToBuy();
155            ItemStack var10 = var8.getSecondItemToBuy();
156            ItemStack var11 = var8.getItemToSell();
157            RenderHelper.enableGUIStandardItemLighting();
158            GL11.glDisable(GL11.GL_LIGHTING);
159            GL11.glEnable(GL12.GL_RESCALE_NORMAL);
160            GL11.glEnable(GL11.GL_COLOR_MATERIAL);
161            GL11.glEnable(GL11.GL_LIGHTING);
162            itemRenderer.zLevel = 100.0F;
163            itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, var9, var5 + 36, var6 + 24);
164            itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, var9, var5 + 36, var6 + 24);
165
166            if (var10 != null)
167            {
168                itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, var10, var5 + 62, var6 + 24);
169                itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, var10, var5 + 62, var6 + 24);
170            }
171
172            itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, var11, var5 + 120, var6 + 24);
173            itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, var11, var5 + 120, var6 + 24);
174            itemRenderer.zLevel = 0.0F;
175            GL11.glDisable(GL11.GL_LIGHTING);
176
177            if (this.isPointInRegion(36, 24, 16, 16, par1, par2))
178            {
179                this.drawItemStackTooltip(var9, par1, par2);
180            }
181            else if (var10 != null && this.isPointInRegion(62, 24, 16, 16, par1, par2))
182            {
183                this.drawItemStackTooltip(var10, par1, par2);
184            }
185            else if (this.isPointInRegion(120, 24, 16, 16, par1, par2))
186            {
187                this.drawItemStackTooltip(var11, par1, par2);
188            }
189
190            GL11.glPopMatrix();
191            GL11.glEnable(GL11.GL_LIGHTING);
192            GL11.glEnable(GL11.GL_DEPTH_TEST);
193            RenderHelper.enableStandardItemLighting();
194        }
195    }
196
197    /**
198     * Gets the Instance of IMerchant interface.
199     */
200    public IMerchant getIMerchant()
201    {
202        return this.theIMerchant;
203    }
204}