Skip to content

Commit de1b017

Browse files
committed
refactor: separate helpers.ts
1 parent 116232e commit de1b017

5 files changed

Lines changed: 215 additions & 154 deletions

File tree

‎config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default {
2626
/**
2727
* The current version.
2828
*/
29-
packVersion: '26.40.0',
29+
packVersion: '26.40.1-alpha.1',
3030

3131
/**
3232
* Minimum Minecraft version required.

‎src/context.ts‎

Lines changed: 5 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -9,106 +9,11 @@
99

1010
import {
1111
Block,
12-
BlockPermutation,
13-
BlockStates,
14-
LiquidType,
1512
Player,
1613
RawMessage
1714
} from '@minecraft/server';
1815

19-
import {
20-
BlockStateSuperset
21-
} from '@minecraft/vanilla-data';
22-
import { defer } from 'utils';
23-
24-
// NOTE: For Java parity, we have to specially handle the 'waterlogged'
25-
// property since it doesn't exist as a block state.
26-
27-
28-
/**
29-
* @type
30-
* Block type identifier.
31-
*/
32-
export type BlockType = string;
33-
34-
/**
35-
* @type
36-
* Block property name.
37-
*/
38-
export type PropName = string;
39-
40-
/**
41-
* @type
42-
* Block property value.
43-
*/
44-
export type PropValue = string | number | boolean;
45-
46-
/**
47-
* @type
48-
* Map of property names and values.
49-
*/
50-
export type PropMap = Record<PropName, PropValue>;
51-
52-
53-
/**
54-
* Creates an entirely new block permutation.
55-
* @param block The reference block.
56-
* @param name The name of the block state to set.
57-
* @param value The value for the state.
58-
* @returns A new BlockPermutation.
59-
*/
60-
export function makeNewPermutation(
61-
block: Block,
62-
name: keyof BlockStateSuperset,
63-
value: string | number | boolean): BlockPermutation
64-
{
65-
return BlockPermutation.resolve(
66-
block.typeId, block.permutation.getAllStates()
67-
).withState(name, value);
68-
}
69-
70-
71-
/**
72-
* Executes a full block state update instead of a simple permutation swap.
73-
* Note: blocks with other components/special NBT should not use this.
74-
* Workaround for MCPE-238217 and gh-24.
75-
* @param block The block to update.
76-
* @param stateName The block state to change.
77-
* @param value The value for the state.
78-
*/
79-
export function fullBlockStateUpdate(
80-
block: Block, stateName: keyof BlockStateSuperset, value: PropValue): void
81-
{
82-
// work around #24
83-
const loc = block.location;
84-
const dim = block.dimension;
85-
const canBeWaterlogged = block.canContainLiquid(LiquidType.Water);
86-
const isWaterlogged = block.isWaterlogged;
87-
const newPermu = makeNewPermutation(block, stateName, value);
88-
89-
dim.setBlockType(loc, 'minecraft:air');
90-
91-
// update the block next tick
92-
defer(() => {
93-
dim.setBlockPermutation(loc, newPermu);
94-
if (canBeWaterlogged)
95-
dim.getBlock(loc)?.setWaterlogged(isWaterlogged);
96-
});
97-
}
98-
99-
100-
/**
101-
* List of blocks requiring {@link fullBlockStateUpdate}.
102-
*/
103-
export const blocksRequiringFullUpdate = new Set([
104-
'minecraft:piston',
105-
'minecraft:sticky_piston',
106-
'minecraft:observer',
107-
'minecraft:powered_repeater',
108-
'minecraft:unpowered_repeater',
109-
'minecraft:powered_comparator',
110-
'minecraft:unpowered_comparator',
111-
]);
16+
import { DebugPropertySelections } from './selection.js';
11217

11318

11419
/**
@@ -127,6 +32,8 @@ export class DebugStickContext<T> {
12732
this.block = block;
12833
this.player = player;
12934
this.event = event;
35+
// TODO: item-bound selection data
36+
this.sels = new DebugPropertySelections(player.id);
13037
}
13138

13239
/**
@@ -144,47 +51,10 @@ export class DebugStickContext<T> {
14451
*/
14552
readonly event: T;
14653

147-
148-
/**
149-
* Returns an array of valid property values for the given property.
150-
* @param prop The property name.
151-
* @returns Valid prop values.
152-
*/
153-
static getPropValidValues(prop: PropName): PropValue[] {
154-
if (prop === 'waterlogged')
155-
return [false, true];
156-
return BlockStates.get(prop).validValues;
157-
}
158-
159-
16054
/**
161-
* Returns all the properties of a block.
162-
* @returns Record<PropName, PropValue>
55+
* Player selections.
16356
*/
164-
getAllProps(): PropMap {
165-
const props = this.block.permutation.getAllStates() || {};
166-
if (this.block.canContainLiquid(LiquidType.Water))
167-
props['waterlogged'] = this.block.isWaterlogged;
168-
return props;
169-
}
170-
171-
172-
/**
173-
* Updates a block's property.
174-
* Note: Can't work in read-only mode; defer execution.
175-
* @param prop The property to change.
176-
* @param value The value to set.
177-
*/
178-
setBlockProp(prop: PropName, value: PropValue): void {
179-
if (prop == 'waterlogged')
180-
this.block.setWaterlogged(value as boolean);
181-
else if (blocksRequiringFullUpdate.has(this.block.typeId))
182-
fullBlockStateUpdate(this.block,
183-
prop as keyof BlockStateSuperset, value);
184-
else
185-
this.block.setPermutation(this.block.permutation.withState(
186-
prop as keyof BlockStateSuperset, value));
187-
}
57+
readonly sels: DebugPropertySelections;
18858

18959

19060
/**

‎src/debug-stick.ts‎

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
* See LICENSE for the full terms.
88
*/
99

10-
import { DebugPropertySelections } from './selection.js';
11-
import { DebugStickContext } from './context.js';
1210
import { 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

1419
import {
1520
PlayerBreakBlockBeforeEvent,
@@ -31,16 +36,15 @@ export const DEBUG_STICK_ID = 'vyt:debug_stick';
3136
export 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:
5357
export 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

Comments
 (0)