Skip to content

Commit 96fa91d

Browse files
author
koca2000
committed
v1.1.3
Api is now more thread save.
1 parent bbbc661 commit 96fa91d

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
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.1.2</version>
9+
<version>1.1.3</version>
1010
<repositories>
1111
<repository>
1212
<id>spigot-repo</id>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class NoteBlockPlayerMain extends JavaPlugin {
1313
public HashMap<String, ArrayList<SongPlayer>> playingSongs = new HashMap<String, ArrayList<SongPlayer>>();
1414
public HashMap<String, Byte> playerVolume = new HashMap<String, Byte>();
1515

16+
public boolean disabling = false;
17+
1618
public static boolean isReceivingSong(Player p) {
1719
return ((plugin.playingSongs.get(p.getName()) != null) && (!plugin.playingSongs.get(p.getName()).isEmpty()));
1820
}
@@ -46,6 +48,7 @@ public void onEnable() {
4648

4749
@Override
4850
public void onDisable() {
51+
disabling = true;
4952
Bukkit.getScheduler().cancelTasks(this);
5053
}
5154
}

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.ArrayList;
77
import java.util.Collections;
88
import java.util.List;
9+
import java.util.concurrent.Callable;
910

1011
public abstract class SongPlayer {
1112

@@ -78,12 +79,12 @@ protected void calculateFade() {
7879
}
7980

8081
protected void createThread() {
81-
playerThread = new Thread(new Runnable() {
82+
playerThread = new Thread(new Runnable() {
8283
@Override
8384
public void run() {
8485
while (!destroyed) {
8586
long startTime = System.currentTimeMillis();
86-
synchronized (SongPlayer.this) {
87+
/*synchronized (SongPlayer.this) {
8788
if (playing) {
8889
calculateFade();
8990
tick++;
@@ -107,7 +108,50 @@ public void run() {
107108
playTick(p, tick);
108109
}
109110
}
111+
}*/
112+
113+
try {
114+
synchronized (Bukkit.getScheduler()) {
115+
if (destroyed || NoteBlockPlayerMain.plugin.disabling){
116+
break;
117+
}
118+
Bukkit.getScheduler().callSyncMethod(NoteBlockPlayerMain.plugin, new Callable<Boolean>() {
119+
public Boolean call() {
120+
if (playing) {
121+
calculateFade();
122+
tick++;
123+
if (tick > song.getLength()) {
124+
playing = false;
125+
tick = -1;
126+
SongEndEvent event = new SongEndEvent(SongPlayer.this);
127+
Bukkit.getPluginManager().callEvent(event);
128+
if (autoDestroy) {
129+
destroy();
130+
return false;
131+
}
132+
}
133+
for (String s : playerList) {
134+
@SuppressWarnings("deprecation")
135+
Player p = Bukkit.getPlayerExact(s);
136+
if (p == null) {
137+
// offline...
138+
continue;
139+
}
140+
playTick(p, tick);
141+
}
142+
}
143+
return true;
144+
}
145+
});
146+
}
147+
} catch (java.lang.IllegalStateException e) {
148+
e.printStackTrace();
149+
}
150+
151+
if (destroyed){
152+
break;
110153
}
154+
111155
long duration = System.currentTimeMillis() - startTime;
112156
float delayMillis = song.getDelay() * 50;
113157
if (duration < delayMillis) {

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: NoteBlockAPI
22

33
main: com.xxmicloxx.NoteBlockAPI.NoteBlockPlayerMain
4-
version: 1.1.2
4+
version: 1.1.3
55

66
description: a developer interface to play nbs-files ingame
77
authors: [xxmicloxx, michidk, koca2000]

0 commit comments

Comments
 (0)