@occultus/toolkit 0.23.2 → 0.23.4
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/LICENSE +9 -9
- package/README.md +30 -30
- package/package.json +1 -1
- package/src/entity/EntitiesUtils.ts +121 -121
- package/src/entity/entityUtils.ts +327 -366
- package/src/entity/health.ts +71 -0
- package/src/identifier/namespace.ts +23 -23
- package/src/index.ts +9 -9
- package/src/item/container.ts +227 -227
- package/src/item/cooldown.ts +26 -26
- package/src/item/item.ts +98 -98
- package/tsconfig.json +21 -21
package/src/item/item.ts
CHANGED
|
@@ -1,98 +1,98 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ItemStack,
|
|
3
|
-
Entity,
|
|
4
|
-
ItemComponent,
|
|
5
|
-
ItemComponentTypes,
|
|
6
|
-
ItemDurabilityComponent,
|
|
7
|
-
Player,
|
|
8
|
-
GameMode
|
|
9
|
-
} from "@minecraft/server";
|
|
10
|
-
import { OccultusSDKError } from "@occultus/core";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 添加物品的损坏值,即消耗其耐久。当`entity`是创造模式或是`undefined`,会直接返回`item`
|
|
14
|
-
* @param item 要添加损坏值的物品
|
|
15
|
-
* @param value 要添加的损坏值
|
|
16
|
-
* @param entity 当物品损坏时,向手持物品的实体播放声音
|
|
17
|
-
* @returns 添加损坏值的 {@link ItemStack}
|
|
18
|
-
* @author RawDiamondMC, FangLimao
|
|
19
|
-
*/
|
|
20
|
-
export function consumeDurability(
|
|
21
|
-
item: ItemStack,
|
|
22
|
-
value: number,
|
|
23
|
-
entity?: Entity
|
|
24
|
-
): ItemStack | undefined {
|
|
25
|
-
const isPlayer = entity instanceof Player;
|
|
26
|
-
if (isPlayer && entity.getGameMode() === GameMode.Creative) {
|
|
27
|
-
// 创造模式下不消耗物品耐久
|
|
28
|
-
return item;
|
|
29
|
-
}
|
|
30
|
-
const durability: undefined | ItemComponent = item.getComponent(
|
|
31
|
-
ItemComponentTypes.Durability
|
|
32
|
-
);
|
|
33
|
-
if (
|
|
34
|
-
durability === undefined ||
|
|
35
|
-
!(durability instanceof ItemDurabilityComponent)
|
|
36
|
-
) {
|
|
37
|
-
return item;
|
|
38
|
-
}
|
|
39
|
-
if (durability.damage + value >= durability.maxDurability) {
|
|
40
|
-
if (isPlayer) {
|
|
41
|
-
entity.playSound("random.break");
|
|
42
|
-
}
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
durability.damage += value;
|
|
46
|
-
return item;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 消耗物品的数量
|
|
51
|
-
*
|
|
52
|
-
* 注意!这个函数不会直接修改传入的物品堆,而是返回一个新的物品堆。在原版,直接修改物品堆的数量不会正确同步,需要重新设定。
|
|
53
|
-
* F**K U Mojang
|
|
54
|
-
*
|
|
55
|
-
* @param item 要消耗物品的数量
|
|
56
|
-
* @param value 要消耗的数量
|
|
57
|
-
* @returns 消耗后的物品堆
|
|
58
|
-
* @throws 如果物品数量不足,则抛出错误
|
|
59
|
-
* @author RawDiamondMC
|
|
60
|
-
*/
|
|
61
|
-
export function consumeAmount(
|
|
62
|
-
item: ItemStack,
|
|
63
|
-
value: number
|
|
64
|
-
): ItemStack | undefined {
|
|
65
|
-
const amount: number = item.amount;
|
|
66
|
-
if (amount === value) {
|
|
67
|
-
return undefined;
|
|
68
|
-
}
|
|
69
|
-
if (amount - value < 0) {
|
|
70
|
-
throw new OccultusSDKError(
|
|
71
|
-
`The number of items is insufficient! Actual amount: ${amount} Consume amount: ${value}`,
|
|
72
|
-
`${item.typeId}`
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
if (amount - value > item.maxAmount) {
|
|
76
|
-
throw new OccultusSDKError(
|
|
77
|
-
`The max stack of items is insufficient!`,
|
|
78
|
-
`${item.typeId}`
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
const newItem: ItemStack = item.clone();
|
|
82
|
-
newItem.amount = amount - value;
|
|
83
|
-
return newItem;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 向物品添加新的 Lore
|
|
88
|
-
* @param loreText 要添加的 Lore文本
|
|
89
|
-
* @param item 要添加 Lore 文本的物品
|
|
90
|
-
* @returns 更新 Lore 后的物品
|
|
91
|
-
* @author FangLimao
|
|
92
|
-
*/
|
|
93
|
-
export function pushLore(loreText: string, item: ItemStack): ItemStack {
|
|
94
|
-
const lore = item.getLore();
|
|
95
|
-
lore.push(loreText);
|
|
96
|
-
item.setLore(lore);
|
|
97
|
-
return item;
|
|
98
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
ItemStack,
|
|
3
|
+
Entity,
|
|
4
|
+
ItemComponent,
|
|
5
|
+
ItemComponentTypes,
|
|
6
|
+
ItemDurabilityComponent,
|
|
7
|
+
Player,
|
|
8
|
+
GameMode
|
|
9
|
+
} from "@minecraft/server";
|
|
10
|
+
import { OccultusSDKError } from "@occultus/core";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 添加物品的损坏值,即消耗其耐久。当`entity`是创造模式或是`undefined`,会直接返回`item`
|
|
14
|
+
* @param item 要添加损坏值的物品
|
|
15
|
+
* @param value 要添加的损坏值
|
|
16
|
+
* @param entity 当物品损坏时,向手持物品的实体播放声音
|
|
17
|
+
* @returns 添加损坏值的 {@link ItemStack}
|
|
18
|
+
* @author RawDiamondMC, FangLimao
|
|
19
|
+
*/
|
|
20
|
+
export function consumeDurability(
|
|
21
|
+
item: ItemStack,
|
|
22
|
+
value: number,
|
|
23
|
+
entity?: Entity
|
|
24
|
+
): ItemStack | undefined {
|
|
25
|
+
const isPlayer = entity instanceof Player;
|
|
26
|
+
if (isPlayer && entity.getGameMode() === GameMode.Creative) {
|
|
27
|
+
// 创造模式下不消耗物品耐久
|
|
28
|
+
return item;
|
|
29
|
+
}
|
|
30
|
+
const durability: undefined | ItemComponent = item.getComponent(
|
|
31
|
+
ItemComponentTypes.Durability
|
|
32
|
+
);
|
|
33
|
+
if (
|
|
34
|
+
durability === undefined ||
|
|
35
|
+
!(durability instanceof ItemDurabilityComponent)
|
|
36
|
+
) {
|
|
37
|
+
return item;
|
|
38
|
+
}
|
|
39
|
+
if (durability.damage + value >= durability.maxDurability) {
|
|
40
|
+
if (isPlayer) {
|
|
41
|
+
entity.playSound("random.break");
|
|
42
|
+
}
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
durability.damage += value;
|
|
46
|
+
return item;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 消耗物品的数量
|
|
51
|
+
*
|
|
52
|
+
* 注意!这个函数不会直接修改传入的物品堆,而是返回一个新的物品堆。在原版,直接修改物品堆的数量不会正确同步,需要重新设定。
|
|
53
|
+
* F**K U Mojang
|
|
54
|
+
*
|
|
55
|
+
* @param item 要消耗物品的数量
|
|
56
|
+
* @param value 要消耗的数量
|
|
57
|
+
* @returns 消耗后的物品堆
|
|
58
|
+
* @throws 如果物品数量不足,则抛出错误
|
|
59
|
+
* @author RawDiamondMC
|
|
60
|
+
*/
|
|
61
|
+
export function consumeAmount(
|
|
62
|
+
item: ItemStack,
|
|
63
|
+
value: number
|
|
64
|
+
): ItemStack | undefined {
|
|
65
|
+
const amount: number = item.amount;
|
|
66
|
+
if (amount === value) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
if (amount - value < 0) {
|
|
70
|
+
throw new OccultusSDKError(
|
|
71
|
+
`The number of items is insufficient! Actual amount: ${amount} Consume amount: ${value}`,
|
|
72
|
+
`${item.typeId}`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
if (amount - value > item.maxAmount) {
|
|
76
|
+
throw new OccultusSDKError(
|
|
77
|
+
`The max stack of items is insufficient!`,
|
|
78
|
+
`${item.typeId}`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
const newItem: ItemStack = item.clone();
|
|
82
|
+
newItem.amount = amount - value;
|
|
83
|
+
return newItem;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 向物品添加新的 Lore
|
|
88
|
+
* @param loreText 要添加的 Lore文本
|
|
89
|
+
* @param item 要添加 Lore 文本的物品
|
|
90
|
+
* @returns 更新 Lore 后的物品
|
|
91
|
+
* @author FangLimao
|
|
92
|
+
*/
|
|
93
|
+
export function pushLore(loreText: string, item: ItemStack): ItemStack {
|
|
94
|
+
const lore = item.getLore();
|
|
95
|
+
lore.push(loreText);
|
|
96
|
+
item.setLore(lore);
|
|
97
|
+
return item;
|
|
98
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
{
|
|
2
|
-
"include": ["src/**/*"],
|
|
3
|
-
"exclude": ["./out"],
|
|
4
|
-
"Modules": {
|
|
5
|
-
"resolvePackageJsonExports": true
|
|
6
|
-
},
|
|
7
|
-
"compilerOptions": {
|
|
8
|
-
"noEmit": true,
|
|
9
|
-
"noEmitOnError": true,
|
|
10
|
-
"target": "es2022",
|
|
11
|
-
"lib": ["
|
|
12
|
-
"strict": true,
|
|
13
|
-
"moduleResolution": "bundler",
|
|
14
|
-
"esModuleInterop": true,
|
|
15
|
-
"module": "es2022",
|
|
16
|
-
"outDir": ".",
|
|
17
|
-
"removeComments": true,
|
|
18
|
-
"newLine": "lf",
|
|
19
|
-
"resolveJsonModule": true
|
|
20
|
-
}
|
|
21
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"include": ["src/**/*"],
|
|
3
|
+
"exclude": ["./out"],
|
|
4
|
+
"Modules": {
|
|
5
|
+
"resolvePackageJsonExports": true
|
|
6
|
+
},
|
|
7
|
+
"compilerOptions": {
|
|
8
|
+
"noEmit": true,
|
|
9
|
+
"noEmitOnError": true,
|
|
10
|
+
"target": "es2022",
|
|
11
|
+
"lib": ["es2021", "dom"],
|
|
12
|
+
"strict": true,
|
|
13
|
+
"moduleResolution": "bundler",
|
|
14
|
+
"esModuleInterop": true,
|
|
15
|
+
"module": "es2022",
|
|
16
|
+
"outDir": ".",
|
|
17
|
+
"removeComments": true,
|
|
18
|
+
"newLine": "lf",
|
|
19
|
+
"resolveJsonModule": true
|
|
20
|
+
}
|
|
21
|
+
}
|