-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathomegga.plugin.ts
executable file
·139 lines (124 loc) · 3.82 KB
/
omegga.plugin.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
import OmeggaPlugin, { OL, PC, PS, WriteSaveObject } from 'omegga';
import fs from 'fs';
import CooldownProvider from './util.cooldown.js';
import fontParser from './util.fontParser.js';
// load in saves in font_fontname.brs format
let fonts;
const textFonts = {};
type Config = {
'only-authorized': boolean;
'authorized-users': { id: string; name: string }[];
'authorized-roles': string[];
cooldown: number;
};
type Storage = {};
export default class TextGen implements OmeggaPlugin<Config, Storage> {
omegga: OL;
config: PC<Config>;
store: PS<Storage>;
constructor(omegga: OL, config: PC<Config>, store: PS<Storage>) {
this.omegga = omegga;
this.config = config;
this.store = store;
}
async init() {
// parse fonts on load
(async () => {
const matches = fs
.readdirSync(__dirname + '/../fonts')
.map(f => f.match(/font_([a-z0-9_]+)\.brs$/))
.filter(f => f);
const parsedFonts = [];
console.info('Parsing', matches.length, 'fonts');
for (const match of matches) {
try {
parsedFonts.push([
match[1],
await fontParser(__dirname + '/../fonts/' + match[0]),
]);
} catch (err) {
console.error('Error parsing font', match[1], ':', err);
}
}
fonts = Object.fromEntries(parsedFonts);
console.info('Loaded', parsedFonts.length, 'fonts');
})();
const authorized = (name: string) => {
const player = this.omegga.getPlayer(name);
return (
!this.config['only-authorized'] ||
player.isHost() ||
this.config['authorized-users'].some(p => player.id === p.id) ||
player
.getRoles()
.some(role => this.config['authorized-roles'].includes(role))
);
};
const duration = Math.max(this.config.cooldown * 1000, 0);
const cooldown = duration <= 0 ? () => true : CooldownProvider(duration);
this.omegga
// render text
.on(
'chatcmd:text',
(name: string, ...msg: string[]) =>
authorized(name) &&
cooldown(name) &&
this.cmdText(name, msg.join(' '))
)
// change text font
.on('chatcmd:text:font', (name, font) => {
if (!fonts) {
Omegga.whisper(name, 'Fonts are still being loaded...');
return;
}
if (authorized(name) && cooldown(name) && fonts[font]) {
textFonts[name] = font;
this.omegga.broadcast(
`"Setting <b>${name}</> font to <b>${font}</>"`
);
}
})
// list fonts
.on('chatcmd:text:fonts', (name: string) => {
if (!fonts) {
Omegga.whisper(name, 'Fonts are still being loaded...');
return;
}
if (authorized(name) && cooldown(name)) {
this.omegga.broadcast(
`"<b>Fonts</>: ${Object.keys(fonts)
.map(f => `<code>${f}</>`)
.join(', ')}"`
);
}
});
}
async stop() {}
// load text in
async cmdText(name: string, message: string) {
const player = this.omegga.getPlayer(name);
if (message.trim().length === 0) return;
if (!fonts) {
Omegga.whisper(name, 'Fonts are still being loaded...');
return;
}
try {
const paint = await player.getPaint();
const save: WriteSaveObject = fonts[textFonts[name] || 'default'].text(
message,
{
shift: [0, 0, 0],
color: paint.color || [0, 0, 0],
author: player,
centered: false,
}
);
save.materials = save.materials.map(m => paint.material);
if (save.bricks.length === 0) return;
// load the text save data as this owner
this.omegga.loadSaveDataOnPlayer(save, player);
} catch (e) {
this.omegga.broadcast(`"Could not find <b>${name}</>"`);
}
}
}