summaryrefslogtreecommitdiffstats
path: root/EssentialsGroupManager/src/org/anjocaido/groupmanager/GMConfiguration.java
blob: 153221ed9b8b264533c0c76bf56e6c5647d6780a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.anjocaido.groupmanager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import org.anjocaido.groupmanager.utils.Tasks;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.reader.UnicodeReader;

/**
 *
 * @author gabrielcouto
 */
public class GMConfiguration {

    private GroupManager plugin;
    private Map<String, Object> rootDataNode;
    private File configFile;

    public GMConfiguration(GroupManager plugin) {
        this.plugin = plugin;
        load();
    }

    public void load() {
        if (!plugin.getDataFolder().exists()) {
            plugin.getDataFolder().mkdirs();
        }
        configFile = new File(plugin.getDataFolder(), "config.yml");

        if (!configFile.exists()) {
            try {
                Tasks.copy(plugin.getResourceAsStream("config.yml"), configFile);
            } catch (IOException ex) {
                GroupManager.logger.log(Level.SEVERE, null, ex);
            }
        }

        Yaml yaml = new Yaml(new SafeConstructor());
        FileInputStream rx = null;
        try {
            rx = new FileInputStream(configFile);
        } catch (FileNotFoundException ex) {
            GroupManager.logger.log(Level.SEVERE, null, ex);
        }
        try {
            rootDataNode = (Map<String, Object>) yaml.load(new UnicodeReader(rx));
            if (rootDataNode == null) {
                throw new NullPointerException();
            }
        } catch (Exception ex) {
            throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + configFile.getPath(), ex);
        } finally {
            try {
                rx.close();
            } catch (IOException ex) {
            }
        }
        adjustLoggerLevel();
    }

    public Map<String, Object> getMirrorsMap() {
        if (rootDataNode.get("settings") instanceof Map) {
            Map<String, Object> settingsNode = (Map<String, Object>) rootDataNode.get("settings");
            if (settingsNode.get("permission") instanceof Map) {
                Map<String, Object> permissionNode = (Map<String, Object>) settingsNode.get("permission");
                if (permissionNode.get("world") instanceof Map) {
                    Map<String, Object> worldsNode = (Map<String, Object>) permissionNode.get("world");
                    if (worldsNode.get("mirror") instanceof Map) {
                        Map<String, Object> mirrorsNode = (Map<String, Object>) worldsNode.get("mirror");
                        return mirrorsNode;
                    }
                }
            }
        }
        return null;
    }

    public Integer getSaveInterval() {
        if (rootDataNode.get("settings") instanceof Map) {
            Map<String, Object> settingsNode = (Map<String, Object>) rootDataNode.get("settings");
            if (settingsNode.get("data") instanceof Map) {
                Map<String, Object> dataNode = (Map<String, Object>) settingsNode.get("data");
                if (dataNode.get("save") instanceof Map) {
                    Map<String, Object> saveNode = (Map<String, Object>) dataNode.get("save");
                    if (saveNode.get("minutes") instanceof Integer) {
                        return (Integer) saveNode.get("minutes");
                    }
                }
            }
        }
        return 10;
    }

    public void adjustLoggerLevel() {
        if (rootDataNode.get("settings") instanceof Map) {
            Map<String, Object> settingsNode = (Map<String, Object>) rootDataNode.get("settings");
            if (settingsNode.get("logging") instanceof Map) {
                Map<String, Object> loggingNode = (Map<String, Object>) settingsNode.get("logging");
                if (loggingNode.get("level") instanceof String) {
                    String level = (String) loggingNode.get("level");
                    try {
                        GroupManager.logger.setLevel(Level.parse(level));
                        return;
                    } catch (Exception e) {
                    }
                }
            }
        }
        GroupManager.logger.setLevel(Level.INFO);
    }
}