-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.ts
145 lines (119 loc) · 2.62 KB
/
local.ts
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
import { randomUUID } from 'crypto';
import proxy from '../internal.proxy/local.js';
import { MessageBuilder } from '../prismarine.chat/local.js';
export interface BossbarOptions {
uuid?: string;
health?: number;
clampHealth?: boolean;
title?: MessageBuilder;
visible?: boolean;
color?: number;
dividers?: number;
flags?: number;
}
export default class Bossbar {
public readonly uuid;
protected _health;
public clampHealth;
protected _title;
protected _color;
protected _dividers;
protected _flags;
get health(): number {
return this._health;
}
get title(): MessageBuilder {
return this._title;
}
get color(): number {
return this._color;
}
get dividers(): number {
return this._dividers;
}
get flags(): number {
return this._flags;
}
constructor(options: BossbarOptions = {}) {
this.uuid = options.uuid ?? randomUUID();
this._health = options.health ?? 0;
this.clampHealth = options.clampHealth ?? true;
this._title = options.title ?? MessageBuilder.fromString('Bossbar');
this._color = options.color ?? 6;
this._dividers = options.dividers ?? 0;
this._flags = options.flags ?? 0;
if (options.visible ?? true) {
this.show();
}
}
protected writePacket(data: Record<any, unknown>): void {
proxy.writeDownstream('boss_bar', { entityUUID: this.uuid, ...data });
}
public show(): void {
this.writePacket({
action: 0,
title: JSON.stringify(this.title),
entityUUID: this.uuid,
health: this.health,
color: this.color,
dividers: this.dividers,
flags: this.flags,
});
}
public hide(): void {
this.writePacket({
action: 1,
});
}
set health(health: number) {
if (this.clampHealth) {
health = Math.max(0, health);
health = Math.min(1, health);
}
this._health = health;
this.writePacket({
action: 2,
health,
});
}
set title(title: MessageBuilder) {
this._title = title;
this.updateTitle();
}
public updateTitle(): void {
this.writePacket({
action: 3,
title: JSON.stringify(this.title),
});
}
set color(color: number) {
this._color = color;
this.writePacket({
action: 4,
color,
});
}
set dividers(dividers: number) {
this._dividers = dividers;
this.writePacket({
action: 4,
dividers,
});
}
set flags(flags: number) {
this._flags = flags;
this.writePacket({
action: 5,
flags,
});
}
static COLOR = {
PINK: 0,
BLUE: 1,
RED: 2,
GREEN: 3,
YELLOW: 4,
PURPLE: 5,
WHITE: 6,
};
}