aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/overlays/FuelBar.java
blob: 25148ba3d1e3c1472d935efe4bd4a4ef775a22ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package io.github.moulberry.notenoughupdates.overlays;

import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.core.config.Position;
import io.github.moulberry.notenoughupdates.options.NEUConfig;
import io.github.moulberry.notenoughupdates.util.SBInfo;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL14;

import java.awt.*;
import java.util.Random;

public class FuelBar {

    public static final ResourceLocation FUEL_BAR = new ResourceLocation("notenoughupdates:fuel_bar.png");

    private float fuelAmount = -1;
    private String fuelString = "";

    @SubscribeEvent
    public void onTick(TickEvent.ClientTickEvent event) {
        fuelAmount = -1;

        if(SBInfo.getInstance().getLocation() == null) return;
        if(!(SBInfo.getInstance().getLocation().startsWith("mining_")||SBInfo.getInstance().getLocation().equals("crystal_hollows"))) return;

        if(Minecraft.getMinecraft().thePlayer == null) return;
        if(!NotEnoughUpdates.INSTANCE.config.mining.drillFuelBar) return;

        ItemStack held = Minecraft.getMinecraft().thePlayer.getHeldItem();
        if(held != null) {
            String internalname = NotEnoughUpdates.INSTANCE.manager.getInternalNameForItem(held);
            if(internalname != null && (internalname.contains("_DRILL_") || internalname.equals("DIVAN_DRILL"))) {
                String[] lore = NotEnoughUpdates.INSTANCE.manager.getLoreFromNBT(held.getTagCompound());
                for(String line : lore) {
                    try {
                        if(line.startsWith("\u00A77Fuel: ")) {
                            String[] split = Utils.cleanColour(line).split("/");
                            if(split.length == 2) {
                                String fuelS = split[0].split(" ")[1];
                                int fuel = Integer.parseInt(fuelS.replace(",","").trim());

                                String maxFuelS = split[1].trim();
                                int mult = 1;
                                if(maxFuelS.endsWith("k")) {
                                    mult = 1000;
                                }
                                int maxFuel = Integer.parseInt(maxFuelS.replace("k", "").trim())*mult;
                                fuelAmount = fuel/(float)maxFuel;
                                if(fuelAmount > 1) {
                                    fuelAmount = 1;
                                }
                                fuelString = line;

                                break;
                            }
                        }
                    } catch(Exception ignored) {}
                }
            }
        }
    }

    @SubscribeEvent
    public void onRenderScreen(RenderGameOverlayEvent.Post event) {
        if(fuelAmount < 0) return;
        if(!NotEnoughUpdates.INSTANCE.config.mining.drillFuelBar) return;
        if(event.type == RenderGameOverlayEvent.ElementType.ALL) {
            ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft());

            Position position = NotEnoughUpdates.INSTANCE.config.mining.drillFuelBarPosition;
            int x = position.getAbsX(scaledResolution, NotEnoughUpdates.INSTANCE.config.mining.drillFuelBarWidth);
            int y = position.getAbsY(scaledResolution, 12);
            x -= NotEnoughUpdates.INSTANCE.config.mining.drillFuelBarWidth/2;
            renderBar(x, y+4, NotEnoughUpdates.INSTANCE.config.mining.drillFuelBarWidth, fuelAmount);

            String str = fuelString.replace("\u00A77", EnumChatFormatting.DARK_GREEN.toString()) +
                    EnumChatFormatting.GOLD + String.format(" (%d%%)", (int)(fuelAmount*100));

            GlStateManager.enableBlend();
            GL14.glBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
            GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);

            String clean = Utils.cleanColourNotModifiers(str);
            for(int xO=-2; xO<=2; xO++) {
                for(int yO=-2; yO<=2; yO++) {
                    if(Math.abs(xO) != Math.abs(yO)) {
                        Minecraft.getMinecraft().fontRendererObj.drawString(clean,
                                x+2+xO/2f, y+yO/2f,
                                new Color(0, 0, 0, 200/Math.max(Math.abs(xO), Math.abs(yO))).getRGB(), false);
                    }
                }
            }
            Minecraft.getMinecraft().fontRendererObj.drawString(str,
                    x+2, y, 0xffffff, false);
        }
    }

    private void renderBarCap(float x, float y, boolean left, float rCapSize, float completion) {
        float size = left ? 10 : rCapSize;
        int startTexX = left ? 0 : (170 + 11-(int)Math.ceil(rCapSize));

        if(completion < 1) {
            Utils.drawTexturedRect(x, y, size, 5,
                    startTexX/181f, 1, 0/10f, 5/10f, GL11.GL_NEAREST);
        }
        if(completion > 0) {
            Utils.drawTexturedRect(x, y, size*completion, 5,
                    startTexX/181f, (startTexX+size*completion)/181f, 5/10f, 10/10f, GL11.GL_NEAREST);
        }
    }

    private void renderBarNotch(float x, float y, int id, float completion) {
        id = id % 16;

        int startTexX = 10 + id*10;

        if(completion < 1) {
            Utils.drawTexturedRect(x, y, 10, 5,
                    startTexX/181f, (startTexX+10)/181f, 0/10f, 5/10f, GL11.GL_NEAREST);
        }
        if(completion > 0) {
            Utils.drawTexturedRect(x, y, 10*completion, 5,
                    startTexX/181f, (startTexX+10*completion)/181f, 5/10f, 10/10f, GL11.GL_NEAREST);
        }

    }

    private void renderBar(float x, float y, float xSize, float completed) {
        Minecraft.getMinecraft().getTextureManager().bindTexture(FUEL_BAR);

        GlStateManager.pushMatrix();
        GlStateManager.translate(x, y, 0);
        xSize = xSize/1.5f;
        GlStateManager.scale(1.5f, 1.5f, 1);

        Color c = Color.getHSBColor(148/360f*completed-20/360f, 0.9f, 1-0.5f*completed);
        GlStateManager.color(c.getRed()/255f, c.getGreen()/255f, c.getBlue()/255f, 1);

        float offsetCompleteX = xSize*completed;
        for(int xOffset = 0; xOffset < xSize; xOffset += 10) {
            float notchCompl = 1;
            if(xOffset > offsetCompleteX) {
                notchCompl = 0;
            } else if(xOffset+10 > offsetCompleteX) {
                notchCompl = (offsetCompleteX-xOffset)/10f;
            }
            if(xOffset == 0) {
                renderBarCap(0, 0, true, 0, notchCompl);
            } else if(xOffset + 11 > xSize) {
                renderBarCap(xOffset, 0, false, xSize-xOffset, notchCompl);
            } else {
                renderBarNotch(xOffset, 0, xOffset/10, notchCompl);
            }
        }

        GlStateManager.popMatrix();
    }

}