77 * See LICENSE for the full terms.
88 */
99
10- import { DebugPropertySelections } from './selection.js' ;
11- import { DebugStickContext } from './context.js' ;
1210import { cycleArray , defer , safeCall } from './utils.js' ;
11+ import { DebugStickContext } from './context.js' ;
12+
13+ import {
14+ getAllProps ,
15+ getPropValidValues ,
16+ setBlockProp ,
17+ } from './helpers.js' ;
1318
1419import {
1520 PlayerBreakBlockBeforeEvent ,
@@ -31,16 +36,15 @@ export const DEBUG_STICK_ID = 'vyt:debug_stick';
3136export function changeSelectedProperty ( ctx :
3237 DebugStickContext < PlayerBreakBlockBeforeEvent > )
3338{
34- const props = ctx . getAllProps ( ) ;
39+ const props = getAllProps ( ctx . block ) ;
3540 const propNames = Object . keys ( props ) ;
3641 if ( ! propNames . length )
3742 return ctx . notify ( `${ ctx . block . typeId } has no properties` ) ;
3843
3944 // Cycle through all property names.
40- const sels = new DebugPropertySelections ( ctx . player . id ) ;
41- let currProp = sels . getForBlock ( ctx . block . typeId ) ;
45+ let currProp = ctx . sels . getForBlock ( ctx . block . typeId ) ;
4246 currProp = cycleArray ( propNames , currProp ) ;
43- sels . setForBlock ( ctx . block . typeId , currProp ) ;
47+ ctx . sels . setForBlock ( ctx . block . typeId , currProp ) ;
4448
4549 ctx . notify ( `selected "${ currProp } " (${ props [ currProp ] } )` ) ;
4650}
@@ -53,40 +57,49 @@ export function changeSelectedProperty(ctx:
5357export function updateBlockProperty ( ctx :
5458 DebugStickContext < PlayerInteractWithBlockBeforeEvent > )
5559{
56- const props = ctx . getAllProps ( ) ;
60+ const props = getAllProps ( ctx . block ) ;
5761 const propNames = Object . keys ( props ) ;
5862 if ( ! propNames . length )
5963 return ctx . notify ( `${ ctx . block . typeId } has no properties` ) ;
6064
6165 // Get the currenty selected property.
62- const sels = new DebugPropertySelections ( ctx . player . id ) ;
63- const currProp = sels . getForBlock ( ctx . block . typeId ) ?? propNames [ 0 ] ;
66+ const currProp = ctx . sels . getForBlock ( ctx . block . typeId ) ?? propNames [ 0 ] ;
6467
6568 // Cycle through property values.
66- const validVals = DebugStickContext . getPropValidValues ( currProp ) ;
69+ const validVals = getPropValidValues ( currProp ) ;
6770 const newVal = cycleArray ( validVals , props [ currProp ] ) ;
6871
69- ctx . setBlockProp ( currProp , newVal ) ;
72+ setBlockProp ( ctx . block , currProp , newVal ) ;
7073 ctx . notify ( `"${ currProp } " to ${ newVal } ` ) ;
7174}
7275
7376
7477/**
75- * The block viewer feature
78+ * Create block viewer text.
7679 * @param ctx
80+ * @returns The text.
7781 */
78- export function displayBlockInfo ( ctx :
79- DebugStickContext < PlayerInteractWithBlockBeforeEvent > )
82+ export function genBlockViewerText < K > ( ctx : DebugStickContext < K > )
8083{
8184 const block = ctx . block ;
8285 let info = '§l§b' + block . typeId + '§r' ;
8386
8487 // Basic block info.
88+ // e.g.,
89+ // -123 60 456
90+ // redstone power: 0
8591 info += '\n§4' + block . x + ' §a' + block . y + ' §9' + block . z ;
8692 info += '\n§o§7redstone power§r§8: §c' + ( block . getRedstonePower ( ) ?? 0 ) ;
8793
94+ // Get current property selection on block
95+ // to we can give it a mark.
96+ const currSel = ctx . sels . getForBlock ( ctx . block . typeId ) ;
97+
8898 // The set block states.
89- for ( const [ prop , value ] of Object . entries ( ctx . getAllProps ( ) ) ) {
99+ // e.g.,
100+ // cardinal_direction: north
101+ // waterlogged: false *
102+ for ( const [ prop , value ] of Object . entries ( getAllProps ( ctx . block ) ) ) {
90103 info += '\n§7' + prop + '§r§8: ' ;
91104 switch ( typeof value ) {
92105 case 'string' : info += '§e' ; break ;
@@ -95,11 +108,26 @@ export function displayBlockInfo(ctx:
95108 default : info += '§8' ;
96109 }
97110 info += value ;
111+ if ( currSel == prop )
112+ info += ' §c*' ;
98113 } ;
99114
100- // Additional block tags.
115+ // Additional block tags
116+ // e.g.,
117+ // #wood
101118 block . getTags ( ) . forEach ( v => info += '\n§d#' + v ) ;
119+ return info ;
120+ }
121+
102122
123+ /**
124+ * Display block info to player's action bar.
125+ * @param ctx
126+ */
127+ export function displayBlockInfo ( ctx :
128+ DebugStickContext < PlayerInteractWithBlockBeforeEvent > )
129+ {
130+ const info = genBlockViewerText ( ctx ) ;
103131 ctx . notify ( info ) ;
104132}
105133
@@ -129,17 +157,20 @@ export function enableDebugStick() {
129157 return ;
130158 isEnabled = true ;
131159
160+
132161 // Short tap/click triggers.
133162 blockInteractListener = world . beforeEvents
134163 . playerInteractWithBlock . subscribe ( ev =>
135164 {
136165 if ( ev . itemStack ?. typeId != DEBUG_STICK_ID )
137166 return ;
138167 ev . cancel = true ;
168+
139169 const ctx = new DebugStickContext ( ev . block , ev . player , ev ) ;
170+
140171 defer ( ( ) => {
141172 let isError , result ;
142- if ( ev . player . isSneaking )
173+ if ( ev . player . isSneaking ) // show block viewer
143174 [ isError , result ] = safeCall ( displayBlockInfo , ctx ) ;
144175 else
145176 [ isError , result ] = safeCall ( updateBlockProperty , ctx ) ;
@@ -148,14 +179,17 @@ export function enableDebugStick() {
148179 } ) ;
149180 } ) ;
150181
182+
151183 // Long press/block break triggers.
152184 breakBlockListener = world . beforeEvents
153185 . playerBreakBlock . subscribe ( ev =>
154186 {
155187 if ( ev . itemStack ?. typeId != DEBUG_STICK_ID )
156188 return ;
157189 ev . cancel = true ;
190+
158191 const ctx = new DebugStickContext ( ev . block , ev . player , ev ) ;
192+
159193 defer ( ( ) => {
160194 let [ isError , result ] = safeCall ( changeSelectedProperty , ctx ) ;
161195 if ( isError )
0 commit comments