@mbler/mcx 0.0.2-alpha.r4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/createApp.js +7 -0
- package/dist/createApp.js.map +1 -0
- package/dist/event.js +131 -0
- package/dist/event.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +154 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/mcx.js +12 -0
- package/dist/lib/mcx.js.map +1 -0
- package/dist/types/createApp.d.ts +3 -0
- package/dist/types/event.d.ts +24 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/lib/mcx.d.ts +7 -0
- package/dist/types/types.d.ts +5 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 RuanhoR
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createApp.js","sourceRoot":"","sources":["../src/createApp.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,MAAM,EACP,MAAM,cAAc,CAAA;AACrB,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,OAAgB;IAChD,IAAI,OAAO,CAAC,IAAI,KAAK,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;IAElG,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,CAAA;AAC5B,CAAC"}
|
package/dist/event.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { world as _world, } from "@minecraft/server";
|
|
2
|
+
function exports() {
|
|
3
|
+
let world = _world;
|
|
4
|
+
const Event = class Event {
|
|
5
|
+
#currenyRun = [];
|
|
6
|
+
#canRun = [];
|
|
7
|
+
#eventList = {};
|
|
8
|
+
#on;
|
|
9
|
+
constructor(data) {
|
|
10
|
+
this.#canRun = Object.keys(data.data);
|
|
11
|
+
this.#eventList = data.data;
|
|
12
|
+
this.#on = data.on;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* @param eventName {string} - eventName
|
|
17
|
+
* @returns {boolean} - if succeess
|
|
18
|
+
*/
|
|
19
|
+
#bind_event(eventItem) {
|
|
20
|
+
if (this.#currenyRun.includes(eventItem))
|
|
21
|
+
return true;
|
|
22
|
+
if (this.#on == "after" && eventItem in world.afterEvents) {
|
|
23
|
+
const key = eventItem;
|
|
24
|
+
const subscribe = world.afterEvents[key];
|
|
25
|
+
const handler = this.#eventList[eventItem];
|
|
26
|
+
if (!handler || typeof handler !== "function")
|
|
27
|
+
throw new Error("[event bind]: mcx bind event: handler is not right");
|
|
28
|
+
subscribe.subscribe(handler);
|
|
29
|
+
this.#currenyRun.push(eventItem);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
if (this.#on == "before" && eventItem in world.beforeEvents) {
|
|
33
|
+
const key = eventItem;
|
|
34
|
+
const subscribe = world.beforeEvents[key];
|
|
35
|
+
const handler = this.#eventList[eventItem];
|
|
36
|
+
if (!handler || typeof handler !== "function")
|
|
37
|
+
throw new Error("[event bind]: mcx bind event: handler is not right");
|
|
38
|
+
subscribe.subscribe(handler);
|
|
39
|
+
this.#currenyRun.push(eventItem);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
subscribe(...events) {
|
|
45
|
+
if (!events || events.length == 0) {
|
|
46
|
+
// 求差集
|
|
47
|
+
const list = this.#canRun.filter((item) => {
|
|
48
|
+
return !this.#currenyRun.includes(item);
|
|
49
|
+
});
|
|
50
|
+
// all subscribe
|
|
51
|
+
for (const eventIndex in list) {
|
|
52
|
+
const eventItem = this.#canRun[eventIndex];
|
|
53
|
+
if (!eventItem)
|
|
54
|
+
continue;
|
|
55
|
+
const s = this.#bind_event(eventItem);
|
|
56
|
+
if (!s)
|
|
57
|
+
throw new Error("[bind event]: bind event '" + eventItem + "' error");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
for (const eventName of events) {
|
|
62
|
+
if (typeof eventName !== "string")
|
|
63
|
+
throw new Error("[bind event]: bind eventname is not ");
|
|
64
|
+
const status = this.#bind_event(eventName);
|
|
65
|
+
if (!status)
|
|
66
|
+
throw new Error("[bind event]: event: '" + eventName + "' error");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
#unbind_event(eventName) {
|
|
72
|
+
if (!this.#currenyRun.includes(eventName)) {
|
|
73
|
+
throw new Error("[bind event]: can't close a not running event");
|
|
74
|
+
}
|
|
75
|
+
if (this.#on == "after" && eventName in world.afterEvents) {
|
|
76
|
+
const key = eventName;
|
|
77
|
+
const subscribe = world.afterEvents[key];
|
|
78
|
+
const handler = this.#eventList[eventName];
|
|
79
|
+
if (!handler || typeof handler !== "function")
|
|
80
|
+
throw new Error("[event bind]: mcx bind event: handler is not right");
|
|
81
|
+
subscribe.unsubscribe(handler);
|
|
82
|
+
this.#currenyRun.push(eventName);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
if (this.#on == "before" && eventName in world.beforeEvents) {
|
|
86
|
+
const key = eventName;
|
|
87
|
+
const subscribe = world.beforeEvents[key];
|
|
88
|
+
const handler = this.#eventList[eventName];
|
|
89
|
+
if (!handler || typeof handler !== "function")
|
|
90
|
+
throw new Error("[event bind]: mcx bind event: handler is not right");
|
|
91
|
+
subscribe.unsubscribe(handler);
|
|
92
|
+
this.#currenyRun.push(eventName);
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* unscribe
|
|
99
|
+
*/
|
|
100
|
+
unscribe(...events) {
|
|
101
|
+
if (!events || events.length == 0) {
|
|
102
|
+
// all run event
|
|
103
|
+
for (const eventIndex in this.#currenyRun) {
|
|
104
|
+
const eventItem = this.#canRun[eventIndex];
|
|
105
|
+
if (!eventItem)
|
|
106
|
+
continue;
|
|
107
|
+
const s = this.#unbind_event(eventItem);
|
|
108
|
+
if (!s)
|
|
109
|
+
throw new Error("[bind event]: bind event '" + eventItem + "' error");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
for (const eventName of events) {
|
|
114
|
+
if (typeof eventName !== "string")
|
|
115
|
+
throw new Error("");
|
|
116
|
+
const status = this.#unbind_event(eventName);
|
|
117
|
+
if (!status)
|
|
118
|
+
throw new Error("[bind event]: event: '" + eventName + "' error");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
useWorld(_world) {
|
|
124
|
+
world = _world;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
return Event;
|
|
128
|
+
}
|
|
129
|
+
const im = exports();
|
|
130
|
+
export default im;
|
|
131
|
+
//# sourceMappingURL=event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event.js","sourceRoot":"","sources":["../src/event.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,IAAI,MAAM,GAGhB,MAAM,mBAAmB,CAAC;AAE3B,SAAS,OAAO;IACd,IAAI,KAAK,GAAU,MAAM,CAAC;IAC1B,MAAM,KAAK,GAAG,MAAM,KAAK;QACvB,WAAW,GAAa,EAAE,CAAC;QAC3B,OAAO,GAAa,EAAE,CAAC;QACvB,UAAU,GAAqB,EAAE,CAAC;QAClC,GAAG,CAAqB;QACxB,YAAY,IAAc;YACxB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACtC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC;YAC5B,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,EAAE,CAAC;QACrB,CAAC;QACD;;;;WAIG;QACH,WAAW,CAAC,SAAiB;YAC3B,IAAI,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,SAAS,CAAC;gBAAE,OAAO,IAAI,CAAC;YACtD,IAAI,IAAI,CAAC,GAAG,IAAI,OAAO,IAAI,SAAS,IAAI,KAAK,CAAC,WAAW,EAAE,CAAC;gBAC1D,MAAM,GAAG,GAAG,SAAmC,CAAC;gBAChD,MAAM,SAAS,GAAG,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;gBACzC,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;gBAC3C,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAA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AC;oBACvD,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;oBAC7C,IAAI,CAAC,MAAM;wBACT,MAAM,IAAI,KAAK,CAAC,wBAAwB,GAAG,SAAS,GAAG,SAAS,CAAC,CAAC;gBACtE,CAAC;YACH,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;QACM,QAAQ,CAAC,MAAa;YAC3B,KAAK,GAAG,MAAM,CAAC;QACjB,CAAC;KACF,CAAC;IACF,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,EAAE,GAAG,OAAO,EAAE,CAAC;AACrB,eAAe,EAAE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { World } from '@minecraft/server';
|
|
2
|
+
import { EventOpt, MCXFile } from '@mbler/mcx-types';
|
|
3
|
+
|
|
4
|
+
declare const im: {
|
|
5
|
+
new (data: EventOpt): {
|
|
6
|
+
"__#private@#currenyRun": string[];
|
|
7
|
+
"__#private@#canRun": string[];
|
|
8
|
+
"__#private@#eventList": EventOpt["data"];
|
|
9
|
+
"__#private@#on": "after" | "before";
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @param eventName {string} - eventName
|
|
13
|
+
* @returns {boolean} - if succeess
|
|
14
|
+
*/
|
|
15
|
+
"__#private@#bind_event"(eventItem: string): boolean;
|
|
16
|
+
subscribe(...events: string[]): boolean;
|
|
17
|
+
"__#private@#unbind_event"(eventName: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* unscribe
|
|
20
|
+
*/
|
|
21
|
+
unscribe(...events: string[]): boolean;
|
|
22
|
+
useWorld(_world: World): void;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
declare class MCXApp {
|
|
27
|
+
private content;
|
|
28
|
+
constructor(content: MCXFile<"app">);
|
|
29
|
+
mout(world: World): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare function createApp(content: MCXFile<"app">): MCXApp;
|
|
33
|
+
|
|
34
|
+
export { createApp, im as event };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var server = require('@minecraft/server');
|
|
4
|
+
|
|
5
|
+
function exports$1() {
|
|
6
|
+
let world = server.world;
|
|
7
|
+
const Event = class Event {
|
|
8
|
+
#currenyRun = [];
|
|
9
|
+
#canRun = [];
|
|
10
|
+
#eventList = {};
|
|
11
|
+
#on;
|
|
12
|
+
constructor(data) {
|
|
13
|
+
this.#canRun = Object.keys(data.data);
|
|
14
|
+
this.#eventList = data.data;
|
|
15
|
+
this.#on = data.on;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @param eventName {string} - eventName
|
|
20
|
+
* @returns {boolean} - if succeess
|
|
21
|
+
*/
|
|
22
|
+
#bind_event(eventItem) {
|
|
23
|
+
if (this.#currenyRun.includes(eventItem))
|
|
24
|
+
return true;
|
|
25
|
+
if (this.#on == "after" && eventItem in world.afterEvents) {
|
|
26
|
+
const key = eventItem;
|
|
27
|
+
const subscribe = world.afterEvents[key];
|
|
28
|
+
const handler = this.#eventList[eventItem];
|
|
29
|
+
if (!handler || typeof handler !== "function")
|
|
30
|
+
throw new Error("[event bind]: mcx bind event: handler is not right");
|
|
31
|
+
subscribe.subscribe(handler);
|
|
32
|
+
this.#currenyRun.push(eventItem);
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
if (this.#on == "before" && eventItem in world.beforeEvents) {
|
|
36
|
+
const key = eventItem;
|
|
37
|
+
const subscribe = world.beforeEvents[key];
|
|
38
|
+
const handler = this.#eventList[eventItem];
|
|
39
|
+
if (!handler || typeof handler !== "function")
|
|
40
|
+
throw new Error("[event bind]: mcx bind event: handler is not right");
|
|
41
|
+
subscribe.subscribe(handler);
|
|
42
|
+
this.#currenyRun.push(eventItem);
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
subscribe(...events) {
|
|
48
|
+
if (!events || events.length == 0) {
|
|
49
|
+
// 求差集
|
|
50
|
+
const list = this.#canRun.filter((item) => {
|
|
51
|
+
return !this.#currenyRun.includes(item);
|
|
52
|
+
});
|
|
53
|
+
// all subscribe
|
|
54
|
+
for (const eventIndex in list) {
|
|
55
|
+
const eventItem = this.#canRun[eventIndex];
|
|
56
|
+
if (!eventItem)
|
|
57
|
+
continue;
|
|
58
|
+
const s = this.#bind_event(eventItem);
|
|
59
|
+
if (!s)
|
|
60
|
+
throw new Error("[bind event]: bind event '" + eventItem + "' error");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
for (const eventName of events) {
|
|
65
|
+
if (typeof eventName !== "string")
|
|
66
|
+
throw new Error("[bind event]: bind eventname is not ");
|
|
67
|
+
const status = this.#bind_event(eventName);
|
|
68
|
+
if (!status)
|
|
69
|
+
throw new Error("[bind event]: event: '" + eventName + "' error");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
#unbind_event(eventName) {
|
|
75
|
+
if (!this.#currenyRun.includes(eventName)) {
|
|
76
|
+
throw new Error("[bind event]: can't close a not running event");
|
|
77
|
+
}
|
|
78
|
+
if (this.#on == "after" && eventName in world.afterEvents) {
|
|
79
|
+
const key = eventName;
|
|
80
|
+
const subscribe = world.afterEvents[key];
|
|
81
|
+
const handler = this.#eventList[eventName];
|
|
82
|
+
if (!handler || typeof handler !== "function")
|
|
83
|
+
throw new Error("[event bind]: mcx bind event: handler is not right");
|
|
84
|
+
subscribe.unsubscribe(handler);
|
|
85
|
+
this.#currenyRun.push(eventName);
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
if (this.#on == "before" && eventName in world.beforeEvents) {
|
|
89
|
+
const key = eventName;
|
|
90
|
+
const subscribe = world.beforeEvents[key];
|
|
91
|
+
const handler = this.#eventList[eventName];
|
|
92
|
+
if (!handler || typeof handler !== "function")
|
|
93
|
+
throw new Error("[event bind]: mcx bind event: handler is not right");
|
|
94
|
+
subscribe.unsubscribe(handler);
|
|
95
|
+
this.#currenyRun.push(eventName);
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* unscribe
|
|
102
|
+
*/
|
|
103
|
+
unscribe(...events) {
|
|
104
|
+
if (!events || events.length == 0) {
|
|
105
|
+
// all run event
|
|
106
|
+
for (const eventIndex in this.#currenyRun) {
|
|
107
|
+
const eventItem = this.#canRun[eventIndex];
|
|
108
|
+
if (!eventItem)
|
|
109
|
+
continue;
|
|
110
|
+
const s = this.#unbind_event(eventItem);
|
|
111
|
+
if (!s)
|
|
112
|
+
throw new Error("[bind event]: bind event '" + eventItem + "' error");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
for (const eventName of events) {
|
|
117
|
+
if (typeof eventName !== "string")
|
|
118
|
+
throw new Error("");
|
|
119
|
+
const status = this.#unbind_event(eventName);
|
|
120
|
+
if (!status)
|
|
121
|
+
throw new Error("[bind event]: event: '" + eventName + "' error");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
useWorld(_world) {
|
|
127
|
+
world = _world;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
return Event;
|
|
131
|
+
}
|
|
132
|
+
const im = exports$1();
|
|
133
|
+
|
|
134
|
+
class MCXApp {
|
|
135
|
+
content;
|
|
136
|
+
constructor(content) {
|
|
137
|
+
this.content = content;
|
|
138
|
+
}
|
|
139
|
+
mout(world) {
|
|
140
|
+
const event = this.content.app.event;
|
|
141
|
+
if (event)
|
|
142
|
+
event.useWorld(world);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function createApp(content) {
|
|
147
|
+
if (content.type !== "app")
|
|
148
|
+
throw new Error("[mcx runtime]: createApp must input a MCXFile<app>");
|
|
149
|
+
return new MCXApp(content);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
exports.createApp = createApp;
|
|
153
|
+
exports.event = im;
|
|
154
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/event.ts","../src/lib/mcx.ts","../src/createApp.ts"],"sourcesContent":[null,null,null],"names":["exports","_world"],"mappings":";;;;AAOA,SAASA,SAAO,GAAA;IACd,IAAI,KAAK,GAAUC,YAAM;IACzB,MAAM,KAAK,GAAG,MAAM,KAAK,CAAA;QACvB,WAAW,GAAa,EAAE;QAC1B,OAAO,GAAa,EAAE;QACtB,UAAU,GAAqB,EAAE;AACjC,QAAA,GAAG;AACH,QAAA,WAAA,CAAY,IAAc,EAAA;YACxB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;AACrC,YAAA,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,IAAI;AAC3B,YAAA,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,EAAE;QACpB;AACA;;;;AAIG;AACH,QAAA,WAAW,CAAC,SAAiB,EAAA;AAC3B,YAAA,IAAI,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,SAAS,CAAC;AAAE,gBAAA,OAAO,IAAI;AACrD,YAAA,IAAI,IAAI,CAAC,GAAG,IAAI,OAAO,IAAI,SAAS,IAAI,KAAK,CAAC,WAAW,EAAE;gBACzD,MAAM,GAAG,GAAG,SAAmC;gBAC/C,MAAM,SAAS,GAAG,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC;gBACxC,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC;AAC1C,gBAAA,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,UAAU;AAC3C,oBAAA,MAAM,IAAI,KAAK,CAAC,oD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MAAM;QAChB;KACD;AACD,IAAA,OAAO,KAAK;AACd;AAEA,MAAM,EAAE,GAAGD,SAAO;;MClIL,MAAM,CAAA;AACG,IAAA,OAAA;AAApB,IAAA,WAAA,CAAoB,OAAuB,EAAA;QAAvB,IAAA,CAAA,OAAO,GAAP,OAAO;IAC3B;AACA,IAAA,IAAI,CAAC,KAAY,EAAA;QACf,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK;AACpC,QAAA,IAAI,KAAK;AAAE,YAAA,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC;IAClC;AACD;;ACJa,SAAU,SAAS,CAAC,OAAuB,EAAA;AACvD,IAAA,IAAI,OAAO,CAAC,IAAI,KAAK,KAAK;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC;AAEjG,IAAA,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC;AAC5B;;;;;"}
|
package/dist/lib/mcx.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcx.js","sourceRoot":"","sources":["../../src/lib/mcx.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,MAAM;IACG;IAApB,YAAoB,OAAgB;QAAhB,YAAO,GAAP,OAAO,CAAS;IACpC,CAAC;IACD,IAAI,CAAC,KAAY;QACf,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC;QACrC,IAAI,KAAK;YAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { World } from "@minecraft/server";
|
|
2
|
+
import { EventOpt } from "@mbler/mcx-types";
|
|
3
|
+
declare const im: {
|
|
4
|
+
new (data: EventOpt): {
|
|
5
|
+
"__#private@#currenyRun": string[];
|
|
6
|
+
"__#private@#canRun": string[];
|
|
7
|
+
"__#private@#eventList": EventOpt["data"];
|
|
8
|
+
"__#private@#on": "after" | "before";
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param eventName {string} - eventName
|
|
12
|
+
* @returns {boolean} - if succeess
|
|
13
|
+
*/
|
|
14
|
+
"__#private@#bind_event"(eventItem: string): boolean;
|
|
15
|
+
subscribe(...events: string[]): boolean;
|
|
16
|
+
"__#private@#unbind_event"(eventName: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* unscribe
|
|
19
|
+
*/
|
|
20
|
+
unscribe(...events: string[]): boolean;
|
|
21
|
+
useWorld(_world: World): void;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
export default im;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mbler/mcx",
|
|
3
|
+
"version": "0.0.2-alpha.r4",
|
|
4
|
+
"description": "a DSL compiler of mcx",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "rollup -c"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"minecraft"
|
|
11
|
+
],
|
|
12
|
+
"module": "commjs",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"registry": "https://registry.npmjs.org",
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"author": "ruanhor",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@mbler/mcx-types": "0.0.2-alpha",
|
|
27
|
+
"@minecraft/server": "2.0.0",
|
|
28
|
+
"@rollup/plugin-commonjs": "^29.0.0",
|
|
29
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
30
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
31
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
32
|
+
"@types/node": "^25.2.0",
|
|
33
|
+
"rollup": "^4.57.1",
|
|
34
|
+
"rollup-plugin-dts": "^6.3.0"
|
|
35
|
+
}
|
|
36
|
+
}
|