@occultus/music-api 0.22.0-beta.1 → 0.22.0-beta.3

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