|
| 1 | +package com.froxynetwork.froxybungee; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.ArrayList; |
| 6 | + |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import net.md_5.bungee.config.Configuration; |
| 11 | +import net.md_5.bungee.config.ConfigurationProvider; |
| 12 | +import net.md_5.bungee.config.YamlConfiguration; |
| 13 | + |
| 14 | +/** |
| 15 | + * FroxyBungee |
| 16 | + * Copyright (C) 2019 FroxyNetwork |
| 17 | + * |
| 18 | + * This program is free software: you can redistribute it and/or modify |
| 19 | + * it under the terms of the GNU General Public License as published by |
| 20 | + * the Free Software Foundation, either version 3 of the License, or |
| 21 | + * (at your option) any later version. |
| 22 | + * |
| 23 | + * This program is distributed in the hope that it will be useful, |
| 24 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | + * GNU General Public License for more details. |
| 27 | + * |
| 28 | + * You should have received a copy of the GNU General Public License |
| 29 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 30 | + * |
| 31 | + * @author 0ddlyoko |
| 32 | + */ |
| 33 | +public class Config { |
| 34 | + private final Logger LOG = LoggerFactory.getLogger(getClass()); |
| 35 | + private File fichierConfig = null; |
| 36 | + private Configuration config = null; |
| 37 | + |
| 38 | + public Config(File file) { |
| 39 | + this.fichierConfig = file; |
| 40 | + loadConfig(); |
| 41 | + } |
| 42 | + |
| 43 | + public void save() { |
| 44 | + try { |
| 45 | + ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, fichierConfig); |
| 46 | + } catch (IOException e) { |
| 47 | + LOG.error("An error has occured while saving file {}", fichierConfig.getPath()); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public void loadConfig() { |
| 52 | + try { |
| 53 | + config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(fichierConfig); |
| 54 | + } catch (IOException ex) { |
| 55 | + ex.printStackTrace(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public void set(String path, Object obj) { |
| 60 | + config.set(path, obj); |
| 61 | + save(); |
| 62 | + } |
| 63 | + |
| 64 | + public String getString(String path) { |
| 65 | + return getString(path, new Object[] {}); |
| 66 | + } |
| 67 | + |
| 68 | + public String getString(String path, Object... lists) { |
| 69 | + String name = config.getString(path); |
| 70 | + if (name != null && lists != null) |
| 71 | + for (int i = 0; i < lists.length; i++) |
| 72 | + name = name.replace("{" + i + "}", lists[i].toString()); |
| 73 | + |
| 74 | + return name == null ? null : name.replace("&", "§"); |
| 75 | + } |
| 76 | + |
| 77 | + public int getInt(String path) { |
| 78 | + return config.getInt(path); |
| 79 | + } |
| 80 | + |
| 81 | + public long getLong(String path) { |
| 82 | + return config.getLong(path); |
| 83 | + } |
| 84 | + |
| 85 | + public boolean getBoolean(String path) { |
| 86 | + return config.getBoolean(path); |
| 87 | + } |
| 88 | + |
| 89 | + public double getDouble(String path) { |
| 90 | + return config.getDouble(path); |
| 91 | + } |
| 92 | + |
| 93 | + public ArrayList<String> getStringList(String path) { |
| 94 | + ArrayList<String> name = new ArrayList<String>(); |
| 95 | + for (String nom : config.getStringList(path)) |
| 96 | + name.add(nom.replace("&", "§")); |
| 97 | + return name; |
| 98 | + } |
| 99 | + |
| 100 | + public ArrayList<Integer> getIntegerList(String path) { |
| 101 | + ArrayList<Integer> name = new ArrayList<Integer>(); |
| 102 | + for (int nom : config.getIntList(path)) |
| 103 | + name.add(nom); |
| 104 | + return name; |
| 105 | + } |
| 106 | + |
| 107 | + public ArrayList<String> getKeys(String path) { |
| 108 | + ArrayList<String> list = new ArrayList<>(); |
| 109 | + if ("".equalsIgnoreCase(path)) { |
| 110 | + for (String section : config.getKeys()) |
| 111 | + list.add(section); |
| 112 | + } else { |
| 113 | + Configuration cs = config.getSection(path); |
| 114 | + if (cs == null) |
| 115 | + return list; |
| 116 | + for (String section : cs.getKeys()) |
| 117 | + list.add(section); |
| 118 | + } |
| 119 | + return list; |
| 120 | + } |
| 121 | + |
| 122 | + public boolean exist(String path) { |
| 123 | + return config.contains(path); |
| 124 | + } |
| 125 | + |
| 126 | + public void reload() { |
| 127 | + loadConfig(); |
| 128 | + } |
| 129 | +} |
0 commit comments