This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (161 loc) · 6.98 KB
/
index.js
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
"use strict";
/// <reference path="./env" />
Object.defineProperty(exports, "__esModule", { value: true });
exports.core = exports.server = exports.plugin = exports.manager = exports.event = exports.command = exports.type = exports.session = exports.unzip = exports.transfer = exports.task = exports.sync = exports.simplify = exports.root = exports.reload = exports.load = exports.format = exports.file = exports.fetch = exports.dev = exports.data = exports.chain = exports.array = void 0;
const base = require("@grakkit/core");
var core_1 = require("@grakkit/core");
Object.defineProperty(exports, "array", { enumerable: true, get: function () { return core_1.array; } });
Object.defineProperty(exports, "chain", { enumerable: true, get: function () { return core_1.chain; } });
Object.defineProperty(exports, "data", { enumerable: true, get: function () { return core_1.data; } });
Object.defineProperty(exports, "dev", { enumerable: true, get: function () { return core_1.dev; } });
Object.defineProperty(exports, "fetch", { enumerable: true, get: function () { return core_1.fetch; } });
Object.defineProperty(exports, "file", { enumerable: true, get: function () { return core_1.file; } });
Object.defineProperty(exports, "format", { enumerable: true, get: function () { return core_1.format; } });
Object.defineProperty(exports, "load", { enumerable: true, get: function () { return core_1.load; } });
Object.defineProperty(exports, "reload", { enumerable: true, get: function () { return core_1.reload; } });
Object.defineProperty(exports, "root", { enumerable: true, get: function () { return core_1.root; } });
Object.defineProperty(exports, "simplify", { enumerable: true, get: function () { return core_1.simplify; } });
Object.defineProperty(exports, "sync", { enumerable: true, get: function () { return core_1.sync; } });
Object.defineProperty(exports, "task", { enumerable: true, get: function () { return core_1.task; } });
Object.defineProperty(exports, "transfer", { enumerable: true, get: function () { return core_1.transfer; } });
Object.defineProperty(exports, "unzip", { enumerable: true, get: function () { return core_1.unzip; } });
/** A session container for this module. */
exports.session = Object.assign({}, base.session, {
event: new Map()
});
/** Imports the specified type from java. */
function type(name) {
if (exports.session.type.has(name)) {
return exports.session.type.get(name);
}
else {
const value = Java.type(name);
exports.session.type.set(name, value);
return value;
}
}
exports.type = type;
const Bukkit = type('org.bukkit.Bukkit');
const EventPriority = type('org.bukkit.event.EventPriority');
const HandlerList = type('org.bukkit.event.HandlerList');
const Listener = Java.extend(type('org.bukkit.event.Listener'), {});
/** Registers a custom command to the server. */
function command(options) {
exports.plugin.register(options.namespace || exports.plugin.getName(), options.name, options.aliases || [], options.permission || '', options.message || '', (sender, label, args) => {
try {
if (!options.permission || sender.hasPermission(options.permission)) {
options.execute && options.execute(sender, ...args);
}
else {
sender.sendMessage(options.message || '');
}
}
catch (error) {
console.error(`An error occured while attempting to execute the "${label}" command!`);
console.error(error.stack || error.message || error);
}
}, (sender, alias, args) => {
try {
return (options.tabComplete && options.tabComplete(sender, ...args)) || [];
}
catch (error) {
console.error(`An error occured while attempting to tab-complete the "${alias}" command!`);
console.error(error.stack || error.message || error);
return [];
}
});
}
exports.command = command;
/** Attaches one or more listeners to a server event. */
function event(name, ...listeners) {
let list;
if (exports.session.event.has(name)) {
list = exports.session.event.get(name);
}
else {
list = new Set();
exports.session.event.set(name, list);
}
const targets = new Set();
for (const listener of listeners) {
if (typeof listener === 'function') {
targets.has('HIGHEST') || targets.add('HIGHEST');
}
else {
targets.has(listener.priority) || targets.add(listener.priority);
}
}
for (const listener of list) {
if (typeof listener === 'function') {
targets.has('HIGHEST') && targets.delete('HIGHEST');
}
else {
targets.has(listener.priority) && targets.delete(listener.priority);
}
}
for (const target of targets) {
const emitter = type(name);
exports.manager.registerEvent(
// @ts-expect-error
emitter.class, instance, EventPriority.valueOf(target),
// @ts-expect-error
(x, signal) => {
if (signal instanceof emitter) {
try {
for (const listener of list) {
if (typeof listener === 'function') {
target === 'HIGHEST' && listener(signal);
}
else {
target === listener.priority && listener.script(signal);
}
}
}
catch (error) {
console.error(`An error occured while attempting to handle the "${name}" event!`);
console.error(error.stack || error.message || error);
}
}
}, exports.plugin);
}
for (const listener of listeners)
list.has(listener) || list.add(listener);
}
exports.event = event;
/** The plugin manager instance. */
exports.manager = Bukkit.getPluginManager();
/** This plugin's instance. */
// @ts-expect-error
exports.plugin = exports.manager.getPlugin('grakkit');
/** The server instance. */
exports.server = Bukkit.getServer();
/** @deprecated */
exports.core = Object.assign({}, base.core, {
command,
event,
manager: exports.manager,
plugin: exports.plugin,
server: exports.server,
type
});
command({
name: 'js',
permission: `${exports.plugin.getName()}.command.js`,
message: `\xa7cYou do not have the required permission to run this command!`,
execute(sender, ...args) {
sender.sendMessage(base.dev.execute(sender, ...args));
},
tabComplete(sender, ...args) {
return base.dev.complete(sender, ...args);
}
});
Core.hook(() => {
HandlerList.unregisterAll(exports.plugin);
});
const instance = new Listener();
Object.assign(globalThis, {
core: exports.core,
manager: exports.manager,
plugin: exports.plugin,
server: exports.server
});