Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mcsaga.plugins;
- import me.evilmidget38.NameFetcher;
- import me.evilmidget38.UUIDFetcher;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.event.Listener;
- import org.bukkit.plugin.java.JavaPlugin;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.PreparedStatement;
- import java.io.File;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Arrays;
- import java.util.UUID;
- public class Main extends JavaPlugin implements Listener {
- static FileConfiguration config;
- File cfile;
- final String username = config.getString("mysql.username"); //Enter in your db username
- final String password = config.getString("mysql.password"); //Enter your password for the db
- final String url = "jdbc:mysql://" + config.getString("mysql.host") + ":" + config.getString("mysql.port") + "/" + config.getString("mysql.database");
- final String createTable = "CREATE TABLE IF NOT EXISTS Status(Something varchar(64));";
- public static Connection connection;
- @Override
- public void onEnable() {
- config = getConfig();
- config.options().copyDefaults(true);
- saveConfig();
- cfile = new File(getDataFolder(), "config.yml");
- try { //We use a try catch to avoid errors, hopefully we don't get any.
- Class.forName("com.mysql.jdbc.Driver"); //this accesses Driver in jdbc.
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.err.println("jdbc driver unavailable!");
- return;
- }
- try { //Another try catch to get any SQL errors (for example connections errors)
- connection = (Connection) DriverManager.getConnection(url,username,password);
- //with the method getConnection() from DriverManager, we're trying to set
- //the connection's url, username, password to the variables we made earlier and
- //trying to get a connection at the same time. JDBC allows us to do this.
- PreparedStatement table = (PreparedStatement) connection.prepareStatement(createTable);
- table.executeUpdate();
- table.close();
- } catch (SQLException e) { //catching errors)
- e.printStackTrace(); //prints out SQLException errors to the console (if any)
- }
- // prepare the statement to be executed
- Bukkit.getServer().getPluginManager().registerEvents(this, this);
- }
- public void onDisable(){
- try {
- if(connection == null || connection.isClosed()){
- connection.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public boolean onCommand(final CommandSender sender, Command cmd, String cmdLabel, String[] args) {
- String prefix = ChatColor.translateAlternateColorCodes('&', config.getString("options.config"));
- final String name, uuid;
- 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}")) {
- // They are typing in a UUID.
- try {
- name = new NameFetcher(Arrays.asList(UUID.fromString(args[0]))).call().get(UUID.fromString(args[0]));
- }
- catch (Exception e) {
- e.printStackTrace();
- return true;
- }
- uuid = args[0];
- }
- else {
- // They are typing in a name.
- name = args[0];
- try {
- uuid = new UUIDFetcher(Arrays.asList(args[0])).call().get(args[0]).toString();
- }
- catch (Exception e) {
- e.printStackTrace();
- return true;
- }
- }
- if (cmdLabel.equalsIgnoreCase("ban")) {
- if(sender.hasPermission("xen.ban")){
- }else{
- sender.sendMessage(prefix + ChatColor.RED + "You do not have enough permissions");
- }
- }
- else if (cmdLabel.equalsIgnoreCase("unban")) {
- sender.sendMessage("test");
- }
- else if (cmdLabel.equalsIgnoreCase("banreload") || cmdLabel.equalsIgnoreCase("breload")){
- config = YamlConfiguration.loadConfiguration(cfile);
- sender.sendMessage(prefix + ChatColor.GREEN + "The Config Has Been Reloaded");
- }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment