@occultus/music-api 0.18.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/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2025 CTN Studios
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # Star Tenon Music API
2
+
3
+ > [!IMPORTANT]
4
+ > 该包仍处于开发阶段,可能会有一些问题亟待修复
5
+
6
+ 本包提供了方便的自定义唱片的 API,可以方便的创建新的音乐唱片。
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@occultus/music-api",
3
+ "version": "0.18.1",
4
+ "description": "Star Tenon music api",
5
+ "main": "src/index.ts",
6
+ "keywords": [
7
+ "Minecraft",
8
+ "Occultus SDK",
9
+ "Script API"
10
+ ],
11
+ "contributors": [
12
+ "FangLimao <mucigames@outlook.com>",
13
+ "RawDiamondMC <RawDiamondMC@outlook.com>"
14
+ ],
15
+ "license": "MIT",
16
+ "author": "CTN Studios",
17
+ "peerDependencies": {
18
+ "@minecraft/server": "2.3.0-beta.1.21.110-preview.20",
19
+ "@occultus/core": ">=0.18.2 || <0.19.0"
20
+ },
21
+ "dependencies": {
22
+ "@occultus/format-api": "0.18.1"
23
+ },
24
+ "devDependencies": {
25
+ "typedoc": "^0.28.9"
26
+ }
27
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * 音乐唱片类,用于表示游戏中的音乐唱片物品
3
+ *
4
+ * 该类封装了音乐唱片的基本信息,包括唱片物品 ID、曲目名称、唱片名称和艺术家。
5
+ */
6
+ export class MusicDisc {
7
+ /**
8
+ * @param typeId 唱片的类型ID,用于区分不同的唱片
9
+ * @param trackName 唱片中的曲目名称
10
+ * @param name 唱片的名称
11
+ * @param artist 唱片的作者
12
+ */
13
+ constructor(
14
+ readonly typeId: string,
15
+ public trackName: string,
16
+ public name: string,
17
+ public artist: string
18
+ ) {}
19
+ }
@@ -0,0 +1,127 @@
1
+ import {
2
+ PlayerBreakBlockBeforeEvent,
3
+ PlayerInteractWithBlockBeforeEvent,
4
+ RawMessage,
5
+ system,
6
+ world,
7
+ } from "@minecraft/server";
8
+ import { MusicDisc } from "./MusicDisc";
9
+ import { Color } from "@occultus/format-api";
10
+
11
+ /**
12
+ * 管理音乐唱片的服务器端逻辑
13
+ */
14
+ export class MusicDiscServer {
15
+ /**
16
+ * 初始化服务端事件监听器
17
+ * @private
18
+ */
19
+ private initServer() {
20
+ world.beforeEvents.playerInteractWithBlock.subscribe((arg) => {
21
+ inputListener(arg, this);
22
+ });
23
+ world.beforeEvents.playerInteractWithBlock.subscribe((arg) => {
24
+ removeListener(arg, this);
25
+ });
26
+ world.beforeEvents.playerBreakBlock.subscribe((arg) => {
27
+ breakListener(arg, this);
28
+ });
29
+ }
30
+ constructor() {
31
+ this.initServer();
32
+ }
33
+
34
+ /** 存储唱片类型ID与音轨名称的映射 */
35
+ records: Map<string, string> = new Map();
36
+ /** 存储唱片类型ID与艺术家名称的映射 */
37
+ artists: Map<string, string> = new Map();
38
+ /** 存储唱片类型ID与唱片显示名称的映射 */
39
+ names: Map<string, string> = new Map();
40
+
41
+ /**
42
+ * 添加一张音乐唱片到服务器
43
+ * @param record 要添加的音乐唱片对象
44
+ */
45
+ addDisc(record: MusicDisc) {
46
+ this.records.set(record.typeId, record.trackName);
47
+ this.artists.set(record.typeId, record.artist);
48
+ this.names.set(record.typeId, record.name);
49
+ }
50
+ }
51
+
52
+ function breakListener(
53
+ event: PlayerBreakBlockBeforeEvent,
54
+ server: MusicDiscServer
55
+ ) {
56
+ const block = event.block;
57
+ const player = event.player;
58
+ if (block?.typeId !== "minecraft:jukebox") return;
59
+ if (player?.isSneaking) return;
60
+ const recordComponent = block.getComponent("record_player");
61
+ if (!recordComponent) return;
62
+ const record = recordComponent.getRecord();
63
+ if (!record) return;
64
+ const sound = server.records.get(record.typeId);
65
+ if (sound) {
66
+ system.run(() => {
67
+ player.runCommand(`stopsound @a ${sound}`);
68
+ });
69
+ }
70
+ }
71
+
72
+ function removeListener(
73
+ event: PlayerInteractWithBlockBeforeEvent,
74
+ server: MusicDiscServer
75
+ ) {
76
+ const block = event.block;
77
+ const player = event.player;
78
+ if (block?.typeId !== "minecraft:jukebox") return;
79
+ if (player?.isSneaking) return;
80
+ const record = block?.getComponent("record_player")?.getRecord()?.typeId;
81
+ if (!record) return;
82
+ const sound = server.records.get(record);
83
+ if (sound) {
84
+ system.run(() => {
85
+ player.runCommand(`stopsound @a ${sound}`);
86
+ });
87
+ }
88
+ }
89
+
90
+ function inputListener(
91
+ event: PlayerInteractWithBlockBeforeEvent,
92
+ server: MusicDiscServer
93
+ ) {
94
+ const { player, itemStack, block } = event;
95
+ if (!itemStack) return;
96
+ if (block?.typeId !== "minecraft:jukebox") return;
97
+ if (player?.isSneaking) return;
98
+ const record = server.records.get(itemStack?.typeId);
99
+ const artist = server.artists.get(itemStack?.typeId);
100
+ const name = server.names.get(itemStack?.typeId);
101
+ if (itemStack) {
102
+ if (record && !block?.getComponent("record_player")?.isPlaying()) {
103
+ system.run(() => {
104
+ block.dimension.playSound(record, block.location);
105
+ block.dimension
106
+ .getPlayers({
107
+ location: block.location,
108
+ minDistance: 0,
109
+ maxDistance: 10,
110
+ })
111
+ .forEach((player) => {
112
+ player.onScreenDisplay.setActionBar([
113
+ Color.lightPurple,
114
+ {
115
+ translate: "record.nowPlaying",
116
+ with: [`${artist} - ${name}`],
117
+ },
118
+ ]);
119
+ });
120
+ });
121
+ } else if (record && block?.getComponent("record_player")?.isPlaying()) {
122
+ system.run(() => {
123
+ player.runCommand(`stopsound @a ${record}`);
124
+ });
125
+ }
126
+ }
127
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./api/MusicDisc";
2
+ export * from "./api/MusicDiscServer";
package/tsconfig.json ADDED
@@ -0,0 +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": ["es2020", "dom"],
12
+ "strict": true,
13
+ "moduleResolution": "node16",
14
+ "esModuleInterop": true,
15
+ "module": "node16",
16
+ "outDir": ".",
17
+ "removeComments": true,
18
+ "newLine": "lf",
19
+ "resolveJsonModule": true
20
+ }
21
+ }