Skip to content

Commit

Permalink
bump to 1.5.0 (mc 1.20.60+; api 1.9.0-beta)
Browse files Browse the repository at this point in the history
  • Loading branch information
vytdev committed Feb 8, 2024
1 parent 2a17101 commit 2510294
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 35 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<div align="center">

![icon](src/pack_icon.png)

# Debug Stick

This add-on implements a debug stick based on Minecraft: Java Edition.

</div>

> [*Now available on MCPEDL!*](https://mcpedl.com/debug-stick/)
### Features
Expand Down
14 changes: 7 additions & 7 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"format_version": 2,

"header": {
"description": "Java Debug Stick on Minecraft: Bedrock Edition. By §bvytdev§r",
"min_engine_version": [ 1, 20, 50 ],
"name": "Debug Stick",
"description": "Java Debug Stick on Minecraft: Bedrock Edition. By §bvytdev§r",
"uuid": "21aadfa6-e27c-400c-c596-596021852939",
"version": "1.4.0"
"version": "1.5.0",
"min_engine_version": [ 1, 20, 60 ]
},

"modules": [
Expand All @@ -18,18 +18,18 @@
},
{
"description": "scripting",
"entry": "scripts/index.js",
"language": "javascript",
"type": "script",
"language": "javascript",
"entry": "scripts/index.js",
"uuid": "86c7bab4-aed9-4297-5f0c-d5d62bd30be1",
"version": [ 1, 0, 0]
"version": [ 1, 0, 0 ]
}
],

"dependencies": [
{
"module_name": "@minecraft/server",
"version": "1.8.0-beta"
"version": "1.9.0-beta"
}
],

Expand Down
95 changes: 67 additions & 28 deletions src/scripts/index.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,141 @@
/* ================================================================
This piece of file makes it possible for the debug stick to work
================================================================ */
/*============================================================================*\
+*
+* This is the core of the debug stick add-on for Minecraft: Bedrock Edition
+*
+* Official links:
+* MCPEDL: https://mcpedl.com/debug-stick
+* GitHub: https://github.com/vytdev/debug-stick
+*
+* Script last updated: 1.5.0 (r1.20.60)
+*
+* Copyright (c) 2023-2024 VYT <https://vytdev.github.io>
+* This project is licensed under the MIT License.
+* See LICENSE for more details.
+*
\*============================================================================*/

import { BlockStates, world, system } from "@minecraft/server";

// record of: player id -> selected property name
const record = {};

// show message to player's actionbar
function message(msg, player) {
return player.runCommandAsync(`title @s actionbar ${msg}`);
}

// listen for interaction with blocks
// change selected property
world.afterEvents.entityHitBlock.subscribe((ev) => {
// check for player
if (ev.damagingEntity.typeId != "minecraft:player")
return;
if (ev.damagingEntity.typeId != "minecraft:player") return;

// the player
const player = world.getAllPlayers().find(v => v.id == ev.damagingEntity.id);
const heldItem = player.getComponent("minecraft:inventory")
.container.getItem(player.selectedSlot);
if (heldItem?.typeId != "vyt:debug_stick")
return;
// block permutation

// check if player holds the debug stick
if (player.getComponent("minecraft:inventory").container
.getItem(player.selectedSlot)?.typeId != "vyt:debug_stick"
) return;

// block and its permutation
const block = ev.hitBlock;
const permutation = block.permutation;
// get next state

// get a list of allowed states for block
const states = permutation.getAllStates();
const names = Object.keys(states);
// check block

// check if block has any property to change
if (!names.length && !block.type.canBeWaterlogged)
return message(`${block.typeId} has no properties`, player);
// retrieve state

// get the next property name
let prop = names[names.indexOf(record[player.id]) + 1];
let val = states[prop];
// loop through properties

// prop is undefined, it means we reached the end of the property list
if (!prop) {
// waterlogged property available
// check if waterlog property is available
if (block.type.canBeWaterlogged) {
prop = "waterlogged";
val = block.isWaterlogged;
}
// reset to 0
// else, use the first property instead
else {
prop = names[0];
val = states[prop];
}
}
// update record

// update the player's record
record[player.id] = prop;
// send message

// send response message
message(`selected "${prop}" (${val})`, player);
});

// listen for clicks in blocks
// sets the state value for the property
world.beforeEvents.itemUseOn.subscribe((ev) => {
// check for player and his held item
if (ev.source.typeId != "minecraft:player" || ev.itemStack?.typeId != "vyt:debug_stick")
return;

// cancel event behaviour
ev.cancel = true;

// the player
const player = world.getAllPlayers().find(v => v.id == ev.source.id);

// block permutation
const block = ev.block;
const permutation = block.permutation;

// get all block states/properties
const states = permutation.getAllStates();
const names = Object.keys(states);

// check block
if (!names.length && !block.type.canBeWaterlogged)
return message(`${block.typeId} has no properties`, player);

// retrieve property name
let prop = record[player.id];
let val;
// check if recorded state are available in the block

// check if recorded state is available on the block
if (prop == "waterlogged" ? !block.type.canBeWaterlogged : !names.includes(prop))
prop = names[0];
if (!prop && block.type.canBeWaterlogged)
prop = "waterlogged";
// toggle waterlogged
// change the waterlog property instead
if (!prop && block.type.canBeWaterlogged) prop = "waterlogged";

// toggle waterlog
if (prop == "waterlogged") {
val = !block.isWaterlogged;
// delayed because we're in read-only mode
system.run(() => {
block.isWaterlogged = val;
block.setWaterlogged(val);
});
}
// a property

// it is a property
else {
// get the valid property values
const valids = BlockStates.get(prop).validValues;
val = valids[valids.indexOf(states[prop]) + 1];
// loop through values
if (typeof val === "undefined")
val = valids[0];

// val is undefined, set to the first value again
if (typeof val === "undefined") val = valids[0];

// update property
system.run(() => {
block.setPermutation(permutation.withState(prop, val));
});
}

// update record
record[player.id] = prop;
// send message
// send response message
message(`"${prop}" to ${val}`, player);
});

0 comments on commit 2510294

Please sign in to comment.