@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@occultus/random-api",
3
- "version": "0.22.0",
3
+ "version": "0.22.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://codeberg.org/TeamOccultus/StarTenonAPI"
@@ -39,18 +39,19 @@ export class EventList {
39
39
  for (const event of this.data) {
40
40
  weightSum += event.weight;
41
41
  }
42
- let value = Random.integer(weightSum - 1);
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 (value < item.weight) {
46
+ if (remaining < item.weight) {
46
47
  item.event?.();
47
48
  return {
48
49
  weightSum: weightSum,
49
- weightRand: value,
50
+ weightRand: randValue,
50
51
  dataIndex: i
51
52
  };
52
53
  }
53
- value -= item.weight;
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
- * TODO 考虑写注释
34
- * @return 一个字符串形式的 UUID
31
+ * @return 一个字符串形式的 UUID。
35
32
  */
36
33
  static UUID(): string {
37
- const currentTimestamp: number = system.currentTick;
38
- return "xxxxxxxx-xxxx-4xxx-yxxx-zxxxxxxx".replace(
39
- /[xyz]/g,
40
- (args: string) => {
41
- const random: number = (Math.random() * 16) | 0;
42
- const value: number = args === "x" ? random : (random & 0x3) | 0x8;
43
- if (args === "z") {
44
- return value.toString(16) + currentTimestamp.toString(16).slice(-4);
45
- }
46
- return value.toString(16);
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
  }