Now playing fix
This commit is contained in:
parent
2e49611035
commit
851b613d2e
@ -16,10 +16,10 @@ public class NBSReader
|
||||
{
|
||||
public static NoteSong loadSong(String fileName) throws FileNotFoundException
|
||||
{
|
||||
return loadSong(new DataInputStream(new FileInputStream(new File(fileName))));
|
||||
return loadSong(new DataInputStream(new FileInputStream(new File(fileName))), fileName);
|
||||
}
|
||||
|
||||
public static NoteSong loadSong(DataInputStream stream)
|
||||
public static NoteSong loadSong(DataInputStream stream, String defaultName)
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -41,6 +41,9 @@ public class NBSReader
|
||||
int blocksRemoved = readInt(stream);
|
||||
String midiFileName = readString(stream);
|
||||
|
||||
if ((name == null || name.length() == 0) && defaultName != null)
|
||||
name = defaultName;
|
||||
|
||||
HashMap<Integer, NoteLayer> layerMap = new HashMap<Integer, NoteLayer>();
|
||||
|
||||
// Note Block Information
|
||||
|
@ -1,10 +1,7 @@
|
||||
package mineplex.core.noteblock;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
@ -43,38 +40,49 @@ public class NotePlayer
|
||||
|
||||
private void startThread()
|
||||
{
|
||||
Thread thread = new Thread(() -> {
|
||||
while (!_finished)
|
||||
Thread thread = new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
_tick++;
|
||||
if (_tick > _song.getLength())
|
||||
long startTime = System.currentTimeMillis();
|
||||
while (!_finished)
|
||||
{
|
||||
if (_loop)
|
||||
_tick++;
|
||||
if (_tick > _song.getLength())
|
||||
{
|
||||
_tick = 1;
|
||||
if (_loop)
|
||||
{
|
||||
_tick = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
_finished = true;
|
||||
|
||||
_plugin.getServer().getScheduler().runTask(_plugin, new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
SongFinishEvent event = new SongFinishEvent(_song);
|
||||
_plugin.getServer().getPluginManager().callEvent(event);
|
||||
}
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
playTick(_tick);
|
||||
|
||||
try
|
||||
{
|
||||
_finished = true;
|
||||
|
||||
_plugin.getServer().getScheduler().runTask(_plugin, () -> {
|
||||
SongFinishEvent event = new SongFinishEvent(_song);
|
||||
_plugin.getServer().getPluginManager().callEvent(event);
|
||||
});
|
||||
|
||||
return;
|
||||
Thread.sleep(_sleepMs);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
playTick(_tick);
|
||||
|
||||
try
|
||||
{
|
||||
Thread.sleep(_sleepMs);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -82,18 +90,28 @@ public class NotePlayer
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private void playNote(Note note, float volume, Collection<? extends Player> players)
|
||||
{
|
||||
players.stream().filter(_verifier::shouldPlay).forEach((player) ->
|
||||
player.playSound(player.getEyeLocation(), UtilNote.getInstrumentSound(note.getInstrument()), volume, (float) UtilNote.getPitch(note.getNote() - 33)));
|
||||
}
|
||||
|
||||
private void playTick(int tick)
|
||||
{
|
||||
Collection<? extends Player> players = UtilServer.getPlayersCollection();
|
||||
Player[] playerArray = UtilServer.getPlayers();
|
||||
List<Player> players = new ArrayList<>(playerArray.length);
|
||||
for (Player player : playerArray)
|
||||
{
|
||||
if (_verifier.shouldPlay(player))
|
||||
players.add(player);
|
||||
}
|
||||
|
||||
float volume = _volumeMult * 1F;
|
||||
_song.getLayers().stream().map(layer -> layer.getNote(tick)).filter(Objects::nonNull).forEach((note) -> playNote(note, volume, players));
|
||||
for (NoteLayer layer : _song.getLayers())
|
||||
{
|
||||
Note note = layer.getNote(tick);
|
||||
if (note != null)
|
||||
{
|
||||
float volume = _volumeMult * (layer.getVolume() / 100F);
|
||||
for (Player player : players)
|
||||
{
|
||||
player.playSound(player.getEyeLocation(), UtilNote.getInstrumentSound(note.getInstrument()), volume, (float) UtilNote.getPitch(note.getNote() - 33));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void cancel()
|
||||
|
@ -261,7 +261,7 @@ public class HubManager extends MiniClientPlugin<HubClient>
|
||||
if (file.getName().endsWith(".nbs"))
|
||||
{
|
||||
System.out.println("Loading Song " + file.getPath());
|
||||
NoteSong song = NBSReader.loadSong(new DataInputStream(new FileInputStream(file)));
|
||||
NoteSong song = NBSReader.loadSong(new DataInputStream(new FileInputStream(file)), file.getName().replace("_", " ").replace(".nbs", ""));
|
||||
if (song != null)
|
||||
{
|
||||
_songs.add(song);
|
||||
|
Loading…
Reference in New Issue
Block a user