Advertisement
Guest User

FreundeSystem

a guest
May 2nd, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. package net.KeinAnze1chen.friends;
  2.  
  3. import java.io.File;
  4.  
  5. import net.KeinAnze1chen.friends.mysql.FileManager;
  6. import net.KeinAnze1chen.friends.mysql.MySQL;
  7. import net.md_5.bungee.api.plugin.Plugin;
  8. import net.md_5.bungee.config.Configuration;
  9.  
  10. public class Friends extends Plugin{
  11.  
  12. private MySQL mysql;
  13. private FileManager filemanager;
  14.  
  15. public void onEnable() {
  16.  
  17. /* Fetch classes*/
  18.  
  19. fetchClasses();
  20.  
  21. /* Loading MySQL Settings*/
  22.  
  23. loadMySQLFiles();
  24.  
  25. /* Broadcast standard enabling message*/
  26.  
  27. }
  28.  
  29. public void onDisable() {
  30.  
  31. }
  32.  
  33. public void fetchClasses(){
  34.  
  35. mysql = new MySQL(this);
  36. filemanager = new FileManager(this);
  37. }
  38.  
  39. public void loadMySQLFiles(){
  40.  
  41. getDataFolder().mkdirs();
  42.  
  43. if(!filemanager.exists("mysql.yml", "Friends")){
  44.  
  45. File file =filemanager.createNewFile("mysql.yml", "Friends");
  46.  
  47. Configuration cfg = filemanager.getConfiguration("mysql.yml", "Friends");
  48.  
  49. cfg.set("user", "localuser");
  50. cfg.set("password", "localpasswort");
  51. cfg.set("host", "localhost");
  52. cfg.set("database", "localdatabse");
  53.  
  54. filemanager.saveFile(file, cfg);
  55.  
  56. }
  57. Configuration cfg = filemanager.getConfiguration("mysql.yml", "Friends");
  58.  
  59. getMySQL().host = cfg.getString("host");
  60. getMySQL().username = cfg.getString("user");
  61. getMySQL().database = cfg.getString("database");
  62. getMySQL().password = cfg.getString("password");
  63.  
  64. }
  65.  
  66. public String getPrefix() {
  67. return "§9Friends§8> §7";
  68. }
  69.  
  70. public MySQL getMySQL(){
  71. return mysql;
  72. }
  73.  
  74. public FileManager getFileManager(){
  75. return filemanager;
  76. }
  77. }
  78.  
  79. package net.KeinAnze1chen.friends.mysql;
  80.  
  81. import java.io.File;
  82. import java.io.IOException;
  83.  
  84. import net.KeinAnze1chen.friends.Friends;
  85. import net.md_5.bungee.config.Configuration;
  86. import net.md_5.bungee.config.ConfigurationProvider;
  87. import net.md_5.bungee.config.YamlConfiguration;
  88.  
  89. public class FileManager {
  90.  
  91. Friends friends;
  92.  
  93. public FileManager(Friends friends){
  94.  
  95. this.friends = friends;
  96.  
  97. }
  98.  
  99. public File createNewFile(String filename, String path){
  100. File f = new File("plugins/"+path, filename);
  101. if(!f.exists()){
  102. try {
  103. f.createNewFile();
  104. return f;
  105. } catch (IOException e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. return null;
  110. }
  111. public File getFile(String filename, String path){
  112. return new File("plugins/"+path, filename);
  113. }
  114. public boolean exists(String filename, String path){
  115. return getFile(filename, path).exists();
  116. }
  117. public void deleteFile(String filename, String path){
  118. File f = new File("plugins/"+path, filename);
  119. if(f.exists()){
  120. f.delete();
  121. }
  122. }
  123. public Configuration getConfiguration(String filename, String path){
  124. try {
  125. return ConfigurationProvider.getProvider(YamlConfiguration.class).load(getFile(filename, path));
  126. } catch (IOException e) {
  127. e.printStackTrace();
  128. }
  129. return null;
  130. }
  131. public void saveFile(File file, Configuration cfg){
  132. try {
  133. ConfigurationProvider.getProvider(YamlConfiguration.class).save(cfg, file);
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. }
  139. import java.sql.DriverManager;
  140. import java.sql.ResultSet;
  141. import java.sql.SQLException;
  142.  
  143. import net.md_5.bungee.api.ProxyServer;
  144. import net.md_5.bungee.api.chat.TextComponent;
  145.  
  146. public class MySQL<Friends> {
  147.  
  148. public String username;
  149. public String password;
  150. public String database;
  151. public String host;
  152. public String port;
  153. public Connection con;
  154.  
  155. Friends friends;
  156.  
  157. public MySQL(Friends friends) {
  158.  
  159. this.friends = friends;
  160.  
  161. }
  162.  
  163. public void connect() {
  164. if (!isConnected()) {
  165. try {
  166. con = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + database + "?autoReconnect=true",
  167. username, password);
  168. ProxyServer.getInstance().getConsole()
  169. .sendMessage(new TextComponent(((net.KeinAnze1chen.friends.Friends) friends).getPrefix() + "§aSuccessfully connected to MySQL-Database."));
  170. } catch (SQLException e) {
  171. ProxyServer.getInstance().getConsole()
  172. .sendMessage(new TextComponent(((net.KeinAnze1chen.friends.Friends) friends).getPrefix() + "§cCould not connect to MySQL-Database, please check your MySQL-Settings."));
  173. }
  174. }
  175. }
  176.  
  177. public void close() {
  178. if (isConnected()) {
  179. try {
  180. con.close();
  181. con = null;
  182. ProxyServer.getInstance().getConsole()
  183. .sendMessage(new TextComponent(((net.KeinAnze1chen.friends.Friends) friends).getPrefix() + "§aSuccessfully closed MySQL-Connection."));
  184. } catch (SQLException e) {
  185. e.printStackTrace();
  186. }
  187. }
  188. }
  189.  
  190. public boolean isConnected() {
  191. return con != null;
  192. }
  193.  
  194. public void update(String qry) {
  195. if (isConnected()) {
  196. try {
  197. con.createStatement().executeUpdate(qry);
  198. } catch (SQLException e) {
  199. e.printStackTrace();
  200. }
  201. }
  202. }
  203.  
  204. public ResultSet getResult(String qry) {
  205. if (isConnected()) {
  206. try {
  207. return con.createStatement().executeQuery(qry);
  208. } catch (SQLException e) {
  209. e.printStackTrace();
  210. }
  211. }
  212. return null;
  213. }
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement