aboutsummaryrefslogtreecommitdiff
path: root/utils/utils.js
blob: baae8228b7002f83c82a92595b7f236722b960a7 (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
const NBTTagString = Java.type('net.minecraft.nbt.NBTTagString');

let utils = {
    addLore: function(item, prefix, value){
        
        const list = item
            .getNBT()
            .getCompoundTag("tag")
            .getCompoundTag("display")
            .getTagMap()
            .get("Lore")
    
        let done = false
        // Gets the current lore lines
        for (let i = 0; i < list.func_74745_c(); i++) {
            if (String(list.func_150307_f(i)).startsWith(prefix)){
                list.func_150304_a(i, new NBTTagString(prefix+value));
                done = true
            }
        }
        if(!done){
            list.func_74742_a( new NBTTagString(prefix+value))
        }
    
        item
            .getNBT()
            .getCompoundTag("tag")
            .getCompoundTag("display")
            .getRawNBT()
            .func_74782_a("Lore", list);
    },
    getSBID: function(item){
        return item?.getNBT()?.getCompoundTag("tag")?.getCompoundTag("ExtraAttributes")?.getString("id") || null
    },
    calculateDistance: function(p1, p2) {
        var a = p2[0] - p1[0];
        var b = p2[1] - p1[1];
        var c = p2[2] - p1[2];
    
        let ret = Math.hypot(a,b,c)
    
        if(ret<0){
            ret *= -1
        }
        return ret;
    },
    calculateDistanceQuick: function(p1, p2) {
        var a = p2[0] - p1[0];
        var b = p2[1] - p1[1];
        var c = p2[2] - p1[2];
    
        let ret = a * a + b * b + c * c
    
        if(ret<0){
            ret *= -1
        }
        return ret;
    },
    /**
     * Please try not to use this
     * it has O(n!)
     * only use if points < 10 or something
     * D:
     * @param {*} startPoint 
     * @param {*} points 
     * @returns 
     */
    fastestPathThrough:function(startPoint, points){
        let ret = []
        while(ret.length<points.length){
            ret.push(ret.length)
        }
        
        let allOrders = utils.permutation(ret)
    
        let lastOrder = []
        let lastOrderLength = Infinity
       
        allOrders.forEach((order)=>{
            let lastPoint = startPoint
            let positions = order.map((a)=>{
                return points[a]
            })
            let len = 0
            positions.forEach((pos)=>{
                len += utils.calculateDistance(lastPoint,pos)
                lastPoint = pos
            })
    
            if(len < lastOrderLength){
                lastOrder = order
                lastOrderLength = len
            }
        })
    
        
        return lastOrder;
    },
    permutation:function(array) {
        function p(array, temp) {
            var i, x;
            if (!array.length) {
                result.push(temp);
            }
            for (i = 0; i < array.length; i++) {
                x = array.splice(i, 1)[0];
                p(array, temp.concat(x));
                array.splice(i, 0, x);
            }
        }
    
        var result = [];
        p(array, []);
        return result;
    }
}

module.exports = utils