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 } ;
0 commit comments