-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNBSSongPlayer.java
More file actions
238 lines (196 loc) · 6.96 KB
/
Copy pathNBSSongPlayer.java
File metadata and controls
238 lines (196 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package cn.nukkitmot.exampleplugin.nbs;
import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.level.Position;
import cn.nukkit.network.protocol.PlaySoundPacket;
import cn.nukkit.scheduler.TaskHandler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class NBSSongPlayer extends SongPlayer {
private static final float[] PITCH_VALUES = {
0.5f, 0.529732f, 0.561231f, 0.594604f, 0.629961f, 0.667420f, 0.707107f, 0.749154f,
0.793701f, 0.840896f, 0.890899f, 0.943874f, 1.0f, 1.059463f, 1.122462f, 1.189207f,
1.259921f, 1.334840f, 1.414214f, 1.498307f, 1.587401f, 1.681793f, 1.781797f, 1.887749f, 2.0f
};
private static final Map<Integer, String> INSTRUMENT_SOUNDS = new HashMap<>();
static {
INSTRUMENT_SOUNDS.put(0, "note.harp");
INSTRUMENT_SOUNDS.put(1, "note.bass");
INSTRUMENT_SOUNDS.put(2, "note.bd");
INSTRUMENT_SOUNDS.put(3, "note.snare");
INSTRUMENT_SOUNDS.put(4, "note.hat");
INSTRUMENT_SOUNDS.put(5, "note.guitar");
INSTRUMENT_SOUNDS.put(6, "note.flute");
INSTRUMENT_SOUNDS.put(7, "note.bell");
INSTRUMENT_SOUNDS.put(8, "note.chime");
INSTRUMENT_SOUNDS.put(9, "note.xylophone");
}
private final Position playPosition;
private final float globalVolume;
private final boolean useDistanceAttenuation;
private final double maxDistance;
private TaskHandler taskFuture;
private int tickCount = 0;
public NBSSongPlayer(Song song) {
this(song, new Position(0, 0, 0, null), 100f, true, 50.0);
}
public NBSSongPlayer(Song song, Position position) {
this(song, position, 100f, true, 50.0);
}
public NBSSongPlayer(Song song, Position position, float volume) {
this(song, position, volume, true, 50.0);
}
public NBSSongPlayer(Song song, Position position, float volume, boolean useDistanceAttenuation, double maxDistance) {
super(song);
this.playPosition = position;
this.globalVolume = volume;
this.useDistanceAttenuation = useDistanceAttenuation;
this.maxDistance = maxDistance;
}
@Override
public void playTick(ArrayList<String> players, int tick) {
if (song == null || song.getLayerHashMap() == null) {
return;
}
float vol = (getVolume() / 100f) * (globalVolume / 100f);
if (vol <= 0f) {
return;
}
ArrayList<Player> targetPlayers = getTargetPlayers();
if (targetPlayers.isEmpty()) {
return;
}
for (Map.Entry<Integer, Layer> entry : song.getLayerHashMap().entrySet()) {
Layer layer = entry.getValue();
if (layer == null) {
continue;
}
Note note = layer.getNote(tick);
if (note == null) {
continue;
}
String soundName = INSTRUMENT_SOUNDS.get((int) note.getInstrument());
if (soundName == null) {
continue;
}
int keyIndex = note.getKey() - 33;
if (keyIndex < 0 || keyIndex >= PITCH_VALUES.length) {
continue;
}
float pitch = PITCH_VALUES[keyIndex];
float layerVol = (layer.getVolume() / 100f);
for (Player player : targetPlayers) {
sendSound(player, soundName, vol * layerVol, pitch);
}
}
}
private ArrayList<Player> getTargetPlayers() {
ArrayList<Player> result = new ArrayList<>();
List<String> playerNames;
synchronized (playerList) {
playerNames = new ArrayList<>(playerList);
}
if (playerNames.isEmpty()) {
return result;
}
for (String name : playerNames) {
Player player = Server.getInstance().getPlayerExact(name);
if (player != null && player.isOnline()) {
if (playPosition.getLevel() == null || player.getLevel() == playPosition.getLevel()) {
result.add(player);
}
}
}
return result;
}
private void sendSound(Player player, String soundName, float volume, float pitch) {
if (useDistanceAttenuation && playPosition.getLevel() != null) {
double distance = player.distance(playPosition);
if (distance > maxDistance) {
return;
}
float distanceFactor = 1.0f - (float) (distance / maxDistance);
distanceFactor = Math.max(0.1f, distanceFactor * distanceFactor);
volume *= distanceFactor;
}
if (volume < 0.01f) {
return;
}
PlaySoundPacket pk = new PlaySoundPacket();
pk.name = soundName;
pk.volume = Math.min(volume * 100f, 100f);
pk.pitch = pitch;
pk.x = (int) playPosition.x;
pk.y = (int) playPosition.y;
pk.z = (int) playPosition.z;
player.dataPacket(pk);
}
public void start() {
if (playing) {
return;
}
playing = true;
tick = -1;
tickCount = 0;
int delay = (int) song.getDelay();
taskFuture = Server.getInstance().getScheduler().scheduleRepeatingTask(
NBSSoundManager.getInstance().getPlugin(),
new Runnable() {
@Override
public void run() {
if (!playing || destroyed) {
return;
}
tick++;
if (tick >= song.getLength()) {
if (autoCycle) {
tick = 0;
} else {
stop();
return;
}
}
ArrayList<String> players;
synchronized (playerList) {
players = new ArrayList<>(playerList);
}
if (!players.isEmpty()) {
playTick(players, tick);
}
}
},
delay,
true
);
}
public void stop() {
playing = false;
if (taskFuture != null) {
taskFuture.cancel();
taskFuture = null;
}
}
public void pause() {
playing = false;
}
public void resume() {
if (!playing && tick >= 0) {
playing = true;
}
}
public Position getPlayPosition() {
return playPosition;
}
public float getGlobalVolume() {
return globalVolume;
}
public boolean isUseDistanceAttenuation() {
return useDistanceAttenuation;
}
public double getMaxDistance() {
return maxDistance;
}
}