· 6 years ago · Oct 05, 2019, 06:02 PM
1package ab.abderrahimlach;
2
3import java.io.File;
4
5
6import java.io.IOException;
7import java.sql.PreparedStatement;
8import java.sql.SQLException;
9
10import org.bukkit.Bukkit;
11import org.bukkit.plugin.Plugin;
12import org.bukkit.plugin.java.JavaPlugin;
13import org.bukkit.plugin.messaging.Messenger;
14
15import ab.abderrahimlach.commands.ReportCmd;
16import ab.abderrahimlach.connection.MySQLConnector;
17import ab.abderrahimlach.utils.GUIManager;
18
19public class Main extends JavaPlugin{
20
21 /*
22 * if(getConfig().getString("auth") == null) {
23 getConfig().set("auth.host", "localhost");
24 getConfig().set("auth.port", 3306);
25 getConfig().set("auth.username", "root");
26 getConfig().set("auth.password", "pass");
27 getConfig().set("auth.database", "database");
28
29 }
30 */
31
32
33
34
35 private MySQLConnector sql;
36
37 public void onEnable() {
38
39 // gets a Message from Bungee
40 registerAllListeners();
41 File file = new File(getDataFolder(), "/config.yml");
42 if(!file.exists()) {
43 try {
44 file.createNewFile();
45 } catch (IOException e) {
46
47
48 Bukkit.getConsoleSender().sendMessage("§cCould not create new config file.");
49 }
50
51 }
52 getConfig().options().copyDefaults(true);
53 saveDefaultConfig();
54
55 mySQLConnection();
56
57 }
58
59 public MySQLConnector getMySQLConnection() {
60
61 return sql;
62 }
63
64
65 public void mySQLConnection() {
66 String host = getConfig().getString("auth.host");
67 int port = getConfig().getInt("auth.port");
68 String username = getConfig().getString("auth.username");
69 String password = getConfig().getString("auth.password");
70 String database = getConfig().getString("auth.database");
71
72
73 sql = new MySQLConnector(host, port, username, password, database);
74 try {
75
76 sql.connect();
77 Bukkit.getConsoleSender().sendMessage("§aSuccessfully connected to the MySQL database.");
78 } catch (ClassNotFoundException e) {
79 Bukkit.getConsoleSender().sendMessage("§ccom.mysql.jdbc.Driver: not found.");
80 } catch (SQLException e) {
81 Bukkit.getConsoleSender().sendMessage("§cCould not connect to the MySQL database. Shutting the plugin down.");
82 Bukkit.getPluginManager().disablePlugin(this);
83 }
84
85
86 try {
87
88 String sqltable = "CREATE TABLE IF NOT EXISTS reports(ID int(37), ReportedPlayer varchar(37), IssuedBy varchar(37), ReportClicked varchar(37), Server varchar(37), Reason varchar(37));";
89 PreparedStatement stmt = sql.getConnection().prepareStatement(sqltable);
90 stmt.executeUpdate();
91 Bukkit.getConsoleSender().sendMessage("§aSuccessfully loaded table.");
92
93 } catch (SQLException e) {
94 Bukkit.getConsoleSender().sendMessage("§cCould not create a table in database. ("+database+")");
95
96 }
97
98 }
99 public void registerAllListeners() {
100
101 Messenger messenger = Bukkit.getServer().getMessenger();
102 messenger.registerOutgoingPluginChannel(this, "BungeeCord");
103 messenger.registerOutgoingPluginChannel(this, "AngelsDev");
104 ReportCmd rc = new ReportCmd();
105 getCommand("rr").setExecutor(rc);
106 getCommand("reportlist").setExecutor(rc);
107 getCommand("report").setExecutor(rc);
108 getServer().getPluginManager().registerEvents(new GUIManager(), (Plugin)this);
109 getServer().getPluginManager().registerEvents(rc, (Plugin)this);
110
111
112 }
113
114
115}