Dev Config

From MBedwars
Jump to navigation Jump to search

! Legacy Warning !

You are currently visiting a prehistoric website of the Marcely's Bedwars plugin for v4 and older.

In 2021, we released version 5 and completely overhauled the wiki. A lot of information that you find here likely won't be up-to-date anymore. You may find the updated wiki on: https://wiki.mbedwars.com

THIS SECTION IS MORE OR LESS DEPRECATED!
We are now using ConfigManager-2.









First of all, make sure that you have the BedwarsAddon object.
Click here to get to the documentation for that.
To save some things or to allow the user to configurate your AddOn, we added a way that's easier for you to add a configuration file that's looking the same as the configurations of this plugin.
Creating a config file is a bit more complicated than for example creating a command, but after a few minutes you should be able to fully understand how it works because of how simple the API for that is.
First of all we need the ConfigManager object for that.
We can get that through the getConfig method in our BedwarsAddon Object (Example:

ConfigManager cm = bedwarsAddon.getConfig();

). Lets create the base for our loading and saving class:

import de.marcely.bedwars.config.ConfigManager;

public class Config {
	public static ConfigManager cm = BedwarsAddonMultipleBeds.bedwarsAddon.getConfig();
	
 	public static void load(){
		cm.load(); // load the config file
		
		// LOAD STUFF IN HERE
		
		cm.clear(); // clear the cache in our ConfigManager to get memory space
	}
	
	public static void save(){
		cm.clear(); // clear to prevent that we save some things twice
		
		// SAVE STUFF IN HERE
		
		cm.save(); // save everything
	} 
}

Saving

Comments

Let's start by adding comments.
We do that by simply using the addComment(String str) method.
Example:

cm.addComment("Hello World! Watcha doin'? vv Please don't change this value vv ");

It will at the end look like this:

# Hello World! Watcha doin'? vv Please don't change this value vv

Empty Lines

To separate some things in our configuration file and to decorate it a bit, we are adding empty lines.
We do that by simply using the addEmptyLine() method.

Simple Configurations

This is probably the type you are going to use the most.
We do that by simply using the addConfig(String str, Object value) method.
Example:

String spigotPassword = "123456";
cm.addConfig("spigot-password", spigotPassword);

At the end it will look like this:

spigot-password: 123456

Advaned Configurations (Inside Configurations)

Now we are coming to the complex part.
You are probably (not) thinking how we for example this did:

votes_hillaryclinton {
   voted_yes: 218
   voted_no: 276
}
votes_donaldtrump {
   voted_yes = 276
   voted_no = 218
}

We call that thing 'inside configurations'.
If you take a look how we this did, you are probably going to understand because using this method we can go more 'deeper' or more 'inside'.
If you still don't understand why we are calling it 'inside configurations', here is an other example:

votes {
   huge {
      hillaryclinton {
         yes: 218
         no: 276
      }
      donaldtrump {
         yes: 276
         no: 218
      }
   }
   others {
      yes: -1
      no: -1
   }
}

We tried to create that as simple as posible.
Here's the way we did for the example above:

cm.addConfig("votes.huge.hillaryclinton.yes", 218);
cm.addConfig("votes.huge.hillaryclinton.no", 276);
cm.addConfig("votes.huge.donaldtrump.yes", 276);
cm.addConfig("votes.huge.donaldtrump.no", 218);
cm.addConfig("votes.others.yes", -1);
cm.addConfig("votes.others.no", -1);

Less complex than you probably thought, hmm?

Loading

Simple Configurations

Loading those type of configurations is more complex than saving it.
Here's the we do that:

Double resourcePrice = cm.getConfigDouble("resource-price");

if(resourcePrice != null)
   // do something with it

We check if it's not null because maybe the config doesn't exist.
These methods exists for getting the value of a configuration:

  • String getConfigString(String name)
  • Boolean getConfigBoolean(String name)
  • Double getConfigDouble(String name)
  • Int getConfigInt(String name)

Advanced Configurations (Inside Configurations)

Loading those types of configurations works nearly the same like loading the simple configuration.
Here's an example:

Int votes_huge_hillaryclinton_yes = cm.getConfigInt("votes.huge.hillaryclinton.yes");
Int votes_huge_hillaryclinton_no = cm.getConfigInt("votes.huge.hillaryclinton.no");
Int votes_huge_donaldtrump_yes = cm.getConfigInt("votes.huge.donaldtrump.yes");
Int votes_huge_donaldtrump_no = cm.getConfigInt("votes.huge.donaldtrump.no");
Int votes_others_yes = cm.getConfigInt("votes.others.yes");
Int votes_others_no = cm.getConfigInt("votes.others.no");

if(votes_huge_hillaryclinton_yes != null)
   // do something with it
if(votes_huge_hillaryclinton_no != null)
   // do something with it
if(votes_huge_donaldtrump_yes != null)
   // do something with it
if(votes_huge_donaldtrump_no != null)
   // do something with it
if(votes_others_yes != null)
   // do something with it
if(votes_others_no != null)
   // do something with it