How to create a basic plugin [Part 1]

Denic

Supporter
INTRODUCTION
Hello, in this thread I will be teaching you the ground basics of Nukkit plugin development. Now because this isn't a Java tutorial, I expect that you have a good understanding of Java. As a plugin developer, I get questions daily asking how I create plugins and how to do specific things. So in this thread I will be giving out all my knowledge to you guys so it makes my life easier. Now that that's out of the way, lets get started.

SETUP
Now the first thing you need is an IDE. I personally use Eclipse but there are many other IDE's since there is not a required IDE. Now download the latest version of Nukkit from here or add the dependency to your Maven. I strongly recommend using Maven or Gradle because you will get the latest Nukkit version without having to download multiple files. You can learn how to setup Maven for Nukkit here. Now that you have setup your IDE you can now start to write your code! Create a new project and add Nukkit to your Java build path (properties>>Java Build Path>>Libraries>>Aadd External JAR for Eclipse). Now create a file in the base directory of your plugin called plugin.yml. This is where your plugins information will be stored. the YML fomats are for YAML, a coding language made for configuration files. You can read more about YAML! Now a standard YML looks like this:
YAML:
name: TutorialPlugin
version: "1.0"
api: ["1.0.0"]
author: Civiled
description: A basic plugin for NukkitX
main: com.civiled.TutorialPlugin.main
CODE
Now that we have our plugin file taken care of, we must create a class within the directory that we stated our main class would be. In this case, I have to create a package called "com.civiled.TutorialPlugin" so I create a package with that name. Now, within that package I stated that there will be a class with a name of "main". Now we have a class and everything is now properly setup so we can now start to write our code! Right now our main class looks like this:
Java:
package com.civiled.TutorialPlugin;
public class main
{

}
Now that we have our main class, we need to make some adjustments. First off, in Nukkit our main class always extends "PluginBase". This allows us to access everything that the server has. Now that our plugin extends PluginBase we need to make the plugin do something. Now, lets create two methods within the class called "onEnable" and "onDisable". This will tell nukkit to run everything within that method when the plugin is enabled or disabled. Now that our methods are created, we need to add the Override annotation before each one. Now that we have that, our plugin should look like this:
Java:
package com.civiled.TutorialPlugin;
public class main extends PluginBase
{
     @Override
    public void onEnable()
    {

    }
     @Override
    public void onDisable()
    {

    }
}
Okay, we now have our main classes layout finished! Now we can do some small things and experiment with nukkit! For example, every time the plugin starts I want it to print "Plugin Enabled" in a green font and every time the plugin is disabled I want it to print out "Plugin Disabled" in a red font. We can do this using Nukkits Logger function with a little help from TextFormat! First off, we need to access the logger using this line of code:
Java:
this.getLogger().info("Hello World!");
Now lets experiment a little bit!
Java:
String name = "Civiled";
this.getLogger().info("Hello, my name is " + name);

this.getLogger().info(TextFormat.RED + "Red!" + TextFormat.GREEN + "Green!" + TextFormat.BLUE + "Blue!");
Now, you see that we can print text to the console and in colors, we can finish our program! Lets add these final strips of code to the program now!
Java:
package com.civiled.TutorialPlugin;

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")
    }
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
 
Last edited by a moderator:

ScraM-Team

Member
Cilived, thanks for this great tutorial on coloring text in Nukkit!

We will add support for this to ScraM soon. Then plugins will be able to generate colored text in chat. When it's ready we will publish the ScraM API.
 

Denic

Supporter
Cilived, thanks for this great tutorial on coloring text in Nukkit!

We will add support for this to ScraM soon. Then plugins will be able to generate colored text in chat. When it's ready we will publish the ScraM API.
No problem! If you need anything else, feel free to contact me here
 

Smeo00

Member
Very informative and a great basic starter to get people up and experimenting further :)
TY for the effort Civiled, certainly help me a step along the way!
 

Hannahwalters74

New Member
Hello! In this thread, I'll teach you the basics of Nukkit plugin development. I expect you to know Java well since this isn't a Java tutorial. As a plugin developer, I get asked how to create plugins and do specific things daily. So, in this thread, I'll share all my knowledge with you. Let's get started! If you need a custom website development company, Branex is a great option.
 
Top