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    private String field_94082_v;
030
031    public GuiMerchant(InventoryPlayer par1, IMerchant par2, World par3World, String par4)
032    {
033        super(new ContainerMerchant(par1, par2, par3World));
034        this.theIMerchant = par2;
035        this.field_94082_v = par4 != null && par4.length() >= 1 ? par4 : StatCollector.translateToLocal("entity.Villager.name");
036    }
037
038    /**
039     * Adds the buttons (and other controls) to the screen in question.
040     */
041    public void initGui()
042    {
043        super.initGui();
044        int i = (this.width - this.xSize) / 2;
045        int j = (this.height - this.ySize) / 2;
046        this.buttonList.add(this.nextRecipeButtonIndex = new GuiButtonMerchant(1, i + 120 + 27, j + 24 - 1, true));
047        this.buttonList.add(this.previousRecipeButtonIndex = new GuiButtonMerchant(2, i + 36 - 19, j + 24 - 1, false));
048        this.nextRecipeButtonIndex.enabled = false;
049        this.previousRecipeButtonIndex.enabled = false;
050    }
051
052    /**
053     * Draw the foreground layer for the GuiContainer (everything in front of the items)
054     */
055    protected void drawGuiContainerForegroundLayer(int par1, int par2)
056    {
057        this.fontRenderer.drawString(this.field_94082_v, this.xSize / 2 - this.fontRenderer.getStringWidth(this.field_94082_v) / 2, 6, 4210752);
058        this.fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
059    }
060
061    /**
062     * Called from the main game loop to update the screen.
063     */
064    public void updateScreen()
065    {
066        super.updateScreen();
067        MerchantRecipeList merchantrecipelist = this.theIMerchant.getRecipes(this.mc.thePlayer);
068
069        if (merchantrecipelist != null)
070        {
071            this.nextRecipeButtonIndex.enabled = this.currentRecipeIndex < merchantrecipelist.size() - 1;
072            this.previousRecipeButtonIndex.enabled = this.currentRecipeIndex > 0;
073        }
074    }
075
076    /**
077     * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
078     */
079    protected void actionPerformed(GuiButton par1GuiButton)
080    {
081        boolean flag = false;
082
083        if (par1GuiButton == this.nextRecipeButtonIndex)
084        {
085            ++this.currentRecipeIndex;
086            flag = true;
087        }
088        else if (par1GuiButton == this.previousRecipeButtonIndex)
089        {
090            --this.currentRecipeIndex;
091            flag = true;
092        }
093
094        if (flag)
095        {
096            ((ContainerMerchant)this.inventorySlots).setCurrentRecipeIndex(this.currentRecipeIndex);
097            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
098            DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
099
100            try
101            {
102                dataoutputstream.writeInt(this.currentRecipeIndex);
103                this.mc.getNetHandler().addToSendQueue(new Packet250CustomPayload("MC|TrSel", bytearrayoutputstream.toByteArray()));
104            }
105            catch (Exception exception)
106            {
107                exception.printStackTrace();
108            }
109        }
110    }
111
112    /**
113     * Draw the background layer for the GuiContainer (everything behind the items)
114     */
115    protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
116    {
117        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
118        this.mc.renderEngine.bindTexture("/gui/trading.png");
119        int k = (this.width - this.xSize) / 2;
120        int l = (this.height - this.ySize) / 2;
121        this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
122        MerchantRecipeList merchantrecipelist = this.theIMerchant.getRecipes(this.mc.thePlayer);
123
124        if (merchantrecipelist != null && !merchantrecipelist.isEmpty())
125        {
126            int i1 = this.currentRecipeIndex;
127            MerchantRecipe merchantrecipe = (MerchantRecipe)merchantrecipelist.get(i1);
128
129            if (merchantrecipe.func_82784_g())
130            {
131                this.mc.renderEngine.bindTexture("/gui/trading.png");
132                GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
133                GL11.glDisable(GL11.GL_LIGHTING);
134                this.drawTexturedModalRect(this.guiLeft + 83, this.guiTop + 21, 212, 0, 28, 21);
135                this.drawTexturedModalRect(this.guiLeft + 83, this.guiTop + 51, 212, 0, 28, 21);
136            }
137        }
138    }
139
140    /**
141     * Draws the screen and all the components in it.
142     */
143    public void drawScreen(int par1, int par2, float par3)
144    {
145        super.drawScreen(par1, par2, par3);
146        MerchantRecipeList merchantrecipelist = this.theIMerchant.getRecipes(this.mc.thePlayer);
147
148        if (merchantrecipelist != null && !merchantrecipelist.isEmpty())
149        {
150            int k = (this.width - this.xSize) / 2;
151            int l = (this.height - this.ySize) / 2;
152            int i1 = this.currentRecipeIndex;
153            MerchantRecipe merchantrecipe = (MerchantRecipe)merchantrecipelist.get(i1);
154            GL11.glPushMatrix();
155            ItemStack itemstack = merchantrecipe.getItemToBuy();
156            ItemStack itemstack1 = merchantrecipe.getSecondItemToBuy();
157            ItemStack itemstack2 = merchantrecipe.getItemToSell();
158            RenderHelper.enableGUIStandardItemLighting();
159            GL11.glDisable(GL11.GL_LIGHTING);
160            GL11.glEnable(GL12.GL_RESCALE_NORMAL);
161            GL11.glEnable(GL11.GL_COLOR_MATERIAL);
162            GL11.glEnable(GL11.GL_LIGHTING);
163            itemRenderer.zLevel = 100.0F;
164            itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, itemstack, k + 36, l + 24);
165            itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, itemstack, k + 36, l + 24);
166
167            if (itemstack1 != null)
168            {
169                itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, itemstack1, k + 62, l + 24);
170                itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, itemstack1, k + 62, l + 24);
171            }
172
173            itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, itemstack2, k + 120, l + 24);
174            itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, itemstack2, k + 120, l + 24);
175            itemRenderer.zLevel = 0.0F;
176            GL11.glDisable(GL11.GL_LIGHTING);
177
178            if (this.isPointInRegion(36, 24, 16, 16, par1, par2))
179            {
180                this.drawItemStackTooltip(itemstack, par1, par2);
181            }
182            else if (itemstack1 != null && this.isPointInRegion(62, 24, 16, 16, par1, par2))
183            {
184                this.drawItemStackTooltip(itemstack1, par1, par2);
185            }
186            else if (this.isPointInRegion(120, 24, 16, 16, par1, par2))
187            {
188                this.drawItemStackTooltip(itemstack2, par1, par2);
189            }
190
191            GL11.glPopMatrix();
192            GL11.glEnable(GL11.GL_LIGHTING);
193            GL11.glEnable(GL11.GL_DEPTH_TEST);
194            RenderHelper.enableStandardItemLighting();
195        }
196    }
197
198    /**
199     * Gets the Instance of IMerchant interface.
200     */
201    public IMerchant getIMerchant()
202    {
203        return this.theIMerchant;
204    }
205}