1
|
|
|
package net.labymod.serverapi; |
2
|
|
|
|
3
|
|
|
import lombok.Getter; |
4
|
|
|
|
5
|
|
|
import java.io.File; |
6
|
|
|
import java.util.HashMap; |
7
|
|
|
import java.util.Map; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class created by qlow | Jan |
11
|
|
|
*/ |
12
|
|
|
public abstract class LabyModConfig { |
13
|
|
|
|
14
|
|
|
protected File file; |
15
|
|
|
|
16
|
|
|
@Getter |
17
|
|
|
private Map<Permission, Boolean> permissions = new HashMap<>(); |
|
|
|
|
18
|
|
|
|
19
|
|
|
public LabyModConfig( File file ) { |
20
|
|
|
this.file = file; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Called once to initialize the config |
25
|
|
|
* |
26
|
|
|
* @param file the config-file |
27
|
|
|
*/ |
28
|
|
|
public abstract void init( File file ); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Gets a key's value |
32
|
|
|
* |
33
|
|
|
* @param key the key the value should be resolved from |
34
|
|
|
* @return the value according to this key or <code>null</code> if there is no value according to this key |
35
|
|
|
*/ |
36
|
|
|
public abstract Object getValue( String key ); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Adds a default to the config |
40
|
|
|
* |
41
|
|
|
* @param key the key |
42
|
|
|
* @param value the default value |
43
|
|
|
*/ |
44
|
|
|
public abstract void addDefault( String key, Object value ); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Saves the config |
48
|
|
|
*/ |
49
|
|
|
public abstract void saveConfig(); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Adds the config's defaults |
53
|
|
|
*/ |
54
|
|
|
public void addDefaults() { |
55
|
|
|
// Iterating through all permissions |
56
|
|
|
for ( Permission permission : Permission.values() ) { |
57
|
|
|
// Putting the default value in |
58
|
|
|
addDefault( "permissions." + permission.name(), permission.isDefaultEnabled() ); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Loads the config values after adding the defaults |
64
|
|
|
*/ |
65
|
|
|
public void loadValues() { |
66
|
|
|
// Iterating through all permissions |
67
|
|
|
for ( Permission permission : Permission.values() ) { |
68
|
|
|
Object value = getValue( "permissions." + permission.name() ); |
69
|
|
|
|
70
|
|
|
// Checking whether there is a value according to this permission |
71
|
|
|
if ( value != null && value instanceof Boolean ) { |
72
|
|
|
// Checking whether the permission value was modified |
73
|
|
|
permissions.put( permission, ( Boolean ) value ); |
74
|
|
|
} else { |
75
|
|
|
permissions.put( permission, permission.isDefaultEnabled() ); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
The Java documentation explain EnumMap.