How to create a basic plugin [Part 2]

Should I do a part 3?


  • Total voters
    1
  • Poll closed .

Denic

Supporter
INTRODUCTION
Hello, in this thread we will be extending what we learned in the last one. If you haven't read that one there will be a link here. In today's thread I will be explaining the basics of the events. If you dont know already, you will have to learn how to navigate through the Javadocs.

SETUP
All we require for this is a basic knowledge of Java and Nukkit with the code from part 1. First off, we will create a new folder in our package called "Events". In here we will store our events. Now that we have everything setup, lets begin!

CODE
Now that we have our BasicPlugin template everything SHOULD be in place and ready. Now our main class should like this:
Code:
public class main extends PluginBase{
    @Override
    public void onEnable()
    {
        this.getLogger().info(TextFormat.GREEN + "Plugin Enabled");
    }
   
    @Override
    public void onDisable()
    {
        this.getLogger().info(TextFormat.RED + "Plugin Disabled");
    }
}
Now to start everything up, we should create a new folder with the name of "Events" in our package. In here we will create a class called "PlayerMove". When developers create a plugin, they create folders for Events, Tasks, and other things. This is mainly for the plugins organization but it is also extremely helpful when you have a huge plugin with hundreds of classes. Now that we have that clarified and done, we can now start to write code! Now that we have our PlayerMove class, we should probably start to actually add the event. So lets make our class implement Listener. Now that our class implements listener we need to refer to the cn.nukkit...events portion of the JavaDocs. This will give s a list of events and when we click on one of them, we can see a list of things we can do with that event. In this case, we will use PlayerMoveEvent. Now this same method applies to almost any event.
Code:
public class PlayerMove implements Listener {
    public void onMove(PlayerMoveEvent event)
    {
       
    }
}
Now in any case, we would change the Event to be the event we want to use. Now we must add a few more things for this to work. First off, before any event we MUST add the EventHandler enum. This allows us to set a events priority, and much much more. Also, we much register the event on the plugin start. This tells us that there is an event in the class and when the console starts, it runs that event. Now lets edit our event class to do something. In this case, every time a player moves, I want it to tell the player "You moved!". Now we can do this by storing the instance of a player in the Player variable. For example:
Code:
public class PlayerMove implements Listener {
    public void onMove(PlayerMoveEvent event)
    {
        Player player = event.getPlayer();
    }
}
Now we have a player stored in our code! We can do a LOT of things with a player. In this case, we want to send them a popup message. So we can refer to the JavaDocs player type and see how we can do that. We can do so by doing the following:
Code:
player.sendPopup("Message");
Now that we know how to get a player and send them a message, we can apply this to our code! First, lets add the EventHandler enum. We add this before the event itself though. Now, we can add the popup message within the event itself. Note that this event only fires of when the event happens. For example: PlayerMoveEvent only starts when a player moves, PlayerInteractEvent only starts when a player interacts with something. This means that every time a player moves, everything in the braces will start. We can now finish our class with the following:
Code:
public class PlayerMove implements Listener {
    @EventHandler
    public void onMove(PlayerMoveEvent event)
    {
        Player player = event.getPlayer();
        player.sendPopup("You Moved!");
    }
}
There we go! Now all we have to do is register the event in our main class and export. First off, in our Main class in the onEnable method, we access things within the PluginBase using the "This" method. Now, we need to register the event through pluginManager. Use the following in your main onEnable method:
Code:
this.getServer().getPluginManager().registerEvents(new PlayerMove(), this);
The first parameter is the events class while the second one is your plugins main class. I recommend following this format every time. In the end both of our classes should look like this:

Main
Code:
public class main extends PluginBase{
    @Override
    public void onEnable()
    {
        this.getLogger().info(TextFormat.GREEN + "Plugin Enabled");
        this.getServer().getPluginManager().registerEvents(new PlayerMove(), this);
    }
   
    @Override
    public void onDisable()
    {
        this.getLogger().info(TextFormat.RED + "Plugin Disabled");
    }
}
PlayerMove
Code:
public class PlayerMove implements Listener {

    @EventHandler
    public void onMove(PlayerMoveEvent event)
    {
        Player player = event.getPlayer();
        player.sendPopup("You Moved!");
    }
}
CONCLUSION
Now we have successfully created a fully functional plugin! You can now export it as a JAR and put it in your plugins folder! This knowledge can be taken to a great extent! I deeply recommend looking at other people work and seeing how other people deal with problems as it will make your life so much easier. Also, if you look at Bukkit, hence the name, you will be suprised how alike the two are.

Thank you all for reading this because it truly strains the amount of pressure put on the developers shoulders because you are willing to read an article instead of asking a bunch of questions. If you have any suggestions or questions, feel free to ask me on Discord: Civiled#1713

Good luck!
-Civiled
 
  • Like
Reactions: G.M
Top