Solved Hook into Towny

Discussion in 'Plugin Development' started by basdv98, Jun 13, 2013.

Thread Status:
Not open for further replies.
  1. Hello,

    Could anyone please tell me how to hook into towny with my plugin?
    I need to get this information from Towny:
    The name of the town a specified player is in.
    The owner of the town a specified player is in .
    In what Nation the Town with a specified player in.
    All other members of the town a specified player is in.

    I would love to get a reply as quick as possible, please just give me a really good guide so it will work, please also tell me how to import the things.

    Kind Regards,
    Bas
     
  2. Offline

    Thej0y

    This info would help me too :p, I asked the same thing a couple days ago and never got answered. If someone knows how, please tell us XD

    (my problem was that towny always returning that the resident dont belong to any town)
     

  3. Yes I also checked your thread
     
  4. Offline

    kreashenz

    Check if the main file of Towny is there, and then make a boolean and see if you can find if. If it's found, return true, and if it's not return false (and maybe disable plugin).
     
  5. Offline

    Tomskied

    Perhaps if you stayed in the irc for longer than 8 minutes, you would get the help you need.

    To answer your question:
    I cant link you directly to their site, (Which is why they took their project off bukkit) But google Towny javadocs. First result will take you to their javadocs where everything is documented.
    Just use :
    Code:
    depend: [Towny]
    
    in your plugin.yml, from there you can start using Towny's methods from the javadocs.
    Simples.


    For example:
    Code:java
    1. Resident r = TownyUniverse.getDataSource().getResident(name);


    Will get you the resident object from the player name.
     
  6. Could you please just give me the code ?
    I need this:
    If the user belongs to a town then print the town name, if it's not then print: None
     
  7. Offline

    LlmDl

    basdv98 I doubt he'll just give you the code after you told him his support in #towny was bad.
     
  8. I asked for it and he didn't give me

    Code:java
    1. Resident r = TownyUniverse.getDataSource().getResident(p.getName());

    I can only get that with Try and Catch but I just want it to print the town name or if the user doesn't belong to a town it just prints: None

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

    Tomskied

    Check if the resident exists, there will be an exception thrown if it isn't. Probably the Towny NotRegisteredException.

    If the resident exists, check its town, r.getTowny() it will return NotRegisteredException if it has no town.
    [​IMG]
    From the resident class javadocs.....
     
  10. Thanks for your help but....
    Is there a way to change the text: NotRegisteredException into: None or something like that?

    Thanks.

    Regards,
    Bas
     
  11. Offline

    Tomskied

    Use a try and catch, if the exception is thrown, you can just return none, or output none.
     
  12. It's a bit weird but now eclipse doesn't give a problem when i don't use any throw for it,
    can you please give me the code to make a string witch is the Town name the specified player belongs to or if the player doesn't belong to a town it's just: None
    Thanks!

    I have this code on it so far:
    Code:java
    1. Resident r;
    2. try {
    3. r = TownyUniverse.getDataSource().getResident(p.getName());
    4. Resident resident = r;
    5. } catch (NotRegisteredException e) {
    6. String resident = "None";
    7. }
    8. Town town = resident.getTown();
    9. Resident mayor = town.getMayor();
    10. Nation nation = town.getNation();


    It goes wrong right here:
    Code:java
    1. Town town = resident.getTown();

    there is a red stripe under: resident

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

    Tomskied

    Do you know how to code in Java? It doesn't look like it. It would be more worth your time to go check out some tutorials on java then attempt to jump in the deepish end.

    Code:java
    1. Resident r;
    2. try {
    3. r = TownyUniverse.getDataSource().getResident(p.getName());
    4. } catch (NotRegisteredException e) {
    5. //Output none here
    6. }
    7. Town town = r.getTown();
    8.  
    9. //Your resident still may not have a town. So check this.
    10. Resident mayor = town.getMayor();
    11. Nation nation = town.getNation();
    12. //Your resident still may not have a town.
     
  14. I solved my problem, I got helped by @PeterKramer
    This is the code to get the town name of the specified player:
    Code:java
    1. public String getTownName(Player p)
    2. {
    3. Resident r;
    4. try
    5. {
    6. r = TownyUniverse.getDataSource().getResident(p.getName());
    7. }
    8. catch (NotRegisteredException e)
    9. {
    10. return "none";
    11. }
    12. Town town;
    13. try
    14. {
    15. town = r.getTown();
    16. }
    17. catch (NotRegisteredException e)
    18. {
    19. return "none";
    20. }
    21. if( town == null )
    22. {
    23. return "none";
    24. }
    25. else
    26. {
    27. return town.getName();
    28. }
    29. }


    and you have to place this on the location where you want it to be showed:
    Code:java
    1. getTownName(p)

    LOOK OUT: the p got to be the player, so if you got p.getName() to get the playername then it's just p because the name already get's checked in the void.
     
Thread Status:
Not open for further replies.

Share This Page