Multiple classes?

Discussion in 'Plugin Development' started by maxxb123, Mar 5, 2014.

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

    maxxb123

    So, I've always wanted to create a big Bukkit plugin. But the problem is, for some reason, using multiple classes is not working! I want to have one command per class or something like that. Do I have to link them together? What do I do?
     
  2. Offline

    viper_monster

  3. Offline

    Wolfey

    You can also use multiple classes for utils.
     
  4. Offline

    Axe2760

    I'd suggest trying to read up on Object Oriented Programming--using objects in java is probably a good idea if you want to structure your plugin (definitely needed if it's going to be a bigger plugin; this is where all the different classes come in). I don't know a good way to explain it, but...

    If you wanted to keep certain attributes per player on a team, you might go about making a class to have all the player's stuff, i think code might be a better example:
    Code:java
    1.  
    2. public class MyPlayer{
    3.  
    4. //Ideally you would do the variables first..
    5.  
    6. private String name; //Stores the name of the player; you don't want to keep a Player object here.
    7. //if you want to access the player object, you would make a call to Bukkit.getPlayer(name).methodHere();.
    8. //we want to simplify this by making a getPlayer() method later on.
    9. private int kills; //just storing a variable here
    10. private int deaths; //just putting another \o/
    11. private int killstreak; //and another
    12. private int swagLevel; //and so forth
    13. private boolean isAbeast; //you can have different types of objects in here, not just integers or strings!
    14.  
    15. //Now you need a constructor. So when you have a class you made like this one, and want to instantiate it into a
    16. //variable you can use...
    17. //MyPlayer(event.getPlayer()) would be an example.
    18. //instantiating objects, meh, you would just have MyPlayer player = MyPlayer(event.getPlayer());
    19. //or alternatively
    20. // MyPlayer player; //at the beginning of the class, like above
    21. // player = MyPlayer(event.getPlayer()); //somewhere along the line, in a different part of the class
    22. // the "player" could be accessed from anywhere in the class, then.
    23. //but on to the constructor...
    24. public MyPlayer(Player player){ //kind of like a method. When you instantiate the class like above,
    25. //this code is called
    26. //you probably want to instantiate your variables here... so here we go
    27. name = player.getName();
    28. kills = 0;
    29. deaths = 0;
    30. killstreak = 0;
    31. swagLevel = 9000;
    32. isAbeast = swagLevel > 9000;
    33.  
    34. // now we can use any of the variables from the class
    35. }
    36.  
    37. //let's make a method in the class
    38. //like i mentioned earlier... getPlayer() might be helpful in this case
    39. public Player getPlayer(){
    40. return Bukkit.getPlayer(name);
    41. }
    42.  
    43. //another method
    44. public float getKDR(){ //get the kill/death ratio
    45. return (float)kills/deaths;
    46. }
    47.  
    48. //need something to access the kills/deaths, changing them...
    49. public void addKill(){
    50. kills += 1;
    51. }
    52.  
    53. public void addDeaths(){
    54. deaths += 1;
    55. }
    56.  
    57. public String getName(){
    58. return name;
    59. }
    60.  
    61. public int getKills(){
    62. return kills;
    63. }
    64.  
    65. public int getDeaths(){
    66. return deaths;
    67. }
    68.  

    Hopefully you get the idea of making another class. But the question can be...why? you could do all of the above with hashmaps, or lists, or something of that sort all in one class...

    But when we want to structure something in a way that can't be done in a low-level way, OOP seems to be the answer. Using the MyPlayer class above...

    Code:java
    1.  
    2. public class Manager{
    3. private List<MyPlayer> members = new ArrayList<>();
    4.  
    5. public void addMember(Player player){
    6. members.add(new MyPlayer(player));
    7. }
    8.  
    9. public MyPlayer getMember(String name){ //Now...i'm not sure if there's a better way to do this?
    10. for (MyPlayer player : members){
    11. if (player.getName().equalsIgnoreCase(name)) return player;
    12. }
    13. return null;
    14. }
    15. //you could probably put a lot more in this class...but this post is a bit long and i'm not feeling especially inclined towards
    16. //writing more than i believe to be necessary
    17. }
    18.  


    Right, so now you need to keep an instance of the Manager class somewhere... how about in a main class?

    Code:java
    1.  
    2. public class MyPlugin extends JavaPlugin implements Listener{
    3. Manager manager;
    4. public void onEnable(){
    5. this.getServer().getPluginManager().registerEvents(this,this);
    6. manager = new Manager();
    7. }
    8.  
    9. //this method isn't optimal because i'm calling manager.getMember() twice, but meh... this is just supposed to be an
    10. //example of using objects in such a way to make your life easier.
    11. @EventHandler
    12. public void onSomethingRandom(PlayerInteractEvent event){ //just something random, probably wouldn't be used in this way... a player interact event :confused:
    13. if (manager.getMember(event.getPlayer().getName()) == null){
    14. manager.addMember(event.getPlayer());
    15. }
    16. MyPlayer player = manager.getMember(event.getPlayer());
    17. player.addKill();
    18. event.getPlayer().sendMessage("New KDR: "+String.valueOf(player.getKDR()));
    19. }
    20. }
    21.  


    Anyway, that's just an idea... something i've picked up along the way, hopefully this makes sense!

    I'd like to say that getting a grasp on OOP would help you code in pretty much any object oriented language, and java doesn't seem to be such a bad way to learn it :D Good luck dude, hope this was some sort of help

    EDIT: It came to my attention that the code tag was really hard to read with all the comments... edited, hopefully this makes more sense now
     
    97waterpolo likes this.
  5. Offline

    Minesuchtiiii

    use in your main class at the onEnable part:

    Code:java
    1. this.getCommand("YOUR_COMMAND").setExecutor(new [CLASS_NAME_WHICH_THE_COMMAND_IS_IN]());
     
  6. Offline

    maxxb123

    Thanks for the help, everyone.
     
  7. Offline

    Axe2760

    maxxb123 Edited the above post, now it's a lot easier to read :) sorry about that
     
Thread Status:
Not open for further replies.

Share This Page