aboutsummaryrefslogtreecommitdiff
path: root/features/dataLoader/index.js
blob: e24c34ca4c43e2b464abd1ac31f85489eb750aed (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
/// <reference types="../../../CTAutocomplete" />
/// <reference lib="es2015" />
import Feature from "../../featureClass/class";

class DataLoader extends Feature {
    constructor() {
        super()
    }

    onEnable(){
        this.initVariables()

        this.stats = {}

        this.area = undefined

        this.isInSkyblock = false

        this.registerStep(true, 2, this.step)

        this.registerEvent("worldLoad", this.worldLoad)

        this.api_loaded_event = this.createCustomEvent("apiLoad")

        this.lastApiData = {
            "skyblock": undefined,
            "player": undefined,
            "skyblock_raw": undefined, //the _raw is loaded from hypixel api instead of soopy api
            "player_raw": undefined
        }

        this.loadApi()
    }

    worldLoad(){
        this.area = undefined
    }

    loadApi(){
        let data = JSON.parse(FileLib.getUrlContent("http://soopymc.my.to/api/v2/player_skyblock/" + Player.getUUID().replace(/-/g, "")))

        if(!data.success) return

        this.api_loaded_event.trigger(data, "skyblock", true, true)
        this.lastApiData.skyblock = data
    }

    loadApiData(type, soopyServer){
        while(this.FeatureManager.features["globalSettings"] === undefined || this.FeatureManager.features["globalSettings"].class.apiKeySetting === undefined){
            Thread.sleep(100)
        }
        let key = this.FeatureManager.features["globalSettings"].class.apiKeySetting.getValue()
        if(!key) return

        if(soopyServer){

        }else{
            if(type === "skyblock"){
                let data = JSON.parse(FileLib.getUrlContent("https://api.hypixel.net/skyblock/profiles?key=" + key + "&uuid=" + Player.getUUID().replace(/-/g, "")))
        
                if(!data.success) return
        
                this.api_loaded_event.trigger(data, "skyblock", false, true)
                this.lastApiData.skyblock_raw = data
            }
        }
    }

    step(){ //2fps
        this.stats["Area"] = undefined
        this.stats["Dungeon"] = undefined

        if(TabList && TabList.getNames()){
            TabList.getNames().forEach(n=>{
                n = ChatLib.removeFormatting(n)
                if(n.includes(": ")){
                    this.stats[n.split(": ")[0].trim()] = n.split(": ")[1].trim()
                }
            })
        }

        if(this.stats["Dungeon"]){
            this.stats["Area"] = this.stats["Dungeon"]
            this.isInDungeon = true
        }else{
            this.isInDungeon = false
        }

        this.dungeonFloor = undefined
        Scoreboard.getLines().forEach(line=>{
            let name = ChatLib.removeFormatting(line.getName()).replace(/[^A-z0-9 \:\(\)\.]/g, "")
            if(this.isInDungeon){
                if(name.includes("The Catacombs (")){
                    this.dungeonFloor = name.split("(")[1].split(")")[0].toUpperCase()
                }
            }
            if(ChatLib.removeFormatting(line).startsWith(" ⏣ ")){
                this.areaFine = ChatLib.removeFormatting(line).split(" ⏣ ")[1].replace(/[^A-z0-9 \:\(\)\.\-]/g, "")
            }
            if(name.startsWith("Purse: ")){
                this.purse = parseInt(name.split("Purse: ")[1].split(" ")[0])
            }
            if(name.startsWith("Bits: ")){
                this.bits = parseInt(name.split("Bits: ")[1].split(" ")[0])
            }
        })

        this.isInSkyblock = Scoreboard.getTitle()?.removeFormatting().includes("SKYBLOCK")
        this.area = this.stats["Area"]
    }

    initVariables(){
        this.stats = undefined
        this.isInDungeon = false

        this.dungeonFloor = undefined
        this.area = undefined
        this.areaFine = undefined
        this.bits = undefined
        this.purse = undefined
        this.lastApiData = undefined
        this.isInSkyblock = undefined
    }

    onDisable(){
        this.initVariables()
    }
}

module.exports = {
    class: new DataLoader()
}