-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstate.ts
More file actions
65 lines (57 loc) · 1.92 KB
/
state.ts
File metadata and controls
65 lines (57 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*!
* Debug Stick -- A Bedrock port of the debug stick tool from Java Edition.
* Copyright (c) 2023-2026 Vincent Yanzee J. Tan <https://vytdev.github.io>
*
* This project is licensed under the MIT License.
* This software is provided "as is" without warranty of any kind.
* See LICENSE for the full terms.
*/
import { Block, BlockStates, LiquidType, system } from '@minecraft/server';
import { BlockStateSuperset } from '@minecraft/vanilla-data';
import type { StateName, StateValue } from './types';
// NOTE: For Java parity, we have to specially handle the 'waterlogged'
// property until it's alright :]
/**
* Returns an array of valid block state values for the given block state.
* @param stateName The block state name.
* @returns Valid block state values.
*/
export function getStateValidValues(stateName: StateName): StateValue[] {
if (stateName === 'waterlogged')
return [false, true];
return BlockStates.get(stateName).validValues;
}
/**
* Returns all the block states of a block.
* @param block The block.
* @returns Record<StateName, StateValue>
*/
export function getStatesOfBlock(block: Block): Record<StateName, StateValue> {
const states = block.permutation.getAllStates() || {};
if (block.canContainLiquid(LiquidType.Water))
states['waterlogged'] = block.isWaterlogged;
return states;
}
/**
* Set a block's state.
* @param block The block to update.
* @param stateName The name of the state to change.
* @param value The value to set.
* @returns Promise
*/
export function setBlockState(block: Block, stateName: StateName,
value: StateValue): Promise<void> {
return new Promise((res, rej) => system.run(() => {
try {
if (stateName == 'waterlogged')
block.setWaterlogged(value as boolean);
else
block.setPermutation(block.permutation.withState(
stateName as keyof BlockStateSuperset, value));
res();
}
catch (e) {
rej(e);
}
}));
}