kustom-mc 0.1.7 → 0.1.8
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/dist/commands/new.js +20 -2
- package/dist/compiler/postprocess.js +3 -1
- package/dist/compiler/rhino-plugin.js +2 -1
- package/dist/runtime.d.ts +22 -2
- package/dist/runtime.js +23 -0
- package/dist/types/index.d.ts +45 -23
- package/package.json +1 -1
package/dist/commands/new.js
CHANGED
|
@@ -84,11 +84,28 @@ export default defineScript({
|
|
|
84
84
|
screen.open(player);
|
|
85
85
|
}
|
|
86
86
|
});
|
|
87
|
+
`,
|
|
88
|
+
command: `import { defineCommand } from 'kustom-mc';
|
|
89
|
+
|
|
90
|
+
export default defineCommand({
|
|
91
|
+
id: "{{name}}",
|
|
92
|
+
description: "A custom command",
|
|
93
|
+
execute(context) {
|
|
94
|
+
if (context.executor.isPlayer()) {
|
|
95
|
+
context.player.sendMessage("Hello from {{name}} command!");
|
|
96
|
+
} else {
|
|
97
|
+
context.process.getLogger().info("Command executed from console");
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
tabComplete(context) {
|
|
101
|
+
return ["option1", "option2"];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
87
104
|
`
|
|
88
105
|
};
|
|
89
106
|
export const newCommand = new Command('new')
|
|
90
107
|
.description('Generate a new script from template')
|
|
91
|
-
.argument('<type>', 'Type of script (script, block, item, gui)')
|
|
108
|
+
.argument('<type>', 'Type of script (script, block, item, gui, command)')
|
|
92
109
|
.argument('<name>', 'Name of the script')
|
|
93
110
|
.option('-f, --force', 'Overwrite existing file')
|
|
94
111
|
.action(async (type, name, options) => {
|
|
@@ -104,7 +121,8 @@ export const newCommand = new Command('new')
|
|
|
104
121
|
script: 'scripts',
|
|
105
122
|
block: 'scripts',
|
|
106
123
|
item: 'scripts',
|
|
107
|
-
gui: 'scripts'
|
|
124
|
+
gui: 'scripts',
|
|
125
|
+
command: 'scripts'
|
|
108
126
|
};
|
|
109
127
|
const dir = dirMap[type];
|
|
110
128
|
const fileName = `${name}.ts`;
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
export function postProcessForRhino(code) {
|
|
13
13
|
let result = code;
|
|
14
14
|
// Remove import statements for kustom-mc (handle all define functions)
|
|
15
|
-
result = result.replace(/import\s*{\s*(?:defineScript|defineBlock|defineItem)(?:\s*,\s*(?:defineScript|defineBlock|defineItem))*\s*}\s*from\s*['"]kustom-mc['"];?\s*/g, '');
|
|
15
|
+
result = result.replace(/import\s*{\s*(?:defineScript|defineBlock|defineItem|defineCommand)(?:\s*,\s*(?:defineScript|defineBlock|defineItem|defineCommand))*\s*}\s*from\s*['"]kustom-mc['"];?\s*/g, '');
|
|
16
16
|
result = result.replace(/import\s+type\s*{[^}]*}\s*from\s*['"]kustom-mc['"];?\s*/g, '');
|
|
17
17
|
// Convert export default defineScript({...}) to exports.default = { __type: "script", ...}
|
|
18
18
|
result = result.replace(/export\s+default\s+defineScript\s*\(\s*({[\s\S]*})\s*\)\s*;?/, (match, content) => injectTypeProperty(content, 'script'));
|
|
@@ -20,6 +20,8 @@ export function postProcessForRhino(code) {
|
|
|
20
20
|
result = result.replace(/export\s+default\s+defineBlock\s*\(\s*({[\s\S]*})\s*\)\s*;?/, (match, content) => injectTypeProperty(content, 'block'));
|
|
21
21
|
// Convert export default defineItem({...}) to exports.default = { __type: "item", ...}
|
|
22
22
|
result = result.replace(/export\s+default\s+defineItem\s*\(\s*({[\s\S]*})\s*\)\s*;?/, (match, content) => injectTypeProperty(content, 'item'));
|
|
23
|
+
// Convert export default defineCommand({...}) to exports.default = { __type: "command", ...}
|
|
24
|
+
result = result.replace(/export\s+default\s+defineCommand\s*\(\s*({[\s\S]*})\s*\)\s*;?/, (match, content) => injectTypeProperty(content, 'command'));
|
|
23
25
|
// Convert any remaining export default to exports.default
|
|
24
26
|
result = result.replace(/export\s+default\s+/g, 'exports.default = ');
|
|
25
27
|
// Convert const/let to var for maximum Rhino compatibility
|
|
@@ -42,12 +42,13 @@ export function transformForRhino(code) {
|
|
|
42
42
|
// 3. Remove any remaining import statements (shouldn't be any, but just in case)
|
|
43
43
|
result = result.replace(/^import\s+.*?from\s+['"][^'"]+['"];?\s*$/gm, '');
|
|
44
44
|
result = result.replace(/^import\s+['"][^'"]+['"];?\s*$/gm, '');
|
|
45
|
-
// 4. Transform defineScript/defineBlock/defineItem calls
|
|
45
|
+
// 4. Transform defineScript/defineBlock/defineItem/defineCommand calls
|
|
46
46
|
// Pattern: var xxx = defineScript({ ... });
|
|
47
47
|
// Result: var xxx = { __type: "script", ... };
|
|
48
48
|
result = transformDefineCall(result, 'defineScript', 'script');
|
|
49
49
|
result = transformDefineCall(result, 'defineBlock', 'block');
|
|
50
50
|
result = transformDefineCall(result, 'defineItem', 'item');
|
|
51
|
+
result = transformDefineCall(result, 'defineCommand', 'command');
|
|
51
52
|
// 5. Handle ESM export: export { xxx as default };
|
|
52
53
|
result = result.replace(/export\s*\{\s*(\w+)\s+as\s+default\s*\}\s*;?/g, 'exports.default = $1;');
|
|
53
54
|
// 6. Handle other ESM exports: export { foo, bar };
|
package/dist/runtime.d.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* plain objects with __type property injected.
|
|
9
9
|
*/
|
|
10
10
|
import './types/globals.js';
|
|
11
|
-
import type { ScriptConfig, ScriptConfigWithMethods, ExportedScript, PropSchema, EventSchema, EmptyEventSchema, BlockConfig, BlockDefinition, ItemConfig, ItemDefinition, ScreenConstructor, ContainerConstructor, ElementFactory, ScriptableLocationFactory, PropsAPI, CellStateUtils, MethodMap } from './types/index.js';
|
|
11
|
+
import type { ScriptConfig, ScriptConfigWithMethods, ExportedScript, PropSchema, EventSchema, EmptyEventSchema, BlockConfig, BlockDefinition, ItemConfig, ItemDefinition, CommandConfig, CommandDefinition, ScreenConstructor, ContainerConstructor, ElementFactory, ScriptableLocationFactory, PropsAPI, CellStateUtils, MethodMap } from './types/index.js';
|
|
12
12
|
/**
|
|
13
13
|
* Screen constructor - create new screens for GUIs.
|
|
14
14
|
* Actual value is injected by Java at runtime.
|
|
@@ -131,4 +131,24 @@ export declare function defineBlock(config: BlockConfig): BlockDefinition;
|
|
|
131
131
|
* });
|
|
132
132
|
*/
|
|
133
133
|
export declare function defineItem(config: ItemConfig): ItemDefinition;
|
|
134
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Define a custom command with type safety.
|
|
136
|
+
*
|
|
137
|
+
* Commands are registered with the CommandManager and can have
|
|
138
|
+
* arguments, permissions, and execution handlers.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* import { defineCommand } from 'kustom-mc';
|
|
142
|
+
*
|
|
143
|
+
* export default defineCommand({
|
|
144
|
+
* name: "greet",
|
|
145
|
+
* description: "Greet a player",
|
|
146
|
+
* permission: "kustom.greet",
|
|
147
|
+
*
|
|
148
|
+
* execute(ctx) {
|
|
149
|
+
* ctx.sender.sendMessage("Hello!");
|
|
150
|
+
* }
|
|
151
|
+
* });
|
|
152
|
+
*/
|
|
153
|
+
export declare function defineCommand(config: CommandConfig): CommandDefinition;
|
|
154
|
+
export type { ScriptConfig, ScriptDefinition, ExportedScript, BaseScriptContext, ScriptContext, PropSchema, PropAPI, InferProps, PropsAPI, WrappedMethods, MethodMap, EventSchema, EmptyEventSchema, InferEventPayload, ScriptModule, ScriptProcess, TypedScriptProcess, InferScriptModule, McModuleAPI, TypedMcModuleAPI, ProcessExecutorAPI, CameraAPI, SessionAPI, ChatGUIAPI, CommandManager, BetterModelEntityAPI, ShaperAPI, ItemAPI, DefinitionBuilder, ScriptablePlayer, BlockConfig, BlockDefinition, BlockClickEvent, BlockBreakEvent, BlockPlaceEvent, ItemConfig, ItemDefinition, CommandConfig, CommandDefinition, ScreenConstructor, ContainerConstructor, ElementFactory, ScriptableLocationFactory, CellStateUtils, } from './types/index.js';
|
package/dist/runtime.js
CHANGED
|
@@ -69,3 +69,26 @@ export function defineItem(config) {
|
|
|
69
69
|
// Simply return the config - the Java side will handle registration
|
|
70
70
|
return config;
|
|
71
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Define a custom command with type safety.
|
|
74
|
+
*
|
|
75
|
+
* Commands are registered with the CommandManager and can have
|
|
76
|
+
* arguments, permissions, and execution handlers.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* import { defineCommand } from 'kustom-mc';
|
|
80
|
+
*
|
|
81
|
+
* export default defineCommand({
|
|
82
|
+
* name: "greet",
|
|
83
|
+
* description: "Greet a player",
|
|
84
|
+
* permission: "kustom.greet",
|
|
85
|
+
*
|
|
86
|
+
* execute(ctx) {
|
|
87
|
+
* ctx.sender.sendMessage("Hello!");
|
|
88
|
+
* }
|
|
89
|
+
* });
|
|
90
|
+
*/
|
|
91
|
+
export function defineCommand(config) {
|
|
92
|
+
// Simply return the config - the Java side will handle registration
|
|
93
|
+
return config;
|
|
94
|
+
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -316,39 +316,57 @@ export interface ItemConfig {
|
|
|
316
316
|
export interface ItemDefinition extends ItemConfig {
|
|
317
317
|
readonly __type: "item";
|
|
318
318
|
}
|
|
319
|
+
export interface ExecutionContext {
|
|
320
|
+
process: McModuleAPI;
|
|
321
|
+
executor: ProcessExecutorAPI;
|
|
322
|
+
player?: ScriptablePlayer;
|
|
323
|
+
camera: CameraAPI;
|
|
324
|
+
session: SessionAPI;
|
|
325
|
+
chatGUI: ChatGUIAPI;
|
|
326
|
+
command: CommandManager;
|
|
327
|
+
entity: BetterModelEntityAPI;
|
|
328
|
+
shaper: ShaperAPI;
|
|
329
|
+
items: ItemAPI;
|
|
330
|
+
definition: DefinitionBuilder;
|
|
331
|
+
storage: StorageAPI;
|
|
332
|
+
}
|
|
333
|
+
export interface ScriptContext<P = Record<string, unknown>, E extends EventSchema = EmptyEventSchema, Methods = Record<string, never>> extends ExecutionContext {
|
|
334
|
+
process: TypedMcModuleAPI<E>;
|
|
335
|
+
props: P;
|
|
336
|
+
methods: Methods;
|
|
337
|
+
}
|
|
338
|
+
export interface CommandContext extends ExecutionContext {
|
|
339
|
+
args: string[];
|
|
340
|
+
}
|
|
341
|
+
export interface CommandConfig {
|
|
342
|
+
/** Unique identifier for the command (e.g., "hello", "spawn") */
|
|
343
|
+
id: string;
|
|
344
|
+
/** Description shown in help text */
|
|
345
|
+
description?: string;
|
|
346
|
+
/** Bukkit permission node. If set, only players with this permission can use the command. Compatible with LuckPerms and other permission plugins. */
|
|
347
|
+
permission?: string;
|
|
348
|
+
/** Called when the command is executed. Receives a CommandContext object. */
|
|
349
|
+
execute: (context: CommandContext) => void;
|
|
350
|
+
/** Called for tab completion. Receives a CommandContext object. Return an array of suggestions. */
|
|
351
|
+
tabComplete?: (context: CommandContext) => string[];
|
|
352
|
+
}
|
|
353
|
+
export interface CommandDefinition extends CommandConfig {
|
|
354
|
+
readonly __type: "command";
|
|
355
|
+
}
|
|
319
356
|
/**
|
|
320
357
|
* Base context passed to method functions.
|
|
321
358
|
* Same as ScriptContext but without `methods` to avoid circular generic inference.
|
|
322
359
|
* At runtime, Java actually injects the full context including methods.
|
|
360
|
+
* Extends ExecutionContext for consistency across all context types.
|
|
323
361
|
*
|
|
324
362
|
* @typeParam P - Inferred props type
|
|
325
363
|
* @typeParam E - Event schema type
|
|
326
364
|
*/
|
|
327
|
-
export interface BaseScriptContext<P = Record<string, unknown>, E extends EventSchema = EmptyEventSchema> {
|
|
365
|
+
export interface BaseScriptContext<P = Record<string, unknown>, E extends EventSchema = EmptyEventSchema> extends ExecutionContext {
|
|
328
366
|
/** Process API for script management (typed for events if declared) */
|
|
329
367
|
process: TypedMcModuleAPI<E>;
|
|
330
368
|
/** Validated props */
|
|
331
369
|
props: P;
|
|
332
|
-
/** Who executed this script */
|
|
333
|
-
executor: ProcessExecutorAPI;
|
|
334
|
-
/** Camera system for cinematic views */
|
|
335
|
-
camera: CameraAPI;
|
|
336
|
-
/** Chat session system */
|
|
337
|
-
session: SessionAPI;
|
|
338
|
-
/** Chat GUI system */
|
|
339
|
-
chatGUI: ChatGUIAPI;
|
|
340
|
-
/** Command registration system */
|
|
341
|
-
command: CommandManager;
|
|
342
|
-
/** BetterModel entity system */
|
|
343
|
-
entity: BetterModelEntityAPI;
|
|
344
|
-
/** Shaper (custom block) system */
|
|
345
|
-
shaper: ShaperAPI;
|
|
346
|
-
/** Custom item system */
|
|
347
|
-
items: ItemAPI;
|
|
348
|
-
/** Model definition builder */
|
|
349
|
-
definition: DefinitionBuilder;
|
|
350
|
-
/** Persistent storage system */
|
|
351
|
-
storage: StorageAPI;
|
|
352
370
|
}
|
|
353
371
|
/**
|
|
354
372
|
* Full context passed to the script `run()` function.
|
|
@@ -1397,8 +1415,12 @@ export interface ColumnConfig {
|
|
|
1397
1415
|
setMargin(margin: number): ColumnConfig;
|
|
1398
1416
|
}
|
|
1399
1417
|
export interface CommandManager {
|
|
1400
|
-
|
|
1401
|
-
|
|
1418
|
+
/**
|
|
1419
|
+
* Register a command. The callback receives (sender, ...args) where sender is the
|
|
1420
|
+
* player name and args are the space-separated arguments from `/kustom command <name> <args>`.
|
|
1421
|
+
* Returns the command ID.
|
|
1422
|
+
*/
|
|
1423
|
+
register(name: string, callback: (sender: string, ...args: string[]) => unknown): number;
|
|
1402
1424
|
unregister(commandId: number): void;
|
|
1403
1425
|
givePlayer(playerName: string, commandId: number): void;
|
|
1404
1426
|
removePlayer(playerName: string, commandId: number): void;
|