Created
August 11, 2015 20:40
-
-
Save Fabricio20/7c19c3d143a5032bb53c to your computer and use it in GitHub Desktop.
SimpleConfig Lib For SpigotMC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import org.bukkit.configuration.ConfigurationSection; | |
import org.bukkit.configuration.file.FileConfiguration; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import org.bukkit.plugin.java.JavaPlugin; | |
public class SimpleConfig { | |
private int comments; | |
private SimpleConfigManager manager; | |
private File file; | |
private FileConfiguration config; | |
@SuppressWarnings("deprecation") | |
public SimpleConfig(InputStream configStream, File configFile, | |
int comments, JavaPlugin plugin) { | |
this.comments = comments; | |
this.manager = new SimpleConfigManager(plugin); | |
this.file = configFile; | |
this.config = YamlConfiguration.loadConfiguration(configStream); | |
} | |
public Object get(String path) { | |
return this.config.get(path); | |
} | |
public Object get(String path, Object def) { | |
return this.config.get(path, def); | |
} | |
public String getString(String path) { | |
return this.config.getString(path); | |
} | |
public String getString(String path, String def) { | |
return this.config.getString(path, def); | |
} | |
public int getInt(String path) { | |
return this.config.getInt(path); | |
} | |
public int getInt(String path, int def) { | |
return this.config.getInt(path, def); | |
} | |
public boolean getBoolean(String path) { | |
return this.config.getBoolean(path); | |
} | |
public boolean getBoolean(String path, boolean def) { | |
return this.config.getBoolean(path, def); | |
} | |
public void createSection(String path) { | |
this.config.createSection(path); | |
} | |
public ConfigurationSection getConfigurationSection(String path) { | |
return this.config.getConfigurationSection(path); | |
} | |
public double getDouble(String path) { | |
return this.config.getDouble(path); | |
} | |
public double getDouble(String path, double def) { | |
return this.config.getDouble(path, def); | |
} | |
public List<?> getList(String path) { | |
return this.config.getList(path); | |
} | |
public List<?> getList(String path, List<?> def) { | |
return this.config.getList(path, def); | |
} | |
public List<String> getStringList(String path) { | |
List<?> list = getList(path); | |
if (list == null) { | |
return new ArrayList<String>(0); | |
} | |
List<String> result = new ArrayList<String>(); | |
for (Object object : list) { | |
if ((object instanceof String) || (isPrimitiveWrapper(object))) { | |
result.add(String.valueOf(object)); | |
} | |
} | |
return result; | |
} | |
public List<Integer> getIntegerList(String path) { | |
List<?> list = getList(path); | |
if (list == null) { | |
return new ArrayList<Integer>(0); | |
} | |
List<Integer> result = new ArrayList<Integer>(); | |
for (Object object : list) { | |
if (object instanceof Integer) { | |
result.add((Integer) object); | |
} else if (object instanceof String) { | |
try { | |
result.add(Integer.valueOf((String) object)); | |
} catch (Exception ex) { | |
} | |
} else if (object instanceof Character) { | |
result.add((int) ((Character) object).charValue()); | |
} else if (object instanceof Number) { | |
result.add(((Number) object).intValue()); | |
} | |
} | |
return result; | |
} | |
public List<Boolean> getBooleanList(String path) { | |
List<?> list = getList(path); | |
if (list == null) { | |
return new ArrayList<Boolean>(0); | |
} | |
List<Boolean> result = new ArrayList<Boolean>(); | |
for (Object object : list) { | |
if (object instanceof Boolean) { | |
result.add((Boolean) object); | |
} else if (object instanceof String) { | |
if (Boolean.TRUE.toString().equals(object)) { | |
result.add(true); | |
} else if (Boolean.FALSE.toString().equals(object)) { | |
result.add(false); | |
} | |
} | |
} | |
return result; | |
} | |
public List<Double> getDoubleList(String path) { | |
List<?> list = getList(path); | |
if (list == null) { | |
return new ArrayList<Double>(0); | |
} | |
List<Double> result = new ArrayList<Double>(); | |
for (Object object : list) { | |
if (object instanceof Double) { | |
result.add((Double) object); | |
} else if (object instanceof String) { | |
try { | |
result.add(Double.valueOf((String) object)); | |
} catch (Exception ex) { | |
} | |
} else if (object instanceof Character) { | |
result.add((double) ((Character) object).charValue()); | |
} else if (object instanceof Number) { | |
result.add(((Number) object).doubleValue()); | |
} | |
} | |
return result; | |
} | |
public List<Float> getFloatList(String path) { | |
List<?> list = getList(path); | |
if (list == null) { | |
return new ArrayList<Float>(0); | |
} | |
List<Float> result = new ArrayList<Float>(); | |
for (Object object : list) { | |
if (object instanceof Float) { | |
result.add((Float) object); | |
} else if (object instanceof String) { | |
try { | |
result.add(Float.valueOf((String) object)); | |
} catch (Exception ex) { | |
} | |
} else if (object instanceof Character) { | |
result.add((float) ((Character) object).charValue()); | |
} else if (object instanceof Number) { | |
result.add(((Number) object).floatValue()); | |
} | |
} | |
return result; | |
} | |
public List<Long> getLongList(String path) { | |
List<?> list = getList(path); | |
if (list == null) { | |
return new ArrayList<Long>(0); | |
} | |
List<Long> result = new ArrayList<Long>(); | |
for (Object object : list) { | |
if (object instanceof Long) { | |
result.add((Long) object); | |
} else if (object instanceof String) { | |
try { | |
result.add(Long.valueOf((String) object)); | |
} catch (Exception ex) { | |
} | |
} else if (object instanceof Character) { | |
result.add((long) ((Character) object).charValue()); | |
} else if (object instanceof Number) { | |
result.add(((Number) object).longValue()); | |
} | |
} | |
return result; | |
} | |
public List<Byte> getByteList(String path) { | |
List<?> list = getList(path); | |
if (list == null) { | |
return new ArrayList<Byte>(0); | |
} | |
List<Byte> result = new ArrayList<Byte>(); | |
for (Object object : list) { | |
if (object instanceof Byte) { | |
result.add((Byte) object); | |
} else if (object instanceof String) { | |
try { | |
result.add(Byte.valueOf((String) object)); | |
} catch (Exception ex) { | |
} | |
} else if (object instanceof Character) { | |
result.add((byte) ((Character) object).charValue()); | |
} else if (object instanceof Number) { | |
result.add(((Number) object).byteValue()); | |
} | |
} | |
return result; | |
} | |
public List<Character> getCharacterList(String path) { | |
List<?> list = getList(path); | |
if (list == null) { | |
return new ArrayList<Character>(0); | |
} | |
List<Character> result = new ArrayList<Character>(); | |
for (Object object : list) { | |
if (object instanceof Character) { | |
result.add((Character) object); | |
} else if (object instanceof String) { | |
String str = (String) object; | |
if (str.length() == 1) { | |
result.add(str.charAt(0)); | |
} | |
} else if (object instanceof Number) { | |
result.add((char) ((Number) object).intValue()); | |
} | |
} | |
return result; | |
} | |
public List<Short> getShortList(String path) { | |
List<?> list = getList(path); | |
if (list == null) { | |
return new ArrayList<Short>(0); | |
} | |
List<Short> result = new ArrayList<Short>(); | |
for (Object object : list) { | |
if (object instanceof Short) { | |
result.add((Short) object); | |
} else if (object instanceof String) { | |
try { | |
result.add(Short.valueOf((String) object)); | |
} catch (Exception ex) { | |
} | |
} else if (object instanceof Character) { | |
result.add((short) ((Character) object).charValue()); | |
} else if (object instanceof Number) { | |
result.add(((Number) object).shortValue()); | |
} | |
} | |
return result; | |
} | |
public List<Map<?, ?>> getMapList(String path) { | |
List<?> list = getList(path); | |
List<Map<?, ?>> result = new ArrayList<Map<?, ?>>(); | |
if (list == null) { | |
return result; | |
} | |
for (Object object : list) { | |
if (object instanceof Map) { | |
result.add((Map<?, ?>) object); | |
} | |
} | |
return result; | |
} | |
public boolean contains(String path) { | |
return this.config.contains(path); | |
} | |
public void removeKey(String path) { | |
this.config.set(path, null); | |
} | |
public void set(String path, Object value) { | |
this.config.set(path, value); | |
} | |
public void set(String path, Object value, String comment) { | |
if (!this.config.contains(path)) { | |
this.config.set(manager.getPluginName() + "_COMMENT_" + comments, | |
" " + comment); | |
comments++; | |
} | |
this.config.set(path, value); | |
} | |
public void set(String path, Object value, String[] comment) { | |
for (String comm : comment) { | |
if (!this.config.contains(path)) { | |
this.config.set(manager.getPluginName() + "_COMMENT_" | |
+ comments, " " + comm); | |
comments++; | |
} | |
} | |
this.config.set(path, value); | |
} | |
public void setHeader(String[] header) { | |
manager.setHeader(this.file, header); | |
this.comments = header.length + 2; | |
this.reloadConfig(); | |
} | |
@SuppressWarnings("deprecation") | |
public void reloadConfig() { | |
this.config = YamlConfiguration.loadConfiguration(manager | |
.getConfigContent(file)); | |
} | |
public void saveConfig() { | |
String config = this.config.saveToString(); | |
manager.saveConfig(config, this.file); | |
} | |
public Set<String> getKeys() { | |
return this.config.getKeys(false); | |
} | |
protected boolean isPrimitiveWrapper(Object input) { | |
return input instanceof Integer || input instanceof Boolean | |
|| input instanceof Character || input instanceof Byte | |
|| input instanceof Short || input instanceof Double | |
|| input instanceof Long || input instanceof Float; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.ByteArrayInputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.nio.charset.Charset; | |
import org.bukkit.plugin.java.JavaPlugin; | |
public class SimpleConfigManager { | |
private JavaPlugin plugin; | |
/* | |
* Manage custom configurations and files | |
*/ | |
public SimpleConfigManager(JavaPlugin plugin) { | |
this.plugin = plugin; | |
} | |
/* | |
* Get new configuration with header | |
* | |
* @param filePath - Path to file | |
* | |
* @return - New SimpleConfig | |
*/ | |
public SimpleConfig getNewConfig(String filePath, String[] header) { | |
File file = this.getConfigFile(filePath); | |
if (!file.exists()) { | |
this.prepareFile(filePath); | |
if (header != null && header.length != 0) { | |
this.setHeader(file, header); | |
} | |
} | |
SimpleConfig config = new SimpleConfig(this.getConfigContent(filePath), | |
file, this.getCommentsNum(file), plugin); | |
return config; | |
} | |
/* | |
* Get new configuration | |
* | |
* @param filePath - Path to file | |
* | |
* @return - New SimpleConfig | |
*/ | |
public SimpleConfig getNewConfig(String filePath) { | |
return this.getNewConfig(filePath, null); | |
} | |
/* | |
* Get configuration file from string | |
* | |
* @param file - File path | |
* | |
* @return - New file object | |
*/ | |
private File getConfigFile(String file) { | |
if (file.isEmpty() || file == null) { | |
return null; | |
} | |
File configFile; | |
if (file.contains("/")) { | |
if (file.startsWith("/")) { | |
configFile = new File(plugin.getDataFolder() | |
+ file.replace("/", File.separator)); | |
} else { | |
configFile = new File(plugin.getDataFolder() + File.separator | |
+ file.replace("/", File.separator)); | |
} | |
} else { | |
configFile = new File(plugin.getDataFolder(), file); | |
} | |
return configFile; | |
} | |
/* | |
* Create new file for config and copy resource into it | |
* | |
* @param file - Path to file | |
* | |
* @param resource - Resource to copy | |
*/ | |
public void prepareFile(String filePath, String resource) { | |
File file = this.getConfigFile(filePath); | |
if (file.exists()) { | |
return; | |
} | |
try { | |
file.getParentFile().mkdirs(); | |
file.createNewFile(); | |
if (resource != null && !resource.isEmpty()) { | |
this.copyResource(plugin.getResource(resource), file); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
/* | |
* Create new file for config without resource | |
* | |
* @param file - File to create | |
*/ | |
public void prepareFile(String filePath) { | |
this.prepareFile(filePath, null); | |
} | |
/* | |
* Adds header block to config | |
* | |
* @param file - Config file | |
* | |
* @param header - Header lines | |
*/ | |
public void setHeader(File file, String[] header) { | |
if (!file.exists()) { | |
return; | |
} | |
try { | |
String currentLine; | |
StringBuilder config = new StringBuilder(""); | |
BufferedReader reader = new BufferedReader(new FileReader(file)); | |
while ((currentLine = reader.readLine()) != null) { | |
config.append(currentLine + "\n"); | |
} | |
reader.close(); | |
config.append("# +----------------------------------------------------+ #\n"); | |
for (String line : header) { | |
if (line.length() > 50) { | |
continue; | |
} | |
int lenght = (50 - line.length()) / 2; | |
StringBuilder finalLine = new StringBuilder(line); | |
for (int i = 0; i < lenght; i++) { | |
finalLine.append(" "); | |
finalLine.reverse(); | |
finalLine.append(" "); | |
finalLine.reverse(); | |
} | |
if (line.length() % 2 != 0) { | |
finalLine.append(" "); | |
} | |
config.append("# < " + finalLine.toString() + " > #\n"); | |
} | |
config.append("# +----------------------------------------------------+ #"); | |
BufferedWriter writer = new BufferedWriter(new FileWriter(file)); | |
writer.write(this.prepareConfigString(config.toString())); | |
writer.flush(); | |
writer.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
/* | |
* Read file and make comments SnakeYAML friendly | |
* | |
* @param filePath - Path to file | |
* | |
* @return - File as Input Stream | |
*/ | |
public InputStream getConfigContent(File file) { | |
if (!file.exists()) { | |
return null; | |
} | |
try { | |
int commentNum = 0; | |
String addLine; | |
String currentLine; | |
String pluginName = this.getPluginName(); | |
StringBuilder whole = new StringBuilder(""); | |
BufferedReader reader = new BufferedReader(new FileReader(file)); | |
while ((currentLine = reader.readLine()) != null) { | |
if (currentLine.startsWith("#")) { | |
addLine = currentLine.replaceFirst("#", pluginName | |
+ "_COMMENT_" + commentNum + ":"); | |
whole.append(addLine + "\n"); | |
commentNum++; | |
} else { | |
whole.append(currentLine + "\n"); | |
} | |
} | |
String config = whole.toString(); | |
InputStream configStream = new ByteArrayInputStream( | |
config.getBytes(Charset.forName("UTF-8"))); | |
reader.close(); | |
return configStream; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
/* | |
* Get comments from file | |
* | |
* @param file - File | |
* | |
* @return - Comments number | |
*/ | |
private int getCommentsNum(File file) { | |
if (!file.exists()) { | |
return 0; | |
} | |
try { | |
int comments = 0; | |
String currentLine; | |
BufferedReader reader = new BufferedReader(new FileReader(file)); | |
while ((currentLine = reader.readLine()) != null) { | |
if (currentLine.startsWith("#")) { | |
comments++; | |
} | |
} | |
reader.close(); | |
return comments; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return 0; | |
} | |
} | |
/* | |
* Get config content from file | |
* | |
* @param filePath - Path to file | |
* | |
* @return - readied file | |
*/ | |
public InputStream getConfigContent(String filePath) { | |
return this.getConfigContent(this.getConfigFile(filePath)); | |
} | |
private String prepareConfigString(String configString) { | |
int lastLine = 0; | |
int headerLine = 0; | |
String[] lines = configString.split("\n"); | |
StringBuilder config = new StringBuilder(""); | |
for (String line : lines) { | |
if (line.startsWith(this.getPluginName() + "_COMMENT")) { | |
String comment = "#" | |
+ line.trim().substring(line.indexOf(":") + 1); | |
if (comment.startsWith("# +-")) { | |
/* | |
* If header line = 0 then it is header start, if it's equal | |
* to 1 it's the end of header | |
*/ | |
if (headerLine == 0) { | |
config.append(comment + "\n"); | |
lastLine = 0; | |
headerLine = 1; | |
} else if (headerLine == 1) { | |
config.append(comment + "\n\n"); | |
lastLine = 0; | |
headerLine = 0; | |
} | |
} else { | |
/* | |
* Last line = 0 - Comment Last line = 1 - Normal path | |
*/ | |
String normalComment; | |
if (comment.startsWith("# ' ")) { | |
normalComment = comment.substring(0, | |
comment.length() - 1) | |
.replaceFirst("# ' ", "# "); | |
} else { | |
normalComment = comment; | |
} | |
if (lastLine == 0) { | |
config.append(normalComment + "\n"); | |
} else if (lastLine == 1) { | |
config.append("\n" + normalComment + "\n"); | |
} | |
lastLine = 0; | |
} | |
} else { | |
config.append(line + "\n"); | |
lastLine = 1; | |
} | |
} | |
return config.toString(); | |
} | |
/* | |
* Saves configuration to file | |
* | |
* @param configString - Config string | |
* | |
* @param file - Config file | |
*/ | |
public void saveConfig(String configString, File file) { | |
String configuration = this.prepareConfigString(configString); | |
try { | |
BufferedWriter writer = new BufferedWriter(new FileWriter(file)); | |
writer.write(configuration); | |
writer.flush(); | |
writer.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public String getPluginName() { | |
return plugin.getDescription().getName(); | |
} | |
/* | |
* Copy resource from Input Stream to file | |
* | |
* @param resource - Resource from .jar | |
* | |
* @param file - File to write | |
*/ | |
private void copyResource(InputStream resource, File file) { | |
try { | |
OutputStream out = new FileOutputStream(file); | |
int lenght; | |
byte[] buf = new byte[1024]; | |
while ((lenght = resource.read(buf)) > 0) { | |
out.write(buf, 0, lenght); | |
} | |
out.close(); | |
resource.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment