Simple, yet not so simple.

Discussion in 'Plugin Development' started by BlueMustache, Jun 16, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    BlueMustache

    Hey Guys,
    I didn't know if you had any quick examples.
    I know your not gonna "spoonfeed" some code.
    How might I check all the plugins, in the server's plugin folder, to see if they depend or soft depend on a plugin.
    Thanks,
    -Blue
     
  2. Offline

    DoctorDark

    BlueMustache

    You could try something like this:
    Code:java
    1. for (Plugin plugins : Bukkit.getServer().getPluginManager().getPlugins()) {
    2.  
    3. if (plugins.getDescription().getDepend().contains("pluginname")) {
    4.  
    5. System.out.println("test");
    6.  
    7. }
    8.  
    9. }
     
  3. Offline

    1Rogue

    Code:java
    1. public void copyPluginYaml(JarFile jar, File base) {
    2. for (Enumeration<JarEntry> enu = jar.entries(); enu.hasMoreElements();) {
    3. JarEntry entry = enu.next();
    4. if ("plugin.yml".equalsIgnoreCase(entry.getName()) {
    5. File f = new File(base, jar.getName() + "-plugin.yml");
    6. InputStream is = jar.getInputStream(entry);
    7. FileOutputStream fos = new java.io.FileOutputStream(f);
    8. while (is.available() > 0) {
    9. fos.write(is.read());
    10. }
    11. fos.close();
    12. is.close();
    13. }
    14. }


    Example implementation:

    Code:java
    1. File base = new File(plugin.getDataFolder(), "temp" + File.separator);
    2. for (File f : /* your plugin folder file reference */.listFiles(/* can supply a file filter for just jarfiles */)) {
    3. JarFile jar = new JarFile(f);
    4. copyPluginYaml(jar, base);
    5. }


    From there it's a matter of just reading all the files for the "softdepend" / "depend" line.

    However DoctorDark wrote a much simpler, API-orientated way of doing it rather than reading jarfiles if you'd rather that route.
     
  4. Offline

    BlueMustache

    DoctorDark

    Thanks!
    But, am I doing something wrong here?

    Code:java
    1. static String getDependantPlugins()
    2. {
    3. String toreturn = "DEPENDING: ";
    4. for (Plugin plugins : Bukkit.getServer().getPluginManager().getPlugins()) {
    5. if ((plugins.getDescription().getDepend().contains("ForceResource")) || (plugins.getDescription().getSoftDepend().contains("ForceResource"))) {
    6. toreturn = toreturn + plugins.getName() + ", ";
    7. return toreturn;
    8. }
    9. }
    10. return null;
    11. }
     
  5. Offline

    desht

    You're returning the string as soon a plugin which has a dependency on ForceResource is encountered, so your method will return at most one plugin.

    Also, string concatenation inside a loop is a Bad Thing™ - http://kaioa.com/node/59
     
  6. Offline

    BlueMustache

    desht

    A Bukkit pro has spoken!
    Thanks for pointing that out.
    I was trying to use List for the first time, but that kinda flopped.
    Any alternatives to List and String.
    Where can I get the names of all the plugins that are using my plugin?
    Thank You.
    -Blue
     
Thread Status:
Not open for further replies.

Share This Page