ConfigSection - advansed way to use Config

fromgate

Administrator
Yesterday I made pull-request with Config class modifications:


Firstly, a separate ConfigSection class was added. This class inherits the LinkedHashMap class and now it is base of Config class (previously LinkedHashMap used to store data in Config).


Second, was added few constructors and methods, which allows to use Config without linking to the pre-defined file. Now you don't need to define file if you working with autonomous config objects or if you going to fill config with data stored in jar file.


Updated Config is fully compatible with previous verisions of Config and SimpleConfig class, so your current plugins will not requires modification.


Main goal of modifications - make working with configuration contained multiple objects easier.

For example you have configuration, that contains sections "players" with sub-sections, named according to player names" — players.<имяИгрока>.mana

Code:
players:
  fromgate:
	mana: 100
  bob:
	mana: 10
  john:
	mana: 30
You can directly access to mana value of know player — getConfig().getInt ("players.fromgate.mana");

But what if you need to get all player names? Or access to all values in loop?

Java:
	public void readPlayersMana(){
		ConfigSection section = this.getConfig().getSections("players");

		//Get all player names (subkeys of "players" section):
		Set<String> names = section.getKeys(false); 

		// Now you can loop through all player names
		names.forEach(player ->{
			int mana = ((ConfigSection) section.get(player)).getInt("mana");
			this.getLogger().info(player+"'s mana is "+mana);
		});

		// Or you can loop through all player-sections

		section.entrySet().forEach(entry ->{
			int mana = ((ConfigSection) entry.getValue()).getInt("mana");
			this.getLogger().info(entry.getKey()+"'s mana is "+mana);
		});
	}
ConfigSection contains all required methods, such as "set", "isSection", "isInt", "getInt", "getString", etc.
 

Pub4Game

Модератор всея Nukkit.ru
Moderator
Очень и очень полезная штука, спасибо!
 
Top