Dev Config: Difference between revisions

From MBedwars
Jump to navigation Jump to search
No edit summary
No edit summary
Line 78: Line 78:
     }
     }
  }
  }
We tried to create that as simple as posible.<br />
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?

Revision as of 16:45, 9 November 2016

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.clear(); // same reason as in load() but this time optional
	} 
}

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?