|
| 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