API creation problems

Discussion in 'Plugin Development' started by jertocvil, Oct 24, 2011.

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

    jertocvil

    How to share data between plugins?
    I'm developing two simple plugins: the first plugin allow players to join a team, and the other (and possibly a few more) reads in which team (from the other plugin) the player is. How can I do that?

    EDIT: Now I need a way to visually identify which team the player belongs. I was thinking to change the color of the name (not in the chat, but the name that hovers over the player's head) or the skin. Is there a way to do this only through bukkit (without external plugins/APIs)? Do you know another way to do this? Thanks!
     
  2. Offline

    desht

    Your first plugin will need to expose a public method or methods to read the necessary data from it. Then the second plugin can get an instance of the first plugin with the PluginManager getMethod() call, and from there access its public methods.

    Obviously there's a fair bit of error checking that needs to happen - the second plugin will need to ensure that the getMethod() call succeeds, and that the returned Plugin object is actually an instance of your first plugin before casting it.

    This is the basics of API creation. If you're creating such an API, it's worth designing it carefully, especially if plugins other than yours will be calling it. Once published, you don't want to be removing methods from it, or changing the signature of existing methods unless absolutely unavoidable (and then you should use deprecation if possible).

    It's also worth looking at using a Interface to define what your API will look like, and then implementing that in a specific class in your first plugin. Then you could add a getHandler() method to the main plugin class, which returns an instance of the separate class you created. Other plugins could then call the getHandler() method to get your API handler.
     
    jertocvil likes this.
  3. Offline

    jertocvil

    Thank you! Now I have another question.
    Now I need a way to visually identify which team the player belongs. I was thinking to change the color of the name (not in the chat, but the name that hovers over the player's head) or the skin. Is there a way to do this only through bukkit (without external plugins/APIs)? Do you know another way to do this? Thanks!
     
  4. Offline

    2n3904

    This is something I've been looking for as well. The only solution I have found through Bukkit is putting wool blocks on player's heads. However, this is a terrible solution since players can remove the blocks whenever they want and Bukkit has no listener calls which let you prevent this.

    Changing the text above player heads is only possible through spout, AFAIK
     
  5. A) @2n3904 you can prohibit the players from deleting the blocks using a BlockListener and onBlockBreak
    B) The easiest way to is to use spout...
    Heres my code for a upcoming project
    Code:java
    1.  
    2. public static void updateNames() {
    3. for (int i = 0; i < getTeams().size(); i++) {
    4. Team t = getTeams().get(i);
    5. for (String player : t.getPlayers()) {
    6. Player p = RushMe.getInstance().getServer().getPlayer(player);
    7. if (p != null) {
    8. for (Player onlinePlayer : RushMe.getInstance().getServer()
    9. .getOnlinePlayers()) {
    10. Team team = getPlayersTeam(onlinePlayer);
    11. if (team == null) {
    12. SpoutPlayer spoutP = SpoutManager.getPlayer(p);
    13. SpoutManager.getAppearanceManager().setPlayerTitle(
    14. spoutP, onlinePlayer,
    15. ChatColor.WHITE + onlinePlayer.getName());
    16. } else if (t.equals(team)) {
    17. SpoutPlayer spoutP = SpoutManager.getPlayer(p);
    18. SpoutManager.getAppearanceManager().setPlayerTitle(
    19. spoutP, onlinePlayer,
    20. ChatColor.GREEN + onlinePlayer.getName());
    21. } else {
    22. SpoutPlayer spoutP = SpoutManager.getPlayer(p);
    23. SpoutManager.getAppearanceManager().setPlayerTitle(
    24. spoutP, onlinePlayer,
    25. ChatColor.RED + onlinePlayer.getName());
    26. }
    27. }
    28. }
    29. }
    30. }
    31. }
    32.  
     
    jertocvil likes this.
  6. Offline

    2n3904

    onBlockBreak triggers for when you remove a block from your helmet slot??

    And yes, I do plan on incorporating spout but I would like the non-spout version to have some kind of similar option as well - which is why this is still up in the air for me.

     
  7. oops nope, that doesn't. I believe thats a SpoutInventoryEvent ;)
     
  8. Offline

    jertocvil

    I'm trying to do this, but I can't find any getMethod() call, can you post an example?
     
  9. Offline

    desht

    Doh. I meant getPlugin(), not getMethod() :)
     
  10. Offline

    jertocvil

    I've done it! Thank you! I'll post the code later, and the plugin when finished :)
     
Thread Status:
Not open for further replies.

Share This Page