Skip to content

Commit 9cd518e

Browse files
committed
Added support for new .nbs structure #28, option to specify position of songs added to playlist #29
Custom instruments may not work in this build.
1 parent f9ff893 commit 9cd518e

File tree

5 files changed

+118
-19
lines changed

5 files changed

+118
-19
lines changed

pom.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,23 @@
2323
<id>jitpack.io</id>
2424
<url>https://jitpack.io</url>
2525
</repository>
26+
<repository>
27+
<id>CodeMC</id>
28+
<url>https://repo.codemc.org/repository/maven-public</url>
29+
</repository>
2630
</repositories>
2731

2832
<dependencies>
2933
<dependency>
3034
<groupId>org.spigotmc</groupId>
3135
<artifactId>spigot-api</artifactId>
32-
<version>1.14-pre5-SNAPSHOT</version>
36+
<version>1.14-R0.1-SNAPSHOT</version>
3337
<scope>provided</scope>
3438
</dependency>
3539
<dependency>
36-
<groupId>org.bstats.bStats-Metrics</groupId>
40+
<groupId>org.bstats</groupId>
3741
<artifactId>bstats-bukkit</artifactId>
38-
<version>1.3</version>
42+
<version>1.5</version>
3943
<scope>compile</scope>
4044
</dependency>
4145
</dependencies>

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ public void add(Song ...songs){
2727
checkNull(songs);
2828
this.songs.addAll(Arrays.asList(songs));
2929
}
30+
31+
/**
32+
* Insert array of {@link Song} at a specified index
33+
* @param index
34+
* @param songs
35+
*/
36+
public void insert(int index, Song ...songs){
37+
if (songs.length == 0){
38+
return;
39+
}
40+
if (index > this.songs.size()){
41+
throw new IllegalArgumentException("Index is higher than playlist size");
42+
}
43+
checkNull(songs);
44+
this.songs.addAll(index, Arrays.asList(songs));
45+
}
3046

3147
private void checkNull(Song ...songs){
3248
List<Song> songList = Arrays.asList(songs);

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

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

3+
import com.xxmicloxx.NoteBlockAPI.utils.InstrumentUtils;
4+
35
import java.io.File;
46
import java.util.HashMap;
57

@@ -19,6 +21,7 @@ public class Song implements Cloneable {
1921
private float speed;
2022
private float delay;
2123
private CustomInstrument[] customInstruments;
24+
private int firstCustomInstrumentIndex;
2225

2326
/**
2427
* Create Song instance by copying other Song parameters
@@ -27,18 +30,53 @@ public class Song implements Cloneable {
2730
public Song(Song other) {
2831
this(other.getSpeed(), other.getLayerHashMap(), other.getSongHeight(),
2932
other.getLength(), other.getTitle(), other.getAuthor(),
30-
other.getDescription(), other.getPath(), other.getCustomInstruments());
33+
other.getDescription(), other.getPath(), other.getFirstCustomInstrumentIndex(), other.getCustomInstruments());
3134
}
3235

36+
/**
37+
* @deprecated Use {@link #Song(float, HashMap, short, short, String, String, String, File, int)}
38+
* @param speed
39+
* @param layerHashMap
40+
* @param songHeight
41+
* @param length
42+
* @param title
43+
* @param author
44+
* @param description
45+
* @param path
46+
*/
3347
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
3448
short songHeight, final short length, String title, String author,
3549
String description, File path) {
36-
this(speed, layerHashMap, songHeight, length, title, author, description, path, new CustomInstrument[0]);
50+
this(speed, layerHashMap, songHeight, length, title, author, description, path, InstrumentUtils.getCustomInstrumentFirstIndex(), new CustomInstrument[0]);
51+
}
52+
53+
/**
54+
* @deprecated Use {@link #Song(float, HashMap, short, short, String, String, String, File, int, CustomInstrument[])}
55+
* @param speed
56+
* @param layerHashMap
57+
* @param songHeight
58+
* @param length
59+
* @param title
60+
* @param author
61+
* @param description
62+
* @param path
63+
* @param customInstruments
64+
*/
65+
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
66+
short songHeight, final short length, String title, String author,
67+
String description, File path, CustomInstrument[] customInstruments) {
68+
this(speed, layerHashMap, songHeight, length, title, author, description, path, InstrumentUtils.getCustomInstrumentFirstIndex(), customInstruments);
69+
}
70+
71+
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
72+
short songHeight, final short length, String title, String author,
73+
String description, File path, int firstCustomInstrumentIndex) {
74+
this(speed, layerHashMap, songHeight, length, title, author, description, path, firstCustomInstrumentIndex, new CustomInstrument[0]);
3775
}
3876

3977
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
4078
short songHeight, final short length, String title, String author,
41-
String description, File path, CustomInstrument[] customInstruments) {
79+
String description, File path, int firstCustomInstrumentIndex, CustomInstrument[] customInstruments) {
4280
this.speed = speed;
4381
delay = 20 / speed;
4482
this.layerHashMap = layerHashMap;
@@ -48,6 +86,7 @@ public Song(float speed, HashMap<Integer, Layer> layerHashMap,
4886
this.author = author;
4987
this.description = description;
5088
this.path = path;
89+
this.firstCustomInstrumentIndex = firstCustomInstrumentIndex;
5190
this.customInstruments = customInstruments;
5291
}
5392

@@ -137,6 +176,7 @@ public Song clone() {
137176
return new Song(this);
138177
}
139178

140-
141-
179+
public int getFirstCustomInstrumentIndex() {
180+
return firstCustomInstrumentIndex;
181+
}
142182
}

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,28 +204,53 @@ public static void playSound(Player player, Location location, Sound sound,
204204
/**
205205
* Gets instruments which were added post-1.12
206206
* @return ArrayList of instruments
207-
* @deprecated Use {@link #getVersionCustomInstruments()}
207+
* @deprecated Alternative not ready yet
208208
*/
209209
public static ArrayList<CustomInstrument> get1_12Instruments(){
210-
return getVersionCustomInstruments();
210+
ArrayList<CustomInstrument> instruments = new ArrayList<>();
211+
instruments.add(new CustomInstrument((byte) 0, "Guitar", "guitar.ogg"));
212+
instruments.add(new CustomInstrument((byte) 0, "Flute", "flute.ogg"));
213+
instruments.add(new CustomInstrument((byte) 0, "Bell", "bell.ogg"));
214+
instruments.add(new CustomInstrument((byte) 0, "Chime", "icechime.ogg"));
215+
instruments.add(new CustomInstrument((byte) 0, "Xylophone", "xylobone.ogg"));
216+
return instruments;
211217
}
212218

213-
public static ArrayList<CustomInstrument> getVersionCustomInstruments(){
219+
/* NOT READY TO USE YET
220+
public static ArrayList<CustomInstrument> getVersionCustomInstruments(float serverVersion){
214221
ArrayList<CustomInstrument> instruments = new ArrayList<>();
215-
if (getServerVersion() < 0.0112f){
222+
if (serverVersion < 0.0112f){
216223
instruments.add(new CustomInstrument((byte) 0, "Guitar", "guitar.ogg"));
217224
instruments.add(new CustomInstrument((byte) 0, "Flute", "flute.ogg"));
218225
instruments.add(new CustomInstrument((byte) 0, "Bell", "bell.ogg"));
219226
instruments.add(new CustomInstrument((byte) 0, "Chime", "icechime.ogg"));
220227
instruments.add(new CustomInstrument((byte) 0, "Xylophone", "xylobone.ogg"));
221228
}
222229
223-
if (getServerVersion() < 0.0114f){
224-
//TODO: Implement once custom instruments problems will be solved
230+
if (serverVersion < 0.0114f){
231+
instruments.add(new CustomInstrument((byte) 0, "Iron Xylophone", "iron_xylophone.ogg"));
232+
instruments.add(new CustomInstrument((byte) 0, "Cow Bell", "cow_bell.ogg"));
233+
instruments.add(new CustomInstrument((byte) 0, "Didgeridoo", "didgeridoo.ogg"));
234+
instruments.add(new CustomInstrument((byte) 0, "Bit", "bit.ogg"));
235+
instruments.add(new CustomInstrument((byte) 0, "Banjo", "banjo.ogg"));
236+
instruments.add(new CustomInstrument((byte) 0, "Pling", "pling.ogg"));
225237
}
226238
return instruments;
227239
}
228240
241+
public static ArrayList<CustomInstrument> getVersionCustomInstrumentsForSong(int firstCustomInstrumentIndex){
242+
ArrayList<CustomInstrument> instruments = new ArrayList<>();
243+
244+
if (getServerVersion() < 0.0112f && firstCustomInstrumentIndex < 10){
245+
instruments.addAll(getVersionCustomInstruments(0.0111f));
246+
} else if (getServerVersion() < 0.0114f && firstCustomInstrumentIndex < 16){
247+
instruments.addAll(getVersionCustomInstruments(0.0113f));
248+
}
249+
250+
return instruments;
251+
}*/
252+
253+
229254
public static float getServerVersion(){
230255
if (serverVersion != -1){
231256
return serverVersion;

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ private static Song parse(InputStream inputStream, File songFile) {
6363
try {
6464
DataInputStream dataInputStream = new DataInputStream(inputStream);
6565
short length = readShort(dataInputStream);
66+
int firstcustominstrument = InstrumentUtils.getCustomInstrumentFirstIndex();
67+
int firstcustominstrumentdiff = 0;
68+
int nbsversion = 0;
69+
if (length == 0){
70+
nbsversion = dataInputStream.readByte();
71+
firstcustominstrument = dataInputStream.readByte();
72+
firstcustominstrumentdiff = InstrumentUtils.getCustomInstrumentFirstIndex() - firstcustominstrument;
73+
}
6674
short songHeight = readShort(dataInputStream);
6775
String title = readString(dataInputStream);
6876
String author = readString(dataInputStream);
@@ -96,13 +104,19 @@ private static Song parse(InputStream inputStream, File songFile) {
96104
layer += jumpLayers;
97105
//System.out.println("Layer: " + layer);
98106
byte instrument = dataInputStream.readByte();
99-
if (instrument > biggestInstrumentIndex) {
100-
biggestInstrumentIndex = instrument;
107+
108+
if (firstcustominstrumentdiff > 0 && instrument >= firstcustominstrument){
109+
instrument += firstcustominstrumentdiff;
101110
}
102111
setNote(layer, tick, instrument /* instrument */,
103112
dataInputStream.readByte() /* note */, layerHashMap);
104113
}
105114
}
115+
116+
if (nbsversion > 0) {
117+
length = tick;
118+
}
119+
106120
for (int i = 0; i < songHeight; i++) {
107121
Layer layer = layerHashMap.get(i);
108122

@@ -124,14 +138,14 @@ private static Song parse(InputStream inputStream, File songFile) {
124138
dataInputStream.readByte();//key
125139
}
126140

127-
if (InstrumentUtils.isCustomInstrument((byte) (biggestInstrumentIndex - customAmnt))) {
128-
ArrayList<CustomInstrument> customInstruments = CompatibilityUtils.get1_12Instruments();
141+
if (firstcustominstrumentdiff < 0){
142+
ArrayList<CustomInstrument> customInstruments = CompatibilityUtils.get1_12Instruments();//CompatibilityUtils.getVersionCustomInstrumentsForSong(firstcustominstrument);
129143
customInstruments.addAll(Arrays.asList(customInstrumentsArray));
130144
customInstrumentsArray = customInstruments.toArray(customInstrumentsArray);
131145
}
132146

133147
return new Song(speed, layerHashMap, songHeight, length, title,
134-
author, description, songFile, customInstrumentsArray);
148+
author, description, songFile, firstcustominstrument, customInstrumentsArray);
135149
} catch (FileNotFoundException e) {
136150
e.printStackTrace();
137151
} catch (EOFException e) {

0 commit comments

Comments
 (0)