Skip to content

Commit 256fd11

Browse files
committed
further refactor
added javadocs, I do not know all method/field uses for this API so some may be unclear any references to player names were changed to use UUIDs instead deprecate Instrument, move to InstrumentUtils for better clarity of the usage of the class
1 parent 9c1bdca commit 256fd11

19 files changed

+657
-104
lines changed

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

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@
99
import org.bukkit.Sound;
1010
import org.bukkit.entity.Player;
1111

12+
/**
13+
* Fields/methods for reflection & version checking
14+
*
15+
*/
1216
public class CompatibilityUtils {
1317

1418
public static final String OBC_DIR = Bukkit.getServer().getClass().getPackage().getName();
1519
public static final String NMS_DIR = OBC_DIR.replaceFirst("org.bukkit.craftbukkit", "net.minecraft.server");
1620

21+
/**
22+
* Gets NMS class from given name
23+
* @param name of class (w/ package)
24+
* @return Class of given name
25+
*/
1726
public static Class<?> getMinecraftClass(String name) {
1827
try {
1928
return Class.forName(NMS_DIR + "." + name);
@@ -23,6 +32,11 @@ public static Class<?> getMinecraftClass(String name) {
2332
}
2433
}
2534

35+
/**
36+
* Gets CraftBukkit class from given name
37+
* @param name of class (w/ package)
38+
* @return Class of given name
39+
*/
2640
public static Class<?> getCraftBukkitClass(String name) {
2741
try {
2842
return Class.forName(OBC_DIR + "." + name);
@@ -32,28 +46,23 @@ public static Class<?> getCraftBukkitClass(String name) {
3246
}
3347
}
3448

35-
public static class NoteBlockCompatibility {
36-
public static final int pre1_9 = 0;
37-
public static final int pre1_12 = 1;
38-
public static final int v1_12 = 2;
39-
public static final int post1_12 = 3;
40-
public static final int post1_13 = 4;
41-
}
42-
43-
protected static int getCompatibility() {
44-
if (Bukkit.getVersion().contains("1.8") || Bukkit.getVersion().contains("1.7")) {
45-
return NoteBlockCompatibility.pre1_9;
46-
} else if (Bukkit.getVersion().contains("1.9")
47-
|| Bukkit.getVersion().contains("1.10")
48-
|| Bukkit.getVersion().contains("1.11")) {
49-
return NoteBlockCompatibility.pre1_12;
50-
} else if (Bukkit.getVersion().contains("1.12")) {
51-
return NoteBlockCompatibility.v1_12;
52-
} else {
53-
return NoteBlockCompatibility.post1_13;
49+
/**
50+
* Returns whether the version of Bukkit is or is after 1.12
51+
* @return version is after 1.12
52+
*/
53+
public static boolean isPost1_12() {
54+
if (!isSoundCategoryCompatible() || Bukkit.getVersion().contains("1.11")) {
55+
return false;
5456
}
57+
return true;
5558
}
5659

60+
/**
61+
* Returns if SoundCategory is able to be used
62+
* @see org.bukkit.SoundCategory
63+
* @see SoundCategory
64+
* @return can use SoundCategory
65+
*/
5766
protected static boolean isSoundCategoryCompatible() {
5867
if (Bukkit.getVersion().contains("1.7")
5968
|| Bukkit.getVersion().contains("1.8")
@@ -65,6 +74,15 @@ protected static boolean isSoundCategoryCompatible() {
6574
}
6675
}
6776

77+
/**
78+
* Plays a sound using NMS & reflection
79+
* @param player
80+
* @param location
81+
* @param sound
82+
* @param category
83+
* @param volume
84+
* @param pitch
85+
*/
6886
protected static void playSound(Player player, Location location, String sound,
6987
SoundCategory category, float volume, float pitch) {
7088
try {
@@ -95,6 +113,15 @@ protected static void playSound(Player player, Location location, String sound,
95113
}
96114
}
97115

116+
/**
117+
* Plays a sound using NMS & reflection
118+
* @param player
119+
* @param location
120+
* @param sound
121+
* @param category
122+
* @param volume
123+
* @param pitch
124+
*/
98125
public static void playSound(Player player, Location location, Sound sound,
99126
SoundCategory category, float volume, float pitch) {
100127
try {
@@ -125,6 +152,10 @@ public static void playSound(Player player, Location location, Sound sound,
125152
}
126153
}
127154

155+
/**
156+
* Gets instruments which were added post-1.12
157+
* @return ArrayList of instruments
158+
*/
128159
public static ArrayList<CustomInstrument> get1_12Instruments(){
129160
ArrayList<CustomInstrument> instruments = new ArrayList<>();
130161
instruments.add(new CustomInstrument((byte) 0, "Guitar", "guitar.ogg"));

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package com.xxmicloxx.NoteBlockAPI;
22

3+
/**
4+
* Create custom instruments from a sound file
5+
*
6+
*/
37
public class CustomInstrument {
48

59
private byte index;
610
private String name;
711
private String soundFileName;
812
private org.bukkit.Sound sound;
913

10-
@Deprecated
14+
/**
15+
* Creates a CustomInstrument
16+
* @deprecated Unused parameters
17+
*/
1118
public CustomInstrument(byte index, String name, String soundFileName, byte pitch, byte press) {
1219
this.index = index;
1320
this.name = name;
@@ -17,6 +24,12 @@ public CustomInstrument(byte index, String name, String soundFileName, byte pitc
1724
}
1825
}
1926

27+
/**
28+
* Creates a CustomInstrument
29+
* @param index
30+
* @param name
31+
* @param soundFileName
32+
*/
2033
public CustomInstrument(byte index, String name, String soundFileName) {
2134
this.index = index;
2235
this.name = name;
@@ -26,23 +39,43 @@ public CustomInstrument(byte index, String name, String soundFileName) {
2639
}
2740
}
2841

42+
/**
43+
* Gets index of CustomInstrument
44+
* @return index
45+
*/
2946
public byte getIndex() {
3047
return index;
3148
}
3249

50+
/**
51+
* Gets name of CustomInstrument
52+
* @return name
53+
*/
3354
public String getName() {
3455
return name;
3556
}
3657

58+
/**
59+
* Gets file name of the sound
60+
* @deprecated misleading name
61+
*/
3762
@Deprecated
3863
public String getSoundfile() {
3964
return getSoundFileName();
4065
}
4166

67+
/**
68+
* Gets file name of the sound
69+
* @return file name
70+
*/
4271
public String getSoundFileName() {
4372
return soundFileName;
4473
}
4574

75+
/**
76+
* Gets the Sound enum for this CustomInstrument
77+
* @return
78+
*/
4679
public org.bukkit.Sound getSound() {
4780
return sound;
4881
}

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

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

3-
import com.xxmicloxx.NoteBlockAPI.CompatibilityUtils.NoteBlockCompatibility;
4-
3+
/**
4+
* Various methods for working with instruments
5+
* @deprecated Moved to InstrumentUtils
6+
*/
57
public class Instrument {
68

9+
/**
10+
* Returns the org.bukkit.Sound enum for the current server version
11+
* @param instrument
12+
* @see Sound
13+
* @return Sound enum (for the current server version)
14+
*/
715
public static org.bukkit.Sound getInstrument(byte instrument) {
816
return org.bukkit.Sound.valueOf(getInstrumentName(instrument));
917
}
1018

19+
/**
20+
* Returns the name of the org.bukkit.Sound enum for the current server version
21+
* @param instrument
22+
* @see Sound
23+
* @return Sound enum name (for the current server version)
24+
*/
1125
public static String getInstrumentName(byte instrument) {
1226
switch (instrument) {
1327
case 0:
@@ -35,6 +49,11 @@ public static String getInstrumentName(byte instrument) {
3549
}
3650
}
3751

52+
/**
53+
* Returns the name of the org.bukkit.Instrument enum for the current server version
54+
* @param instrument
55+
* @return Instrument enum (for the current server version)
56+
*/
3857
public static org.bukkit.Instrument getBukkitInstrument(byte instrument) {
3958
switch (instrument) {
4059
case 0:
@@ -62,26 +81,34 @@ public static org.bukkit.Instrument getBukkitInstrument(byte instrument) {
6281
}
6382
}
6483

84+
/**
85+
* If true, the byte given represents a custom instrument
86+
* @param instrument
87+
* @return
88+
*/
6589
public static boolean isCustomInstrument(byte instrument) {
66-
if (CompatibilityUtils.getCompatibility() != NoteBlockCompatibility.post1_12) {
67-
if (instrument > 4) {
68-
return true;
69-
}
70-
return false;
71-
} else {
90+
if (CompatibilityUtils.isPost1_12()) {
7291
if (instrument > 9) {
7392
return true;
7493
}
7594
return false;
7695
}
96+
if (instrument > 4) {
97+
return true;
98+
}
99+
return false;
77100
}
78101

102+
/**
103+
* Gets the first index in which a custom instrument
104+
* can be added to the existing list of instruments
105+
* @return instrument as a byte
106+
*/
79107
public static byte getCustomInstrumentFirstIndex() {
80-
if (CompatibilityUtils.getCompatibility() != NoteBlockCompatibility.post1_12) {
81-
return 5;
82-
} else {
108+
if (CompatibilityUtils.isPost1_12()) {
83109
return 10;
84110
}
111+
return 5;
85112
}
86113

87114
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.xxmicloxx.NoteBlockAPI;
2+
3+
/**
4+
* Various methods for working with instruments
5+
*/
6+
public class InstrumentUtils {
7+
8+
/**
9+
* Returns the org.bukkit.Sound enum for the current server version
10+
* @param instrument
11+
* @see Sound
12+
* @return Sound enum (for the current server version)
13+
*/
14+
public static org.bukkit.Sound getInstrument(byte instrument) {
15+
return org.bukkit.Sound.valueOf(getInstrumentName(instrument));
16+
}
17+
18+
/**
19+
* Returns the name of the org.bukkit.Sound enum for the current server version
20+
* @param instrument
21+
* @see Sound
22+
* @return Sound enum name (for the current server version)
23+
*/
24+
public static String getInstrumentName(byte instrument) {
25+
switch (instrument) {
26+
case 0:
27+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_HARP").name();
28+
case 1:
29+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_BASS").name();
30+
case 2:
31+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_BASEDRUM").name();
32+
case 3:
33+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_SNARE").name();
34+
case 4:
35+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_HAT").name();
36+
case 5:
37+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_GUITAR").name();
38+
case 6:
39+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_FLUTE").name();
40+
case 7:
41+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_BELL").name();
42+
case 8:
43+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_CHIME").name();
44+
case 9:
45+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_XYLOPHONE").name();
46+
default:
47+
return Sound.getFromBukkitName("BLOCK_NOTE_BLOCK_HARP").name();
48+
}
49+
}
50+
51+
/**
52+
* Returns the name of the org.bukkit.Instrument enum for the current server version
53+
* @param instrument
54+
* @return Instrument enum (for the current server version)
55+
*/
56+
public static org.bukkit.Instrument getBukkitInstrument(byte instrument) {
57+
switch (instrument) {
58+
case 0:
59+
return org.bukkit.Instrument.PIANO;
60+
case 1:
61+
return org.bukkit.Instrument.BASS_GUITAR;
62+
case 2:
63+
return org.bukkit.Instrument.BASS_DRUM;
64+
case 3:
65+
return org.bukkit.Instrument.SNARE_DRUM;
66+
case 4:
67+
return org.bukkit.Instrument.STICKS;
68+
case 5:
69+
return org.bukkit.Instrument.GUITAR;
70+
case 6:
71+
return org.bukkit.Instrument.FLUTE;
72+
case 7:
73+
return org.bukkit.Instrument.BELL;
74+
case 8:
75+
return org.bukkit.Instrument.CHIME;
76+
case 9:
77+
return org.bukkit.Instrument.XYLOPHONE;
78+
default:
79+
return org.bukkit.Instrument.PIANO;
80+
}
81+
}
82+
83+
/**
84+
* If true, the byte given represents a custom instrument
85+
* @param instrument
86+
* @return
87+
*/
88+
public static boolean isCustomInstrument(byte instrument) {
89+
if (CompatibilityUtils.isPost1_12()) {
90+
if (instrument > 9) {
91+
return true;
92+
}
93+
return false;
94+
}
95+
if (instrument > 4) {
96+
return true;
97+
}
98+
return false;
99+
}
100+
101+
/**
102+
* Gets the first index in which a custom instrument
103+
* can be added to the existing list of instruments
104+
* @return instrument as a byte
105+
*/
106+
public static byte getCustomInstrumentFirstIndex() {
107+
if (CompatibilityUtils.isPost1_12()) {
108+
return 10;
109+
}
110+
return 5;
111+
}
112+
113+
}

0 commit comments

Comments
 (0)