Skip to content

Commit 37aa7bf

Browse files
committed
Added 1.14 instruments (#25), events now called sync (#27)
Custom instruments may not work (#26)
1 parent 380442e commit 37aa7bf

File tree

10 files changed

+131
-99
lines changed

10 files changed

+131
-99
lines changed

pom.xml

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

66
<groupId>com.xxmicloxx</groupId>
77
<artifactId>NoteBlockAPI</artifactId>
8-
<version>1.2.5</version>
8+
<version>1.3</version>
99
<name>NoteBlockAPI</name>
1010

1111
<properties>
@@ -27,9 +27,9 @@
2727

2828
<dependencies>
2929
<dependency>
30-
<groupId>org.bukkit</groupId>
31-
<artifactId>bukkit</artifactId>
32-
<version>1.13-R0.1-SNAPSHOT</version>
30+
<groupId>org.spigotmc</groupId>
31+
<artifactId>spigot-api</artifactId>
32+
<version>1.14-pre5-SNAPSHOT</version>
3333
<scope>provided</scope>
3434
</dependency>
3535
<dependency>

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

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

33
import com.xxmicloxx.NoteBlockAPI.model.Sound;
4+
import com.xxmicloxx.NoteBlockAPI.utils.InstrumentUtils;
45

56
/**
67
* @deprecated {@link com.xxmicloxx.NoteBlockAPI.utils.InstrumentUtils}
@@ -46,6 +47,18 @@ public static String getInstrumentName(byte instrument) {
4647
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_CHIME").name();
4748
case 9:
4849
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_XYLOPHONE").name();
50+
case 10:
51+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_IRON_XYLOPHONE").name();
52+
case 11:
53+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_COW_BELL").name();
54+
case 12:
55+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_DIDGERIDOO").name();
56+
case 13:
57+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_BIT").name();
58+
case 14:
59+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_BANJO").name();
60+
case 15:
61+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_PLING").name();
4962
default:
5063
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_HARP").name();
5164
}
@@ -57,35 +70,7 @@ public static String getInstrumentName(byte instrument) {
5770
* @return Instrument enum (for the current server version)
5871
*/
5972
public static org.bukkit.Instrument getBukkitInstrument(byte instrument) {
60-
switch (instrument) {
61-
case 0:
62-
return org.bukkit.Instrument.PIANO;
63-
case 1:
64-
return org.bukkit.Instrument.BASS_GUITAR;
65-
case 2:
66-
return org.bukkit.Instrument.BASS_DRUM;
67-
case 3:
68-
return org.bukkit.Instrument.SNARE_DRUM;
69-
case 4:
70-
return org.bukkit.Instrument.STICKS;
71-
default: {
72-
if (CompatibilityUtils.isPost1_12()) {
73-
switch (instrument) {
74-
case 5:
75-
return org.bukkit.Instrument.valueOf("GUITAR");
76-
case 6:
77-
return org.bukkit.Instrument.valueOf("FLUTE");
78-
case 7:
79-
return org.bukkit.Instrument.valueOf("BELL");
80-
case 8:
81-
return org.bukkit.Instrument.valueOf("CHIME");
82-
case 9:
83-
return org.bukkit.Instrument.valueOf("XYLOPHONE");
84-
}
85-
}
86-
return org.bukkit.Instrument.PIANO;
87-
}
88-
}
73+
return InstrumentUtils.getBukkitInstrument(instrument);
8974
}
9075

9176
/**
@@ -94,16 +79,7 @@ public static org.bukkit.Instrument getBukkitInstrument(byte instrument) {
9479
* @return whether the byte represents a custom instrument
9580
*/
9681
public static boolean isCustomInstrument(byte instrument) {
97-
if (CompatibilityUtils.isPost1_12()) {
98-
if (instrument > 9) {
99-
return true;
100-
}
101-
return false;
102-
}
103-
if (instrument > 4) {
104-
return true;
105-
}
106-
return false;
82+
return InstrumentUtils.isCustomInstrument(instrument);
10783
}
10884

10985
/**
@@ -112,10 +88,7 @@ public static boolean isCustomInstrument(byte instrument) {
11288
* @return index where an instrument can be added
11389
*/
11490
public static byte getCustomInstrumentFirstIndex() {
115-
if (CompatibilityUtils.isPost1_12()) {
116-
return 10;
117-
}
118-
return 5;
91+
return InstrumentUtils.getCustomInstrumentFirstIndex();
11992
}
12093

12194
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public enum NotePitch {
3838
public float pitchPre1_9;
3939
public float pitchPost1_9;
4040

41-
private NotePitch(int note, float pitchPre1_9, float pitchPost1_9) {
41+
NotePitch(int note, float pitchPre1_9, float pitchPost1_9) {
4242
this.note = note;
4343
this.pitchPre1_9 = pitchPre1_9;
4444
this.pitchPost1_9 = pitchPost1_9;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ public void destroy() {
375375
lock.lock();
376376
try {
377377
SongDestroyingEvent event = new SongDestroyingEvent(this);
378-
Bukkit.getPluginManager().callEvent(event);
378+
plugin.doSync(() -> Bukkit.getPluginManager().callEvent(event));
379379
//Bukkit.getScheduler().cancelTask(threadId);
380380
if (event.isCancelled()) {
381381
return;
@@ -406,7 +406,7 @@ public void setPlaying(boolean playing) {
406406
this.playing = playing;
407407
if (!playing) {
408408
SongStoppedEvent event = new SongStoppedEvent(this);
409-
Bukkit.getPluginManager().callEvent(event);
409+
plugin.doSync(() -> Bukkit.getPluginManager().callEvent(event));
410410
}
411411
CallUpdate("playing", playing);
412412
}
@@ -452,7 +452,7 @@ private void removePlayer(Player player, boolean notify) {
452452
NoteBlockPlayerMain.plugin.playingSongs.put(player.getName(), songs);
453453
if (playerList.isEmpty() && autoDestroy) {
454454
SongEndEvent event = new SongEndEvent(this);
455-
Bukkit.getPluginManager().callEvent(event);
455+
plugin.doSync(() -> Bukkit.getPluginManager().callEvent(event));
456456
destroy();
457457
}
458458
} finally {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public enum NotePitch {
3838
public float pitchPre1_9;
3939
public float pitchPost1_9;
4040

41-
private NotePitch(int note, float pitchPre1_9, float pitchPost1_9) {
41+
NotePitch(int note, float pitchPre1_9, float pitchPost1_9) {
4242
this.note = note;
4343
this.pitchPre1_9 = pitchPre1_9;
4444
this.pitchPost1_9 = pitchPost1_9;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ public enum Sound {
2121
NOTE_BELL("NOTE_BELL", "BLOCK_NOTE_BELL", "BLOCK_NOTE_BLOCK_BELL"),
2222
NOTE_CHIME("NOTE_CHIME", "BLOCK_NOTE_CHIME", "BLOCK_NOTE_BLOCK_CHIME"),
2323
NOTE_XYLOPHONE("NOTE_XYLOPHONE", "BLOCK_NOTE_XYLOPHONE", "BLOCK_NOTE_BLOCK_XYLOPHONE"),
24-
NOTE_PLING("NOTE_PLING", "BLOCK_NOTE_PLING", "BLOCK_NOTE_BLOCK_PLING");
24+
NOTE_PLING("NOTE_PLING", "BLOCK_NOTE_PLING", "BLOCK_NOTE_BLOCK_PLING"),
25+
NOTE_IRON_XYLOPHONE("BLOCK_NOTE_BLOCK_IRON_XYLOPHONE"),
26+
NOTE_COW_BELL("BLOCK_NOTE_BLOCK_COW_BELL"),
27+
NOTE_DIDGERIDOO("BLOCK_NOTE_BLOCK_DIDGERIDOO"),
28+
NOTE_BIT("BLOCK_NOTE_BLOCK_BIT"),
29+
NOTE_BANJO("BLOCK_NOTE_BLOCK_BANJO");
2530

2631
private String[] versionDependentNames;
2732
private org.bukkit.Sound cached = null;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ public void destroy() {
545545
lock.lock();
546546
try {
547547
SongDestroyingEvent event = new SongDestroyingEvent(this);
548-
Bukkit.getPluginManager().callEvent(event);
548+
plugin.doSync(() -> Bukkit.getPluginManager().callEvent(event));
549549
//Bukkit.getScheduler().cancelTask(threadId);
550550
if (event.isCancelled()) {
551551
return;
@@ -576,7 +576,7 @@ public void setPlaying(boolean playing) {
576576
this.playing = playing;
577577
if (!playing) {
578578
SongStoppedEvent event = new SongStoppedEvent(this);
579-
Bukkit.getPluginManager().callEvent(event);
579+
plugin.doSync(() -> Bukkit.getPluginManager().callEvent(event));
580580
}
581581
CallUpdate("playing", playing);
582582
}
@@ -633,7 +633,7 @@ private void removePlayer(UUID player, boolean notify) {
633633
NoteBlockAPI.setSongPlayersByPlayer(player, songs);
634634
if (playerList.isEmpty() && autoDestroy) {
635635
SongEndEvent event = new SongEndEvent(this);
636-
Bukkit.getPluginManager().callEvent(event);
636+
plugin.doSync(() -> Bukkit.getPluginManager().callEvent(event));
637637
destroy();
638638
}
639639
} finally {

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

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class CompatibilityUtils {
2121
public static final String OBC_DIR = Bukkit.getServer().getClass().getPackage().getName();
2222
public static final String NMS_DIR = OBC_DIR.replaceFirst("org.bukkit.craftbukkit", "net.minecraft.server");
2323

24+
25+
private static float serverVersion = -1;
26+
2427
/**
2528
* Gets NMS class from given name
2629
* @param name of class (w/ package)
@@ -52,12 +55,10 @@ public static Class<?> getCraftBukkitClass(String name) {
5255
/**
5356
* Returns whether the version of Bukkit is or is after 1.12
5457
* @return version is after 1.12
58+
* @deprecated Compare {@link #getServerVersion()} with 0.0112f
5559
*/
5660
public static boolean isPost1_12() {
57-
if (!isSoundCategoryCompatible() || Bukkit.getVersion().contains("1.11")) {
58-
return false;
59-
}
60-
return true;
61+
return getServerVersion() >= 0.0112f;
6162
}
6263

6364
/**
@@ -67,14 +68,7 @@ public static boolean isPost1_12() {
6768
* @return can use SoundCategory
6869
*/
6970
protected static boolean isSoundCategoryCompatible() {
70-
if (Bukkit.getVersion().contains("1.7")
71-
|| Bukkit.getVersion().contains("1.8")
72-
|| Bukkit.getVersion().contains("1.9")
73-
|| Bukkit.getVersion().contains("1.10")) {
74-
return false;
75-
} else {
76-
return true;
77-
}
71+
return getServerVersion() >= 0.0111f;
7872
}
7973

8074
/**
@@ -210,15 +204,49 @@ public static void playSound(Player player, Location location, Sound sound,
210204
/**
211205
* Gets instruments which were added post-1.12
212206
* @return ArrayList of instruments
207+
* @deprecated Use {@link #getVersionCustomInstruments()}
213208
*/
214209
public static ArrayList<CustomInstrument> get1_12Instruments(){
210+
return getVersionCustomInstruments();
211+
}
212+
213+
public static ArrayList<CustomInstrument> getVersionCustomInstruments(){
215214
ArrayList<CustomInstrument> instruments = new ArrayList<>();
216-
instruments.add(new CustomInstrument((byte) 0, "Guitar", "guitar.ogg"));
217-
instruments.add(new CustomInstrument((byte) 0, "Flute", "flute.ogg"));
218-
instruments.add(new CustomInstrument((byte) 0, "Bell", "bell.ogg"));
219-
instruments.add(new CustomInstrument((byte) 0, "Chime", "icechime.ogg"));
220-
instruments.add(new CustomInstrument((byte) 0, "Xylophone", "xylobone.ogg"));
215+
if (getServerVersion() < 0.0112f){
216+
instruments.add(new CustomInstrument((byte) 0, "Guitar", "guitar.ogg"));
217+
instruments.add(new CustomInstrument((byte) 0, "Flute", "flute.ogg"));
218+
instruments.add(new CustomInstrument((byte) 0, "Bell", "bell.ogg"));
219+
instruments.add(new CustomInstrument((byte) 0, "Chime", "icechime.ogg"));
220+
instruments.add(new CustomInstrument((byte) 0, "Xylophone", "xylobone.ogg"));
221+
}
222+
223+
if (getServerVersion() < 0.0114f){
224+
//TODO: Implement once custom instruments problems will be solved
225+
}
221226
return instruments;
222227
}
223228

229+
public static float getServerVersion(){
230+
if (serverVersion != -1){
231+
return serverVersion;
232+
}
233+
234+
String versionInfo = Bukkit.getServer().getVersion();
235+
int start = versionInfo.lastIndexOf('(');
236+
int end = versionInfo.lastIndexOf(')');
237+
238+
String[] versionParts = versionInfo.substring(start + 5, end).split("\\.");
239+
240+
String versionString = "0.";
241+
for (String part : versionParts){
242+
if (part.length() == 1){
243+
versionString += "0";
244+
}
245+
246+
versionString += part;
247+
}
248+
serverVersion = Float.parseFloat(versionString);
249+
return serverVersion;
250+
}
251+
224252
}

0 commit comments

Comments
 (0)