[Tutorial] Using Multiple Classes

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

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


    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.

    1. package YOUR.PACKAGE.NAME;
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.plugin.Plugin;
    6. import org.bukkit.plugin.java.JavaPlugin;
    9. public class Main extends JavaPlugin{ //Extending JavaPlugin so that Bukkit knows its the main class...
    11. private static Plugin plugin;
    13. public void onEnable() {
    14. plugin = this;
    15. //This is where we register our events/commands
    16. }
    18. public void onDisable() {
    19. plugin = null;//To stop memory leeks
    21. }
    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. }
    31. //To access the plugin variable from other classes
    32. public static Plugin getPlugin() {
    33. return plugin;
    34. }
    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.
    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.
    1. registerEvents(this, new ListenerClass());

    Remember, you don't have to create this method 3 times, to register three classes:
    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:

    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.
    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.

    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.

    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


    Awesome tutorial :D
  3. Offline


    Good tutorial for beginners :)
  4. Offline


    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


    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


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


    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


    Ultimate_n00b likes this.
  9. Offline


    He did do that
  10. Offline


  11. Offline


    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?

  12. Offline


    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


    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


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


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


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


  18. Offline


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


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


    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


    Great tutorial, Keep making more!
  22. Offline


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


    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.

  24. Offline


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


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


  27. Offline


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

    Orange Tabby

  29. Offline


    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