Fixed loading error etc. PLAY WORKS

Ain't nobody needs a second init
This commit is contained in:
Norbi Peti 2017-11-15 22:48:37 +01:00
parent 58e5cb0536
commit b5ee3f0244
2 changed files with 23 additions and 21 deletions

View file

@ -80,18 +80,18 @@ public class Analyzer //Based on NativeBass example 'Spectrum'
private ByteBuffer buffer; private ByteBuffer buffer;
private TimerTask tt; private TimerTask tt;
public void run(CommandSender sender, String file) public boolean run(CommandSender sender, String file)
{ {
if (!init) if (!init)
{ {
return; return false;
} }
// check the correct BASS was loaded // check the correct BASS was loaded
if (((BASS_GetVersion() & 0xFFFF0000) >> 16) != BassInit.BASSVERSION()) if (((BASS_GetVersion() & 0xFFFF0000) >> 16) != BassInit.BASSVERSION())
{ {
printfExit("An incorrect version of BASS.DLL was loaded"); printfExit("An incorrect version of BASS.DLL was loaded");
return; return false;
} }
// initialize BASS // initialize BASS
@ -99,14 +99,14 @@ public class Analyzer //Based on NativeBass example 'Spectrum'
{ {
error("Can't initialize device", sender); error("Can't initialize device", sender);
stop(); stop();
return; return false;
} }
if (!playFile(sender, file)) if (!playFile(sender, file))
{ {
// start a file playing // start a file playing
BASS_Free(); BASS_Free();
stop(); stop();
return; return false;
} }
// setup update timer (50hz) // setup update timer (50hz)
timer.scheduleAtFixedRate(tt = new TimerTask() timer.scheduleAtFixedRate(tt = new TimerTask()
@ -117,6 +117,7 @@ public class Analyzer //Based on NativeBass example 'Spectrum'
BASS_ChannelGetData(chan, buffer, BASS_DATA_FFT2048); //Get the FFT data BASS_ChannelGetData(chan, buffer, BASS_DATA_FFT2048); //Get the FFT data
} }
}, 50, 50); }, 50, 50);
return true;
} }
public boolean isRunning() public boolean isRunning()
@ -141,6 +142,7 @@ public class Analyzer //Based on NativeBass example 'Spectrum'
if (!new File(file).exists()) if (!new File(file).exists())
{ {
sender.sendMessage("§cFile not found: " + file); sender.sendMessage("§cFile not found: " + file);
return false;
} }
HSTREAM stream = null; HSTREAM stream = null;
HMUSIC music = null; HMUSIC music = null;

View file

@ -33,19 +33,9 @@ public class PluginMain extends JavaPlugin
{ {
try try
{ {
System.setProperty("org.jouvieje.libloader.debug", "true"); //System.setProperty("org.jouvieje.libloader.debug", "true");
an = new Analyzer();
Bukkit.getConsoleSender().sendMessage("§bInitializing analyzer..."); Bukkit.getConsoleSender().sendMessage("§bInitializing analyzer...");
br = new BarsRenderer(bars = an.init()); an = new Analyzer();
for (short i = 0; i < 4; i++)
{
MapView map = Bukkit.getMap(i);
if (map == null)
map = Bukkit.createMap(Bukkit.getWorlds().get(0));
map.getRenderers().clear();
map.addRenderer(br);
}
//an.init(); - It's a good idea to use the libraries *after* they are loaded
URL dirURL = getClassLoader().getResource("res"); URL dirURL = getClassLoader().getResource("res");
String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!"));
JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8")); JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
@ -66,7 +56,15 @@ public class PluginMain extends JavaPlugin
jar.close(); jar.close();
for (File f : getDataFolder().listFiles()) for (File f : getDataFolder().listFiles())
addLibraryPath(f.getAbsolutePath()); addLibraryPath(f.getAbsolutePath());
an.init(); br = new BarsRenderer(bars = an.init());
for (short i = 0; i < 4; i++)
{
MapView map = Bukkit.getMap(i);
if (map == null)
map = Bukkit.createMap(Bukkit.getWorlds().get(0));
map.getRenderers().clear();
map.addRenderer(br);
}
Bukkit.getConsoleSender().sendMessage("§bDone!"); Bukkit.getConsoleSender().sendMessage("§bDone!");
} catch (Exception e) } catch (Exception e)
{ {
@ -104,8 +102,10 @@ public class PluginMain extends JavaPlugin
return true; return true;
} else if (command.getName().equalsIgnoreCase("play")) } else if (command.getName().equalsIgnoreCase("play"))
{ {
an.run(sender, Arrays.stream(args).skip(1).collect(Collectors.joining(" "))); if (an.run(sender, Arrays.stream(args).collect(Collectors.joining(" "))))
sender.sendMessage("Started playing music"); sender.sendMessage("Started playing music");
else
sender.sendMessage("§cFailed to play music.");
return true; return true;
} else } else
{ {
@ -123,7 +123,6 @@ public class PluginMain extends JavaPlugin
*/ */
public static void addLibraryPath(String pathToAdd) throws Exception public static void addLibraryPath(String pathToAdd) throws Exception
{ {
//System.out.println("Adding " + pathToAdd);
final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths"); final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
usrPathsField.setAccessible(true); usrPathsField.setAccessible(true);
@ -143,5 +142,6 @@ public class PluginMain extends JavaPlugin
final String[] newPaths = Arrays.copyOf(paths, paths.length + 1); final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
newPaths[newPaths.length - 1] = pathToAdd; newPaths[newPaths.length - 1] = pathToAdd;
usrPathsField.set(null, newPaths); usrPathsField.set(null, newPaths);
System.setProperty("java.library.path", Arrays.stream(newPaths).collect(Collectors.joining(";")));
} }
} }