Core/src/eu/univento/core/api/Config.java

172 lines
4.1 KiB
Java

package eu.univento.core.api;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
/**
* gets data from config file
* @author joethei
* @version 1.1
*/
public class Config {
/**config file*/
private static File file = new File("plugins/Core", "config.yml");
/**load configuration */
private static FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
/**
* write default data to config
* @throws ClassNotFoundException Class couldn't be found
* @throws SQLException SQL server not available or throwing error
* @throws IOException I/O failed
*/
public static void writeDefault() throws ClassNotFoundException, SQLException, IOException {
//editable messages will be set here, but do not edit this messages.
//seting the default MySQL config.
cfg.addDefault("MySQL.Host", "192.168.0.101");
cfg.addDefault("MySQL.Port", "3306");
cfg.addDefault("MySQL.DB", "core");
cfg.addDefault("MySQL.User", "root");//best user name
cfg.addDefault("MySQL.Pass", "");//best password
cfg.addDefault("TS.IP", "ts.univento.eu");
cfg.addDefault("TS.QueryPort", 0);
cfg.addDefault("TS.QueryUser", "ServerQuery");
cfg.addDefault("TS.QueryPass", "password");
cfg.options().copyDefaults(true);
cfg.save(file);
}
/**
* write data as string to config
* @param path path to data
* @param obj data
*/
public static void write(String path, String obj) {
cfg.set(path, obj);
try {
cfg.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* write data as integer to config
* @param path path to data
* @param obj data
*/
public static void write(String path, int obj) {
cfg.set(path, obj);
try {
cfg.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* write data as double to config
* @param path path to data
* @param obj data
*/
public static void write(String path, double obj) {
cfg.set(path, obj);
try {
cfg.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* write location to config
* @param path path to data
* @param loc location
*/
public static void write(String path, Location loc) {
String world = loc.getWorld().getName();
double x = loc.getX();
double y = loc.getY();
double z = loc.getZ();
double yaw = (double) loc.getYaw();
double pitch = (double) loc.getPitch();
cfg.set(path + ".World", world);
cfg.set(path + ".X", x);
cfg.set(path + ".Y", y);
cfg.set(path + ".Z", z);
cfg.set(path + ".Yaw", yaw);
cfg.set(path + ".Pitch", pitch);
try {
cfg.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* read integer from config
* @param path path to data
* @return Integer
*/
public static int readInt(String path) {
return cfg.getInt(path);
}
/**
* read double from config
* @param path path to data
* @return Double
*/
public static double readDouble(String path) {
return cfg.getDouble(path);
}
/**
* read string from config
* @param path path to data
* @return String
*/
public static String readString(String path) {
return cfg.getString(path);
}
/**
* read location from config
* @param path path to data
* @return Location
*/
public static Location readLocation(String path) {
String world = cfg.getString(path + ".World");
double x = cfg.getDouble(path + ".X");
double y = cfg.getDouble(path + ".Y");
double z = cfg.getDouble(path + ".Z");
float yaw = (float) cfg.getDouble(path + ".Yaw");
float pitch = (float) cfg.getDouble(path + ".Pitch");
Location loc = new Location(Bukkit.getWorld(world), x, y, z, yaw, pitch);
return loc;
}
/**
* checks if data is existing
* @param path path do data
* @return true / false
*/
public static boolean isExsisting(String path) {
return cfg.contains(path);
}
}