Denic
Supporter
INTRODUCTION
Hello! In the last tutorial we learned how to use basic events. In this tutorial we will be learning how to use Config files effectively AND efficiently.
SETUP
All we require for this is a basic knowledge of Java and Nukkit with the code from part 1. Now that we have everything setup, lets begin!
CODE
In our previous tutorials we learned how to create a proper skeleton. We will require the same skeleton with a few additions. We will add a getInstance() method to our main class! Now, our code currently looks like this:
Now, because we learned Events in the last tutorial, I am going to explain what the skeleton means and does. When the plugin is ran, it gets the String "main" from your plugin.yml. With this string it locates your Main class. This all happens when the server is loaded up. It looks inside every plugin and grabs the main file. The onEnable() and onDisable() events run when the plugin is enabled and when the plugin is disabled. Now that you have a basic knowledge on this, I am going to introduce a new method for your main class. The onLoad() method. This is ran as soon as the plugin is starting to load and is quite useful. So now with our knowledge of the onLoad() method, lets add it to our code!
Now that it is added to our code we can create the getInstance method! The whole point of this method is to have the ability of using PluginBase methods outside of the Main class such as getLogger() and getServer(). So this method must be static. But if we are making a static method, how can we access the class it is in since it doesn't belong to that class? Well you see, it is quite simple. By adding the onLoad method we can now initiate a variable when the plugin is loading. So with that being said, we can can create a static variable with the type of your main class! Here is the code:
Great! Now we have a getInstance method! Now, we can access the Main class by doing Main.getInstance(). From this point on, it is limitless. You do not have to do anything I say as these are just examples.
Now that the Main class is setup, lets create another class called "Options". This is not required as you can do this in the Main class but the best practice is to have the least amount of code possible in your Main class to make the code more readable. In options lets create these two methods: getOption() and setOption(). Now these are essentially getter and setters so there is really no need for explanation. Here is the code:
Now that is what our current "Options" class is looking like. Now this is where our getInstance() method comes in handy. In Nukkit, config files are accessed with the "Config" class. In a later tutorial I will be teaching you how to use Config files to a full extent. But for the sake of this tutorial, I will be simply teaching you how to get your plugins default config file and read from/write to the file itself. So we will be using the getConfig() method from PluginBase. Now, in our "Options" class lets add some code to the methods. So in the setOption() method we want to assign a boolean value to a key. So we will be pass a String and boolean value through the method. Inside the method we are going to get the config file from Main, set the value of the boolean to the key, and save the config file. We can do this by doing to the following:
Now what this code does is it gets the default config file and saves it to "config". Then the values "key" and "value" are being passed into config.set(). This is setting the value of "key" to "value" in the config file. Now changes are not automatically saved so we have to call the config.save() method to save everything. The following is the code:
Now that out setOption() method is complete, we need a way to get the values. In our getOption() method add the following:
What this code does is sets the value of "config" to the default config and gets the boolean stored under "key". Note: When getting variables, you do not need to save them. Now this is the code with the code added:
Now everything is finished! Now anywhere you can save booleans to the default config and get booleans from the default config. Here is an example of the code:
Conclusion
Now you know how to save values to keys, get values from keys, get the default config file, and access the Main class from other classes. The possibilities are endless with this, so I highly recommend you play around with Config and use other data types. An advanced Config tutorial will be coming out soon!
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
Hello! In the last tutorial we learned how to use basic events. In this tutorial we will be learning how to use Config files effectively AND efficiently.
SETUP
All we require for this is a basic knowledge of Java and Nukkit with the code from part 1. Now that we have everything setup, lets begin!
CODE
In our previous tutorials we learned how to create a proper skeleton. We will require the same skeleton with a few additions. We will add a getInstance() method to our main class! Now, our code currently looks like this:
Code:
public class Main extends PluginBase{
@Override
public void onEnable(){
}
@Override
public void onDisable(){
}
}
Code:
public class Main extends PluginBase{
@Override
public void onEnable(){
}
@Override
public void onDisable(){
}
@Override
public void onLoad(){
}
}
Code:
public class Main extends PluginBase{
private static Main main;
@Override
public void onEnable(){
}
@Override
public void onDisable(){
}
@Override
public void onLoad(){
main = this;
}
public static Main getInstance(){
return main;
}
}
Now that the Main class is setup, lets create another class called "Options". This is not required as you can do this in the Main class but the best practice is to have the least amount of code possible in your Main class to make the code more readable. In options lets create these two methods: getOption() and setOption(). Now these are essentially getter and setters so there is really no need for explanation. Here is the code:
Code:
public class Options{
public static void setOption(String key, boolean value){
}
public static boolean getOption(String option){
}
}
Code:
Config config = Main.getInstance().getConfig();
config.set(key,value);
config.save();
Code:
public class Options{
public static void setOption(String key, boolean value){
Config config = Main.getInstance().getConfig();
config.set(key,value);
config.save();
}
public static boolean getOption(String option){
}
}
Code:
Config config = Main.getInstance().getConfig();
return config.getBoolean(key);
Code:
public class Options{
public static void setOption(String key, boolean value){
Config config = Main.getInstance().getConfig();
config.set(key,value);
config.save();
}
public static boolean getOption(String option){
Config config = Main.getInstance().getConfig();
return config.getBoolean(key);
}
}
Code:
boolean canBreak = Options.getOption("World.canBreak");
Options.setOption("World.canBreak", false);
Conclusion
Now you know how to save values to keys, get values from keys, get the default config file, and access the Main class from other classes. The possibilities are endless with this, so I highly recommend you play around with Config and use other data types. An advanced Config tutorial will be coming out soon!
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