@occultus/toolkit 0.22.4 → 0.23.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@occultus/toolkit",
3
- "version": "0.22.4",
3
+ "version": "0.23.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://codeberg.org/TeamOccultus/StarTenonAPI"
@@ -30,7 +30,7 @@
30
30
  "@occultus/common": "0.20.0"
31
31
  },
32
32
  "devDependencies": {
33
- "typedoc": "^0.28.15"
33
+ "typedoc": "^0.28.16"
34
34
  },
35
35
  "scripts": {
36
36
  "test": "tsc"
@@ -173,7 +173,7 @@ export function clearEffect(
173
173
  * 状态效果持续时间,以刻为单位 *(20刻 = 1秒)*
174
174
  *
175
175
  * 其值必须在范围`[0, 20000000]`内
176
- *
176
+ *
177
177
  * 如果值为`"infinite"`,则状态效果将无限期持续
178
178
  * @param options 状态效果选项
179
179
  * @since Starock 0.6.0 (0.1.0)
@@ -304,3 +304,63 @@ export function heal(entity: Entity, amount: number): number {
304
304
  } else health.setCurrentValue(newHealth);
305
305
  return newHealth;
306
306
  }
307
+
308
+ /**
309
+ * 获取实体当前生命值
310
+ * @param entity 要获取生命值的实体
311
+ * @returns
312
+ */
313
+ export function getCurrentHealth(entity: Entity): number {
314
+ const health = entity.getComponent("minecraft:health");
315
+ if (!health)
316
+ throw new OccultusSDKError("The entity does not have health component!");
317
+ return health.currentValue;
318
+ }
319
+
320
+ /**
321
+ * 设置实体当前生命值
322
+ * @param entity 要设置生命值的实体
323
+ * @param value 要设置的生命值,若超出实体最大生命值,则重置为最大生命值
324
+ * @returns
325
+ */
326
+ export function setCurrentHealth(entity: Entity, value: number): number {
327
+ const health = entity.getComponent("minecraft:health");
328
+ if (!health)
329
+ throw new OccultusSDKError("The entity does not have health component!");
330
+ if(value > health.effectiveMax) {
331
+ health.resetToMaxValue();
332
+ return health.effectiveMax;
333
+ }
334
+ health.setCurrentValue(value);
335
+ return value;
336
+ }
337
+
338
+ /**
339
+ * 消耗实体生命值,这一行为不会被视为攻击
340
+ * @param entity
341
+ * @param amount
342
+ * @returns
343
+ */
344
+ export function consumeHealth(entity: Entity, amount: number): number {
345
+ const newHealth = getCurrentHealth(entity) - amount;
346
+ if(newHealth <= 0) {
347
+ entity.kill();
348
+ return 0;
349
+ }
350
+ return setCurrentHealth(entity, getCurrentHealth(entity) - amount);
351
+ }
352
+
353
+ /**
354
+ * 以指定倍率消耗实体的生命值,这一行为不会被视为攻击
355
+ * @param entity
356
+ * @param amplifier 消耗生命的倍率,例如`0.8`将会被视为消耗实体当前 80% 的生命值
357
+ * @returns
358
+ */
359
+ export function consumeHealthAmplifier(entity: Entity, amplifier: number): number {
360
+ const newHealth = getCurrentHealth(entity) - (getCurrentHealth(entity) * amplifier);
361
+ if(newHealth <= 0) {
362
+ entity.kill();
363
+ return 0;
364
+ }
365
+ return setCurrentHealth(entity, newHealth);
366
+ }
@@ -1,4 +1,3 @@
1
-
2
1
  /**
3
2
  * 确保命名空间存在,若不存在则自动加上`minecraft:`命名空间
4
3
  * @param identifier 要检查的标识符
@@ -21,4 +20,4 @@ export function ensureNoNamespace(identifier: string): string {
21
20
  return identifier.split(":")[1];
22
21
  }
23
22
  return identifier;
24
- }
23
+ }
package/src/index.ts CHANGED
@@ -5,4 +5,5 @@ export * from "./entity/EntitiesUtils";
5
5
  export * from "./entity/entityUtils";
6
6
  export * from "./item/item";
7
7
  export * from "./item/container";
8
- export * from "./identifier/namespace";
8
+ export * from "./identifier/namespace";
9
+ export * from "./item/cooldown";
@@ -0,0 +1,26 @@
1
+ import { ItemStack, Player } from "@minecraft/server";
2
+
3
+ /**
4
+ * 返回物品是否处于冷却状态
5
+ * @param item 要检查的物品
6
+ * @param player 持有物品的玩家
7
+ * @return 是否处于冷却状态
8
+ */
9
+ export function isInCooldown(item: ItemStack, player: Player): boolean {
10
+ const cooldown = item.getComponent("minecraft:cooldown");
11
+ if (!cooldown) return false;
12
+ return cooldown.getCooldownTicksRemaining(player) !== 0;
13
+ }
14
+
15
+ /**
16
+ * 让物品开始冷却
17
+ * @param item 要执行冷却操作的物品
18
+ * @param player 持有物品的玩家
19
+ * @return 是否成功执行冷却操作,若物品不支持冷却则返回`false`
20
+ */
21
+ export function startCooldown(item: ItemStack, player: Player): boolean {
22
+ const cooldown = item.getComponent("minecraft:cooldown");
23
+ if (!cooldown) return false;
24
+ cooldown.startCooldown(player);
25
+ return true;
26
+ }