bspkrs icon

Example Mod for FML MLProps issue

bspkrs | PRO | 08/21/12 06:30:28 AM UTC | 0 ⭐ | 356 👁️ | Never ⏰ | []
Java |

7.96 KB

|

None

|

0 👍

/

0 👎

package net.minecraft.src;
 
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;
import java.util.logging.Level;
 
import org.lwjgl.opengl.GL11;
 
import net.minecraft.client.Minecraft;
 
public class mod_StatusEffectHUD extends BaseMod 
{
    protected float zLevel = 0.0F;
    private ScaledResolution scaledResolution;
    @MLProp(info="Valid alignment strings are topleft, topcenter, topright, middleleft, middlecenter, middleright, bottomleft, bottomcenter (not recommended), bottomright")
    public static String alignMode = "topleft";
    @MLProp(info="Set to true to see the effect background box, false to disable")
    public static boolean enableBackground = true;
    @MLProp(info="Set to true to show effect names, false to disable\n\n**ONLY EDIT WHAT IS BELOW THIS**")
    public static boolean enableEffectName = true;
    private boolean checkUpdate;
    
    public mod_StatusEffectHUD() 
    {
        ModLoader.setInGameHook(this, true, false);
        checkUpdate = true;
    }
 
    @Override
    public String getVersion() 
    {
        return "v1.3 (1.3.2)";
    }
 
    @Override
    public void load() 
    {
        if(!isCurrentVersion(ModLoader.getMinecraftInstance()))
            ModLoader.getLogger().log(Level.INFO, "Your version of StatusEffectHUD is out of date! Visit http://www.minecraftforum.net/topic/1114612- for the latest version.");
    }
 
    public boolean onTickInGame(float f, Minecraft mc)
    {
        if(mc.inGameHasFocus && !mc.gameSettings.showDebugInfo)
        {
            scaledResolution = new ScaledResolution(mc.gameSettings, mc.displayWidth, mc.displayHeight);
            displayStatusEffects(mc);
        }
        
        if(checkUpdate)
        {
            if(!isCurrentVersion(mc) && mc.inGameHasFocus)
            {
                mc.thePlayer.addChatMessage("&cYour version of StatusEffectHUD is out of date!");
                mc.thePlayer.addChatMessage("&cVisit http://www.minecraftforum.net/topic/1114612- for the latest version.");
            }
            checkUpdate = false;
        }
        return true;
    }
    
    private boolean isCurrentVersion(Minecraft minecraft)
    {
        try
        {
            String[] version = loadTextFromURL(new URL("https://dl.dropbox.com/u/20748481/Minecraft/1.3.1/statusEffectHUD.version"));
            return version[0].equalsIgnoreCase(getVersion());
        }
        catch(Exception e)
        {
            ModLoader.getLogger().log(Level.WARNING, "Error getting current version info: " + e.getStackTrace());
            return true;
        }
    }
 
    private String[] loadTextFromURL(URL url)
    {
        ArrayList arraylist = new ArrayList();
        Scanner scanner = null;
        try
        {
            scanner = new Scanner(url.openStream(), "UTF-8");
        }
        catch(FileNotFoundException filenotfoundexception) { } catch (IOException e) {
            ModLoader.getLogger().log(Level.WARNING, "Error getting current version info: " + e.getStackTrace());
        }
        while(scanner.hasNextLine())
        {
            arraylist.add(scanner.nextLine());
        }
        scanner.close();
        return (String[])arraylist.toArray(new String[arraylist.size()]);
    }
 
    private int getX(int width)
    {
        if(alignMode.equalsIgnoreCase("topcenter") || alignMode.equalsIgnoreCase("middlecenter") || alignMode.equalsIgnoreCase("bottomcenter"))
            return scaledResolution.getScaledWidth() / 2 - width / 2;
        else if(alignMode.equalsIgnoreCase("topright") || alignMode.equalsIgnoreCase("middleright")|| alignMode.equalsIgnoreCase("bottomright"))
            return scaledResolution.getScaledWidth() - width - 2;
        else
            return 2;
    }
 
    private int getY(int rowCount, int yOffset)
    {
        if(alignMode.equalsIgnoreCase("middleleft") || alignMode.equalsIgnoreCase("middlecenter") || alignMode.equalsIgnoreCase("middleright"))
            return (scaledResolution.getScaledHeight()/2) - ((rowCount * yOffset)/2);
        else if(alignMode.equalsIgnoreCase("bottomleft") || alignMode.equalsIgnoreCase("bottomcenter") || alignMode.equalsIgnoreCase("bottomright"))
            return scaledResolution.getScaledHeight() - (rowCount * yOffset) - 2;
        else
            return 2;
    }
    
    private void displayStatusEffects(Minecraft mc)
    {                                       
        Collection activeEffects = mc.thePlayer.getActivePotionEffects();
        int guiTexture = mc.renderEngine.getTexture("/gui/inventory.png");
 
        if (!activeEffects.isEmpty())
        {
            int yOffset = enableBackground ? 33 : enableEffectName ? 20 : 18;
            if (activeEffects.size() > 5 && enableBackground)
                yOffset = 132 / (activeEffects.size() - 1);
 
            int yBase = getY(activeEffects.size(),yOffset);
 
            for (Iterator iteratorPotionEffect = activeEffects.iterator(); iteratorPotionEffect.hasNext(); yBase += yOffset)
            {
                PotionEffect potionEffect = (PotionEffect)iteratorPotionEffect.next();
                Potion potion = Potion.potionTypes[potionEffect.getPotionID()];
                GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
                mc.renderEngine.bindTexture(guiTexture);
                int xBase = getX(enableBackground ? 120 : 18 + 4 + mc.fontRenderer.getStringWidth("0:00"));
                String potionName = "";
                
                if(enableEffectName)
                {
                    potionName = StatCollector.translateToLocal(potion.getName());
    
                    if (potionEffect.getAmplifier() == 1)
                    {
                        potionName = potionName + " II";
                    }
                    else if (potionEffect.getAmplifier() == 2)
                    {
                        potionName = potionName + " III";
                    }
                    else if (potionEffect.getAmplifier() == 3)
                    {
                        potionName = potionName + " IV";
                    }
                    
                    xBase = getX(enableBackground ? 120 : 18 + 4 + mc.fontRenderer.getStringWidth(potionName));
                }
                if(enableBackground)
                    this.drawTexturedModalRect(xBase, yBase, 0, 166, 140, 32);
 
                if (potion.hasStatusIcon())
                {
                    int potionStatusIcon = potion.getStatusIconIndex();
                    drawTexturedModalRect(xBase + (enableBackground ? 6 : 0), yBase + (enableBackground ? 7 : 0), 0 + potionStatusIcon % 8 * 18, 166 + 32 + potionStatusIcon / 8 * 18, 18, 18);
                }
                mc.fontRenderer.drawStringWithShadow(potionName, xBase + (enableBackground ? 10 : 4) + 18, yBase + (enableBackground ? 6 : 0), 0xffffff);
                String effectDuration = Potion.getDurationString(potionEffect);
                mc.fontRenderer.drawStringWithShadow(effectDuration, xBase + (enableBackground ? 10 : 4) + 18, yBase + (enableBackground ? 6 : 0) + (enableEffectName ? 10: 5), 0xffffff);
            }
        }
    }
    
    public void drawTexturedModalRect(int x, int y, int u, int v, int width, int height)
    {
        float var7 = 0.00390625F;
        float var8 = 0.00390625F;
        Tessellator tessellator = Tessellator.instance;
        tessellator.startDrawingQuads();
        tessellator.addVertexWithUV((double)(x + 0), (double)(y + height), (double)this.zLevel, (double)((float)(u + 0) * var7), (double)((float)(v + height) * var8));
        tessellator.addVertexWithUV((double)(x + width), (double)(y + height), (double)this.zLevel, (double)((float)(u + width) * var7), (double)((float)(v + height) * var8));
        tessellator.addVertexWithUV((double)(x + width), (double)(y + 0), (double)this.zLevel, (double)((float)(u + width) * var7), (double)((float)(v + 0) * var8));
        tessellator.addVertexWithUV((double)(x + 0), (double)(y + 0), (double)this.zLevel, (double)((float)(u + 0) * var7), (double)((float)(v + 0) * var8));
        tessellator.draw();
    }
}

Comments