Skip to content

Commit 5d5208c

Browse files
jayly-botJaylyDev
andauthored
add multiple scripts (JaylyDev#89)
* Add kill-death-counter * Add kill-death-counter * Add kill-death-counter * Add kill-death-counter * Add chat-rank * Add chat-rank * Add chat-rank * Add force-show * Add force-show * Add kill-death-counter * Add force-show * Update index.js * Add get-block-data * Add get-block-data * Add get-block-data * Add sleep * Delete index.js from sleep by Jayly#1397 * Add sleep * Add sleep * Add sleep * Add sleep * Add entity-death-event * Add get-item-amount * Add entity-death-event * Add entity-death-event * Add entity-death-event * Delete entity-death-event-test.js from entity-death-event by Jayly#1397 * Add deprecate * Add player-death-event * Update PlayerDeathEvent.js * Update readme.md * eg * deprecate PlayerDeathEvent * revert PlayerExistEvent Co-authored-by: Jayly <[email protected]>
1 parent 06021bf commit 5d5208c

11 files changed

Lines changed: 195 additions & 61 deletions

File tree

scripts/deprecate/deprecate.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Script examples for ScriptAPI
2+
// Author: Jayly#1397 <Jayly Discord>
3+
4+
/**
5+
* @type {string[]}
6+
*/
7+
const emittedIds = [];
8+
/**
9+
* The `deprecate()` method wraps `fn` (which may be a function or class) in
10+
* such a way that it is marked as deprecated.
11+
*
12+
* ```js
13+
* exports.obsoleteFunction = deprecate(() => {
14+
* // Do something here.
15+
* }, 'obsoleteFunction() is deprecated. Use newShinyFunction() instead.');
16+
* ```
17+
*
18+
* When called, `deprecate()` will return a function that will emit a`DeprecationWarning` using the `'warning'` event. The warning will
19+
* be emitted and printed to `stderr` the first time the returned function is
20+
* called. After the warning is emitted, the wrapped function is called without
21+
* emitting a warning.
22+
*
23+
* If the same optional `id` is supplied in multiple calls to `deprecate()`,
24+
* the warning will be emitted only once for that `id`.
25+
*
26+
* ```js
27+
* const fn1 = deprecate(someFunction, someMessage, 'DEP0001');
28+
* const fn2 = deprecate(someOtherFunction, someOtherMessage, 'DEP0001');
29+
* fn1(); // Emits a deprecation warning with id DEP0001
30+
* fn2(); // Does not emit a deprecation warning because it has the same id
31+
* ```
32+
*
33+
*
34+
* @template {Function} T
35+
* @param {T} fn The function that is being deprecated.
36+
* @param {string} msg A warning message to display when the deprecated function is invoked.
37+
* @param {string} [id] A deprecation id. See the `list of deprecated APIs` for a list of ids.
38+
* @return {T} The deprecated function wrapped to emit a warning.
39+
*/
40+
export function deprecate(fn, msg, id) {
41+
let deprecationMessage = "";
42+
if (typeof id === "string") {
43+
if (emittedIds.includes(id)) return fn;
44+
emittedIds.push(id);
45+
deprecationMessage += `[${id}] `;
46+
}
47+
deprecationMessage += `DeprecationWarning: ${msg}`;
48+
49+
function deprecated() {
50+
console.warn(deprecationMessage);
51+
return fn.call(this, arguments);
52+
}
53+
54+
// @ts-ignore
55+
return deprecated;
56+
}
Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,70 @@
1+
// Script examples for ScriptAPI
2+
// Author: Jayly#1397 <Jayly Discord>
3+
4+
import { EntityHealthComponent, EntityHurtEvent, world } from "@minecraft/server";
5+
16
/**
2-
* @license MIT
3-
* @author JaylyMC
7+
* @typedef EntityDeathCallback
8+
* @property {(arg: EntityHurtEvent) => void} callback
9+
* @property {import("@minecraft/server").EntityEventOptions} options
410
*/
5-
import { EntityHealthComponent, world, Entity } from "mojang-minecraft";
6-
711
/**
8-
* Contains information related to an entity death.
12+
* @type {EntityDeathCallback[]}
913
*/
10-
export class EntityDeathEvent {
11-
/**
12-
* @param {string} cause
13-
* @param {number} damage
14-
* @param {Entity} damagingEntity
15-
* @param {Entity} hurtEntity
16-
* @param {Entity} projectile
17-
*/
18-
constructor (cause, damage, damagingEntity, hurtEntity, projectile) {
19-
this.cause = cause;
20-
this.damage = damage;
21-
this.damagingEntity = damagingEntity;
22-
this.deadEntity = hurtEntity;
23-
this.projectile = projectile;
14+
const callbacks = [];
15+
16+
// backend
17+
world.events.entityHurt.subscribe((event) => {
18+
const { hurtEntity } = event;
19+
20+
if (!hurtEntity) return;
21+
22+
/** @type {EntityHealthComponent} */
23+
// @ts-expect-error
24+
const health = hurtEntity.getComponent(EntityHealthComponent.componentId);
25+
26+
if (health.current > 0) return;
27+
28+
for (const callback of callbacks) {
29+
const { entities, entityTypes } = callback.options;
30+
if (entities instanceof Array && !entities?.includes(hurtEntity)) break;
31+
if (entityTypes instanceof Array && !entityTypes?.includes(hurtEntity.typeId)) break;
32+
callback.callback(event);
2433
};
25-
};
34+
});
35+
2636

2737
/**
2838
* Manages callbacks that are connected to when an entity dies.
2939
*/
30-
export class EntityDeathEventSignal {
40+
class EntityDeathEventSignal {
3141
/**
32-
* Subscribe
33-
* @param {(arg: EntityDeathEvent) => void} arg
34-
* @return {(arg: EntityDeathEvent) => void}
42+
* @remarks
43+
* Adds a callback that will be called when an entity dies.
44+
* @param {(arg: EntityHurtEvent) => void} callback
45+
* @param {import("@minecraft/server").EntityEventOptions} options
46+
* @returns {(arg: EntityHurtEvent) => void}
3547
*/
36-
subscribe (arg) {
37-
arg["entityDeath"] = true;
38-
39-
let callback = world.events.entityHurt.subscribe(function ({ cause, damage, damagingEntity, hurtEntity, projectile }) {
40-
if (arg["entityDeath"] !== true) world.events.entityHurt.unsubscribe(callback);
41-
/** @type {EntityHealthComponent} */
42-
// @ts-ignore
43-
const health = hurtEntity.getComponent("health");
44-
45-
if (health.current <= 0) arg(new EntityDeathEvent(cause, damage, damagingEntity, hurtEntity, projectile));
46-
});
47-
48-
return arg;
48+
subscribe(callback, options = {}) {
49+
callbacks.push({ options, callback });
50+
return callback;
4951
};
50-
5152
/**
52-
* Unsubscribe
53-
* @param {(arg: EntityDeathEvent) => void} arg
54-
* @return {void}
53+
* @remarks
54+
* Removes a callback from being called when an entity dies.
55+
* @param {(arg: EntityHurtEvent) => void} callback
56+
* @throws This function can throw errors.
5557
*/
56-
unsubscribe (arg) {
57-
arg["entityDeath"] = false;
58+
unsubscribe(callback) {
59+
const index = callbacks.findIndex((value) => value.callback === callback);
60+
callbacks.splice(index);
5861
};
59-
};
62+
}
63+
64+
const entityDeath = new EntityDeathEventSignal();
65+
66+
entityDeath.subscribe(() => {
67+
console.warn("Hello");
68+
})
69+
70+
export { entityDeath, EntityDeathEventSignal };
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
// Script examples for ScriptAPI
2+
// Author: Jayly#1397 <Jayly Discord>
3+
14
import { EntityDeathEventSignal } from "./index.js";
25

36
const entityDeath = new EntityDeathEventSignal();
47

58
let callback = entityDeath.subscribe((evd) => {
6-
evd.deadEntity.runCommandAsync("say this entity is dead");
9+
evd.hurtEntity.runCommandAsync("say this entity is dead");
710

811
entityDeath.unsubscribe(callback);
912
});

scripts/get-gamemode/index.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
// Script examples for ScriptAPI
2-
// Author: Jayly#1397 <Jayly Discord>
3-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
4-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
5-
if (ar || !(i in from)) {
6-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
7-
ar[i] = from[i];
8-
}
9-
}
10-
return to.concat(ar || Array.prototype.slice.call(from));
11-
};
1+
// Script examples for ScriptAPI
2+
// Author: Jayly#1397 <Jayly Discord>
123
import { GameMode, world } from "@minecraft/server";
134
/**
145
* Gets the Gamemode of a player
156
* @author Smell of Curry
167
* @param {Player} player player to get
17-
* @returns {keyof typeof GameMode}
8+
* @returns {GameMode}
189
* @example if (getGamemode(player) == "creative") return;
1910
*/
2011
export function getGamemode(player) {
21-
return Object.values(GameMode).find(function (g) { return __spreadArray([], world.getPlayers({ name: player.name, gameMode: g }), true).length; });
12+
return Object.values(GameMode).find((g) => [...world.getPlayers({ name: player.name, gameMode: g })].length);
2213
}

scripts/get-item-amount/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Script examples for ScriptAPI
2+
// Author: Herobrine64#3928 <Bedrock Add-Ons>
3+
4+
import { EntityInventoryComponent, Player } from "@minecraft/server";
5+
6+
/**
7+
* Check item amount from a player inventory
8+
* @param {Player} player
9+
* @param {string} itemId
10+
* @param {boolean} clearItems
11+
* @returns {number} item amount
12+
*/
13+
export function checkItemAmount(player, itemId, clearItems = false) {
14+
/**
15+
* @type {EntityInventoryComponent}
16+
*/
17+
// @ts-expect-error
18+
const component = player.getComponent("minecraft:inventory");
19+
const inventory = component.container;
20+
let itemAmount = 0;
21+
for (let i = 0; i < 36; i++) {
22+
let item = inventory.getItem(i);
23+
if (item?.typeId != itemId) continue;
24+
itemAmount += item.amount;
25+
}
26+
if (clearItems) player.runCommandAsync(`clear @s ${itemId}`);
27+
return itemAmount;
28+
}

scripts/player-death-event/PlayerDeathEvent.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
// Script examples for ScriptAPI
2+
// Author: Jayly#1397 <Jayly Discord>
3+
14
/**
25
* @license MIT
36
* @author JaylyMC
47
*/
58
import { EntityHealthComponent, world, Player } from "mojang-minecraft";
9+
import { deprecate } from "deprecate/deprecate";
610

711
/**
812
* Contains information related to an player death.
13+
* @deprecated
914
*/
1015
export class PlayerDeathEvent {
1116
/**
@@ -18,6 +23,7 @@ export class PlayerDeathEvent {
1823

1924
/**
2025
* Manages callbacks that are connected to when an player dies.
26+
* @deprecated
2127
*/
2228
export class PlayerDeathEventSignal {
2329
/**
@@ -68,4 +74,14 @@ export class PlayerDeathEventSignal {
6874
unsubscribe (arg) {
6975
arg["playerDeath"] = false;
7076
};
71-
};
77+
};
78+
79+
PlayerDeathEventSignal.prototype.subscribe = deprecate(
80+
PlayerDeathEventSignal.prototype.subscribe,
81+
'PlayerDeathEvent.subscribe() is deprecated. Use EntityDeathEvent.subscribe() instead.',
82+
);
83+
84+
PlayerDeathEventSignal.prototype.unsubscribe = deprecate(
85+
PlayerDeathEventSignal.prototype.unsubscribe,
86+
'PlayerDeathEvent.unsubscribe() is deprecated. Use EntityDeathEvent.unsubscribe() instead.'
87+
);

scripts/player-death-event/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Deprecated
2+
3+
PlayerDeathEvent class is deprecated. Use [EntityDeathEvent](../entity-death-event) instead.
4+
5+
---
16
This package detects player death event
27

38
# Usage
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Deprecated
2+
3+
player-exist-event is no longer supported, as [PlayerSpawnEvent class](https://learn.microsoft.com/en-us/minecraft/creator/scriptapi/minecraft/server/playerspawnevent) is being added as a native event in @minecraft/server module in Minecraft 1.19.60
4+
5+
---

scripts/player-exist-event/README.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

scripts/player-exist-event/player-exist-event-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MessageFormData } from "mojang-minecraft-ui";
44
let playerExist = new PlayerExistEventSignal();
55

66
let callback = playerExist.subscribe(({player, timeTaken}) => {
7-
player.runCommand("say I just joined this world, and it only took me " + timeTaken + " milliseconds");
7+
player.runCommandAsync("say I just joined this world, and it only took me " + timeTaken + " milliseconds");
88

99
new MessageFormData()
1010
.title("Welcome")

0 commit comments

Comments
 (0)