· 9 years ago · Dec 14, 2016, 05:42 AM
1package com.mcsaga.plugins;
2
3import me.evilmidget38.NameFetcher;
4import me.evilmidget38.UUIDFetcher;
5import org.bukkit.Bukkit;
6import org.bukkit.ChatColor;
7import org.bukkit.command.Command;
8import org.bukkit.command.CommandSender;
9import org.bukkit.configuration.file.FileConfiguration;
10import org.bukkit.configuration.file.YamlConfiguration;
11import org.bukkit.event.Listener;
12import org.bukkit.plugin.java.JavaPlugin;
13
14import com.mysql.jdbc.Connection;
15import com.mysql.jdbc.PreparedStatement;
16
17import java.io.File;
18import java.sql.DriverManager;
19import java.sql.SQLException;
20import java.util.Arrays;
21import java.util.UUID;
22
23public class Main extends JavaPlugin implements Listener {
24 static FileConfiguration config;
25 File cfile;
26 final String username = config.getString("mysql.username"); //Enter in your db username
27 final String password = config.getString("mysql.password"); //Enter your password for the db
28 final String url = "jdbc:mysql://" + config.getString("mysql.host") + ":" + config.getString("mysql.port") + "/" + config.getString("mysql.database");
29 final String createTable = "CREATE TABLE IF NOT EXISTS Status(Something varchar(64));";
30 public static Connection connection;
31
32 @Override
33 public void onEnable() {
34 config = getConfig();
35 config.options().copyDefaults(true);
36 saveConfig();
37 cfile = new File(getDataFolder(), "config.yml");
38 try { //We use a try catch to avoid errors, hopefully we don't get any.
39 Class.forName("com.mysql.jdbc.Driver"); //this accesses Driver in jdbc.
40 } catch (ClassNotFoundException e) {
41 e.printStackTrace();
42 System.err.println("jdbc driver unavailable!");
43 return;
44 }
45 try { //Another try catch to get any SQL errors (for example connections errors)
46 connection = (Connection) DriverManager.getConnection(url,username,password);
47 //with the method getConnection() from DriverManager, we're trying to set
48 //the connection's url, username, password to the variables we made earlier and
49 //trying to get a connection at the same time. JDBC allows us to do this.
50 PreparedStatement table = (PreparedStatement) connection.prepareStatement(createTable);
51 table.executeUpdate();
52 table.close();
53 } catch (SQLException e) { //catching errors)
54 e.printStackTrace(); //prints out SQLException errors to the console (if any)
55 }
56
57 // prepare the statement to be executed
58
59 Bukkit.getServer().getPluginManager().registerEvents(this, this);
60
61 }
62
63 public void onDisable(){
64 try {
65 if(connection == null || connection.isClosed()){
66 connection.close();
67 }
68 } catch (Exception e) {
69 e.printStackTrace();
70 }
71 }
72
73 @Override
74 public boolean onCommand(final CommandSender sender, Command cmd, String cmdLabel, String[] args) {
75 String prefix = ChatColor.translateAlternateColorCodes('&', config.getString("options.config"));
76 final String name, uuid;
77
78 if (args[0].matches("[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}")) {
79 // They are typing in a UUID.
80 try {
81 name = new NameFetcher(Arrays.asList(UUID.fromString(args[0]))).call().get(UUID.fromString(args[0]));
82 }
83
84 catch (Exception e) {
85 e.printStackTrace();
86 return true;
87 }
88
89 uuid = args[0];
90 }
91
92 else {
93 // They are typing in a name.
94 name = args[0];
95
96 try {
97 uuid = new UUIDFetcher(Arrays.asList(args[0])).call().get(args[0]).toString();
98 }
99
100 catch (Exception e) {
101 e.printStackTrace();
102 return true;
103 }
104 }
105
106 if (cmdLabel.equalsIgnoreCase("ban")) {
107 if(sender.hasPermission("xen.ban")){
108
109 }else{
110 sender.sendMessage(prefix + ChatColor.RED + "You do not have enough permissions");
111 }
112 }
113
114 else if (cmdLabel.equalsIgnoreCase("unban")) {
115 sender.sendMessage("test");
116 }
117
118 else if (cmdLabel.equalsIgnoreCase("banreload") || cmdLabel.equalsIgnoreCase("breload")){
119 config = YamlConfiguration.loadConfiguration(cfile);
120 sender.sendMessage(prefix + ChatColor.GREEN + "The Config Has Been Reloaded");
121 }
122
123 return true;
124 }
125
126}