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