forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (73 loc) · 2.2 KB
/
index.js
File metadata and controls
77 lines (73 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Script example for ScriptAPI
// Author: Jayly <https://github.com/JaylyDev>
// Project: https://github.com/JaylyDev/ScriptAPI
import { Block, Entity, EntityHitEntityAfterEvent, Player, world } from "@minecraft/server";
/**
* @implements {Omit<EntityHitEntityAfterEvent, "damagingEntity">}
*/
export class EntityCriticalHitAfterEvent {
/**
* @param {Entity} damagingEntity
* @param {Entity} hitEntity
*/
constructor(damagingEntity, hitEntity) {
if (!(damagingEntity instanceof Player)) {
console.log("Failed to call function EntityCriticalHitAfterEvent, damagingEntity is not a player");
return;
};
this.player = damagingEntity;
this.hitEntity = hitEntity;
};
/**
* @remarks
* Entity that made a hit/melee attack.
* @readonly
* @type {Player}
*/
player;
/**
* @remarks
* Entity that was hit by the attack, or undefined if the hit
* attack did not hit an damagingEntity. If both hitEntity and hitBlock
* are undefined, then the damagingEntity basically swiped into the
* air.
* @readonly
* @type {Entity | undefined}
*/
hitEntity;
};
export class EntityCriticalHitAfterEventSignal {
/**
* @private
* @readonly
* @type {((event: EntityCriticalHitAfterEvent) => void)[]}
*/
handlers = [];
/**
* @param {(event: EntityCriticalHitAfterEvent) => void} callback
*/
subscribe(callback) {
this.handlers.push(callback);
return callback;
}
/**
* @param {(event: EntityCriticalHitAfterEvent) => void} callback
*/
unsubscribe(callback) {
this.handlers.splice(this.handlers.findIndex(v => v === callback));
}
/**
* @private
* @param {EntityHitEntityAfterEvent} event
*/
trigger(event) {
this.handlers.forEach((callback) => callback(new EntityCriticalHitAfterEvent(event.damagingEntity, event.hitEntity)));
}
constructor() {
world.afterEvents.entityHitEntity.subscribe((event) => {
// checks if damagingEntity has a critical hit and is a player
if (event.damagingEntity instanceof Player && !!event.hitEntity && event.damagingEntity.getVelocity().y !== 0) this.trigger(event);
});
}
}
export const damagingEntityCriticalHit = new EntityCriticalHitAfterEventSignal();