Advertisement
Guest User

Untitled

a guest
Dec 30th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. package me.reberion.chatcolor;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.UUID;
  9.  
  10. import org.bukkit.command.Command;
  11. import org.bukkit.command.CommandSender;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.Listener;
  14. import org.bukkit.plugin.java.JavaPlugin;
  15.  
  16. import net.md_5.bungee.api.ChatColor;
  17.  
  18. public class Main extends JavaPlugin implements Listener {
  19. public String table = "chatcolor_";
  20. public Connection connection;
  21. String name;
  22. String dname;
  23. String prefix = "&8[&6RBEU&8] &r";
  24. String color;
  25.  
  26. public void onEnable() {
  27. getServer().getPluginManager().registerEvents(this, this);
  28. connectSQL();
  29. }
  30.  
  31. public void connectSQL() {
  32. try {
  33. synchronized (this) {
  34. if ((getConnection() != null) && (!getConnection().isClosed())) {
  35. return;
  36. }
  37. Class.forName("com.mysql.jdbc.Driver");
  38. setConnection(DriverManager.getConnection("jdbc:mysql://137.74.234.6:3306/sqlstats", "mcsg",
  39. "RodBosssss"));
  40. }
  41. } catch (ClassNotFoundException e) {
  42. e.printStackTrace();
  43. System.err.println("jdbc driver unavailable!");
  44. return;
  45. } catch (SQLException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49.  
  50. public void onDisable() {
  51. try {
  52. if ((this.connection != null) && (!this.connection.isClosed())) {
  53. this.connection.close();
  54. }
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }
  59.  
  60. public Connection getConnection() {
  61. return this.connection;
  62. }
  63.  
  64. public void setConnection(Connection connection) {
  65. this.connection = connection;
  66. }
  67.  
  68. public void updatedata(final UUID uuid, final Player player, final String color) {
  69. getServer().getScheduler().runTaskAsynchronously(this, new Runnable() {
  70. public void run() {
  71. try {
  72. if (Main.this.connection == null) {
  73. Main.this.connectSQL();
  74. } else if (Main.this.connection.isClosed()) {
  75. Main.this.connectSQL();
  76. }
  77. } catch (SQLException e1) {
  78. e1.printStackTrace();
  79. }
  80. try {
  81. PreparedStatement statement = Main.this.getConnection()
  82. .prepareStatement("SELECT * FROM " + Main.this.table + " WHERE UUID='" + uuid + "'");
  83. ResultSet results = statement.executeQuery();
  84. if (!results.next()) {
  85. PreparedStatement ps = Main.this.connection
  86. .prepareStatement("INSERT INTO " + Main.this.table + "(UUID, NAME, COLOR) VALUES ('"
  87. + uuid + "', '" + player.getName() + "', '" + color + "');");
  88. ps.executeUpdate();
  89. } else {
  90. PreparedStatement st = Main.this.getConnection()
  91. .prepareStatement("UPDATE " + Main.this.table + " SET COLOR='" + color + "', NAME='"
  92. + player.getName() + "' WHERE UUID='" + player.getUniqueId() + "';");
  93. st.executeUpdate();
  94. }
  95. } catch (SQLException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. });
  100. }
  101.  
  102. public String getColor(Player player) {
  103. try {
  104. PreparedStatement statement = getConnection()
  105. .prepareStatement("SELECT * FROM " + this.table + " WHERE UUID='" + player.getUniqueId() + "';");
  106. ResultSet results = statement.executeQuery();
  107. if (results.next()) {
  108. if (results.getString("COLOR") == null) {
  109. this.color = "&f";
  110. } else {
  111. this.color = results.getString("COLOR");
  112. }
  113. }
  114. } catch (SQLException e) {
  115. e.printStackTrace();
  116. }
  117. return this.color;
  118. }
  119.  
  120. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  121. if (cmd.getName().equalsIgnoreCase("reconnectcc")) {
  122. connectSQL();
  123. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cMYSQL connected!"));
  124. }
  125. if ((sender instanceof Player)) {
  126. Player player = (Player) sender;
  127. if ((cmd.getName().equalsIgnoreCase("chatcolor")) && (player.hasPermission("chat.color"))) {
  128. if (args.length == 1) {
  129. if (args[0].equalsIgnoreCase("red")) {
  130. this.color = "&c";
  131. } else if (args[0].equalsIgnoreCase("yellow")) {
  132. this.color = "&e";
  133. } else if (args[0].equalsIgnoreCase("green")) {
  134. this.color = "&a";
  135. } else if (args[0].equalsIgnoreCase("gold")) {
  136. this.color = "&6";
  137. } else if (args[0].equalsIgnoreCase("gray")) {
  138. this.color = "&7";
  139. } else if (args[0].equalsIgnoreCase("blue")) {
  140. this.color = "&3";
  141. } else if (args[0].equalsIgnoreCase("cyan")) {
  142. this.color = "&b";
  143. } else if (args[0].equalsIgnoreCase("darkblue")) {
  144. this.color = "&9";
  145. } else if (args[0].equalsIgnoreCase("pink")) {
  146. this.color = "&d";
  147. } else if (args[0].equalsIgnoreCase("purple")) {
  148. this.color = "&5";
  149. } else if (args[0].equalsIgnoreCase("off")) {
  150. this.color = "&f";
  151. } else {
  152. player.sendMessage(ChatColor.translateAlternateColorCodes('&',
  153. this.prefix + "&eAvailable options: " + "&cred " + "&3blue " + "&eyellow " + "&agreen "
  154. + "&9darkblue " + "&bcyan " + "&7gray " + "&dpink " + "&5purple " + "&6gold "
  155. + "&foff"));
  156. return true;
  157. }
  158. updatedata(player.getUniqueId(), player, this.color);
  159. player.sendMessage(ChatColor.translateAlternateColorCodes('&',
  160. this.prefix + "Your chat color has been updated!"));
  161. } else {
  162. player.sendMessage(ChatColor.translateAlternateColorCodes('&',
  163. this.prefix + "&eCorrect Usage&8: &6/chatcolor <color>"));
  164. }
  165. }
  166. }
  167. return false;
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement