Skip to content

Commit 060de7c

Browse files
committed
Added TimeUtils (#15), setPlaylist method
1 parent 6599ecf commit 060de7c

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.xxmicloxx</groupId>
88
<artifactId>NoteBlockAPI</artifactId>
9-
<version>1.2.2.1</version>
9+
<version>1.2.3</version>
1010
<name>NoteBlockAPI</name>
1111

1212
<properties>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,14 @@ public Playlist getPlaylist() {
594594
return playlist;
595595
}
596596

597+
/**
598+
* Sets the Playlist being played by this SongPlayer. Will affect next Song
599+
* @return
600+
*/
601+
public void setPlaylist(Playlist playlist) {
602+
this.playlist = playlist;
603+
}
604+
597605
/**
598606
* Get index of actually played {@link Song} in {@link Playlist}
599607
* @return
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.xxmicloxx.NoteBlockAPI.utils;
2+
3+
import com.xxmicloxx.NoteBlockAPI.songplayer.SongPlayer;
4+
5+
public class TimeUtils {
6+
7+
/**
8+
* Returns {@link SongPlayer} song actual time in specified format
9+
* @param format use:
10+
* <ul>
11+
* <li>hh for two-digit hours</li>
12+
* <li>h for one-digit hours</li>
13+
* <li>mm for two-digit minutes</li>
14+
* <li>m for one-digit minutes</li>
15+
* <li>ss for two-digit seconds</li>
16+
* <li>s for one-digit seconds</li>
17+
* <li>m for miliseconds (do not use without seconds, would be more than 4 digits)</li>
18+
* @param songPlayer
19+
* @return formatted string
20+
*/
21+
public static String getActualTime(String format, SongPlayer songPlayer){
22+
return getTime(format, songPlayer.getTick(), songPlayer.getSong().getSpeed());
23+
}
24+
25+
/**
26+
* Returns {@link SongPlayer} song length in specified format
27+
* @param format use:
28+
* <ul>
29+
* <li>hh for two-digit hours</li>
30+
* <li>h for one-digit hours</li>
31+
* <li>mm for two-digit minutes</li>
32+
* <li>m for one-digit minutes</li>
33+
* <li>ss for two-digit seconds</li>
34+
* <li>s for one-digit seconds</li>
35+
* <li>m for miliseconds (do not use without seconds, would be more than 4 digits)</li>
36+
* @param songPlayer
37+
* @return formatted string
38+
*/
39+
public static String getLength(String format, SongPlayer songPlayer){
40+
return getTime(format, songPlayer.getSong().getLength(), songPlayer.getSong().getSpeed());
41+
}
42+
43+
private static String getTime(String format, short ticks, float speed){
44+
String time = format;
45+
long milisTotal = (long) ((ticks / speed) * 1000);
46+
47+
int hours = 0;
48+
if (time.contains("h")){
49+
hours = (int) Math.floor(milisTotal / 1000 / 60 / 60);
50+
milisTotal -= hours * 1000 * 60 * 60;
51+
}
52+
53+
int minutes = 0;
54+
if (time.contains("m")){
55+
minutes = (int) Math.floor(milisTotal / 1000 / 60);
56+
milisTotal -= minutes * 1000 * 60;
57+
}
58+
59+
int seconds = 0;
60+
if (time.contains("s")){
61+
seconds = (int) Math.floor(milisTotal / 1000);
62+
milisTotal -= seconds * 1000;
63+
}
64+
65+
time = time.replace("hh", String.format("%02", hours));
66+
time = time.replace("h", hours + "");
67+
68+
time = time.replace("mm", String.format("%02", minutes));
69+
time = time.replace("m", minutes + "");
70+
71+
time = time.replace("ss", String.format("%02", seconds));
72+
time = time.replace("s", seconds + "");
73+
74+
time = time.replace("n", milisTotal + "");
75+
76+
return time;
77+
}
78+
}

0 commit comments

Comments
 (0)