Create a method from API detect if they have the plugin

Discussion in 'Plugin Development' started by javoris767, Jul 28, 2013.

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

    javoris767

    I created a method to use if they have factions and whenever I load it without factions I get errors saying that factions isn't there. With factions it works fine. This is more of an optional dependency for people who use factions.

    Code:
        private String FactionTag(UPlayer player) {
            String name = player.getFactionName();
            String role = player.getRole().getPrefix();
            ChatColor rcolor = player.getColorTo(player);
            String tag = rcolor + role + name + ChatColor.RESET;
            return tag;
        }
     
  2. Offline

    chasechocolate

    To see if a plugin is installed, use if(pluginManager.getPlugin("Factions") != null).
     
  3. Offline

    javoris767

    I tried that, but It's still trying to load this method o:
    Code:
        private String FactionTag(UPlayer player) {
            if(Bukkit.getPluginManager().getPlugin("Factions") != null) {
                String name = player.getFactionName();
                String role = player.getRole().getPrefix();
                ChatColor rcolor = player.getColorTo(player);
                String tag = rcolor + role + name + ChatColor.RESET;
                return tag;
            }
            return null;
        }
    Is this wrong?
     
  4. Offline

    xTrollxDudex

    javoris767
    Try to return null if the plugin is null, else execute code.
    Might work better.
     
  5. Offline

    javoris767

  6. Offline

    chasechocolate

    javoris767 I'm assuming UPlayer is a class from the Factions plugin? Maybe have a global boolean to see if factions is installed and in the onEnable() set it equal to this.getServer().getPluginManager().getPlugin("Factions") != null. Then, before you call that method, check if that boolean is true.
     
  7. Offline

    javoris767

    The method isn't being called anywhere, which is odd.
     
  8. Offline

    javoris767

    Just having the method in the code, not doing anything without faction's jar in the file causes an error.
     
  9. Offline

    Omerrg

    Because you import the UPlayer, and its part of factions..
     
  10. Offline

    Tarestudio

    javoris767
    Have you set Factions to the soft dependencies of your plugin? so its loaded after factions.
     
  11. Offline

    Adversarius

    to compile it, you need the jar as a library in your Project, doesnt matter if the plugin is on the Server or not.
     
  12. Offline

    javoris767

    Tarestudio
    Yup, It's a soft depend.
    Adversarius
    I have it in my library. Whenever I load the plugin without Faction in the server I get an error about it trying to load the classes I used. However, it works with Factions.
     
  13. Offline

    Pawnguy7

    It should compile, then, and shouldn't break at runtime unless you actually call it. Are you sure you aren't calling this method? If it errors, there must be a stack trace.
     
  14. Offline

    javoris767

    Here's the error. I'm 100% sure its not being called anywhere.
    Code:
    2013-08-02 09:14:55 [SEVERE] Could not load 'plugins/SupaChat.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: com/massivecraft/factions/RelationParticipator
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:184)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugins(CraftServer.java:239)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.reload(CraftServer.java:603)
        at org.bukkit.Bukkit.reload(Bukkit.java:275)
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerConnection.java:962)
        at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.java:880)
        at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:837)
        at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
        at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.NoClassDefFoundError: com/massivecraft/factions/RelationParticipator
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:173)
        ... 21 more
    Caused by: java.lang.ClassNotFoundException: com.massivecraft.factions.RelationParticipator
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader.java:80)
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:53)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        ... 24 more
     
  15. Offline

    Pawnguy7

    Hrm... it doesn't load? Perhaps the problem is in the plugin.yml?
     
  16. Offline

    javoris767

    Pawnguy7 It's fine o:
    Code:
    name: SupaChat
    main: me.javoris767.supachat.SupaChat
    version: 2.6.5
    author: javoris767
    authors: [Drakia, javoris767]
    commands:
      supachat:
        description: Used to reload the plugin.
    softdepend: [bPermissions, PermissionsBukkit, PermissionsEx, GroupManager, Factions]
     
  17. Offline

    Pawnguy7

    Does SupaChat load?
     
  18. Offline

    javoris767

    Nope :/
     
  19. Offline

    xTrollxDudex

    javoris767
    Factions needs to load before the plugin tries to detect it, try delaying the getPlugin a few seconds
     
  20. Offline

    AmShaegar

    That is not helpful at all. The problem only exists when Factions is NOT there. Which is intended because it's an optional dependency.

    This might be helpful:
     
  21. Offline

    xTrollxDudex

    AmShaegar
    Even with depend/softdepend you still will get null if you try to get Factions before it is loaded.
     
  22. Offline

    AmShaegar

    That's why bukkit uses load and enable. First every plugin is loaded. Then enabled. If you access plugins in onEnable or later everythin is loaded. But this is not the problem. The problem is:

     
  23. Offline

    ZachBora

    I've had issues with optional dependency in the past. Factions (I think it was) raises errors in it's API. And trying to catch their exception type would prevent plugin from loading if factions wasn't present. I fixed it by catching Exception instead of the specific exception.

    UPlayer extends SenderEntity<E extends SenderEntity<E>> which in turn extends Entity<E>.

    Try to replace UPlayer by Entity and inside the code do a type check and convert.

    private String FactionTag(Entity entity) {
    UPlayer player = (UPlayer) entity;

    It might not work, I don't have the tools to try it.
     
  24. Offline

    javoris767

  25. Offline

    ZachBora

    javoris767 Can you show us your whole code somewhere? Perhaps github? If not, put it in a pastebin, it's easier to read then in syntax or code blocks.
     
  26. Offline

    hatstand

    This is your problem:
    Simplest way to get around this is to have a separate class that interfaces with Factions, which you then load and use if Factions is present.

    Additionally, I would guess you're getting a UPlayer from somewhere to call that method. A class is loaded the first time it is used, so don't attempt to even create a UPlayer unless you know Factions is there.

    Something like:
    Code:
    public String getTag(Player p)
    {
        String tag;
    
        if(factions)
        {
            tag = FactionsInterface.getTag(p);
        }
        else
        {
            //Default tag handling
        }
        
        return tag;
    }
    
    Note that the method isn't using UPlayer, but Player. FactionsInterface uses the Player to create a UPlayer and get the required things, so you're never touching classes of dependencies that might not be present.
     
  27. Offline

    javoris767

    hatstand Sorry, What do you mean by "that interfaces with Factions"?
     
  28. Offline

    blackwolf12333

    I have been reading through all the comments, and the only thing I can think of without seeing the full source code would be that you actually do call that method. That is the only way such an error can occur.

    But there is another problem, you are talking about that function FactionTag all the time, that function uses a Class/interface UPlayer from Factions. But the error does not point out that it can't find UPlayer.
    Search through your source for
    Code:
    com.massivecraft.factions.RelationParticipator
    because you use that class in some function while enabling/loading your plugin. That is the actual cause of the error as far as I can see.
     
  29. Offline

    Janmm14

    Why yu don't use Vault as Softdepend instead of all the permission plugins? Remember xou can use player.hasPermission() for permission checks. Try to make a new class and add all faction needed methods to it, as static methods. So you have no faction importy in your main class.
     
  30. Offline

    Wingzzz

    Make a class such as: "FactionsHook". The point of doing so would be to attempt to "hook" into Factions, if it's not loaded, then it will simply set a boolean such as "isHooked" to false and log that it couldn't hook into factions.

    You can add methods to check certain features or grab certain things from factions, but you will mainly use a method such as isHooked(); to ensure you're not going to run into any errors/exceptions if Factions is not loaded.

    When it was said that to make a class to "interface" with factions, it's basically saying make a class that will handle all factions requests. This way in-case you don't have factions loaded, it'll be fine.

    Example usage:
    Code:java
    1. if(FactionsHook.isHooked()) {
    2. // It's hooked, input factions logic here...
    3. } else {
    4. // It's not hooked, no worries, we'll just move on and forget about it, or log it to console etc...
    5. }


    Example implementation:
    http://www.hastebin.com/huwoharadu.avrasm
    http://www.hastebin.com/kuyokuloqe.avrasm

    Hook and PluginHook classes used:
    http://www.hastebin.com/qicaciqobi.java
    http://www.hastebin.com/vohocalohi.java
     
Thread Status:
Not open for further replies.

Share This Page