[Tutorial] Using Multiple Classes

Discussion in 'Resources' started by JPG2000, Oct 3, 2013.

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

    JPG2000

    Weather your code is crammed into a single class, you need a better method for organization, or you just plain don't know how to do this, multiple classes will help you.

    This tutorial, is going to cover how you can use multiple class files. This is not going to cover what you can do with multiple class files, but how you can make them accesible and work with your plugin.

    This tutorial will cover: Using commands in different classes, Events/Listeners in different classes, and using both.

    Before I continue with the tutorial, I will have all of the code on paste bin, with links at the bottom.


    Also, there is no need to tag me. I've watched this thread, and receive alerts when theres a new comment.
    Part 1: Main Class

    I suggest you have a main class, witch contains your onEnable and onDisable. This class should only contain what I say below, but your other code/parts of your plugin should be in other classes.

    Lets set up your main class. I'll call it, Main.


    Code:java
    1. package YOUR.PACKAGE.NAME;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.plugin.Plugin;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8.  
    9. public class Main extends JavaPlugin{ //Extending JavaPlugin so that Bukkit knows its the main class...
    10.  
    11. private static Plugin plugin;
    12.  
    13. public void onEnable() {
    14. plugin = this;
    15. //This is where we register our events/commands
    16. }
    17.  
    18. public void onDisable() {
    19. plugin = null;//To stop memory leeks
    20.  
    21. }
    22.  
    23.  
    24. //Much eaisier then registering events in 10 diffirent methods
    25. public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
    26. for (Listener listener : listeners) {
    27. Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
    28. }
    29. }
    30.  
    31. //To access the plugin variable from other classes
    32. public static Plugin getPlugin() {
    33. return plugin;
    34. }
    35.  
    36.  
    37.  
    38.  
    39. }



    Now, this should look fine. If you start creating separate classes from this point, it won't work (this code wont run), assuming your classes use events/commands. You need to do extra steps to make it to work, I will explain in parts 2, 3, and 4.


    Part 2: Using Events(Listeners)

    In the case that you are going to use events (ie PlayerMoveEvent etc.), you will need to follow these steps:

    PRE-NOTE: You do NOT need to do the steps (1 and 2) multiple times for multiple events. If you have 1 event, or even 20, it will work. You just have to do it for every class you have an event in.
    1) Where you define your class, implement Listener.
    Code:java
    1. public class ListenerClass implements Listener {

    As seen Here < http://s13.postimg.org/4vc7yatrb/Screen_Shot_2013_10_03_at_5_27_38_PM.png

    2) Head over to your main class, and in the on enable method, register your events. Read the pre-note above.
    Code:java
    1. registerEvents(this, new ListenerClass());

    Remember, you don't have to create this method 3 times, to register three classes:
    Code:java
    1. registerEvents(this, new ListenerClass(), new otherListenerClass(), new otherNewerListenerClass());

    As seen Here < http://s24.postimg.org/yc646hdwl/Screen_Shot_2013_10_03_at_5_35_35_PM.png
    NOTE: In the image above, I changed my constructors for those classes, so the (this) in when I define the classes, are NOT needed.


    Part 3: Using Commands

    If you want to do a command (do it normally, ie public boolean onCommand etc.), you have to go through similar steps to part 2.
    PRE-NOTE: Unlike events, you HAVE to do this for each command. In your class, if you have only 1 command, then you don't have to do the usual:

    Code:java
    1. if (commandLabel.equalsIgnoreCase("command") {
    2. //Stuff
    3. }

    It takes up more memory. But if you have more then 1 command, you will have to.


    ALSO: You still have to register the command in your plugin.yml. Its basically the same as a normal command.

    1) Head over to your main class, and in the on enable method, register your commands. Read the pre-note above. Also, this is completely new, and not covered in parts before the tutorial, so read carefully.
    Code:java
    1. getCommand("your_command_in_lower_case").setExecutor(new commandClass());

    As seen Here < http://postimg.org/image/u61cl45cz/
    NOTE: In the image above, I changed my constructors for those classes, so the (this) in when I define the classes, are NOT needed.

    2) Like Part 1, you have to implement CommandExecutor. In part 1, you implement Listener, now you have to implement CommandExectuor.

    Code:java
    1. public class myCommandClass implements CommandExecutor {
    2. }


    As seen Here < http://postimg.org/image/4wiixvwrl/

    Part 3: Using Both

    This will be a short section. In the event (ha, did you see what I did there? :D) that you will have a class that uses BOTH commands and events, you will have to implements Listener and implements CommandExecutor.

    You will have to register the command, AND register your event's.


    Code:java
    1. public class Dueo implements Listener, CommandExecutor {


    Part 2: Summing Up


    I tried to make this as small as I could, so I would't overwhelm you. Incase I did, I wiped up a few sample classes, that has events, commands, and BOTH.



    Main Class: http://pastebin.com/g8P295tV
    Listener: http://pastebin.com/H6fhERGz
    Command: http://pastebin.com/Lt5AbNbT
    Both: http://pastebin.com/zctCNtrL





    - Jake
     
  2. Offline

    AguilaAudaz

    Awesome tutorial :D
     
  3. Offline

    Loogeh

    Good tutorial for beginners :)
     
  4. Offline

    Ultimate_n00b

    This is REALLY good. I saw the title and I was like, go post this on a java forum and post a link.
    I then start reading through it, and I love how you show people how to use bukkit with multiple classes (even though in reality, people should how to do it).

    I'll be linking people to this now.
     
    FirecatHD and JPG2000 like this.
  5. Offline

    JPG2000

    Ultimate_n00b Thank you so much! I tried to include as many examples as possible, including pictures, examples and whole classes. It means a lot to me! :)
     
  6. Offline

    ZeusAllMighty11

    You should be setting the plugin instance to null when the plugin disables, or beware of memory leaks.
     
    b00pie16 likes this.
  7. Offline

    Ultimate_n00b

    I actually do have a comment.
    Why do you make the register class static and require the instance of the plugin? Just make it non-static, and use this.
     
    JPG2000 likes this.
  8. Offline

    JPG2000

    Ultimate_n00b likes this.
  9. Offline

    CD3

    He did do that
     
  10. Offline

    JPG2000

  11. Offline

    JPG2000

    I've updated this. Hopefully, people can see how it works.

    A side note, ive been looking into Register Commands (with multiple classes). I have seen ways to use commands, and commands that you don't need to put in the plugin.yml. Im not sure how, but I've heard people talk about it.

    Any ideas?

    Thanks.
     
  12. Offline

    CraftBang

    JPG2000 thanks man looks awesome just one question :

    1. getCommand("hello").setExecutor(new Both());
    What if I say like /hello test
    will it recognize hello, and set the executor to Both and if you have in both like check if args 1 is test will it work ?
     
  13. Offline

    JPG2000

    CraftBang It only does the base command. You could register hello and have 50 arvs it doesnt matter. Just rrgister base
     
    CraftBang likes this.
  14. Offline

    Legocowchaser

    JPG2000 Thanks. Forget what I said earlier about the classes please since I found this tutorial, thanks.:)[diamond]
     
    JPG2000 likes this.
  15. Offline

    Pitazzo

    Well donde! I really love your tutorials!! Keep up!
     
    JPG2000 likes this.
  16. Offline

    ldyte1

    Thanks dude great tutorial nearly needed help at first but after 10 more minutes i figured it out!
     
  17. Offline

    JPG2000

  18. Offline

    Newby

    JPG2000 What about for making a scheduler in a separate class? Would you do:
    Code:
    Bukkit.getServer().getScheduler().<someScheduler>(Main.getPlugin(), new Runnable()
    or would you make a constructor and do:
    Code:
    Bukkit.getServer().getScheduler().<someScheduler>(plugin, new Runnable()
    ?
     
    JPG2000 likes this.
  19. Offline

    JPG2000

    Either would work. But in the first case you the "getPlugin" method in your main claim.
     
  20. Offline

    sockmonkey1

    I think your tutorials will make me a better plugin developer. Thanks @JPG200. I started looking at your other tutorials and just started to click around. Thanks a bunch, man.
     
  21. Offline

    Googlelover1234

    Great tutorial, Keep making more!
     
  22. Offline

    MinecraftMart

    How would this work with getting configs and hashmaps?
     
  23. Offline

    PedroPicas

    Nice tuts but I need help: I registered everything like you said but when I write, for example, /superlol it says on the chat: /superlol.

    Greatfully,
    Pedro
     
  24. Offline

    will98789

    When I tried to do this, it wouldn't let me define: plugin = this.
     
  25. Offline

    user_90854156


    PHP:
    private static Plugin plugin;
     
     
    public 
    void onEnable() {
        
    plugin this;
    }
    Did you write it like this?
     
  26. Offline

    teej107

  27. Offline

    will98789

    I hadn't done the private static thing. It works now thank!
     
  28. Offline

    Orange Tabby

  29. Offline

    MineStein

    This is a really awesome tutorial for those who are just starting out! Good job :)
     
  30. Great Tutorial!
     
Thread Status:
Not open for further replies.

Share This Page