    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.util.Random;
    006    import org.lwjgl.opengl.GL11;
    007    import org.lwjgl.opengl.GL12;
    008    import org.lwjgl.util.glu.GLU;
    009
    010    @SideOnly(Side.CLIENT)
    011    public class GuiEnchantment extends GuiContainer
    012    {
    013        /** The book model used on the GUI. */
    014        private static ModelBook bookModel = new ModelBook();
    015        private Random rand = new Random();
    016
    017        /** ContainerEnchantment object associated with this gui */
    018        private ContainerEnchantment containerEnchantment;
    019        public int field_74214_o;
    020        public float field_74213_p;
    021        public float field_74212_q;
    022        public float field_74211_r;
    023        public float field_74210_s;
    024        public float field_74209_t;
    025        public float field_74208_u;
    026        ItemStack field_74207_v;
    027
    028        public GuiEnchantment(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5)
    029        {
    030            super(new ContainerEnchantment(par1InventoryPlayer, par2World, par3, par4, par5));
    031            this.containerEnchantment = (ContainerEnchantment)this.inventorySlots;
    032        }
    033
    034        /**
    035         * Draw the foreground layer for the GuiContainer (everything in front of the items)
    036         */
    037        protected void drawGuiContainerForegroundLayer()
    038        {
    039            this.fontRenderer.drawString(StatCollector.translateToLocal("container.enchant"), 12, 6, 4210752);
    040            this.fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
    041        }
    042
    043        /**
    044         * Called from the main game loop to update the screen.
    045         */
    046        public void updateScreen()
    047        {
    048            super.updateScreen();
    049            this.func_74205_h();
    050        }
    051
    052        /**
    053         * Called when the mouse is clicked.
    054         */
    055        protected void mouseClicked(int par1, int par2, int par3)
    056        {
    057            super.mouseClicked(par1, par2, par3);
    058            int var4 = (this.width - this.xSize) / 2;
    059            int var5 = (this.height - this.ySize) / 2;
    060
    061            for (int var6 = 0; var6 < 3; ++var6)
    062            {
    063                int var7 = par1 - (var4 + 60);
    064                int var8 = par2 - (var5 + 14 + 19 * var6);
    065
    066                if (var7 >= 0 && var8 >= 0 && var7 < 108 && var8 < 19 && this.containerEnchantment.enchantItem(this.mc.thePlayer, var6))
    067                {
    068                    this.mc.playerController.sendEnchantPacket(this.containerEnchantment.windowId, var6);
    069                }
    070            }
    071        }
    072
    073        /**
    074         * Draw the background layer for the GuiContainer (everything behind the items)
    075         */
    076        protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
    077        {
    078            int var4 = this.mc.renderEngine.getTexture("/gui/enchant.png");
    079            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    080            this.mc.renderEngine.bindTexture(var4);
    081            int var5 = (this.width - this.xSize) / 2;
    082            int var6 = (this.height - this.ySize) / 2;
    083            this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, this.ySize);
    084            GL11.glPushMatrix();
    085            GL11.glMatrixMode(GL11.GL_PROJECTION);
    086            GL11.glPushMatrix();
    087            GL11.glLoadIdentity();
    088            ScaledResolution var7 = new ScaledResolution(this.mc.gameSettings, this.mc.displayWidth, this.mc.displayHeight);
    089            GL11.glViewport((var7.getScaledWidth() - 320) / 2 * var7.getScaleFactor(), (var7.getScaledHeight() - 240) / 2 * var7.getScaleFactor(), 320 * var7.getScaleFactor(), 240 * var7.getScaleFactor());
    090            GL11.glTranslatef(-0.34F, 0.23F, 0.0F);
    091            GLU.gluPerspective(90.0F, 1.3333334F, 9.0F, 80.0F);
    092            float var8 = 1.0F;
    093            GL11.glMatrixMode(GL11.GL_MODELVIEW);
    094            GL11.glLoadIdentity();
    095            RenderHelper.enableStandardItemLighting();
    096            GL11.glTranslatef(0.0F, 3.3F, -16.0F);
    097            GL11.glScalef(var8, var8, var8);
    098            float var9 = 5.0F;
    099            GL11.glScalef(var9, var9, var9);
    100            GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F);
    101            this.mc.renderEngine.bindTexture(this.mc.renderEngine.getTexture("/item/book.png"));
    102            GL11.glRotatef(20.0F, 1.0F, 0.0F, 0.0F);
    103            float var10 = this.field_74208_u + (this.field_74209_t - this.field_74208_u) * par1;
    104            GL11.glTranslatef((1.0F - var10) * 0.2F, (1.0F - var10) * 0.1F, (1.0F - var10) * 0.25F);
    105            GL11.glRotatef(-(1.0F - var10) * 90.0F - 90.0F, 0.0F, 1.0F, 0.0F);
    106            GL11.glRotatef(180.0F, 1.0F, 0.0F, 0.0F);
    107            float var11 = this.field_74212_q + (this.field_74213_p - this.field_74212_q) * par1 + 0.25F;
    108            float var12 = this.field_74212_q + (this.field_74213_p - this.field_74212_q) * par1 + 0.75F;
    109            var11 = (var11 - (float)MathHelper.truncateDoubleToInt((double)var11)) * 1.6F - 0.3F;
    110            var12 = (var12 - (float)MathHelper.truncateDoubleToInt((double)var12)) * 1.6F - 0.3F;
    111
    112            if (var11 < 0.0F)
    113            {
    114                var11 = 0.0F;
    115            }
    116
    117            if (var12 < 0.0F)
    118            {
    119                var12 = 0.0F;
    120            }
    121
    122            if (var11 > 1.0F)
    123            {
    124                var11 = 1.0F;
    125            }
    126
    127            if (var12 > 1.0F)
    128            {
    129                var12 = 1.0F;
    130            }
    131
    132            GL11.glEnable(GL12.GL_RESCALE_NORMAL);
    133            bookModel.render((Entity)null, 0.0F, var11, var12, var10, 0.0F, 0.0625F);
    134            GL11.glDisable(GL12.GL_RESCALE_NORMAL);
    135            RenderHelper.disableStandardItemLighting();
    136            GL11.glMatrixMode(GL11.GL_PROJECTION);
    137            GL11.glViewport(0, 0, this.mc.displayWidth, this.mc.displayHeight);
    138            GL11.glPopMatrix();
    139            GL11.glMatrixMode(GL11.GL_MODELVIEW);
    140            GL11.glPopMatrix();
    141            RenderHelper.disableStandardItemLighting();
    142            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    143            this.mc.renderEngine.bindTexture(var4);
    144            EnchantmentNameParts.instance.setRandSeed(this.containerEnchantment.nameSeed);
    145
    146            for (int var13 = 0; var13 < 3; ++var13)
    147            {
    148                String var14 = EnchantmentNameParts.instance.generateRandomEnchantName();
    149                this.zLevel = 0.0F;
    150                this.mc.renderEngine.bindTexture(var4);
    151                int var15 = this.containerEnchantment.enchantLevels[var13];
    152                GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    153
    154                if (var15 == 0)
    155                {
    156                    this.drawTexturedModalRect(var5 + 60, var6 + 14 + 19 * var13, 0, 185, 108, 19);
    157                }
    158                else
    159                {
    160                    String var16 = "" + var15;
    161                    FontRenderer var17 = this.mc.standardGalacticFontRenderer;
    162                    int var18 = 6839882;
    163
    164                    if (this.mc.thePlayer.experienceLevel < var15 && !this.mc.thePlayer.capabilities.isCreativeMode)
    165                    {
    166                        this.drawTexturedModalRect(var5 + 60, var6 + 14 + 19 * var13, 0, 185, 108, 19);
    167                        var17.drawSplitString(var14, var5 + 62, var6 + 16 + 19 * var13, 104, (var18 & 16711422) >> 1);
    168                        var17 = this.mc.fontRenderer;
    169                        var18 = 4226832;
    170                        var17.drawStringWithShadow(var16, var5 + 62 + 104 - var17.getStringWidth(var16), var6 + 16 + 19 * var13 + 7, var18);
    171                    }
    172                    else
    173                    {
    174                        int var19 = par2 - (var5 + 60);
    175                        int var20 = par3 - (var6 + 14 + 19 * var13);
    176
    177                        if (var19 >= 0 && var20 >= 0 && var19 < 108 && var20 < 19)
    178                        {
    179                            this.drawTexturedModalRect(var5 + 60, var6 + 14 + 19 * var13, 0, 204, 108, 19);
    180                            var18 = 16777088;
    181                        }
    182                        else
    183                        {
    184                            this.drawTexturedModalRect(var5 + 60, var6 + 14 + 19 * var13, 0, 166, 108, 19);
    185                        }
    186
    187                        var17.drawSplitString(var14, var5 + 62, var6 + 16 + 19 * var13, 104, var18);
    188                        var17 = this.mc.fontRenderer;
    189                        var18 = 8453920;
    190                        var17.drawStringWithShadow(var16, var5 + 62 + 104 - var17.getStringWidth(var16), var6 + 16 + 19 * var13 + 7, var18);
    191                    }
    192                }
    193            }
    194        }
    195
    196        public void func_74205_h()
    197        {
    198            ItemStack var1 = this.inventorySlots.getSlot(0).getStack();
    199
    200            if (!ItemStack.areItemStacksEqual(var1, this.field_74207_v))
    201            {
    202                this.field_74207_v = var1;
    203
    204                do
    205                {
    206                    this.field_74211_r += (float)(this.rand.nextInt(4) - this.rand.nextInt(4));
    207                }
    208                while (this.field_74213_p <= this.field_74211_r + 1.0F && this.field_74213_p >= this.field_74211_r - 1.0F);
    209            }
    210
    211            ++this.field_74214_o;
    212            this.field_74212_q = this.field_74213_p;
    213            this.field_74208_u = this.field_74209_t;
    214            boolean var2 = false;
    215
    216            for (int var3 = 0; var3 < 3; ++var3)
    217            {
    218                if (this.containerEnchantment.enchantLevels[var3] != 0)
    219                {
    220                    var2 = true;
    221                }
    222            }
    223
    224            if (var2)
    225            {
    226                this.field_74209_t += 0.2F;
    227            }
    228            else
    229            {
    230                this.field_74209_t -= 0.2F;
    231            }
    232
    233            if (this.field_74209_t < 0.0F)
    234            {
    235                this.field_74209_t = 0.0F;
    236            }
    237
    238            if (this.field_74209_t > 1.0F)
    239            {
    240                this.field_74209_t = 1.0F;
    241            }
    242
    243            float var5 = (this.field_74211_r - this.field_74213_p) * 0.4F;
    244            float var4 = 0.2F;
    245
    246            if (var5 < -var4)
    247            {
    248                var5 = -var4;
    249            }
    250
    251            if (var5 > var4)
    252            {
    253                var5 = var4;
    254            }
    255
    256            this.field_74210_s += (var5 - this.field_74210_s) * 0.9F;
    257            this.field_74213_p += this.field_74210_s;
    258        }
    259    }