aboutsummaryrefslogtreecommitdiff
path: root/features/stat_next_to_name/index.js
blob: 1c1b2ab0be1205127b38e2909984de9bbfb27190 (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
/// <reference types="../../../CTAutocomplete" />
/// <reference lib="es2015" />
import Feature from "../../featureClass/class";
import soopyV2Server from "../../socketConnection";
import SettingBase from "../settings/settingThings/settingBase";
import * as numberUtils from "../../utils/numberUtils";
import DropdownSetting from "../settings/settingThings/dropdownSetting";

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

    onEnable(){
        new SettingBase("NOTE: A pink star thing (&d⚝§0)", "Means that player is also using SoopyV2", true, "stat_next_to_name_description", this)
        this.statToShow = new DropdownSetting("Stat to show", "", "weight", "stat_selected_nexttoname", this, {
            "weight": "Weight",
            "catacombsLevel": "Catacombs Level",
            "skillAvg": "Skill Average",
            "totalSlayer": "Total Slayer Exp"
        })

        this.decimals = {
            "weight": 0,
            "catacombsLevel": 2,
            "skillAvg": 2,
            "totalSlayer": 0
        }

        this.userStats = {}

        this.loadingStats = []

        soopyV2Server.onPlayerStatsLoaded = (stats)=>{this.playerStatsLoaded.call(this, stats)}

        this.registerStep(false, 5, this.loadPlayerStatsTick)
        this.registerEvent("worldLoad", this.worldLoad)
    }

    loadPlayerStatsTick(){
        let nearestPlayer = undefined
        let nearestDistance = Infinity

        World.getAllPlayers().forEach(player => {
            if(this.userStats[player.getUUID().toString().replace(/-/g, "")]){
                this.updatePlayerNametag(player)
                return
            }
            if(this.loadingStats.includes(player.getUUID().toString().replace(/-/g, ""))) return
            if(Player.getUUID().replace(/-/g, "").toString().substr(12, 1) !== "4") return

            let dist = Math.pow(player.getX() - Player.getX(), 2) + Math.pow(player.getY() - Player.getY(), 2) + Math.pow(player.getZ() - Player.getZ(), 2)
            if(dist < nearestDistance){
                nearestDistance = dist
                nearestPlayer = player.getUUID().toString().replace(/-/g, "")
            }
        })

        if(nearestPlayer){
            this.loadPlayerStats(nearestPlayer)
        }
    }

    worldLoad(){
        let playerStats = this.userStats[Player.getUUID().toString().replace(/-/g, "")]
        this.userStats = {}
        this.loadingStats = []
        if(playerStats){
            this.userStats[Player.getUUID().toString().replace(/-/g, "")] = playerStats
        }
    }

    updatePlayerNametag(player){
        let stats = this.userStats[player.getUUID().toString().replace(/-/g, "")]

        let nameTagString = player.getName()

        nameTagString += " &2["
        if(stats.usingSoopyv2) nameTagString += "&d⚝&2"
        if(stats.exists && stats[this.statToShow.getValue()]){
            nameTagString += numberUtils.numberWithCommas(stats[this.statToShow.getValue()].toFixed(this.decimals[this.statToShow.getValue()]))
        }else{
            nameTagString += "?"
        }
        nameTagString += "]"
        player.setNametagName(new TextComponent(nameTagString));
    }

    loadPlayerStats(uuid){
        // console.log("loading stats for " + uuid)
        soopyV2Server.requestPlayerStats(uuid)
        this.loadingStats.push(uuid)
    }

    playerStatsLoaded(stats){
        // console.log(JSON.stringify(stats, undefined, 2))
        this.userStats[stats.uuid] = stats
    
    }

    onDisable(){
        this.userStats = undefined
    }
}

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