aboutsummaryrefslogtreecommitdiff
path: root/featureClass/class.js
blob: b8a17d97825996b80d4fba3315b9b7d452236032 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/// <reference types="../../CTAutocomplete" />
/// <reference lib="es2015" />

class Feature {
    constructor() {
        this.FeatureManager = undefined
        this.events = {}
        this.customEvents = {}
        this.forgeEvents = {}
        this.soopyEvents = {}
        this.dynamicEvents = new Set()

        this.id = undefined

        this.enabled = false
    }

    setId(id) {
        this.id = id
    }
    getId() {
        return this.id
    }

    _onDisable() {
        Object.values(this.events).forEach(e => this.FeatureManager.unregisterEvent(e)) //calling parent unregister to avoid the set in unregister event
        Object.values(this.customEvents).forEach(e => this.FeatureManager.unregisterCustom(e)) //calling parent unregister to avoid the set in unregister event
        Object.values(this.forgeEvents).forEach(e => this.FeatureManager.unregisterForge(e)) //calling parent unregister to avoid the set in unregister event
        Object.values(this.soopyEvents).forEach(e => this.FeatureManager.unregisterSoopy(e)) //calling parent unregister to avoid the set in unregister event

        this.onDisable()

        this.events = {}
        this.customEvents = {}
        this.enabled = false

        this.stepEvent.unregister()
    }

    _onEnable(parent) {
        this.FeatureManager = parent

        this.enabled = true

        this.stepEvent = this.registerStep(false, 5, () => {
            this.dynamicEvents.forEach(e => e.update())
        })

        this.onEnable()
    }

    onDisable() { }
    onEnable() { }

    registerEvent(event, func) {
        let theEvent = this.FeatureManager.registerEvent(event, func, this)

        this.events[theEvent.id] = theEvent

        return new CtEvent(theEvent, undefined, [event, func], this)
    }

    unregisterEvent(event) {
        this.FeatureManager.unregisterEvent(event)

        delete this.events[event.id]
    }
    registerSoopy(event, func) {
        let theEvent = this.FeatureManager.registerSoopy(event, func, this)

        this.soopyEvents[theEvent.id] = theEvent

        return new SoopyEvent(theEvent, undefined, [event, func], this)
    }

    unregisterSoopy(event) {
        this.FeatureManager.unregisterSoopy(event)

        delete this.soopyEvents[event.id]
    }

    registerForge(event, func, priority) {
        let theEvent
        try {
            theEvent = this.FeatureManager.registerForge(event, func, priority, this)
        } catch (e) {
            ChatLib.chat(this.FeatureManager.messagePrefix + "An error occured while registering the event " + event.class.toString().split(".").pop() + ", this may cause " + this.constructor.name + " to not work properly.")
        }
        if (theEvent) this.forgeEvents[theEvent.id] = theEvent

        return new ForgeEvent(theEvent, theEvent.trigger, [event, func, priority], this)
    }

    unregisterForge(event) {
        if (!event) return

        this.FeatureManager.unregisterForge(event)

        delete this.forgeEvents[event.id]
    }

    registerChat(criteria, func) {
        let theEvent = this.FeatureManager.registerChat(criteria, func, this)

        this.customEvents[theEvent.id] = theEvent

        return new CustomEvent(theEvent, theEvent.trigger, [criteria, func], this)
    }
    registerActionBar(criteria, func) {
        let theEvent = this.FeatureManager.registerActionBar(criteria, func, this)

        this.customEvents[theEvent.id] = theEvent

        return new CustomEvent(theEvent, theEvent.trigger, [criteria, func], this)
    }
    registerStep(isFps, interval, func) {
        let theEvent = this.FeatureManager.registerStep(isFps, interval, func, this)

        this.customEvents[theEvent.id] = theEvent

        return new CustomEvent(theEvent, theEvent.trigger, [isFps, interval, func], this)
    }

    registerCustom(event, func) {
        let theEvent = this.FeatureManager.registerCustom(event, func, this)

        this.customEvents[theEvent.id] = theEvent

        return new CustomEvent(theEvent, theEvent.trigger, [event, func], this)
    }

    registerCommand(name, func) {
        this.FeatureManager.commandFuncs[name] = func

        this.FeatureManager.registerCommand(name, (...args) => {
            if (this.FeatureManager.commandFuncs[name]) {
                this.FeatureManager.commandFuncs[name].call(this, ...(args || []))
            } else {
                ChatLib.chat(this.FeatureManager.messagePrefix + "This command is not available atm")
            }
        }, this)

        return new CommandEvent(name, undefined, [name, func], this)
    }
    unregisterCommand(name) {
        delete this.FeatureManager.commandFuncs[name]
    }

    unregisterCustom(event) {
        this.FeatureManager.unregisterCustom(event)

        delete this.customEvents[event.id]
    }

    createCustomEvent(eventId) {
        return this.FeatureManager.createCustomEvent(eventId)
    }
}

export default Feature

class Event {
    constructor(data, trigger, registerArgs = [], parent) {
        this.data = data
        this.trigger = trigger
        this.registerArgs = registerArgs
        this.parent = parent

        this.enabled = true

        this.when = undefined
    }

    update() {
        let shouldBeEnabled = !!this.when()
        if (this.enabled === shouldBeEnabled) return

        if (this.enabled) {
            this.unregister()
        } else {
            this.register()
        }
    }

    /**
     * Runs the function given as the argument every 5seconds
     * And will make sure that the event is only registered when the condition is true
     */
    registeredWhen(fun) {
        this.when = fun

        if (!fun()) {
            this.unregister()
        }

        this.parent.dynamicEvents.add(this)

        return this
    }

    register() {
        if (this.enabled) return

        this.enabled = true
        this.actuallyRegister()
    }

    unregister() {
        if (!this.enabled) return

        this.enabled = false
        this.actuallyUnregister()
    }

    actuallyRegister() { }

    actuallyUnregister() { }
}

class CtEvent extends Event {
    actuallyRegister() {
        let newEvent = this.parent.registerEvent(...this.registerArgs)
        this.data = newEvent.data
        this.trigger = newEvent.trigger
    }

    actuallyUnregister() {
        this.parent.unregisterEvent(this.data)
    }
}
class SoopyEvent extends Event {
    actuallyRegister() {
        let newEvent = this.parent.registerSoopy(...this.registerArgs)
        this.data = newEvent.data
        this.trigger = newEvent.trigger
    }

    actuallyUnregister() {
        this.parent.unregisterSoopy(this.data)
    }
}

class CommandEvent extends Event {
    actuallyRegister() {
        let newEvent = this.parent.registerCommand(...this.registerArgs)
        this.data = newEvent.data
        this.trigger = newEvent.trigger
    }

    actuallyUnregister() {
        this.parent.unregisterCommand(this.data)
    }
}

class ForgeEvent extends Event {
    actuallyRegister() {
        let newEvent = this.parent.registerForge(...this.registerArgs)
        this.data = newEvent.data
        this.trigger = newEvent.trigger
    }

    actuallyUnregister() {
        this.parent.unregisterForge(this.data)
    }
}

class CustomEvent extends Event {
    actuallyRegister() {
        let newEvent = this.trigger.register()
        this.data = newEvent.data
        this.trigger = newEvent.trigger
    }

    actuallyUnregister() {
        this.trigger.unregister()
    }
}