Java Reference
In-Depth Information
Let's look at how to modify the plugin code to use a config file. The important
part is in the enable() function. As it first starts up, it'll go and grab values
from the configuration file and set them into a few static variables for our
plugin to use. Here's what I added:
SquidBombConfig/src/squidbombconfig/SquidBombConfig.java
private static int numSquids;
private static double squidDropHeight;
private static boolean setFire;
// Server/config/SquidBombConfig/SquidBombConfig.cfg:
//
numSquids=6
//
squidDropHeight=5
//
setFire=false
public boolean enable() {
super.enable(); //Compiler will call this if you don't
logger.info( "Getting config data" );
PropertiesFile config = getConfig();
numSquids = config.getInt( "numSquids" , 6);
squidDropHeight = config.getDouble( "squidDropHeight" , 5.0);
setFire = config.getBoolean( "setFire" , false);
config.save(); // Create a new one if needed
return true;
}
First, we call getConfig() to get the PropertiesFile object for our plugin. (If there is
no actual file yet, we'll get an empty object instead.) With this we can now
make calls to get values out of the config file.
Each call to read data looks like config.get Type () , where Type is the type of the
variable you're trying to get, so you have getInt , getDouble , getBoolean , getString ,
and such.
Note that you have to use the camel-case version of the data type, so it's getInt ,
not getint ; getBoolean , not getboolean ; getDouble , not getdouble ; and so on.
Using functions named get XXX and set XXX to read and save variables is a
pretty standard part of Java. We refer to these kinds of functions as getters
and setters . Okay, perhaps not the most original names.
For each of the config file getters, you can specify a default value to use in
case that setting isn't in the file. So for instance, calling:
numSquids = config.getInt( "numSquids" , 6);
will set numSquids to 6, even if the setting for numSquids doesn't exist in the
config file, or if the file doesn't even exist at all. If there is a setting in the file,
then numSquids will be set to that value.
 
 
 
Search WWH ::




Custom Search