This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
95 lines (85 loc) · 3.29 KB
/
index.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
import uniter from 'uniter';
import phar from 'phar';
import fs from 'fs';
import path from 'path';
import YAML from 'yaml';
import type PluginApi from '@jsprismarine/prismarine/dist/src/plugin/api/versions/1.0/PluginApi';
export default class PharComp {
private api: PluginApi;
constructor(api: PluginApi) {
this.api = api;
}
async onEnable() {
this.api
.getLogger()
.info('§aSuccessfully Loaded PMMP compatibility layer!§r');
const pluginFiles = fs.readdirSync(path.join(process.cwd(), 'plugins'));
const plugins = (
await Promise.all(
pluginFiles.map(async (file) => {
return new Promise((resolve) => {
if (!file.includes('.phar')) return resolve(null);
const archive = new phar.Archive();
archive.loadPharData(
fs.readFileSync(
path.join(process.cwd(), 'plugins', file)
)
);
const files = {};
archive.getFiles().forEach((file) => {
files[file.getName()] = file.getContents();
});
const config = YAML.parse(files['plugin.yml']);
resolve({
name: config.name,
version: config.version,
description: config.description,
main: config.main,
files
});
});
})
)
).filter((a) => a);
plugins.forEach(async (plugin: any) => {
const php = uniter.createEngine('PHP');
const api = this.api;
this.api
.getServer()
.getPluginManager()
.registerClassPlugin(
plugin,
new (class Plugin {
getName() {
return plugin.name;
}
getDisplayName() {
return plugin.name;
}
getVersion() {
return plugin.version;
}
async onEnable() {
const main =
plugin.files[
`src/${plugin.main.replace('\\', '/')}.php`
];
if (!main)
throw new Error('Invalid plugin entry point');
php.getStdout().on('data', (msg) => {
api.getLogger().debug(msg);
});
php.execute(
main,
`src/${plugin.main.replace('\\', '/')}.php`
);
}
async onDisable() {}
} as any)()
);
});
}
async onDisable() {
this.api.getLogger().info('§cPlugin Disabled.§r');
}
}