    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(int par1, int par2)
    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                int var7 = this.currentRecipeIndex;
    125                MerchantRecipe var8 = (MerchantRecipe)var4.get(var7);
    126
    127                if (var8.func_82784_g())
    128                {
    129                    GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.mc.renderEngine.getTexture("/gui/trading.png"));
    130                    GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    131                    GL11.glDisable(GL11.GL_LIGHTING);
    132                    this.drawTexturedModalRect(this.guiLeft + 83, this.guiTop + 21, 212, 0, 28, 21);
    133                    this.drawTexturedModalRect(this.guiLeft + 83, this.guiTop + 51, 212, 0, 28, 21);
    134                }
    135
    136                GL11.glPushMatrix();
    137                ItemStack var9 = var8.getItemToBuy();
    138                ItemStack var10 = var8.getSecondItemToBuy();
    139                ItemStack var11 = var8.getItemToSell();
    140                RenderHelper.enableGUIStandardItemLighting();
    141                GL11.glDisable(GL11.GL_LIGHTING);
    142                GL11.glEnable(GL12.GL_RESCALE_NORMAL);
    143                GL11.glEnable(GL11.GL_COLOR_MATERIAL);
    144                GL11.glEnable(GL11.GL_LIGHTING);
    145                itemRenderer.zLevel = 100.0F;
    146                itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, var9, var5 + 36, var6 + 24);
    147                itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, var9, var5 + 36, var6 + 24);
    148
    149                if (var10 != null)
    150                {
    151                    itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, var10, var5 + 62, var6 + 24);
    152                    itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, var10, var5 + 62, var6 + 24);
    153                }
    154
    155                itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.renderEngine, var11, var5 + 120, var6 + 24);
    156                itemRenderer.renderItemOverlayIntoGUI(this.fontRenderer, this.mc.renderEngine, var11, var5 + 120, var6 + 24);
    157                itemRenderer.zLevel = 0.0F;
    158                GL11.glDisable(GL11.GL_LIGHTING);
    159
    160                if (this.func_74188_c(36, 24, 16, 16, par1, par2))
    161                {
    162                    this.func_74184_a(var9, par1, par2);
    163                }
    164                else if (var10 != null && this.func_74188_c(62, 24, 16, 16, par1, par2))
    165                {
    166                    this.func_74184_a(var10, par1, par2);
    167                }
    168                else if (this.func_74188_c(120, 24, 16, 16, par1, par2))
    169                {
    170                    this.func_74184_a(var11, par1, par2);
    171                }
    172
    173                GL11.glPopMatrix();
    174                GL11.glEnable(GL11.GL_LIGHTING);
    175                GL11.glEnable(GL11.GL_DEPTH_TEST);
    176                RenderHelper.enableStandardItemLighting();
    177            }
    178        }
    179
    180        /**
    181         * Gets the Instance of IMerchant interface.
    182         */
    183        public IMerchant getIMerchant()
    184        {
    185            return this.theIMerchant;
    186        }
    187    }