Solved Towny: Looping through every town

Discussion in 'Plugin Development' started by InsertNameHere, Jun 26, 2019.

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

    InsertNameHere

    I am new to the Towny API and am completely confused as to how I would call a method. For example, how would I get the list of all the towns in a world and get the Mayor and Resident?

    According to this link: http://palmergames.com/javadoc/towny-bukkit/, it seems that the method getTowns() returns an ArrayList of Towns, but how do I call it?

    Code:
    for (Town town: SOMETHING.getTowns()) {
        // rest of the code goes here
    }
    
    Any help?
     
  2. Offline

    Kars

    What do you mean how do you call it? The code you provided calls it.

    If you're wondering what you call it ON, getTowns is called on a TownyWorld object.
    http://palmergames.com/javadoc/towny-bukkit/com/palmergames/bukkit/towny/object/TownyWorld.html

    Your loop iterates every town in the TownyWorld. Then you can call any of the methods from the link you provided on town inside the loop.

    So for example
    PHP:
    town.getMayor();
    gets you the Mayor (which inherits from Resident).

    All the methods and explanations can be found in your link.
     
  3. Offline

    InsertNameHere

    @Kars But whenever I type the code
    Code:
    for (Town town: TownyWorld.getTowns()) {
       // so on
    }
    
    It says non-static methods cannot be referenced from a static context.

    EDIT: I did
    Code:
    for (Town town: TownyUniverse.getDataSource().getTowns()) {
        // So on
    }
    
    And it seems to work...
     
    Last edited: Jun 26, 2019
  4. Offline

    KarimAKL

    @InsertNameHere The method is not static so you require an instance of the class to call the method. Just thought i would explain the warning you had.
     
Thread Status:
Not open for further replies.

Share This Page