|
1 | 1 | // Script example for ScriptAPI |
2 | 2 | // Author: JaylyMC <https://github.com/JaylyDev> |
3 | 3 | // Project: https://github.com/JaylyDev/ScriptAPI |
4 | | - |
5 | | -import { Enchantment, Entity, EntityInventoryComponent, ItemEnchantsComponent, ItemStack, system, TicksPerSecond, world } from '@minecraft/server'; |
6 | | -const tickInterval = TicksPerSecond; |
| 4 | +import { EntityInventoryComponent, ItemEnchantableComponent, system, TicksPerSecond, world } from '@minecraft/server'; |
| 5 | +/** |
| 6 | + * Represents an event indicating incompatible enchantments on an item. |
| 7 | + */ |
7 | 8 | class IncompatibleEnchantmentAlertEvent { |
| 9 | + /** |
| 10 | + * Creates a new instance of IncompatibleEnchantmentAlertEvent. |
| 11 | + * @param {boolean} exceedMaxLevel - Indicates whether the enchantment exceeds its maximum level. |
| 12 | + * @param {boolean} incompatibleEnchantmentType - Indicates whether the enchantment type is incompatible. |
| 13 | + * @param {Enchantment} enchantment - The enchantment causing the alert. |
| 14 | + * @param {ItemStack} item - The item with the incompatible enchantment. |
| 15 | + * @param {Entity} source - The entity triggering the alert. |
| 16 | + */ |
8 | 17 | constructor(exceedMaxLevel, incompatibleEnchantmentType, enchantment, item, source) { |
9 | | - /** |
10 | | - * @type {boolean} |
11 | | - */ |
12 | 18 | this.exceedMaxLevel = exceedMaxLevel; |
13 | | - /** |
14 | | - * @type {boolean} |
15 | | - */ |
16 | 19 | this.incompatibleEnchantmentType = incompatibleEnchantmentType; |
17 | | - /** |
18 | | - * @type {Enchantment} |
19 | | - */ |
20 | 20 | this.enchantment = enchantment; |
21 | | - /** |
22 | | - * @type {ItemStack} |
23 | | - */ |
24 | 21 | this.item = item; |
25 | | - /** |
26 | | - * @type {Entity} |
27 | | - */ |
28 | 22 | this.source = source; |
29 | 23 | } |
30 | 24 | ; |
31 | 25 | } |
32 | 26 | ; |
| 27 | +/** |
| 28 | + * Signal class for subscribing to events related to incompatible enchantments. |
| 29 | + */ |
33 | 30 | class IncompatibleEnchantmentAlertEventSignal { |
34 | 31 | /** |
35 | | - * @param {(arg0: IncompatibleEnchantmentAlertEvent) => void} callback |
36 | | - */ |
| 32 | + * Subscribes to the incompatible enchantment alert event and specifies a callback function. |
| 33 | + * @param {(arg0: IncompatibleEnchantmentAlertEvent) => void} callback - The callback function to be invoked when an alert occurs. |
| 34 | + * Accepts a single parameter of type IncompatibleEnchantmentAlertEvent. |
| 35 | + * @returns {number} - An identifier for the subscription, which can be used for unsubscribing. |
| 36 | + */ |
37 | 37 | subscribe(callback) { |
38 | 38 | return system.runInterval(function () { |
39 | 39 | for (const player of world.getAllPlayers()) { |
40 | | - /** |
41 | | - * @type {EntityInventoryComponent} |
42 | | - */ |
43 | | - // @ts-ignore |
44 | 40 | const inventory = player.getComponent(EntityInventoryComponent.componentId); |
45 | 41 | for (let index = 0; index < inventory.container.size; index++) { |
46 | 42 | const item = inventory.container.getItem(index); |
47 | | - /** @type {ItemEnchantsComponent} */ |
48 | | - // @ts-ignore |
49 | | - const enchantments = item === null || item === void 0 ? void 0 : item.getComponent(ItemEnchantsComponent.componentId); |
50 | | - if (!item || !enchantments) |
| 43 | + if (!item) |
51 | 44 | continue; |
52 | | - for (const enchantment of enchantments.enchantments) { |
53 | | - const enchantmentIsIncompatible = enchantments.enchantments.canAddEnchantment(new Enchantment(enchantment.type)) === false; |
| 45 | + const enchantable = item.getComponent(ItemEnchantableComponent.componentId); |
| 46 | + for (const enchantment of enchantable.getEnchantments()) { |
| 47 | + const enchantmentIsIncompatible = enchantable.canAddEnchantment(enchantment) === false; |
| 48 | + if (typeof enchantment.type !== 'object') |
| 49 | + continue; |
54 | 50 | const enchantmentExcceedsMaxLevel = enchantment.level > enchantment.type.maxLevel; |
55 | 51 | if (!enchantmentIsIncompatible && !enchantmentExcceedsMaxLevel) |
56 | 52 | continue; |
57 | 53 | callback(new IncompatibleEnchantmentAlertEvent(enchantmentExcceedsMaxLevel, enchantmentIsIncompatible, enchantment, item, player)); |
58 | 54 | } |
59 | 55 | } |
60 | 56 | } |
61 | | - }, tickInterval); |
| 57 | + }, TicksPerSecond); |
62 | 58 | } |
63 | 59 | ; |
| 60 | + /** |
| 61 | + * Unsubscribes from the incompatible enchantment alert event using the provided subscription identifier. |
| 62 | + * @param {number} id - The identifier of the subscription to be removed. |
| 63 | + */ |
64 | 64 | unsubscribe(id) { |
65 | 65 | system.clearRun(id); |
66 | 66 | } |
67 | 67 | ; |
68 | 68 | } |
69 | 69 | ; |
| 70 | +/** |
| 71 | + * Global instance of IncompatibleEnchantmentAlertEventSignal for easy access and usage. |
| 72 | + * @type {IncompatibleEnchantmentAlertEventSignal} |
| 73 | + */ |
70 | 74 | export const incompatibleEnchantment = new IncompatibleEnchantmentAlertEventSignal(); |
0 commit comments