@occultus/toolkit 0.22.1 → 0.22.3

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/README.md CHANGED
@@ -28,4 +28,3 @@ The above copyright notice and this permission notice shall be included in all c
28
28
 
29
29
  THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
30
  ```
31
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@occultus/toolkit",
3
- "version": "0.22.1",
3
+ "version": "0.22.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://codeberg.org/TeamOccultus/StarTenonAPI"
@@ -9,7 +9,13 @@ import {
9
9
  ItemStack
10
10
  } from "@minecraft/server";
11
11
  import { EffectGroups, EffectData } from "@occultus/common";
12
- import { addEffect, applyEffectData, clearSlot, giveItem } from "./entityUtils";
12
+ import {
13
+ addEffect,
14
+ applyEffectData,
15
+ clearSlot,
16
+ giveItem,
17
+ heal
18
+ } from "./entityUtils";
13
19
 
14
20
  /**
15
21
  * 适用于批量实体的相关工具
@@ -103,4 +109,13 @@ export class EntitiesUtils {
103
109
  applyEffectData(data: EffectData | EffectData[]): void {
104
110
  return this.query().forEach((entity) => applyEffectData(entity, data));
105
111
  }
112
+ /**
113
+ * 恢复实体的生命值
114
+ * @param amount 恢复的生命值,若治愈后生命值超过最大生命值,则重置为最大生命值
115
+ * @return 治愈后的生命值
116
+ * @throws 如果实体没有`minecraft:health`组件,则抛出`OccultusSDKError`
117
+ */
118
+ heal(amount: number) {
119
+ this.query().forEach((entity) => heal(entity, amount));
120
+ }
106
121
  }
@@ -5,7 +5,6 @@ import {
5
5
  Entity,
6
6
  EntityComponentTypes,
7
7
  EntityEffectOptions,
8
- EntityEquippableComponent,
9
8
  EquipmentSlot,
10
9
  ItemStack,
11
10
  Player
@@ -173,19 +172,27 @@ export function clearEffect(
173
172
  * 状态效果持续时间,以刻为单位 *(20刻 = 1秒)*
174
173
  *
175
174
  * 其值必须在范围`[0, 20000000]`内
175
+ *
176
+ * 如果值为`"infinite"`,则状态效果将无限期持续
176
177
  * @param options 状态效果选项
177
178
  * @since Starock 0.6.0 (0.1.0)
178
179
  */
179
180
  export function addEffect(
180
181
  entity: Entity,
181
182
  effectType: EffectType | EffectType[] | string | string[] | EffectGroups,
182
- duration: number,
183
+ duration: number | "infinite",
183
184
  options?: EntityEffectOptions
184
185
  ): void {
185
186
  const effects =
186
187
  effectGroupMap[effectType as EffectGroups] ||
187
188
  (Array.isArray(effectType) ? effectType : [effectType]);
188
189
  for (const effect of effects) {
190
+ if (duration === "infinite") {
191
+ entity.runCommand(
192
+ `effect @s ${effect} infinite ${options?.amplifier ?? 0} ${options?.showParticles ?? false}`
193
+ );
194
+ return;
195
+ }
189
196
  entity.addEffect(effect, duration, options);
190
197
  }
191
198
  }
@@ -277,3 +284,22 @@ export function consumeEquipmentAmount(
277
284
  if (!item) return false;
278
285
  return setEquipmentItem(player, consumeAmount(item, amount));
279
286
  }
287
+
288
+ /**
289
+ * 恢复实体的生命值
290
+ * @param entity 将要被治疗的实体
291
+ * @param amount 恢复的生命值,若治愈后生命值超过最大生命值,则重置为最大生命值
292
+ * @return 治愈后的生命值
293
+ * @throws 如果实体没有`minecraft:health`组件,则抛出`OccultusSDKError`
294
+ */
295
+ export function heal(entity: Entity, amount: number): number {
296
+ const health = entity.getComponent("minecraft:health");
297
+ if (!health)
298
+ throw new OccultusSDKError("The entity does not have health component!");
299
+ const newHealth = health.currentValue + amount;
300
+ if (newHealth > health.effectiveMax) {
301
+ health.resetToMaxValue();
302
+ return health.effectiveMax;
303
+ } else health.setCurrentValue(newHealth);
304
+ return newHealth;
305
+ }
@@ -1,4 +1,9 @@
1
- import { Container, ItemStack } from "@minecraft/server";
1
+ import { Container, EquipmentSlot, ItemStack, Player } from "@minecraft/server";
2
+ import {
3
+ getContainer,
4
+ getEquipmentItem,
5
+ setEquipmentItem
6
+ } from "../entity/entityUtils";
2
7
 
3
8
  /**
4
9
  * 获取容器中指定含指定物品标签的物品的数量
@@ -70,30 +75,109 @@ export function getItemAmountInContainer(
70
75
  return amount;
71
76
  }
72
77
 
78
+ /**
79
+ * 获取玩家身上的所有物品(包含装备栏与副手)
80
+ * @param player 玩家对象
81
+ * @param typeId 要获取的物品 ID
82
+ * @returns 容器中指定物品的数量
83
+ * @author RawDiamondMC
84
+ */
85
+
86
+ export function getItemAmountOfPlayer(player: Player, typeId: string): number {
87
+ const container = player.getComponent("minecraft:inventory")?.container;
88
+ if (!container) return 0;
89
+ let amount = getItemAmountInContainer(container, typeId);
90
+ // 不包含主手,防止重复计算
91
+ const slots = [
92
+ EquipmentSlot.Head,
93
+ EquipmentSlot.Chest,
94
+ EquipmentSlot.Legs,
95
+ EquipmentSlot.Feet,
96
+ EquipmentSlot.Offhand
97
+ ];
98
+ for (const slot of slots) {
99
+ const itemStack = getEquipmentItem(player, slot);
100
+ if (itemStack?.typeId === typeId) {
101
+ amount += itemStack.amount;
102
+ }
103
+ }
104
+ return amount;
105
+ }
106
+
73
107
  /**
74
108
  * 从容器中移除指定数量的物品
75
109
  * @param container 容器对象
76
110
  * @param typeId 物品 ID
77
111
  * @param amount 要移除物品的数量
112
+ * @returns 未能够移除完的物品数量
78
113
  * @author RawDiamondMC
79
114
  */
80
115
  export function removeItemInContainer(
81
116
  container: Container,
82
117
  typeId: string,
83
118
  amount: number
84
- ): void {
119
+ ): number {
85
120
  for (let slot = 0; slot < container.size; slot++) {
121
+ // 物品已经移除完毕
122
+ if (amount <= 0) return 0;
86
123
  const itemStack: undefined | ItemStack = container.getItem(slot);
87
124
  if (itemStack?.typeId === typeId) {
125
+ // 这个槽位的物品数量足够移除
88
126
  if (itemStack.amount > amount) {
89
127
  itemStack.amount -= amount;
90
128
  container.setItem(slot, itemStack);
91
- return;
129
+ // 直接返回,因为肯定移除完毕了
130
+ return 0;
92
131
  }
93
132
  container.setItem(slot);
94
133
  amount -= itemStack.amount;
95
134
  }
96
135
  }
136
+ return amount;
137
+ }
138
+
139
+ /**
140
+ * 从玩家身上移除一定量的物品
141
+ * @param player 玩家对象
142
+ * @param typeId 物品 ID
143
+ * @param amount 要移除物品的数量
144
+ * @returns 未能够移除完的物品数量
145
+ * @author RawDiamondMC
146
+ */
147
+ export function removeItemOfPlayer(
148
+ player: Player,
149
+ typeId: string,
150
+ amount: number
151
+ ): number {
152
+ // 玩家肯定得有inventory组件
153
+ let remaining = removeItemInContainer(getContainer(player)!, typeId, amount);
154
+ // 已经移除完毕
155
+ if (remaining <= 0) return 0;
156
+ // 不包含主手,防止重复计算
157
+ const slots = [
158
+ EquipmentSlot.Head,
159
+ EquipmentSlot.Chest,
160
+ EquipmentSlot.Legs,
161
+ EquipmentSlot.Feet,
162
+ EquipmentSlot.Offhand
163
+ ];
164
+ for (const slot of slots) {
165
+ // 物品已经移除完毕
166
+ if (remaining <= 0) return 0;
167
+ const itemStack = getEquipmentItem(player, slot);
168
+ if (itemStack?.typeId === typeId) {
169
+ // 这个槽位的物品数量足够移除
170
+ if (itemStack.amount > remaining) {
171
+ itemStack.amount -= remaining;
172
+ setEquipmentItem(player, itemStack, slot);
173
+ // 直接返回,因为肯定移除完毕了
174
+ return 0;
175
+ }
176
+ setEquipmentItem(player, itemStack, slot);
177
+ remaining -= itemStack.amount;
178
+ }
179
+ }
180
+ return remaining;
97
181
  }
98
182
 
99
183
  /**