lyonlancer5.pfsteel.common.event.RenderXPEventHandler.java Source code

Java tutorial

Introduction

Here is the source code for lyonlancer5.pfsteel.common.event.RenderXPEventHandler.java

Source

/* Copyright (c) 2015 Lance Selga <lyonecro55@gmail.com>
 * 
 * This work is free. You can redistribute it and/or modify it under the
 * terms of the Do What The Fuck You Want To Public License, Version 2,
 * as published by Sam Hocevar. See the COPYING file for more details.
 */
package lyonlancer5.pfsteel.common.event;

import java.awt.Color;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.RenderGameOverlayEvent;

import org.lwjgl.opengl.GL11;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

public class RenderXPEventHandler {

    private Minecraft mc = Minecraft.getMinecraft();

    @SubscribeEvent
    public void renderXP(RenderGameOverlayEvent.Pre event) {
        if (event.type == RenderGameOverlayEvent.ElementType.EXPERIENCE) {
            ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);
            int k = res.getScaledWidth();
            int l = res.getScaledHeight();

            EntityPlayer player = this.mc.thePlayer;
            if (!player.capabilities.isCreativeMode) {
                float xpLeft = expCost(player.experienceLevel);
                float xp = player.experience;
                String xpStr = (int) (xpLeft * xp) + "/" + (int) xpLeft;

                GL11.glScalef(1.0F, 1.0F, 1.0F);
                FontRenderer font = this.mc.fontRenderer;
                int j2 = (k - font.getStringWidth(xpStr)) / 2;
                int k2 = l - 48;

                font.drawStringWithShadow(xpStr, j2, k2, new Color(255, 255, 255, 255).getRGB());
            }
            GL11.glScalef(1.0F, 1.0F, 1.0F);
            this.mc.renderEngine.bindTexture(new ResourceLocation("textures/gui/icons.png"));
        }
    }

    private int expCost(int currentLevel) {
        if (currentLevel >= 30)
            return 62 + (currentLevel - 30) * 7;
        if (currentLevel >= 15)
            return 17 + (currentLevel - 15) * 3;
        return 17;
    }

}