Calling methods! I'm a noob!

Discussion in 'Plugin Development' started by ZaneBaney, May 7, 2014.

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

    ZaneBaney

    I need help calling a join team method from my other class "teams" into my main class "main"
    I have no Idea how to do this, I have tried making an object (I think) in main and calling it from that but I'm really new and don't understand method calling all that well.

    Main Class:
    http://pastebin.com/H1HKMGyk
    Teams class:
    http://pastebin.com/5MDmb9Xx

    Please, any help is appreciated :)
     
  2. Offline

    MinecraftMart

    ZaneBaney
    I am new to this so i dont know if i am entirely good or wrong

    But put this above onEnable()

    Code:java
    1. teams ts;
    2.  
    3.  


    and then when the command has been called
    Code:java
    1. ts.JoinTeamBlue();


    I hope this helps.
     
  3. ZaneBaney
    I would recommend you to create a static method inside the "teams" class that returns an instance of it.
    Code:
    static teams instance;
     
    public static teams getInstance() {
        if(instance == null)
            instance = new teams();
     
        return instance;
    }
    Now in your main class, you can call methods inside the teams class by grabbing the instance with teams.getInstance().
    Code:
    teams teamsClass = teams.getInstance();
    teams.someMethod(); // replace with the method you want to call
    Also, class names should start with a capitalized letter.
     
  4. Offline

    ZaneBaney

    MinecraftMart Assist Thank you both for the help, testing now.
    (Will make my classes with capitalized letters in the future ;) )
     
    teej107 likes this.
  5. Offline

    teej107

    Try and avoid using static if possible. It can lead to very lazy and bad coding. Try this instead:
    Code:java
    1. public class main extends JavaPlugin {
    2.  
    3. public Permission playerPermission = new Permission("can.startarena");
    4. private teams teams;
    5.  
    6. @Override
    7. public void onEnable() {
    8. new play(this); //I don't know where this comes from
    9. teams = new teams(this); //This reference lets you access each public method from the class.
    10.  
    11. }
    12.  
    13. @Override
    14. public void onDisable() {
    15.  
    16. }

    To call the join team method do this in your main class:
    Code:java
    1. teams.JoinTeamBlue(Player player, ArrayList<Player> blue);
     
  6. Offline

    Bobit

    Why not just put instance=new teams() in your onEnable?
     
  7. Bobit
    You could do that, but quite often I find myself having to access a class from several other classes, and in his case he needs the lists to be static. You would have to pass the instance from the main class to other classes through some constructor, or by creating the same static method in your main class and calling that from other classes. Which one sounds easier?
     
  8. Offline

    Bobit

    Well, the one I said, because my class holds variables o:
    But, yeah, it made sense here.
     
  9. Offline

    teej107

    You could create getters in those classes: (I'll use part of ZaneBarney's code as example)
    Code:java
    1. public class teams {
    2.  
    3. private main main;
    4. private ArrayList<Player> teamExample ;
    5.  
    6. public teams(main main) {
    7. this.main = main; //Now you can access public declarations in the main class
    8. teamExample = new ArrayList<Player>(); //Creates team arraylist
    9. }
    10.  
    11. //Here is the getter, you can access it from any other class that has this class as a reference.
    12. public ArrayList<Player> getTeamExample() {
    13. return teamExample;
    14. }
    15.  
    16. public static ArrayList<Player> red = new ArrayList<Player>();
    17. public static ArrayList<Player> blue = new ArrayList<Player>();
    18.  
    19. public void JoinTeamBlue(Player player, ArrayList<Player> blue) {
    20. blue.add(player);
    21. }
    22.  
    23. public void JoinTeamRed(Player player, ArrayList<Player> red) {
    24. red.add(player);
    25. }
    26.  
    27. }
     
  10. teej107
    But you would have to create a new instance of it every time, unless you pass the original instance you created in the main class to all other classes (or used the static methods I explained), as I said earlier.
     
  11. Offline

    lukewizzy

    ZaneBaney it doesn't seem like you know a lot about Java, I'd recommend learning that before stepping into the Bukkit API, it'll make it a lot easier for you to understand.
     
    Garris0n likes this.
  12. Offline

    Garris0n

    Read this and fix your class names along with any other improperly named variables/methods/etc.

    You may also be interested in this.
     
  13. Offline

    teej107

    Assist Yes but it is better than using static. Its not that hard to pass along objects in a constructor.
    Garris0n lukewizzy Yes I agree. Coding with the Bukkit API will be a lot easier if you have a knowledge of java.
     
    lukewizzy likes this.
  14. Offline

    coasterman10

    As some have stated, do not use static unless you both know what it does and have a good reason to do so, as it leads to messy and inflexible code that will hinder you later. You should also stop thinking in terms of classes and instead about objects. C is a language where we divide code by functions, Java is not. A TeamManager system would be much better, since it orients around the function of managing teams while still being an object, and gives perspective to what the class should and shouldn't be doing.

    That being said, drop the statics, and then create an instance of your teams class in your main class and work with that. I would highly recommend reading some tutorials/books/etc on Java and object oriented programming as it will both help you program more effectively and get a good OOP mindset. You can continue working with Bukkit while you do this (I learned a lot of Java while writing Bukkit plugins) but you should certainly pick up Java experience before writing the plugin code so you're ready to tackle more complex plugins.
     
    AoH_Ruthless likes this.
  15. Offline

    hexaan

    I'm really starting to wonder why people use static so much without knowing what it does. The moment your code has things like public static ArrayList you should really consider looking up how this can be done in a better way.
     
    Garris0n and coasterman10 like this.
  16. Offline

    Garris0n

    I remember having no idea what I was doing and static "fixed" everything. I didn't understand how instances worked. Newer OOP coders, from what I've seen, tend not to think of everything as objects. They just think "there is one main class". Well, there is, because one is necessary. Then they venture into making multiple classes, and again think there is one of a listener class, for example. And then they can't figure out how to get their one main class into the listener class. "There's only one, how do I access it?" And then they find out about statics and go with that, again not understanding how to pass the instance along. It gets even worse than that.

    Me:
    "Hm, I need to make arenas." So what would a programmer who knows basic Java do? Make an arena class, store everything in that, break it up into more, smaller classes. What would somebody who has no idea what they're doing do? (me at the time) Well, first I tried to use indices to map values together. That didn't work out well, but I quickly found out about maps. So I used those. "Need to store the players in an arena? List<String> arenas, Map<String, List<String>> players in arenas." Yeah. Really. And the best part? All these lists, maps, etc scattered over the code were all static.

    Seriously, learn Java. And if you don't know what you're doing yet, don't use the static keyword.
     
    coasterman10 and hexaan like this.
  17. Offline

    teej107

    Yeah. When I first started learning computer programming (Java), I made a Tic-Tac-Toe game and basically everything was static. I knew that it wasn't good to make everything static, but it was the only way I knew how at the time. Now that I look back on it, I realize how bad the code was for the game.
    You can't make Bukkit plugins without the knowledge of Java. I started using the Bukkit API about 6 months ago and catching on to it was easy since I knew how to program in Java.
     
    hexaan likes this.
Thread Status:
Not open for further replies.

Share This Page