Skip to content

Commit 7d05627

Browse files
committed
use playerInteractWithBlock instead of itemUseOn
1 parent 604945b commit 7d05627

File tree

2 files changed

+11
-46
lines changed

2 files changed

+11
-46
lines changed

pack/items/debug_stick.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
},
1212

1313
"components": {
14-
"vyt:debug_stick": {},
1514
"minecraft:icon": "stick",
1615
"minecraft:hand_equipped": true,
1716
"minecraft:display_name": { "value": "§dDebug Stick§r" },

src/index.ts

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
+* MCPEDL: https://mcpedl.com/debug-stick
77
+* GitHub: https://github.com/vytdev/debug-stick
88
+*
9-
+* Script last updated: July 22, 2025
9+
+* Script last updated: July 27, 2025
1010
+*
1111
+* Copyright (c) 2023-2025 VYT <https://vytdev.github.io>
1212
+* This project is licensed under the MIT License.
@@ -20,9 +20,7 @@ import {
2020
BlockStates,
2121
LiquidType,
2222
world,
23-
system,
24-
ItemCustomComponent,
25-
ItemComponentUseOnEvent,
23+
system
2624
} from "@minecraft/server";
2725

2826
import {
@@ -40,31 +38,16 @@ type BlockStateValue = boolean | number | string;
4038
const DEBUG_STICK_ID = "vyt:debug_stick";
4139

4240

41+
world.beforeEvents.playerInteractWithBlock.subscribe(safeCallWrapper((ev) => {
42+
if (ev.itemStack?.typeId != DEBUG_STICK_ID)
43+
return;
44+
ev.cancel = true;
45+
if (ev.player.isSneaking)
46+
displayBlockInfo(ev.player, ev.block);
47+
else
48+
updateBlockProperty(ev.player, ev.block);
49+
}));
4350

44-
class DebugStickEvents implements ItemCustomComponent {
45-
46-
constructor() {
47-
this.onUseOn = safeCallWrapper(this.onUseOn).bind(this);
48-
}
49-
50-
onUseOn(ev: ItemComponentUseOnEvent) {
51-
if (ev.source.typeId != "minecraft:player")
52-
return;
53-
const player = getPlayerByID(ev.source.id);
54-
if (!player)
55-
return;
56-
if (player.isSneaking)
57-
displayBlockInfo(player, ev.block);
58-
else
59-
updateBlockProperty(player, ev.block);
60-
}
61-
}
62-
63-
64-
// Players should not be able to break blocks using
65-
// the debug stick in survival
66-
//
67-
// TODO: explore other alternatives
6851
world.beforeEvents.playerBreakBlock.subscribe(safeCallWrapper((ev) => {
6952
if (ev.itemStack?.typeId != DEBUG_STICK_ID)
7053
return;
@@ -73,12 +56,6 @@ world.beforeEvents.playerBreakBlock.subscribe(safeCallWrapper((ev) => {
7356
}));
7457

7558

76-
system.beforeEvents.startup.subscribe((ev) => {
77-
ev.itemComponentRegistry.registerCustomComponent(
78-
DEBUG_STICK_ID, new DebugStickEvents());
79-
});
80-
81-
8259
/*============================================================================*\
8360
+* Action functions
8461
\*============================================================================*/
@@ -188,17 +165,6 @@ async function message(msg: string, player: Player) {
188165
});
189166
}
190167

191-
/**
192-
* Utility function to return a player class from an entity ID
193-
* @param id The entity ID
194-
* @returns Player instance or undefined
195-
*/
196-
function getPlayerByID(id: string): Player | undefined {
197-
return world
198-
.getAllPlayers()
199-
.find(v => v.id == id);
200-
}
201-
202168
/**
203169
* Returns all the block states of a block.
204170
* @param block The block.

0 commit comments

Comments
 (0)