Use methods of external Jar's

Discussion in 'Plugin Development' started by twinflyer, Jan 31, 2012.

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

    twinflyer

    Hey guys,
    I'm learning Java for nearly one year now. Untill now I was happy with the things I can, but I need a bit help now.
    I want to check, which Quest a player's doing after typing in a command.
    The quests are made in Citizens.

    I found this:
    https://github.com/CitizensDev/Citi...et/citizensnpcs/questers/QuesterCommands.java

    On line 431 is this piece of Code:

    profile.getProgress().getQuestName()

    How can I use "getQuestName()" in my own plugin?
    Or is this impossible to do?

    twinflyer
     
  2. Offline

    Yurij

  3. Offline

    desht

    Not impossible, but it does require a little knowledge of how to hook into other plugins. Perusing the source code of Citizens shows that all of the classes and methods involved in that call are public, and that you can get a PlayerProfile object with the static method PlayerProfile.getProfile(player.getName()).

    So far so good: you can call those methods from your plugin. But to actually hook into the plugin, you will want to use the Bukkit PluginManager class to ensure it's loaded, something like this:
    Code:java
    1.  
    2. // Static field in your main plugin class. This is not necessarily the "best" way
    3. // to make this field visible to other classes in your plugin, but it's quick & easy
    4. // for this example
    5. public static boolean citizensEnabled = false;
    6.  
    7. // called sometime during your onEnable() method
    8. PluginManager pm = Bukkit.getPluginManager();
    9. Plugin citizensPlugin = pm.getPlugin("Citizens");
    10. if (citizensPlugin != null) {
    11. // the citizens plugin is available to us
    12. citizensEnabled = true;
    13. }
    14.  
    15. // ... somewhere else in your plugin ...
    16. if (YourPlugin.citizensEnabled) {
    17. PlayerProfile profile = PlayerProfile.getProfile(player.getName());
    18. String questName = profile.getProgress.getQuestName();
    19. // ... etc ...
    20. }
    21.  

    Some other comments:
    • You will need to add Citizens as a build dependency in your IDE (or Maven if you're using that to build your plugin).
    • You will also need to add Citizens as a dependency in your plugin.yml using the depend: or softdepend: keyword, depending on whether this functionality is essential or optional in your plugin.
    • Caveat: unless the author(s) of Citizens have actually made this part of a publicly documented API, it's your problem if they change anything (e.g. method or class names/visibility/signatures). If they do, your plugin will break and you'll be rushing to get a new version released.
     
  4. Offline

    twinflyer

    thanks you two :)
    will read trough your Posts/links and post again later.

    [EDIT]
    It says PlayerProfile cannot be resolved (to a type) :S
    I have no Idea wich import I have to use. I thought :
    importnet.citizensnpcs.questers.data.PlayerProfile;
    but this import "cannot be resolved" , too.

    Sorry, but that's totaly new for me, I never hooked into any plugin without a REALLY good documentation.
     
  5. Offline

    desht

    Right, that's because you need to add Citizens as a build dependency in your IDE, in the same way you added Bukkit.
     
  6. Offline

    twinflyer

    I added Citizend as External Jar in eclipse in the same way i did it with the craftbukkit jar (JavaBuilt path -> Add external Jar)

    if I type "import net.c" eclipse shows some citizens imports. But PlayerProfile isn available.
     
  7. Offline

    desht

    Aha. Looks like that particular package isn't in the core Citizens.jar. You'll also need to add Quester.jar as a build dependency.
     
  8. Offline

    twinflyer

    sorry for not answering, I was on holiday ;D

    I added Citizens and Quester as built dependency, but the Quester.jar isn't located in the olugins folder. Its in plugins/Citizens/types/Quester.jar

    I think thats the reason why bukkit is unable to load the quester.jar (UnknownDependencyException)

    Could you tell me how to change the path of the dependencys inside the plugin.yml?

    twinflyer
     
  9. Offline

    xKoKSii

    we now get an "null error" on
    Code:
    PlayerProfile profile = PlayerProfile.getProfile(player
    .getName());
    we have done some null checks with
    Code:
    if (PlayerProfile.getProfile(player.getName()) == null) {
                            System.out.println("KEIN SPIELERPROFILE!");
                        } 
     
  10. Offline

    twinflyer

    the error occures at the "PlayerProfile.getProfile(player.getName())"
    Even the null check throws this error, bevor it really checks if its a null.

    I really don't know whats wrong there, I hope somebody can help us.
     
Thread Status:
Not open for further replies.

Share This Page