Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.reberion.chatcolor;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.UUID;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.event.Listener;
- import org.bukkit.plugin.java.JavaPlugin;
- import net.md_5.bungee.api.ChatColor;
- public class Main extends JavaPlugin implements Listener {
- public String table = "chatcolor_";
- public Connection connection;
- String name;
- String dname;
- String prefix = "&8[&6RBEU&8] &r";
- String color;
- public void onEnable() {
- getServer().getPluginManager().registerEvents(this, this);
- connectSQL();
- }
- public void connectSQL() {
- try {
- synchronized (this) {
- if ((getConnection() != null) && (!getConnection().isClosed())) {
- return;
- }
- Class.forName("com.mysql.jdbc.Driver");
- setConnection(DriverManager.getConnection("jdbc:mysql://137.74.234.6:3306/sqlstats", "mcsg",
- "RodBosssss"));
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.err.println("jdbc driver unavailable!");
- return;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void onDisable() {
- try {
- if ((this.connection != null) && (!this.connection.isClosed())) {
- this.connection.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public Connection getConnection() {
- return this.connection;
- }
- public void setConnection(Connection connection) {
- this.connection = connection;
- }
- public void updatedata(final UUID uuid, final Player player, final String color) {
- getServer().getScheduler().runTaskAsynchronously(this, new Runnable() {
- public void run() {
- try {
- if (Main.this.connection == null) {
- Main.this.connectSQL();
- } else if (Main.this.connection.isClosed()) {
- Main.this.connectSQL();
- }
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- PreparedStatement statement = Main.this.getConnection()
- .prepareStatement("SELECT * FROM " + Main.this.table + " WHERE UUID='" + uuid + "'");
- ResultSet results = statement.executeQuery();
- if (!results.next()) {
- PreparedStatement ps = Main.this.connection
- .prepareStatement("INSERT INTO " + Main.this.table + "(UUID, NAME, COLOR) VALUES ('"
- + uuid + "', '" + player.getName() + "', '" + color + "');");
- ps.executeUpdate();
- } else {
- PreparedStatement st = Main.this.getConnection()
- .prepareStatement("UPDATE " + Main.this.table + " SET COLOR='" + color + "', NAME='"
- + player.getName() + "' WHERE UUID='" + player.getUniqueId() + "';");
- st.executeUpdate();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- });
- }
- public String getColor(Player player) {
- try {
- PreparedStatement statement = getConnection()
- .prepareStatement("SELECT * FROM " + this.table + " WHERE UUID='" + player.getUniqueId() + "';");
- ResultSet results = statement.executeQuery();
- if (results.next()) {
- if (results.getString("COLOR") == null) {
- this.color = "&f";
- } else {
- this.color = results.getString("COLOR");
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return this.color;
- }
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
- if (cmd.getName().equalsIgnoreCase("reconnectcc")) {
- connectSQL();
- sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cMYSQL connected!"));
- }
- if ((sender instanceof Player)) {
- Player player = (Player) sender;
- if ((cmd.getName().equalsIgnoreCase("chatcolor")) && (player.hasPermission("chat.color"))) {
- if (args.length == 1) {
- if (args[0].equalsIgnoreCase("red")) {
- this.color = "&c";
- } else if (args[0].equalsIgnoreCase("yellow")) {
- this.color = "&e";
- } else if (args[0].equalsIgnoreCase("green")) {
- this.color = "&a";
- } else if (args[0].equalsIgnoreCase("gold")) {
- this.color = "&6";
- } else if (args[0].equalsIgnoreCase("gray")) {
- this.color = "&7";
- } else if (args[0].equalsIgnoreCase("blue")) {
- this.color = "&3";
- } else if (args[0].equalsIgnoreCase("cyan")) {
- this.color = "&b";
- } else if (args[0].equalsIgnoreCase("darkblue")) {
- this.color = "&9";
- } else if (args[0].equalsIgnoreCase("pink")) {
- this.color = "&d";
- } else if (args[0].equalsIgnoreCase("purple")) {
- this.color = "&5";
- } else if (args[0].equalsIgnoreCase("off")) {
- this.color = "&f";
- } else {
- player.sendMessage(ChatColor.translateAlternateColorCodes('&',
- this.prefix + "&eAvailable options: " + "&cred " + "&3blue " + "&eyellow " + "&agreen "
- + "&9darkblue " + "&bcyan " + "&7gray " + "&dpink " + "&5purple " + "&6gold "
- + "&foff"));
- return true;
- }
- updatedata(player.getUniqueId(), player, this.color);
- player.sendMessage(ChatColor.translateAlternateColorCodes('&',
- this.prefix + "Your chat color has been updated!"));
- } else {
- player.sendMessage(ChatColor.translateAlternateColorCodes('&',
- this.prefix + "&eCorrect Usage&8: &6/chatcolor <color>"));
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement