Skip to content

Commit 0514a20

Browse files
committed
RadioSongPlayer now playing stereo by default #17
1 parent a78e61c commit 0514a20

File tree

6 files changed

+90
-17
lines changed

6 files changed

+90
-17
lines changed

src/main/java/com/xxmicloxx/NoteBlockAPI/NoteBlockAPI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.bukkit.scheduler.BukkitRunnable;
2020

2121
import com.xxmicloxx.NoteBlockAPI.songplayer.SongPlayer;
22+
import com.xxmicloxx.NoteBlockAPI.utils.MathUtils;
2223
import com.xxmicloxx.NoteBlockAPI.utils.Updater;
2324

2425
/**
@@ -157,6 +158,7 @@ public void run() {
157158
}
158159
}, 20*10);
159160

161+
new MathUtils();
160162
}
161163

162164
@Override
@@ -215,7 +217,6 @@ protected void handleDeprecated(StackTraceElement[] ste){
215217
plugins.removeAll(notResult);
216218
notResult.clear();
217219
if (plugins.size() == 1){
218-
Bukkit.getLogger().info(plugins.get(0).getName());
219220
dependentPlugins.put(plugins.get(0), true);
220221
}
221222
}

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/NoteBlockSongPlayer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ public void playTick(Player player, int tick) {
106106

107107
if (instrument.getSound() != null) {
108108
CompatibilityUtils.playSound(player, noteBlock.getLocation(),
109-
instrument.getSound(), this.soundCategory, volume, pitch);
109+
instrument.getSound(), this.soundCategory, volume, pitch, false);
110110
} else {
111111
CompatibilityUtils.playSound(player, noteBlock.getLocation(),
112-
instrument.getSoundFileName(), this.soundCategory, volume, pitch);
112+
instrument.getSoundFileName(), this.soundCategory, volume, pitch, false);
113113
}
114114
} else {
115115
CompatibilityUtils.playSound(player, noteBlock.getLocation(),
116-
InstrumentUtils.getInstrument(note.getInstrument()), this.soundCategory, volume, pitch);
116+
InstrumentUtils.getInstrument(note.getInstrument()), this.soundCategory, volume, pitch, false);
117117
}
118118

119119
if (isInRange(player)) {

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/PositionSongPlayer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ public void playTick(Player player, int tick) {
9898

9999
if (instrument.getSound() != null) {
100100
CompatibilityUtils.playSound(player, targetLocation, instrument.getSound(),
101-
this.soundCategory, volume, pitch);
101+
this.soundCategory, volume, pitch, false);
102102
} else {
103103
CompatibilityUtils.playSound(player, targetLocation, instrument.getSoundFileName(),
104-
this.soundCategory, volume, pitch);
104+
this.soundCategory, volume, pitch, false);
105105
}
106106
} else {
107107
CompatibilityUtils.playSound(player, targetLocation,
108108
InstrumentUtils.getInstrument(note.getInstrument()), this.soundCategory,
109-
volume, pitch);
109+
volume, pitch, false);
110110
}
111111

112112
if (isInRange(player)) {

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/RadioSongPlayer.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
*
1919
*/
2020
public class RadioSongPlayer extends SongPlayer {
21-
21+
22+
protected boolean stereo = true;
23+
2224
public RadioSongPlayer(Song song) {
2325
super(song);
2426
makeNewClone(com.xxmicloxx.NoteBlockAPI.RadioSongPlayer.class);
@@ -63,19 +65,33 @@ public void playTick(Player player, int tick) {
6365
if (instrument.getSound() != null) {
6466
CompatibilityUtils.playSound(player, player.getEyeLocation(),
6567
instrument.getSound(),
66-
this.soundCategory, volume, pitch);
68+
this.soundCategory, volume, pitch, stereo);
6769
} else {
6870
CompatibilityUtils.playSound(player, player.getEyeLocation(),
6971
instrument.getSoundFileName(),
70-
this.soundCategory, volume, pitch);
72+
this.soundCategory, volume, pitch, stereo);
7173
}
7274
} else {
7375
CompatibilityUtils.playSound(player, player.getEyeLocation(),
74-
InstrumentUtils.getInstrument(note.getInstrument()), this.soundCategory, volume, pitch);
76+
InstrumentUtils.getInstrument(note.getInstrument()), this.soundCategory, volume, pitch, stereo);
7577
}
7678
}
7779
}
7880

81+
/**
82+
* Returns if the SongPlayer will play Notes from two sources as stereo
83+
* @return
84+
*/
85+
public boolean isStereo(){
86+
return stereo;
87+
}
7988

89+
/**
90+
* Sets if the SongPlayer will play Notes from two sources as stereo
91+
* @param stereo
92+
*/
93+
public void setStereo(boolean stereo){
94+
this.stereo = stereo;
95+
}
8096

8197
}

src/main/java/com/xxmicloxx/NoteBlockAPI/utils/CompatibilityUtils.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,29 @@ protected static boolean isSoundCategoryCompatible() {
8787
* @param pitch
8888
*/
8989
public static void playSound(Player player, Location location, String sound,
90-
SoundCategory category, float volume, float pitch) {
90+
SoundCategory category, float volume, float pitch, boolean stereo) {
9191
try {
9292
if (isSoundCategoryCompatible()) {
9393
Method method = Player.class.getMethod("playSound", Location.class, String.class,
9494
Class.forName("org.bukkit.SoundCategory"), float.class, float.class);
9595
Class<? extends Enum> soundCategory =
9696
(Class<? extends Enum>) Class.forName("org.bukkit.SoundCategory");
9797
Enum<?> soundCategoryEnum = Enum.valueOf(soundCategory, category.name());
98-
method.invoke(player, location, sound, soundCategoryEnum, volume, pitch);
98+
if (!stereo){
99+
method.invoke(player, location, sound, soundCategoryEnum, volume, pitch);
100+
} else {
101+
method.invoke(player, MathUtils.stereoSourceLeft(location, 2), sound, soundCategoryEnum, volume, pitch);
102+
method.invoke(player, MathUtils.stereoSourceRight(location, 2), sound, soundCategoryEnum, volume, pitch);
103+
}
99104
} else {
100105
Method method = Player.class.getMethod("playSound", Location.class,
101106
String.class, float.class, float.class);
102-
method.invoke(player, location, sound, volume, pitch);
107+
if (!stereo){
108+
method.invoke(player, location, sound, volume, pitch);
109+
} else {
110+
method.invoke(player, MathUtils.stereoSourceLeft(location, 2), sound, volume, pitch);
111+
method.invoke(player, MathUtils.stereoSourceRight(location, 2), sound, volume, pitch);
112+
}
103113
}
104114
} catch (NoSuchMethodException e) {
105115
e.printStackTrace();
@@ -126,19 +136,29 @@ public static void playSound(Player player, Location location, String sound,
126136
* @param pitch
127137
*/
128138
public static void playSound(Player player, Location location, Sound sound,
129-
SoundCategory category, float volume, float pitch) {
139+
SoundCategory category, float volume, float pitch, boolean stereo) {
130140
try {
131141
if (isSoundCategoryCompatible()) {
132142
Method method = Player.class.getMethod("playSound", Location.class, Sound.class,
133143
Class.forName("org.bukkit.SoundCategory"), float.class, float.class);
134144
Class<? extends Enum> soundCategory =
135145
(Class<? extends Enum>) Class.forName("org.bukkit.SoundCategory");
136146
Enum<?> soundCategoryEnum = Enum.valueOf(soundCategory, category.name());
137-
method.invoke(player, location, sound, soundCategoryEnum, volume, pitch);
147+
if (!stereo){
148+
method.invoke(player, location, sound, soundCategoryEnum, volume, pitch);
149+
} else {
150+
method.invoke(player, MathUtils.stereoSourceLeft(location, 2), sound, soundCategoryEnum, volume, pitch);
151+
method.invoke(player, MathUtils.stereoSourceRight(location, 2), sound, soundCategoryEnum, volume, pitch);
152+
}
138153
} else {
139154
Method method = Player.class.getMethod("playSound", Location.class,
140155
Sound.class, float.class, float.class);
141-
method.invoke(player, location, sound, volume, pitch);
156+
if (!stereo){
157+
method.invoke(player, location, sound, volume, pitch);
158+
} else {
159+
method.invoke(player, MathUtils.stereoSourceLeft(location, 2), sound, volume, pitch);
160+
method.invoke(player, MathUtils.stereoSourceRight(location, 2), sound, volume, pitch);
161+
}
142162
}
143163
} catch (NoSuchMethodException e) {
144164
e.printStackTrace();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.xxmicloxx.NoteBlockAPI.utils;
2+
3+
import org.bukkit.Location;
4+
5+
public class MathUtils {
6+
7+
private static MathUtils instance;
8+
private double cos[] = new double[360];
9+
private double sin[] = new double[360];
10+
11+
public MathUtils(){
12+
instance = this;
13+
for (int deg = 0; deg < 360; deg++) {
14+
cos[deg] = Math.cos(Math.toRadians(deg));
15+
sin[deg] = Math.sin(Math.toRadians(deg));
16+
}
17+
}
18+
19+
private static double[] getCos(){
20+
return MathUtils.instance.cos;
21+
}
22+
23+
private static double[] getSin(){
24+
return MathUtils.instance.sin;
25+
}
26+
27+
public static Location stereoSourceLeft(Location location, float distance) {
28+
float yaw = location.getYaw();
29+
return location.clone().add(-getCos()[(int) (yaw + 360) % 360] * distance, 0, -getSin()[(int) (yaw + 360) % 360] * distance);
30+
}
31+
public static Location stereoSourceRight(Location location, float distance) {
32+
float yaw = location.getYaw();
33+
return location.clone().add(getCos()[(int) (yaw + 360) % 360] * distance, 0, getSin()[(int) (yaw + 360) % 360] * distance);
34+
}
35+
36+
}

0 commit comments

Comments
 (0)