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