Skip to content

Commit 6107b09

Browse files
committed
Readded compatibility for 1.8,.1.9,1.10
1 parent e5c792d commit 6107b09

File tree

10 files changed

+152
-41
lines changed

10 files changed

+152
-41
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.xxmicloxx.NoteBlockAPI;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.Location;
8+
import org.bukkit.Sound;
9+
import org.bukkit.entity.Player;
10+
11+
public class CompatibilityUtils {
12+
13+
public static final String OBC_DIR = Bukkit.getServer().getClass().getPackage().getName();
14+
public static final String NMS_DIR = OBC_DIR.replaceFirst("org.bukkit.craftbukkit", "net.minecraft.server");
15+
16+
public static Class<?> getMinecraftClass(String name){
17+
try {
18+
return Class.forName(NMS_DIR + "." + name);
19+
} catch (ClassNotFoundException e) {
20+
e.printStackTrace();
21+
return null;
22+
}
23+
}
24+
25+
public static Class<?> getCraftBukkitClass(String name){
26+
try {
27+
return Class.forName(OBC_DIR + "." + name);
28+
} catch (ClassNotFoundException e) {
29+
e.printStackTrace();
30+
return null;
31+
}
32+
}
33+
34+
public static class NoteBlockCompatibility{
35+
public static final int pre1_9 = 0;
36+
public static final int pre1_12 = 1;
37+
public static final int post1_12 = 2;
38+
}
39+
40+
protected static int getCompatibility(){
41+
if (Bukkit.getVersion().contains("1.8") || Bukkit.getVersion().contains("1.7")){
42+
return NoteBlockCompatibility.pre1_9;
43+
} else if (Bukkit.getVersion().contains("1.9") || Bukkit.getVersion().contains("1.10") || Bukkit.getVersion().contains("1.11")){
44+
return NoteBlockCompatibility.pre1_12;
45+
} else {
46+
return NoteBlockCompatibility.post1_12;
47+
}
48+
}
49+
50+
protected static boolean isSoundCategoryCompatible(){
51+
if (Bukkit.getVersion().contains("1.7") || Bukkit.getVersion().contains("1.8") || Bukkit.getVersion().contains("1.9") || Bukkit.getVersion().contains("1.10")){
52+
return false;
53+
} else {
54+
return true;
55+
}
56+
}
57+
58+
protected static void playSound(Player p, Location location, String sound, SoundCategory category, float volume, float pitch){
59+
try {
60+
if (isSoundCategoryCompatible()){
61+
Method m = Player.class.getMethod("playSound", Location.class, String.class, Class.forName("org.bukkit.SoundCategory"), float.class, float.class);
62+
Class<? extends Enum> sc = (Class<? extends Enum>) Class.forName("org.bukkit.SoundCategory");
63+
Enum<?> scenum = Enum.valueOf(sc, category.name());
64+
m.invoke(p, location, sound, scenum, volume, pitch);
65+
} else {
66+
Method m = Player.class.getMethod("playSound", Location.class, String.class, float.class, float.class);
67+
m.invoke(p, location, sound, volume, pitch);
68+
}
69+
} catch (NoSuchMethodException e) {
70+
e.printStackTrace();
71+
} catch (SecurityException e) {
72+
e.printStackTrace();
73+
} catch (IllegalAccessException e) {
74+
// TODO Auto-generated catch block
75+
e.printStackTrace();
76+
} catch (IllegalArgumentException e) {
77+
// TODO Auto-generated catch block
78+
e.printStackTrace();
79+
} catch (InvocationTargetException e) {
80+
// TODO Auto-generated catch block
81+
e.printStackTrace();
82+
} catch (ClassNotFoundException e) {
83+
// TODO Auto-generated catch block
84+
e.printStackTrace();
85+
}
86+
}
87+
88+
public static void playSound(Player p, Location location, Sound sound, SoundCategory category, float volume, float pitch) {
89+
try {
90+
if (isSoundCategoryCompatible()){
91+
Method m = Player.class.getMethod("playSound", Location.class, Sound.class, Class.forName("org.bukkit.SoundCategory"), float.class, float.class);
92+
Class<? extends Enum> sc = (Class<? extends Enum>) Class.forName("org.bukkit.SoundCategory");
93+
Enum<?> scenum = Enum.valueOf(sc, category.name());
94+
m.invoke(p, location, sound, scenum, volume, pitch);
95+
} else {
96+
Method m = Player.class.getMethod("playSound", Location.class, Sound.class, float.class, float.class);
97+
m.invoke(p, location, sound, volume, pitch);
98+
}
99+
100+
} catch (NoSuchMethodException e) {
101+
e.printStackTrace();
102+
} catch (SecurityException e) {
103+
e.printStackTrace();
104+
} catch (IllegalAccessException e) {
105+
e.printStackTrace();
106+
} catch (IllegalArgumentException e) {
107+
e.printStackTrace();
108+
} catch (InvocationTargetException e) {
109+
e.printStackTrace();
110+
} catch (ClassNotFoundException e) {
111+
e.printStackTrace();
112+
}
113+
}
114+
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.bukkit.Sound;
44

5+
import com.xxmicloxx.NoteBlockAPI.CompatibilityUtils.NoteBlockCompatibility;
6+
57
public class CustomInstrument {
68

79
private byte index;
@@ -16,12 +18,12 @@ public CustomInstrument(byte index, String name, String soundfile, byte pitch, b
1618
this.name = name;
1719
this.soundfile = soundfile.replaceAll(".ogg", "");
1820
if (this.soundfile.equalsIgnoreCase("pling")){
19-
switch (NoteBlockPlayerMain.getCompatibility()){
20-
case NoteBlockPlayerMain.NoteBlockCompatibility.pre1_9:
21+
switch (CompatibilityUtils.getCompatibility()){
22+
case NoteBlockCompatibility.pre1_9:
2123
this.sound = Sound.valueOf("NOTE_PLING");
2224
break;
23-
case NoteBlockPlayerMain.NoteBlockCompatibility.pre1_12:
24-
case NoteBlockPlayerMain.NoteBlockCompatibility.post1_12:
25+
case CompatibilityUtils.NoteBlockCompatibility.pre1_12:
26+
case NoteBlockCompatibility.post1_12:
2527
this.sound = Sound.valueOf("BLOCK_NOTE_PLING");
2628
break;
2729
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import org.bukkit.Sound;
44

5-
import com.xxmicloxx.NoteBlockAPI.NoteBlockPlayerMain.NoteBlockCompatibility;
5+
import com.xxmicloxx.NoteBlockAPI.CompatibilityUtils.NoteBlockCompatibility;
66

77
public class Instrument {
88

99
public static Sound getInstrument(byte instrument) {
10-
if (NoteBlockPlayerMain.getCompatibility() == NoteBlockCompatibility.pre1_9){
10+
if (CompatibilityUtils.getCompatibility() == NoteBlockCompatibility.pre1_9){
1111
switch (instrument) {
1212
case 0:
1313
return Sound.valueOf("NOTE_PIANO");
@@ -36,7 +36,7 @@ public static Sound getInstrument(byte instrument) {
3636
return Sound.valueOf("BLOCK_NOTE_HAT");
3737
}
3838

39-
if (NoteBlockPlayerMain.getCompatibility() == NoteBlockCompatibility.post1_12){
39+
if (CompatibilityUtils.getCompatibility() == NoteBlockCompatibility.post1_12){
4040
switch (instrument) {
4141
case 5:
4242
return Sound.valueOf("BLOCK_NOTE_GUITAR");
@@ -74,7 +74,7 @@ public static org.bukkit.Instrument getBukkitInstrument(byte instrument) {
7474
}
7575

7676
public static boolean isCustomInstrument(byte instrument){
77-
if (NoteBlockPlayerMain.getCompatibility() != NoteBlockCompatibility.post1_12){
77+
if (CompatibilityUtils.getCompatibility() != NoteBlockCompatibility.post1_12){
7878
if (instrument > 4){
7979
return true;
8080
}
@@ -88,7 +88,7 @@ public static boolean isCustomInstrument(byte instrument){
8888
}
8989

9090
public static byte getCustomInstrumentFirstIndex(){
91-
if (NoteBlockPlayerMain.getCompatibility() != NoteBlockCompatibility.post1_12){
91+
if (CompatibilityUtils.getCompatibility() != NoteBlockCompatibility.post1_12){
9292
return 5;
9393
} else {
9494
return 10;

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ public void onDisable() {
5757
Bukkit.getScheduler().cancelTasks(this);
5858
}
5959

60-
protected static int getCompatibility(){
61-
if (Bukkit.getVersion().contains("1.8") || Bukkit.getVersion().contains("1.7")){
62-
return NoteBlockCompatibility.pre1_9;
63-
} else if (Bukkit.getVersion().contains("1.9") || Bukkit.getVersion().contains("1.10") || Bukkit.getVersion().contains("1.11")){
64-
return NoteBlockCompatibility.pre1_12;
65-
} else {
66-
return NoteBlockCompatibility.post1_12;
67-
}
68-
}
69-
7060
public void doSync(Runnable r) {
7161
getServer().getScheduler().runTask(this, r);
7262
}
@@ -78,10 +68,5 @@ public void doAsync(Runnable r) {
7868
protected boolean isDisabling(){
7969
return disabling;
8070
}
81-
82-
public class NoteBlockCompatibility{
83-
public static final int pre1_9 = 0;
84-
public static final int pre1_12 = 1;
85-
public static final int post1_12 = 2;
86-
}
71+
8772
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.bukkit.Bukkit;
44
import org.bukkit.Material;
5-
import org.bukkit.SoundCategory;
65
import org.bukkit.block.Block;
76
import org.bukkit.entity.Player;
87

@@ -47,19 +46,19 @@ public void playTick(Player p, int tick) {
4746

4847
if (Instrument.isCustomInstrument(note.getInstrument())){
4948
if (song.getCustomInstruments()[note.getInstrument() - Instrument.getCustomInstrumentFirstIndex()].getSound() != null){
50-
p.playSound(noteBlock.getLocation(),
49+
CompatibilityUtils.playSound(p, noteBlock.getLocation(),
5150
song.getCustomInstruments()[note.getInstrument() - Instrument.getCustomInstrumentFirstIndex()].getSound(),
5251
this.soundCategory,((l.getVolume() * (int) volume * (int) playerVolume) / 1000000f) * ((1f/16f) * distance),
5352
NotePitch.getPitch(note.getKey() - 33));
5453
}else {
55-
p.playSound(noteBlock.getLocation(),
54+
CompatibilityUtils.playSound(p, noteBlock.getLocation(),
5655
song.getCustomInstruments()[note.getInstrument() - Instrument.getCustomInstrumentFirstIndex()].getSoundfile(),
57-
((l.getVolume() * (int) volume * (int) playerVolume) / 1000000f) * ((1f/16f) * distance),
56+
this.soundCategory,((l.getVolume() * (int) volume * (int) playerVolume) / 1000000f) * ((1f/16f) * distance),
5857
NotePitch.getPitch(note.getKey() - 33));
5958
}
6059

6160
}else {
62-
p.playSound(noteBlock.getLocation(),
61+
CompatibilityUtils.playSound(p, noteBlock.getLocation(),
6362
Instrument.getInstrument(note.getInstrument()),
6463
this.soundCategory,((l.getVolume() * (int) volume * (int) playerVolume) / 1000000f) * ((1f/16f) * distance),
6564
NotePitch.getPitch(note.getKey() - 33));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.xxmicloxx.NoteBlockAPI;
22

3-
import com.xxmicloxx.NoteBlockAPI.NoteBlockPlayerMain.NoteBlockCompatibility;
3+
import com.xxmicloxx.NoteBlockAPI.CompatibilityUtils.NoteBlockCompatibility;
44

55
public enum NotePitch {
66

@@ -43,7 +43,7 @@ private NotePitch(int note, float pitchPre1_9, float pitchPost1_9) {
4343
public static float getPitch(int note) {
4444
for (NotePitch notePitch : values()) {
4545
if (notePitch.note == note) {
46-
return NoteBlockPlayerMain.getCompatibility() == NoteBlockCompatibility.pre1_9 ? notePitch.pitchPre1_9 : notePitch.pitchPost1_9;
46+
return CompatibilityUtils.getCompatibility() == NoteBlockCompatibility.pre1_9 ? notePitch.pitchPre1_9 : notePitch.pitchPost1_9;
4747
}
4848
}
4949

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.bukkit.Bukkit;
44
import org.bukkit.Location;
5-
import org.bukkit.SoundCategory;
65
import org.bukkit.entity.Player;
76

87
public class PositionSongPlayer extends SongPlayer {
@@ -42,19 +41,19 @@ public void playTick(Player p, int tick) {
4241

4342
if (Instrument.isCustomInstrument(note.getInstrument())){
4443
if (song.getCustomInstruments()[note.getInstrument() - Instrument.getCustomInstrumentFirstIndex()].getSound() != null){
45-
p.playSound(targetLocation,
44+
CompatibilityUtils.playSound(p, targetLocation,
4645
song.getCustomInstruments()[note.getInstrument() - Instrument.getCustomInstrumentFirstIndex()].getSound(),
4746
this.soundCategory,((l.getVolume() * (int) volume * (int) playerVolume) / 1000000f) * ((1f/16f) * distance),
4847
NotePitch.getPitch(note.getKey() - 33));
4948
}else {
50-
p.playSound(targetLocation,
49+
CompatibilityUtils.playSound(p, targetLocation,
5150
song.getCustomInstruments()[note.getInstrument() - Instrument.getCustomInstrumentFirstIndex()].getSoundfile(),
5251
this.soundCategory,((l.getVolume() * (int) volume * (int) playerVolume) / 1000000f) * ((1f/16f) * distance),
5352
NotePitch.getPitch(note.getKey() - 33));
5453
}
5554

5655
}else {
57-
p.playSound(targetLocation,
56+
CompatibilityUtils.playSound(p, targetLocation,
5857
Instrument.getInstrument(note.getInstrument()),
5958
this.soundCategory,((l.getVolume() * (int) volume * (int) playerVolume) / 1000000f) * ((1f/16f) * distance),
6059
NotePitch.getPitch(note.getKey() - 33));

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.xxmicloxx.NoteBlockAPI;
22

3-
import org.bukkit.SoundCategory;
43
import org.bukkit.entity.Player;
54

65
public class RadioSongPlayer extends SongPlayer {
@@ -24,19 +23,19 @@ public void playTick(Player p, int tick) {
2423
}
2524
if (Instrument.isCustomInstrument(note.getInstrument())){
2625
if (song.getCustomInstruments()[note.getInstrument() - Instrument.getCustomInstrumentFirstIndex()].getSound() != null){
27-
p.playSound(p.getEyeLocation(),
26+
CompatibilityUtils.playSound(p, p.getEyeLocation(),
2827
song.getCustomInstruments()[note.getInstrument() - Instrument.getCustomInstrumentFirstIndex()].getSound(),
2928
this.soundCategory,(l.getVolume() * (int) volume * (int) playerVolume) / 1000000f,
3029
NotePitch.getPitch(note.getKey() - 33));
3130
}else {
32-
p.playSound(p.getEyeLocation(),
31+
CompatibilityUtils.playSound(p, p.getEyeLocation(),
3332
song.getCustomInstruments()[note.getInstrument() - Instrument.getCustomInstrumentFirstIndex()].getSoundfile(),
3433
this.soundCategory,(l.getVolume() * (int) volume * (int) playerVolume) / 1000000f,
3534
NotePitch.getPitch(note.getKey() - 33));
3635
}
3736

3837
}else {
39-
p.playSound(p.getEyeLocation(),
38+
CompatibilityUtils.playSound(p, p.getEyeLocation(),
4039
Instrument.getInstrument(note.getInstrument()),
4140
this.soundCategory,(l.getVolume() * (int) volume * (int) playerVolume) / 1000000f,
4241
NotePitch.getPitch(note.getKey() - 33));

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.concurrent.locks.ReentrantLock;
1010

1111
import org.bukkit.Bukkit;
12-
import org.bukkit.SoundCategory;
1312
import org.bukkit.entity.Player;
1413

1514
public abstract class SongPlayer {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.xxmicloxx.NoteBlockAPI;
2+
3+
public enum SoundCategory {
4+
MASTER,
5+
MUSIC,
6+
RECORDS,
7+
WEATHER,
8+
BLOCKS,
9+
HOSTILE,
10+
NEUTRAL,
11+
PLAYERS,
12+
AMBIENT,
13+
VOICE;
14+
}

0 commit comments

Comments
 (0)