Config Bestpractise
Config Bestpractise
As example Ill use an Arena with data like a cuboid, spawn point, name etc
The example
So we have a class like
public
class
Arena
protected
UUID
uid;
protected
String name;
protected
Cuboid cuboid;
protected
Location spawn;
protected
List<
UUID
>
players
=
new
ArrayList<
UUID
>
();
public
Arena
(UUID
uid
, String
name
) {
this
.
uid
=
uid;
this
.
name
=
name;
}
//Etc etc...
Now we would want to store each arena by its UID and store the name, cuboid and spawn.
We would have other classes extend from this Arena class with extra data.
For example if we were to make a race game we would create RaceArena and it would have extra data like a
cuboid for the finish and a map with checkpoints like <Integer(index), CheckPoint>
Checkpoint would then consist of a location where the player would respawn and a cuboid that will trigger
the checkpoint.
Then to make the example more clear we add another game like Spleef and we create a SpleefArena.
This would have a cuboid for the floor to protect it and reset it etc.
So the ideal config would be similar to this.
{uid}
:
#Race arena.
name
:
{name}
spawn
:
{location}
cuboid
:
{cuboid}
checkpoints
:
'1'
:
location
:
{location}
cuboid
:
{cuboid}
'2'
:
etc
:
...
finish
:
{cuboid}
{uid}
:
#Spleef arena.
name
:
{name}
spawn
:
{spawn}
cuboid
:
{cuboid}
floor
:
{cuboid}
Options
Config per arena
A configuration file per arena.
The arena class would have a YamlConfiguration and it would use the values directly from the config
instead of storing them internally in the arena class.
public
class
Arena
{
protected
YamlConfiguration cfg;
public
void
setName
(String
name
) {
cfg
.
set(
"name"
, name);
public
String
getName
() {
return
cfg
.
getString(
"name"
);
Or alternatively store the data in the class too and when setting set it both in the class and config.
Havent really looked into configs so not sure if its an expensive call to do cfg.get
return
data
}