Solved Having troubles with softdepends

Discussion in 'Plugin Development' started by OrangeGuy, May 26, 2014.

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

    OrangeGuy

    Hi everyone, I coded a plugin that softdepends ProtocolLib and Vault, everything works fine with them. However, once I delected ProtocolLib and Vault, my plugin can not been loaded.

    Here is a part of my "onEnable" method:
    Code:java
    1. /*
    2.   * Listen on PacketEvent in order to remove invisible lore
    3.   */
    4. if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null)
    5. {
    6. PacketAdapter itemPacketListener = new PacketAdapter(this,
    7. PacketType.Play.Server.WINDOW_ITEMS)
    8. {
    9. @Override
    10. public void onPacketSending(PacketEvent event)
    11. {
    12. PacketContainer packet = event.getPacket();
    13.  
    14. for (ItemStack item : packet.getItemArrayModifier().read(0))
    15. {
    16. if (item != null
    17. && item.getType() != Material.AIR
    18. && item.hasItemMeta()
    19. && item.getItemMeta().hasLore()
    20. && unhideString(item.getItemMeta().getLore().get(0))
    21. .startsWith(ConfigLoader.getIdentifyString()))
    22. {
    23. // Remove the invisible lore
    24. ItemMeta meta = item.getItemMeta();
    25. meta.setLore(null);
    26. item.setItemMeta(meta);
    27. }
    28. }
    29. }
    30. };
    31. ProtocolLibrary.getProtocolManager().addPacketListener(itemPacketListener);
    32.  
    33. log("Hooked into ProtocolLib successfully.");
    34. }
    35. else
    36. {
    37. log("ProtocolLib not found, skipped.");
    38. }
    39.  
    40. /*
    41.   * Connect with Vault for economy support
    42.   */
    43. if (setupEconomy())
    44. {
    45. log("Hooked into Vault successfully.");
    46. }
    47. else
    48. {
    49. log("Vault not found. Economy support disabled.");
    50. }


    And this is my plugin.yml:
    Code:
    name: SurpriseEggs
    version: 2.0.0
    description: >
                What does a dragon egg contain?
    author: chengzi368
    website: http://dev.bukkit.org/bukkit-plugins/surprise-eggs/
     
    main: me.chengzi368.SurpriseEggs.SurpriseEggs
    softdepend: [ProtocolLib, Vault]
     
    commands:
      SurpriseEggs:
        description: Main command of SurpriseEggs
        usage: |
              /<command> give [type] - Give yourself a dragon egg of that type.
              /<command> open - Open the dragon egg in your hand.
              /<command> reload - Reload configurations.
              /<command> help - Display plugin's help messages.
        aliases: [seggs, se]
     
    permissions:
      SurpriseEggs.open:
        description: Use command to open dragon eggs
        default: op
      SurpriseEggs.use:
        description: Right click to open dragon eggs
        default: op
      SurpriseEggs.give:
        description: Give yourself a dragon egg
        default: op
      SurpriseEggs.reload:
        description: Reload configurations
        default: op
      SurpriseEggs.bypass:
        description: Allows you to place any kind of dragon eggs
        default: false
    
    Where's the problem? :(
    Thanks - OrangeGuy

    And here's the error log:
    Code:
    [21:08:34 WARN]: org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: com/comphenix/protocol/events/PacketListener
    [21:08:34 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131)
    [21:08:34 WARN]:        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329)
    [21:08:34 WARN]:        at com.rylinaux.plugman.util.PluginUtil.load(PluginUtil.java:263)
    [21:08:34 WARN]:        at com.rylinaux.plugman.command.LoadCommand.execute(LoadCommand.java:114)
    [21:08:34 WARN]:        at com.rylinaux.plugman.PlugManCommands.onCommand(PlugManCommands.java:100)
    [21:08:34 WARN]:        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [21:08:34 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180)
    [21:08:34 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:726)
    [21:08:34 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchServerCommand(CraftServer.java:713)
    [21:08:34 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.aB(DedicatedServer.java:319)
    [21:08:34 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:284)
    [21:08:34 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:576)
    [21:08:34 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:482)
    [21:08:34 WARN]:        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628)
    [21:08:34 WARN]: Caused by: java.lang.NoClassDefFoundError: com/comphenix/protocol/events/PacketListener
    [21:08:34 WARN]:        at java.lang.Class.forName0(Native Method)
    [21:08:34 WARN]:        at java.lang.Class.forName(Class.java:266)
    [21:08:34 WARN]:        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:40)
    [21:08:34 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:127)
    [21:08:34 WARN]:        ... 13 more
    [21:08:34 WARN]: Caused by: java.lang.ClassNotFoundException: com.comphenix.protocol.events.PacketListener
    [21:08:34 WARN]:        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    [21:08:34 WARN]:        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    [21:08:34 WARN]:        at java.security.AccessController.doPrivileged(Native Method)
    [21:08:34 WARN]:        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    [21:08:34 WARN]:        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:77)
    [21:08:34 WARN]:        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:62)
    [21:08:34 WARN]:        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    [21:08:34 WARN]:        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    [21:08:34 WARN]:        ... 17 more
    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  2. Offline

    mythbusterma

    Look at the exception, it explictly says that it is a NoClassDefFoundError. It's looking far a class that it can't find, in this case ..../events/PacketListener. Soft depends will not provide protection from that.

    Wait 24 hours before bumping.
     
  3. Offline

    OrangeGuy

    I did check that the server has installed ProtocolLib or not before use anything from ProtocolLib..
     
  4. Offline

    mythbusterma

    But you probably have a statement import ...ProtocolLib.something in your class. When the VM sees this it will try to load the class, causing the NoDefFoundError.
     
  5. Offline

    OrangeGuy

    So how can I fix it? Move all softdepends into another Class?
     
  6. Offline

    mythbusterma

    Thats one of the more common ways of doing it, snd then you would only instantiate the class if the dependecy was there.
     
    OrangeGuy likes this.
  7. Offline

    fireblast709

    OrangeGuy You seem to misunderstand the dependency model. Soft dependencies are optional, which means you cannot rely on it. It only ensures that the plugin, if available, will be loaded before yours. For example:
    Code:java
    1. // Main class
    2. public class Main extends JavaPlugin
    3. {
    4. @Override
    5. public void onEnable()
    6. {
    7. PluginManager pm = Bukkit.getPluginManager();
    8. if(pm.getPlugin("ProtocolLib") != null)
    9. {
    10. ProtocolLibHook.hook(this);
    11. }
    12. }
    13. }
    14.  
    15. // optional hook
    16. public class ProtocolLibHook
    17. {
    18. public static void hook(Main plugin)
    19. {
    20. ProtocolLibrary.getProtocolManager().addPacketListener(...);
    21. }
    22. }
    Using this, the classloader will only attempt to load the classes if the plugin is available. If you always need the plugin to be available, use dependencies instead. This will force the server owner to get the plugin.

    (on a sidenote, this is just an example, your method of hooking can be completely different. Just evade having any reference to soft dependencies in 'regular code')
     
    OrangeGuy likes this.
  8. Offline

    OrangeGuy

    Thank you very much :) Got it working now!

    Thank you, nice idea!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  9. Offline

    Europia79

    OrangeGuy

    http://forums.bukkit.org/threads/tutorial-java-optional-dependencies-at-runtime.256579/

    This link is an example of a softdepend. The example is using BattleTracker as a softdepend, but you can get an idea of the thought process involved.

    I may be doing this completely wrong, but in my example, I ALWAYS instantiate MY class. The constructor handles the conditional presence of the softdepends (BattleTracker).

    I did some quick testing and it worked. Altho on further testing, if anyone encounters problems, I may have to move the conditional check for the presence of the softdepends to BEFORE my class is instantiated.

    EDIT: oh I see you already got it working. nice1
     
    OrangeGuy likes this.
Thread Status:
Not open for further replies.

Share This Page