aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/Danker/features/AbilityCooldowns.java
blob: e98660e0713d4ad50c94a0abc64152aedea57bee (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
package me.Danker.features;

import me.Danker.commands.MoveCommand;
import me.Danker.commands.ScaleCommand;
import me.Danker.commands.ToggleCommand;
import me.Danker.events.RenderOverlayEvent;
import me.Danker.handlers.TextRenderer;
import me.Danker.utils.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StringUtils;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;

import java.util.ArrayList;
import java.util.List;

public class AbilityCooldowns {

    static List<Ability> cooldowns = new ArrayList<>();
    static double mageReduction = 0D;

    @SubscribeEvent(priority = EventPriority.LOW)
    public void onChat(ClientChatReceivedEvent event) {
        if (!Utils.inSkyblock || !ToggleCommand.abilityCooldowns) return;

        if (event.type == 2) {
            String[] actionBarSections = StringUtils.stripControlCodes(event.message.getUnformattedText()).split(" {3,}");

            for (String section : actionBarSections) {
                if (section.charAt(0) == '-' && section.contains("(") && section.charAt(section.length() - 1) == ')') {
                    String ability = section.substring(section.indexOf("(") + 1, section.length() - 1);

                    for (Ability cooldown : cooldowns) {
                        if (cooldown.ability.equals(ability)) return;
                    }

                    cooldowns.add(new Ability(ability, Utils.getCooldownFromAbility(ability) * (1D - mageReduction)));
                }
            }
        } else {
            String message = StringUtils.stripControlCodes(event.message.getUnformattedText());

            if (Utils.inDungeons && message.startsWith("[Mage] Cooldown Reduction ")) {
                mageReduction = Integer.parseInt(message.substring(message.indexOf(">") + 2, message.length() - 1)) / 100D;
            }
        }
    }

    @SubscribeEvent
    public void renderPlayerInfo(RenderOverlayEvent event) {
        if (ToggleCommand.abilityCooldowns && Utils.inSkyblock) {
            StringBuilder sb = new StringBuilder();

            for (int i = cooldowns.size() - 1; i >= 0; i--) {
                Ability ability = cooldowns.get(i);

                if (ability.getCooldown() <= 0) {
                    cooldowns.remove(i);
                    continue;
                }

                sb.insert(0, ability.getTimer() + "\n");
            }

            new TextRenderer(Minecraft.getMinecraft(), sb.toString(), MoveCommand.abilityCooldownsXY[0], MoveCommand.abilityCooldownsXY[1], ScaleCommand.abilityCooldownsScale);
        }
    }

    @SubscribeEvent
    public void onTick(TickEvent.ClientTickEvent event) {
        if (event.phase != TickEvent.Phase.START) return;

        Minecraft mc = Minecraft.getMinecraft();
        EntityPlayerSP player = mc.thePlayer;
        if (mc.currentScreen instanceof GuiChest) {
            if (player == null) return;
            ContainerChest chest = (ContainerChest) player.openContainer;
            List<Slot> invSlots = ((GuiChest) mc.currentScreen).inventorySlots.inventorySlots;
            String chestName = chest.getLowerChestInventory().getDisplayName().getUnformattedText().trim();

            if (ToggleCommand.abilityCooldowns && Utils.inDungeons && chestName.startsWith("Catacombs - ")) {
                ItemStack mage = invSlots.get(30).getStack();
                if (mage == null || mage.getDisplayName() == null) return;
                if (mage.isItemEnchanted()) {
                    String display = mage.getDisplayName();
                    mageReduction = Utils.getCooldownReductionFromLevel(Integer.parseInt(display.substring(display.indexOf(" ") + 1, display.indexOf("]"))));
                } else {
                    mageReduction = 0D;
                }
            }
        }
    }

    @SubscribeEvent
    public void onWorldChange(WorldEvent.Load event) {
        cooldowns.clear();
        mageReduction = 0D;
    }

    public static class Ability {

        public final String ability;
        private final long cooldown;

        public Ability(String ability, double cooldown) {
            this.ability = ability;
            this.cooldown = (long) (System.currentTimeMillis() + cooldown * 1000L);
        }

        public String getTimer() {
            return EnumChatFormatting.GREEN + ability + ": " + EnumChatFormatting.YELLOW + getCooldown() + "s";
        }

        public double getCooldown() {
            return (cooldown - System.currentTimeMillis()) / 1000D;
        }

    }

}