Skip to content

Commit 03a3949

Browse files
committed
refactor: the script code
1 parent 87419d5 commit 03a3949

File tree

9 files changed

+386
-261
lines changed

9 files changed

+386
-261
lines changed
File renamed without changes.

pack/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023-2025 Vincent Yanzee J. Tan
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

pack/README

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This is the core of the debug stick add-on for Minecraft: Bedrock Edition.
2+
3+
Official links:
4+
- CurseForge: https://www.curseforge.com/minecraft-bedrock/addons/debug-stick
5+
- MCPEDL: https://mcpedl.com/debug-stick
6+
- GitHub: https://github.com/vytdev/debug-stick
7+
8+
Last updated: December 21, 2025
9+
10+
Copyright (c) 2023-2025 Vincent Yanzee J. Tan <https://vytdev.github.io>
11+
This project is licensed under the MIT License.
12+
This software is provided "as is" without warranty of any kind.
13+
See LICENSE for the full terms.

src/handlers.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*!
2+
* Debug Stick -- A Bedrock port of the debug stick tool from Java Edition.
3+
* Copyright (c) 2023-2025 Vincent Yanzee J. Tan <https://vytdev.github.io>
4+
*
5+
* This project is licensed under the MIT License.
6+
* This software is provided "as is" without warranty of any kind.
7+
* See LICENSE for the full terms.
8+
*/
9+
10+
import { Block, Player } from '@minecraft/server';
11+
import { message } from './utils.js';
12+
import { getStatesOfBlock, getStateValidValues, setBlockState
13+
} from './state.js';
14+
import { DebugStateSelections } from './selection.js';
15+
16+
17+
/**
18+
* Change the selected block state.
19+
* @param player The player who initiated the change.
20+
* @param block The block the player is working with.
21+
* @param item The debug stick item used.
22+
*/
23+
export function changeSelectedProperty(player: Player, block: Block) {
24+
const states = getStatesOfBlock(block);
25+
const stateNames = Object.keys(states);
26+
if (!stateNames.length)
27+
return message(`${block.typeId} has no properties`, player);
28+
29+
// Cycle through the possible states.
30+
const selections = new DebugStateSelections(player.id);
31+
let currState = selections.getSelectedStateForBlock(block.typeId);
32+
currState = stateNames[(stateNames.indexOf(currState) + 1)
33+
% stateNames.length];
34+
selections.setSelectedStateForBlock(block.typeId, currState);
35+
36+
message(`selected "${currState}" (${states[currState]})`, player);
37+
}
38+
39+
40+
/**
41+
* Cycle the state value of the selected state on a block.
42+
* @param player The player who initiated the cycle.
43+
* @param block The block to update with the debug stick.
44+
* @param item The debug stick item used.
45+
*/
46+
export function updateBlockProperty(player: Player, block: Block) {
47+
const states = getStatesOfBlock(block);
48+
const stateNames = Object.keys(states);
49+
if (!stateNames.length)
50+
return message(`${block.typeId} has no properties`, player);
51+
52+
// Get the currently selected state.
53+
const selections = new DebugStateSelections(player.id);
54+
const currState = selections.getSelectedStateForBlock(block.typeId)
55+
?? stateNames[0];
56+
57+
// Cycle through valid state values.
58+
const valids = getStateValidValues(currState);
59+
const value = valids[(valids.indexOf(states[currState]) + 1) % valids.length];
60+
61+
setBlockState(block, currState, value)
62+
.then(() => message(`"${currState}" to ${value}`, player));
63+
}
64+
65+
66+
/**
67+
* The block viewer feature
68+
* @param player
69+
* @param block
70+
*/
71+
export function displayBlockInfo(player: Player, block: Block) {
72+
let info = '§l§b' + block.typeId + '§r';
73+
74+
// Basic block info.
75+
info += '\n§4' + block.x + ' §a' + block.y + ' §9' + block.z;
76+
info += '\n§7matter state§8: §e';
77+
if (block.isLiquid) info += 'liquid';
78+
else if (block.isAir) info += 'gas';
79+
else info += 'solid';
80+
//info += '\n§7hard block§8: ' + (block.isSolid ? '§ayes' : '§cno');
81+
info += '\n§7redstone power§8: §c' + (block.getRedstonePower() ?? 0);
82+
83+
// The set block states.
84+
for (const [stateName, value] of Object.entries(getStatesOfBlock(block))) {
85+
info += '\n§o§7' + stateName + '§r§8: ';
86+
switch (typeof value) {
87+
case 'string': info += '§e'; break;
88+
case 'number': info += '§3'; break;
89+
case 'boolean': info += '§6'; break;
90+
default: info += '§8';
91+
}
92+
info += value;
93+
};
94+
95+
// Additional block tags.
96+
block.getTags().forEach(v => info += '\n§d#' + v);
97+
98+
message(info, player);
99+
}

0 commit comments

Comments
 (0)