@occultus/toolkit 0.22.1 → 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.1",
3
+ "version": "0.22.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://codeberg.org/TeamOccultus/StarTenonAPI"
@@ -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
@@ -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
  /**