· 8 years ago · May 14, 2018, 11:58 PM
1package me.xikeon.OpenAccount;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.SQLException;
6import java.sql.Statement;
7import java.util.logging.Logger;
8
9import org.bukkit.configuration.file.FileConfiguration;
10import org.bukkit.event.Event;
11import org.bukkit.plugin.PluginManager;
12import org.bukkit.plugin.java.JavaPlugin;
13
14public class OpenAccount extends JavaPlugin {
15 Logger log = Logger.getLogger("Minecraft");
16 PluginManager pm = null;
17 private final OpenAccountPlayerListener playerListener = new OpenAccountPlayerListener(this);
18 protected FileConfiguration config;
19
20 boolean isEnabled;
21
22 Connection conn = null;
23
24 private OpenAccountCommandExecutorOpencraft ExecutorOpencraft;
25 private OpenAccountCommandExecutorOcpassword ExecutorOcpassword;
26 @Override
27 public void onEnable() {
28 log.info("OpenAccount has been enabled!");
29
30 pm = getServer().getPluginManager();
31 config = getConfig();
32 if (!config.contains("enabled")) {
33 config.set("enabled", false);
34 config.set("mysql.host", "localhost");
35 config.set("mysql.database", "default");
36 config.set("mysql.username", "root");
37 config.set("mysql.password", "pwd");
38 saveConfig();
39 }
40
41 boolean isEnabled = config.getBoolean("enabled", false);
42
43 if (isEnabled) {
44 init();
45 } else {
46 log.info("OpenAccount commands not set, config has disabled plugin.");
47 }
48 }
49
50 public void onDisable() {
51 //saveConfig();
52 // save causes changes to be deleted when doing reload..
53 if (conn != null)
54 {
55 try
56 {
57 conn.close ();
58 }
59 catch (Exception e) { }
60 }
61 log.info("OpenAccount has been disabled.");
62 }
63
64 public void init() {
65 initMySQL();
66 initEvents();
67 initcommands();
68 }
69
70 public void initEvents() {
71 pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Event.Priority.Normal, this);
72 }
73
74 public void initcommands() {
75 ExecutorOpencraft = new OpenAccountCommandExecutorOpencraft(this);
76 ExecutorOcpassword = new OpenAccountCommandExecutorOcpassword(this);
77 getCommand("opencraft").setExecutor(ExecutorOpencraft);
78 getCommand("ocpassword").setExecutor(ExecutorOcpassword);
79 }
80
81 public void initMySQL() {
82 try {
83 Class.forName("com.mysql.jdbc.Driver").newInstance();
84 conn = DriverManager.getConnection(
85 "jdbc:mysql://" + config.getString("mysql.host") + "/" + config.getString("mysql.database"),
86 config.getString("mysql.username"),
87 config.getString("mysql.password", "")
88 );
89 } catch (Exception e) {
90 log.info("MySQL failed!");
91 }
92
93 if (conn != null) {
94 Statement s = null;
95 try {
96 s = conn.createStatement();
97 s.executeUpdate(
98 "CREATE TABLE IF NOT EXISTS users ("
99 + "username CHAR(40) NOT NULL,"
100 + "PRIMARY KEY (username),"
101 + "password CHAR(100))"
102 );
103 } catch (SQLException e) {
104 log.info(e.getMessage().toString());
105 log.info("Error creating table...");
106 } finally {
107 if (s != null) {
108 try
109 {
110 s.close();
111 } catch (Exception e) { }
112 }
113 }
114 }
115 }
116}