@@ -21,7 +21,10 @@ import {
2121 ItemStack ,
2222 LiquidType ,
2323 world ,
24- system
24+ system ,
25+ ItemCustomComponent ,
26+ ItemComponentMineBlockEvent ,
27+ ItemComponentUseOnEvent ,
2528} from "@minecraft/server" ;
2629
2730import {
@@ -39,35 +42,46 @@ type BlockStateValue = boolean | number | string;
3942const DEBUG_STICK_ID = "vyt:debug_stick" ;
4043
4144
42- // Some event listeners. Listens for entityHitBkock
43- // and itemUseOn events, which triggers an action onto
44- // the debug stick
45- world . afterEvents . entityHitBlock . subscribe ( safeCallWrapper ( ( ev ) => {
46- if ( ev . damagingEntity . typeId != "minecraft:player" )
47- return ;
48- const player = getPlayerByID ( ev . damagingEntity . id ) ;
49- if ( ! player )
50- return ;
51- if ( ! isHoldingDebugStick ( player ) )
52- return ;
53- changeSelectedProperty ( player , ev . hitBlock ) ;
54- } ) ) ;
5545
46+ class DebugStickEvents implements ItemCustomComponent {
47+
48+ constructor ( ) {
49+ this . onMineBlock = safeCallWrapper ( this . onMineBlock ) . bind ( this ) ;
50+ this . onUseOn = safeCallWrapper ( this . onUseOn ) . bind ( this ) ;
51+ }
52+
53+ onMineBlock ( ev : ItemComponentMineBlockEvent ) {
54+ if ( ev . source . typeId != "minecraft:player" )
55+ return ;
56+ const player = getPlayerByID ( ev . source . id ) ;
57+ if ( ! player )
58+ return ;
59+ if ( ! isHoldingDebugStick ( player ) )
60+ return ;
61+ changeSelectedProperty ( player , ev . block ) ;
62+ }
63+
64+ onUseOn ( ev : ItemComponentUseOnEvent ) {
65+ if ( ev . source . typeId != "minecraft:player" )
66+ return ;
67+ if ( ev . itemStack ?. typeId != DEBUG_STICK_ID )
68+ return ;
69+ const player = getPlayerByID ( ev . source . id ) ;
70+ if ( ! player )
71+ return ;
72+ if ( player . isSneaking )
73+ displayBlockInfo ( player , ev . block ) ;
74+ else
75+ updateBlockProperty ( player , ev . block ) ;
76+ }
77+ }
78+
79+
80+ system . beforeEvents . startup . subscribe ( ( ev ) => {
81+ ev . itemComponentRegistry . registerCustomComponent (
82+ DEBUG_STICK_ID , new DebugStickEvents ( ) ) ;
83+ } ) ;
5684
57- world . beforeEvents . itemUseOn . subscribe ( safeCallWrapper ( ( ev ) => {
58- if ( ev . source . typeId != "minecraft:player" )
59- return ;
60- if ( ev . itemStack ?. typeId != DEBUG_STICK_ID )
61- return ;
62- ev . cancel = true ;
63- const player = getPlayerByID ( ev . source . id ) ;
64- if ( ! player )
65- return ;
66- if ( player . isSneaking )
67- displayBlockInfo ( player , ev . block ) ;
68- else
69- updateBlockProperty ( player , ev . block ) ;
70- } ) ) ;
7185
7286
7387// Players should not be able to break blocks using
@@ -175,11 +189,19 @@ const record: Record<string, Record<string, string>> = {};
175189 * @param msg The message
176190 * @param player The player to message
177191 */
178- function message ( msg : string , player : Player ) {
179- return player
180- . runCommandAsync (
181- `titleraw @s actionbar {"rawtext":[{"text":${ JSON . stringify ( msg ) } }]}`
182- ) ;
192+ async function message ( msg : string , player : Player ) {
193+ return new Promise ( ( res , rej ) => {
194+ system . run ( ( ) => {
195+ try {
196+ res ( player . runCommand (
197+ `titleraw @s actionbar {"rawtext":[{"text":${ JSON . stringify ( msg ) } }]}`
198+ ) ) ;
199+ }
200+ catch ( e ) {
201+ rej ( e ) ;
202+ }
203+ } ) ;
204+ } ) ;
183205}
184206
185207/**
0 commit comments