Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.KeinAnze1chen.friends;
- import java.io.File;
- import net.KeinAnze1chen.friends.mysql.FileManager;
- import net.KeinAnze1chen.friends.mysql.MySQL;
- import net.md_5.bungee.api.plugin.Plugin;
- import net.md_5.bungee.config.Configuration;
- public class Friends extends Plugin{
- private MySQL mysql;
- private FileManager filemanager;
- public void onEnable() {
- /* Fetch classes*/
- fetchClasses();
- /* Loading MySQL Settings*/
- loadMySQLFiles();
- /* Broadcast standard enabling message*/
- }
- public void onDisable() {
- }
- public void fetchClasses(){
- mysql = new MySQL(this);
- filemanager = new FileManager(this);
- }
- public void loadMySQLFiles(){
- getDataFolder().mkdirs();
- if(!filemanager.exists("mysql.yml", "Friends")){
- File file =filemanager.createNewFile("mysql.yml", "Friends");
- Configuration cfg = filemanager.getConfiguration("mysql.yml", "Friends");
- cfg.set("user", "localuser");
- cfg.set("password", "localpasswort");
- cfg.set("host", "localhost");
- cfg.set("database", "localdatabse");
- filemanager.saveFile(file, cfg);
- }
- Configuration cfg = filemanager.getConfiguration("mysql.yml", "Friends");
- getMySQL().host = cfg.getString("host");
- getMySQL().username = cfg.getString("user");
- getMySQL().database = cfg.getString("database");
- getMySQL().password = cfg.getString("password");
- }
- public String getPrefix() {
- return "§9Friends§8> §7";
- }
- public MySQL getMySQL(){
- return mysql;
- }
- public FileManager getFileManager(){
- return filemanager;
- }
- }
- package net.KeinAnze1chen.friends.mysql;
- import java.io.File;
- import java.io.IOException;
- import net.KeinAnze1chen.friends.Friends;
- import net.md_5.bungee.config.Configuration;
- import net.md_5.bungee.config.ConfigurationProvider;
- import net.md_5.bungee.config.YamlConfiguration;
- public class FileManager {
- Friends friends;
- public FileManager(Friends friends){
- this.friends = friends;
- }
- public File createNewFile(String filename, String path){
- File f = new File("plugins/"+path, filename);
- if(!f.exists()){
- try {
- f.createNewFile();
- return f;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- public File getFile(String filename, String path){
- return new File("plugins/"+path, filename);
- }
- public boolean exists(String filename, String path){
- return getFile(filename, path).exists();
- }
- public void deleteFile(String filename, String path){
- File f = new File("plugins/"+path, filename);
- if(f.exists()){
- f.delete();
- }
- }
- public Configuration getConfiguration(String filename, String path){
- try {
- return ConfigurationProvider.getProvider(YamlConfiguration.class).load(getFile(filename, path));
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- public void saveFile(File file, Configuration cfg){
- try {
- ConfigurationProvider.getProvider(YamlConfiguration.class).save(cfg, file);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import net.md_5.bungee.api.ProxyServer;
- import net.md_5.bungee.api.chat.TextComponent;
- public class MySQL<Friends> {
- public String username;
- public String password;
- public String database;
- public String host;
- public String port;
- public Connection con;
- Friends friends;
- public MySQL(Friends friends) {
- this.friends = friends;
- }
- public void connect() {
- if (!isConnected()) {
- try {
- con = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + database + "?autoReconnect=true",
- username, password);
- ProxyServer.getInstance().getConsole()
- .sendMessage(new TextComponent(((net.KeinAnze1chen.friends.Friends) friends).getPrefix() + "§aSuccessfully connected to MySQL-Database."));
- } catch (SQLException e) {
- ProxyServer.getInstance().getConsole()
- .sendMessage(new TextComponent(((net.KeinAnze1chen.friends.Friends) friends).getPrefix() + "§cCould not connect to MySQL-Database, please check your MySQL-Settings."));
- }
- }
- }
- public void close() {
- if (isConnected()) {
- try {
- con.close();
- con = null;
- ProxyServer.getInstance().getConsole()
- .sendMessage(new TextComponent(((net.KeinAnze1chen.friends.Friends) friends).getPrefix() + "§aSuccessfully closed MySQL-Connection."));
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public boolean isConnected() {
- return con != null;
- }
- public void update(String qry) {
- if (isConnected()) {
- try {
- con.createStatement().executeUpdate(qry);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public ResultSet getResult(String qry) {
- if (isConnected()) {
- try {
- return con.createStatement().executeQuery(qry);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement