@occultus/music-api 0.22.0-beta.2 → 0.22.0

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/music-api",
3
- "version": "0.22.0-beta.2",
3
+ "version": "0.22.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://codeberg.org/TeamOccultus/StarTenonAPI"
@@ -1,3 +1,9 @@
1
+ /**
2
+ * 音乐唱片服务端绑定配置
3
+ */
1
4
  export type MusicDiscBindingConfig = {
5
+ /**
6
+ * 绑定组件名称
7
+ */
2
8
  componentName: string;
3
9
  };
@@ -1,12 +1,27 @@
1
1
  import { OccultusSDKError } from "@occultus/core";
2
2
  import { MusicDiscBindingConfig } from "./BindingConfig";
3
- import { ItemStack, system, Vector3, world } from "@minecraft/server";
3
+ import {
4
+ ItemStack,
5
+ system,
6
+ Vector3,
7
+ world,
8
+ PlayerBreakBlockBeforeEvent,
9
+ PlayerInteractWithBlockBeforeEvent
10
+ } from "@minecraft/server";
4
11
  import { Color } from "@occultus/format-api";
5
12
  import { MusicDiscComponentParams } from "./ComponentParams";
6
13
 
14
+ /**
15
+ * 实现数据驱动组件绑定到服务端音乐唱片系统的类
16
+ */
7
17
  export class MusicDiscServerBindings {
8
18
  private static instance: MusicDiscServerBindings;
9
- private static getTrackName(itemStack: ItemStack) {
19
+ /**
20
+ * 获取音乐唱片组件的音轨名称
21
+ * @param itemStack
22
+ * @returns
23
+ */
24
+ static getTrackName(itemStack: ItemStack) {
10
25
  const config = MusicDiscServerBindings.getInstance().config;
11
26
  const params = itemStack.getComponent(config.componentName)
12
27
  ?.customComponentParameters.params as MusicDiscComponentParams;
@@ -14,61 +29,21 @@ export class MusicDiscServerBindings {
14
29
  }
15
30
  private registryComponent(config: MusicDiscBindingConfig) {
16
31
  system.beforeEvents.startup.subscribe((arg) => {
17
- arg.itemComponentRegistry.registerCustomComponent(config.componentName, {
18
- onUseOn(event, arg1) {
19
- const { source, block } = event;
20
- if (block?.typeId !== "minecraft:jukebox") return;
21
- if (source?.isSneaking) return;
22
- const params = arg1.params as MusicDiscComponentParams;
23
- block.dimension.playSound(params.track_name, block.location);
24
- block.dimension
25
- .getPlayers({
26
- location: block.location,
27
- minDistance: 0,
28
- maxDistance: 10
29
- })
30
- .forEach((player) => {
31
- player.onScreenDisplay.setActionBar([
32
- Color.lightPurple,
33
- {
34
- translate: "record.nowPlaying",
35
- with: [`${params.artist} - ${params.name}`]
36
- }
37
- ]);
38
- });
39
- return;
40
- }
41
- });
32
+ arg.itemComponentRegistry.registerCustomComponent(
33
+ config.componentName,
34
+ {}
35
+ );
42
36
  });
43
37
  }
44
38
  private subscribeEvents(config: MusicDiscBindingConfig) {
45
- world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
46
- const { block, player } = event;
47
- if (block?.typeId !== "minecraft:jukebox") return;
48
- if (player?.isSneaking) return;
49
- const musicDisc = block?.getComponent("record_player")?.getRecord();
50
- if (!musicDisc) return;
51
- const sound = MusicDiscServerBindings.getTrackName(musicDisc);
52
- if (sound) {
53
- system.run(() => {
54
- player.runCommand(`stopsound @a ${sound}`);
55
- });
56
- }
39
+ world.beforeEvents.playerInteractWithBlock.subscribe((arg) => {
40
+ inputListener(arg, config);
41
+ });
42
+ world.beforeEvents.playerInteractWithBlock.subscribe((arg) => {
43
+ removeListener(arg);
57
44
  });
58
- world.beforeEvents.playerBreakBlock.subscribe((event) => {
59
- const { block, player } = event;
60
- if (block?.typeId !== "minecraft:jukebox") return;
61
- if (player?.isSneaking) return;
62
- const recordComponent = block.getComponent("record_player");
63
- if (!recordComponent) return;
64
- const record = recordComponent.getRecord();
65
- if (!record) return;
66
- const sound = MusicDiscServerBindings.getTrackName(record);
67
- if (sound) {
68
- system.run(() => {
69
- player.runCommand(`stopsound @a ${sound}`);
70
- });
71
- }
45
+ world.beforeEvents.playerBreakBlock.subscribe((arg) => {
46
+ breakListener(arg);
72
47
  });
73
48
  }
74
49
  private constructor(public config: MusicDiscBindingConfig) {
@@ -77,7 +52,7 @@ export class MusicDiscServerBindings {
77
52
  }
78
53
  /**
79
54
  * 创建绑定实例
80
- * @param config
55
+ * @param config
81
56
  * @throws OccultusSDKError 倘若已创建绑定实例
82
57
  */
83
58
  static create(config: MusicDiscBindingConfig) {
@@ -88,7 +63,7 @@ export class MusicDiscServerBindings {
88
63
  }
89
64
  /**
90
65
  * 获取绑定实例
91
- * @returns
66
+ * @returns
92
67
  * @throws OccultusSDKError 倘若未创建绑定实例
93
68
  */
94
69
  static getInstance(): MusicDiscServerBindings {
@@ -98,3 +73,78 @@ export class MusicDiscServerBindings {
98
73
  return MusicDiscServerBindings.instance;
99
74
  }
100
75
  }
76
+
77
+ export function breakListener(event: PlayerBreakBlockBeforeEvent) {
78
+ const block = event.block;
79
+ const player = event.player;
80
+ if (block?.typeId !== "minecraft:jukebox") return;
81
+ if (player?.isSneaking) return;
82
+ const recordComponent = block.getComponent("record_player");
83
+ if (!recordComponent) return;
84
+ const record = recordComponent.getRecord();
85
+ if (!record) return;
86
+ const sound = MusicDiscServerBindings.getTrackName(record);
87
+ if (sound) {
88
+ system.run(() => {
89
+ player.runCommand(`stopsound @a ${sound}`);
90
+ });
91
+ }
92
+ }
93
+
94
+ export function removeListener(event: PlayerInteractWithBlockBeforeEvent) {
95
+ const block = event.block;
96
+ const player = event.player;
97
+ if (block?.typeId !== "minecraft:jukebox") return;
98
+ if (player?.isSneaking) return;
99
+ const record = block?.getComponent("record_player")?.getRecord();
100
+ if (!record) return;
101
+ const sound = MusicDiscServerBindings.getTrackName(record);
102
+ if (sound) {
103
+ system.run(() => {
104
+ player.runCommand(`stopsound @a ${sound}`);
105
+ });
106
+ }
107
+ }
108
+
109
+ export function inputListener(
110
+ event: PlayerInteractWithBlockBeforeEvent,
111
+ config: MusicDiscBindingConfig
112
+ ) {
113
+ const { player, itemStack, block } = event;
114
+ if (!itemStack) return;
115
+ if (block?.typeId !== "minecraft:jukebox") return;
116
+ if (player?.isSneaking) return;
117
+ const component = itemStack.getComponent(config.componentName);
118
+ if (!component) return;
119
+ const { track_name, artist, name } = component.customComponentParameters
120
+ .params as MusicDiscComponentParams;
121
+ if (itemStack) {
122
+ if (track_name && !block?.getComponent("record_player")?.isPlaying()) {
123
+ system.run(() => {
124
+ block.dimension.playSound(track_name, block.location);
125
+ block.dimension
126
+ .getPlayers({
127
+ location: block.location,
128
+ minDistance: 0,
129
+ maxDistance: 10
130
+ })
131
+ .forEach((player) => {
132
+ player.onScreenDisplay.setActionBar([
133
+ Color.lightPurple,
134
+ {
135
+ translate: "record.nowPlaying",
136
+ with: [`${artist} - ${name}`]
137
+ }
138
+ ]);
139
+ });
140
+ });
141
+ } else if (
142
+ track_name &&
143
+ block?.getComponent("record_player")?.isPlaying()
144
+ ) {
145
+ system.run(() => {
146
+ player.runCommand(`stopsound @a ${track_name}`);
147
+ });
148
+ }
149
+ }
150
+ }