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