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

177 lines
4.0 KiB
Java

/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
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
*/
@Deprecated
public class Config {
/**config file*/
private static final File file = new File("plugins/Core", "config.yml");
/**load configuration */
private static final 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 IOException {
//editable messages will be set here, but do not edit this messages.
//setting the default MySQL config.
cfg.addDefault("MySQL.Host", "hostname");
cfg.addDefault("MySQL.Port", "3306");
cfg.addDefault("MySQL.DB", "core");
cfg.addDefault("MySQL.User", "root");
cfg.addDefault("MySQL.Pass", "");
cfg.addDefault("MongoDB.Host", "Hostname");
cfg.addDefault("MongoDB.Port", 27017);
cfg.addDefault("MongoDB.User", "univento");
cfg.addDefault("MongoDB.Password", "1234");
cfg.addDefault("MongoDB.Database", "univento");
cfg.options().copyDefaults(true);
cfg.save(file);
}
/**
* write data as string to config
* @param obj data
*/
public static void write(String obj) {
cfg.set("lastPlot", 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
* @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");
return new Location(Bukkit.getWorld(world), x, y, z, yaw, pitch);
}
/**
* checks if data is existing
* @param path path do data
* @return true / false
*/
public static boolean isExsisting(String path) {
return cfg.contains(path);
}
}