@occultus/random-api 0.22.0 → 0.22.1
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 +1 -1
- package/src/api/EventList.ts +5 -4
- package/src/api/Random.ts +13 -15
package/package.json
CHANGED
package/src/api/EventList.ts
CHANGED
|
@@ -39,18 +39,19 @@ export class EventList {
|
|
|
39
39
|
for (const event of this.data) {
|
|
40
40
|
weightSum += event.weight;
|
|
41
41
|
}
|
|
42
|
-
|
|
42
|
+
const randValue = Random.integer(weightSum - 1);
|
|
43
|
+
let remaining = randValue;
|
|
43
44
|
for (let i = 0; i < this.data.length; i++) {
|
|
44
45
|
const item = this.data[i];
|
|
45
|
-
if (
|
|
46
|
+
if (remaining < item.weight) {
|
|
46
47
|
item.event?.();
|
|
47
48
|
return {
|
|
48
49
|
weightSum: weightSum,
|
|
49
|
-
weightRand:
|
|
50
|
+
weightRand: randValue,
|
|
50
51
|
dataIndex: i
|
|
51
52
|
};
|
|
52
53
|
}
|
|
53
|
-
|
|
54
|
+
remaining -= item.weight;
|
|
54
55
|
}
|
|
55
56
|
throw new OccultusSDKError("Cannot find the event to call!", "Event List");
|
|
56
57
|
}
|
package/src/api/Random.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { system } from "@minecraft/server";
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* 一个随机数或 UUID 生成器类
|
|
5
3
|
*/
|
|
@@ -30,21 +28,21 @@ export class Random {
|
|
|
30
28
|
/**
|
|
31
29
|
* 生成一个 UUID (version 4)
|
|
32
30
|
* 按照 RFC 9562 实现。https://www.rfc-editor.org/rfc/rfc9562#name-uuid-version-4
|
|
33
|
-
*
|
|
34
|
-
* @return 一个字符串形式的 UUID
|
|
31
|
+
* @return 一个字符串形式的 UUID。
|
|
35
32
|
*/
|
|
36
33
|
static UUID(): string {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
34
|
+
let result = "";
|
|
35
|
+
for (let i = 0; i < 16; i++) {
|
|
36
|
+
let randomByte = Math.floor(Math.random() * 256);
|
|
37
|
+
// 第 7 个字节开头 4bit 为 0100 (ver)
|
|
38
|
+
if (i == 6) randomByte = (randomByte & 0x0f) | 0x40;
|
|
39
|
+
//第 9 个字节前 2bit 为 10 (var)
|
|
40
|
+
else if (i == 8) randomByte = (randomByte & 0x3f) | 0x80;
|
|
41
|
+
result += randomByte.toString(16).padStart(2, '0');
|
|
42
|
+
if (i == 3 || i == 5 || i == 7 || i == 9) {
|
|
43
|
+
result += '-';
|
|
47
44
|
}
|
|
48
|
-
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
49
47
|
}
|
|
50
48
|
}
|