Skip to content

Commit fc2f2cd

Browse files
authored
Add task (JaylyDev#91)
* Add jayly-task * Add jayly-task * Add has-lore * Add has-lore * Add has-lore * Update index.js * Update index.js * Add has-lore * Add chat-command * Update index.js
1 parent c2bfc34 commit fc2f2cd

5 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Script examples for ScriptAPI
2+
// Author: 𝖘𝖆𝖓 𝕵𝖚𝖌𝖘#9251 <Bedrock Add-Ons>
3+
4+
import * as Minecraft from "@minecraft/server";
5+
6+
let prefix = "!";
7+
8+
Minecraft.world.events.beforeChat.subscribe((data) => {
9+
if (data.message.toLowerCase().startsWith(`${prefix}help`)) {
10+
data.cancel = true;
11+
data.sender.runCommandAsync(`tellraw @s {"rawtext":[{"text":"hello"}]}`);
12+
}
13+
});

scripts/has-lore/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Script examples for ScriptAPI
2+
// Author: Andrew2005#8409 <Bedrock Add-Ons>
3+
// Remember M9#8416 <Bedrock Add-Ons>
4+
// ! MP09#1650 <Bedrock Add-Ons>
5+
6+
import { ItemStack } from '@minecraft/server';
7+
/**
8+
* @param {ItemStack} item
9+
*/
10+
export function hasLore (item) {
11+
return item.getLore().length > 0
12+
}

scripts/has-lore/tests.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Script examples for ScriptAPI
2+
// Author: Andrew2005#8409 <Bedrock Add-Ons>
3+
4+
import { hasLore } from './index';
5+
import { world } from '@minecraft/server';
6+
7+
world.events.beforeItemUse.subscribe(({item}) => {
8+
if (hasLore(item)) world.say("Item has lore!");
9+
});

scripts/jayly-task/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Script examples for ScriptAPI
2+
// Author: Jayly#1397 <Jayly Discord>
3+
4+
import { MinecraftDimensionTypes, system, world } from "@minecraft/server";
5+
6+
/**
7+
* @param {string} taskName
8+
* @param {string[]} commands
9+
* @param {boolean} loop
10+
*/
11+
export function task (taskName, commands, loop = false) {
12+
if (typeof taskName !== 'string' || typeof loop !== 'boolean') throw new TypeError('Native type conversion failed.');
13+
if (commands.filter((value) => typeof value !== 'string').length > 0) throw new TypeError('Native variant type conversion failed.');
14+
15+
let id = system.runSchedule(async function runCommandAsync () {
16+
let line = 0;
17+
system.clearRunSchedule(id);
18+
19+
try {
20+
for (const command of commands) {
21+
line++;
22+
await world.getDimension(MinecraftDimensionTypes.overworld)
23+
.runCommandAsync(command);
24+
};
25+
if (loop) id = system.runSchedule(runCommandAsync);
26+
} catch (reason) {
27+
console.warn(`Task ${taskName} failed to load correctly with error(s):`);
28+
console.warn(`Error on line ${line}: command failed to parse with error '${reason}'`);
29+
};
30+
});
31+
};

scripts/jayly-task/tests.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Script examples for ScriptAPI
2+
// Author: Jayly#1397 <Jayly Discord>
3+
4+
import { task } from "./index";
5+
6+
task(
7+
"test1",
8+
["say hello", 'tellraw @a {"rawtext":[{"text":"tellraw"}]}', "scoreboard objectives add test dummy"],
9+
false
10+
);
11+
task(
12+
"test1",
13+
["say loop", 'tellraw @a {"rawtext":[{"text":"loop 1"}]}', "execute as @a run say loop 2"],
14+
true
15+
);

0 commit comments

Comments
 (0)