@occultus/toolkit 0.22.0 → 0.22.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/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.0",
3
+ "version": "0.22.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://codeberg.org/TeamOccultus/StarTenonAPI"
@@ -24,13 +24,13 @@
24
24
  "author": "CTN Studios",
25
25
  "dependencies": {},
26
26
  "peerDependencies": {
27
- "@minecraft/server": ">=2.0.0",
28
- "@minecraft/server-ui": ">=2.0.0",
29
- "@occultus/core": ">=0.18.4 || <0.19.0",
27
+ "@minecraft/server": "^2.4.0",
28
+ "@minecraft/server-ui": "^2.0.0",
29
+ "@occultus/core": "~0.18.4",
30
30
  "@occultus/common": "0.20.0"
31
31
  },
32
32
  "devDependencies": {
33
- "typedoc": "^0.28.9"
33
+ "typedoc": "^0.28.15"
34
34
  },
35
35
  "scripts": {
36
36
  "test": "tsc"
@@ -6,7 +6,7 @@ import {
6
6
  EntityApplyDamageOptions,
7
7
  EffectType,
8
8
  EntityEffectOptions,
9
- ItemStack,
9
+ ItemStack
10
10
  } from "@minecraft/server";
11
11
  import { EffectGroups, EffectData } from "@occultus/common";
12
12
  import { addEffect, applyEffectData, clearSlot, giveItem } from "./entityUtils";
@@ -101,8 +101,6 @@ export class EntitiesUtils {
101
101
  * @returns
102
102
  */
103
103
  applyEffectData(data: EffectData | EffectData[]): void {
104
- return this.query().forEach((entity) =>
105
- applyEffectData(entity, data)
106
- );
104
+ return this.query().forEach((entity) => applyEffectData(entity, data));
107
105
  }
108
106
  }
@@ -3,11 +3,11 @@ import {
3
3
  Effect,
4
4
  EffectType,
5
5
  Entity,
6
+ EntityComponentTypes,
6
7
  EntityEffectOptions,
7
- EntityEquippableComponent,
8
8
  EquipmentSlot,
9
9
  ItemStack,
10
- Player,
10
+ Player
11
11
  } from "@minecraft/server";
12
12
  import { EffectData, effectGroupMap, EffectGroups } from "@occultus/common";
13
13
  import { OccultusSDKError } from "@occultus/core";
@@ -32,9 +32,10 @@ export function tryOperateEntity(
32
32
  }
33
33
 
34
34
  /**
35
- * 获取给定实体的指定槽位物品
35
+ * 获取给定**玩家**的指定装备槽物品 {@see EquipmentSlot}
36
+ *
37
+ * 注意:该方法仅适用于玩家实体
36
38
  *
37
- * **IMPORTANT: 受原版接口限制, 当前只能获取玩家的装备**
38
39
  * @param entity 要获取槽位的实体
39
40
  * @param slot 要获取的槽位,默认为 {@link EquipmentSlot.Mainhand}
40
41
  * @return 槽位中的物品
@@ -44,16 +45,16 @@ export function getEquipmentItem(
44
45
  entity: Entity,
45
46
  slot = EquipmentSlot.Mainhand
46
47
  ): ItemStack | undefined {
47
- const equipment = entity?.getComponent(
48
- "minecraft:equippable"
49
- ) as EntityEquippableComponent;
50
- return equipment?.getEquipment(slot);
48
+ return entity
49
+ ?.getComponent(EntityComponentTypes.Equippable)
50
+ ?.getEquipment(slot);
51
51
  }
52
52
 
53
53
  /**
54
- * 设置给定实体的指定槽位物品
54
+ * 设置给定**玩家**的指定装备槽物品 {@see EquipmentSlot}
55
+ *
56
+ * 注意:该方法仅适用于玩家实体
55
57
  *
56
- * **IMPORTANT: 受原版接口限制, 当前只能获取玩家的装备**
57
58
  * @param entity 要设置槽位的实体
58
59
  * @param item 要设置的物品,如果为undefined则清空该槽位
59
60
  * @param slot 要设置的槽位,默认为 {@link EquipmentSlot.Mainhand}
@@ -64,11 +65,11 @@ export function setEquipmentItem(
64
65
  item?: ItemStack,
65
66
  slot: EquipmentSlot = EquipmentSlot.Mainhand
66
67
  ): boolean {
67
- const equipment = entity?.getComponent(
68
- "minecraft:equippable"
69
- ) as EntityEquippableComponent;
70
- if (!equipment) return false;
71
- return equipment?.setEquipment(slot, item);
68
+ return (
69
+ entity
70
+ ?.getComponent(EntityComponentTypes.Equippable)
71
+ ?.setEquipment(slot, item) ?? false
72
+ );
72
73
  }
73
74
 
74
75
  /**
@@ -79,7 +80,7 @@ export function setEquipmentItem(
79
80
  * @since Starock 0.6.0 (0.1.0)
80
81
  */
81
82
  export function getContainer(entity: Entity): Container | undefined {
82
- return entity.getComponent("minecraft:inventory")?.container;
83
+ return entity.getComponent(EntityComponentTypes.Inventory)?.container;
83
84
  }
84
85
 
85
86
  /**
@@ -140,7 +141,7 @@ export function applyEffectData(
140
141
  return data.flatMap((effectData) => {
141
142
  return entity.addEffect(effectData.effectType, effectData.duration, {
142
143
  amplifier: effectData.amplifier,
143
- showParticles: effectData.showParticles,
144
+ showParticles: effectData.showParticles
144
145
  });
145
146
  });
146
147
  }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @module toolkit
3
3
  */
4
- export * from "./entity/EntitiesUtils"
5
- export * from "./entity/entityUtils"
6
- export * from "./item/item"
7
- export * from "./item/container"
4
+ export * from "./entity/EntitiesUtils";
5
+ export * from "./entity/entityUtils";
6
+ export * from "./item/item";
7
+ export * from "./item/container";
@@ -1,4 +1,57 @@
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";
7
+
8
+ /**
9
+ * 获取容器中指定含指定物品标签的物品的数量
10
+ * @param container 容器对象
11
+ * @param tag 要获取的物品标签
12
+ * @returns 容器中指定物品的数量
13
+ * @author RawDiamondMC, FangLiulii
14
+ */
15
+
16
+ export function getTagItemAmountInContainer(
17
+ container: Container,
18
+ tag: string
19
+ ): number {
20
+ let amount = 0;
21
+ for (let slot = 0; slot < container.size; slot++) {
22
+ const itemStack: undefined | ItemStack = container.getItem(slot);
23
+ if (itemStack?.hasTag(tag)) {
24
+ amount += itemStack.amount;
25
+ }
26
+ }
27
+ return amount;
28
+ }
29
+
30
+ /**
31
+ * 从容器中移除指定数量的含有特定标签的物品
32
+ * @param container 容器对象
33
+ * @param tag 物品标签
34
+ * @param amount 要移除物品的数量
35
+ * @author RawDiamondMC, FangLiulii
36
+ */
37
+ export function removeTagItemInContainer(
38
+ container: Container,
39
+ tag: string,
40
+ amount: number
41
+ ): void {
42
+ for (let slot = 0; slot < container.size; slot++) {
43
+ const itemStack: undefined | ItemStack = container.getItem(slot);
44
+ if (itemStack?.hasTag(tag)) {
45
+ if (itemStack.amount > amount) {
46
+ itemStack.amount -= amount;
47
+ container.setItem(slot, itemStack);
48
+ return;
49
+ }
50
+ container.setItem(slot);
51
+ amount -= itemStack.amount;
52
+ }
53
+ }
54
+ }
2
55
 
3
56
  /**
4
57
  * 获取容器中指定物品的数量
@@ -22,30 +75,109 @@ export function getItemAmountInContainer(
22
75
  return amount;
23
76
  }
24
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
+
25
107
  /**
26
108
  * 从容器中移除指定数量的物品
27
109
  * @param container 容器对象
28
110
  * @param typeId 物品 ID
29
111
  * @param amount 要移除物品的数量
112
+ * @returns 未能够移除完的物品数量
30
113
  * @author RawDiamondMC
31
114
  */
32
115
  export function removeItemInContainer(
33
116
  container: Container,
34
117
  typeId: string,
35
118
  amount: number
36
- ): void {
119
+ ): number {
37
120
  for (let slot = 0; slot < container.size; slot++) {
121
+ // 物品已经移除完毕
122
+ if (amount <= 0) return 0;
38
123
  const itemStack: undefined | ItemStack = container.getItem(slot);
39
124
  if (itemStack?.typeId === typeId) {
125
+ // 这个槽位的物品数量足够移除
40
126
  if (itemStack.amount > amount) {
41
127
  itemStack.amount -= amount;
42
128
  container.setItem(slot, itemStack);
43
- return;
129
+ // 直接返回,因为肯定移除完毕了
130
+ return 0;
44
131
  }
45
132
  container.setItem(slot);
46
133
  amount -= itemStack.amount;
47
134
  }
48
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;
49
181
  }
50
182
 
51
183
  /**
package/src/item/item.ts CHANGED
@@ -5,7 +5,7 @@ import {
5
5
  ItemComponentTypes,
6
6
  ItemDurabilityComponent,
7
7
  Player,
8
- GameMode,
8
+ GameMode
9
9
  } from "@minecraft/server";
10
10
  import { OccultusSDKError } from "@occultus/core";
11
11
 
@@ -48,6 +48,10 @@ export function consumeDurability(
48
48
 
49
49
  /**
50
50
  * 消耗物品的数量
51
+ *
52
+ * 注意!这个函数不会直接修改传入的物品堆,而是返回一个新的物品堆。在原版,直接修改物品堆的数量不会正确同步,需要重新设定。
53
+ * F**K U Mojang
54
+ *
51
55
  * @param item 要消耗物品的数量
52
56
  * @param value 要消耗的数量
53
57
  * @returns 消耗后的物品堆