Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. package me.xxx;
  2.  
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.UUID;
  7.  
  8. import org.bukkit.entity.Player;
  9.  
  10. public class L2 {
  11. DiscordBot plugin = DiscordBot.getPlugin(DiscordBot.class);
  12.  
  13. public boolean playerExists(UUID uuid){
  14.  
  15. try {
  16. PreparedStatement statement = plugin.getConnection().prepareStatement("SELECT * FROM " + plugin.table + "WHERE UUID =?");
  17. statement.setString(1, uuid.toString());
  18.  
  19. ResultSet results = statement.executeQuery();
  20. if(results.next()) {
  21.  
  22. }
  23.  
  24. } catch (SQLException e) {
  25. e.printStackTrace();
  26. }
  27. return false;
  28. }
  29. public void createPlayer(final UUID uuid, Player player) {
  30. try {
  31. PreparedStatement statement = plugin.getConnection().prepareStatement("SELECT * FROM " + plugin.table + "WHERE UUID =?");
  32. statement.setString(1, uuid.toString());
  33. statement.setString(1, uuid.toString());
  34. ResultSet results = statement.executeQuery();
  35. results.next();
  36. if(!(playerExists(uuid) != true)) {
  37. PreparedStatement insert = plugin.getConnection().prepareStatement("INSERT INTO " + plugin.table + "(UUID, NAME, RANK) VALUE (?,?,?)");
  38. insert.setString(1, uuid.toString());
  39. insert.setString(2, player.getName());
  40. insert.setString(3, player.getPrimaryGroup(player));
  41. }
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. Main class below. Second class above.
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. package me.xxx;
  68.  
  69.  
  70. import java.sql.Connection;
  71. import java.sql.DriverManager;
  72. import java.sql.SQLException;
  73.  
  74. import org.bukkit.Bukkit;
  75. import org.bukkit.ChatColor;
  76. import org.bukkit.command.Command;
  77. import org.bukkit.command.CommandSender;
  78. import org.bukkit.entity.Player;
  79. import org.bukkit.plugin.RegisteredServiceProvider;
  80. import org.bukkit.plugin.java.JavaPlugin;
  81.  
  82. import net.milkbowl.vault.permission.Permission;
  83.  
  84.  
  85.  
  86. public class DiscordBot extends JavaPlugin {
  87. public static Permission permission = null;
  88. private Connection connection;
  89. public String host, database, username, password, table;
  90. public int port;
  91. public Connection getConnection() {
  92. return connection;
  93. }
  94.  
  95. public void onEnable() {
  96. registerConfig();
  97. mysqlSetup();
  98.  
  99.  
  100. }
  101.  
  102.  
  103.  
  104. private void registerConfig() {
  105. getConfig().options().copyDefaults(true);
  106. saveConfig();
  107. }
  108. public void setConnection(Connection connection) {
  109. this.connection = connection;
  110. }
  111. public void mysqlSetup() {
  112. host = getConfig().getString("Dbhost");
  113. database = getConfig().getString("Db");
  114. username = getConfig().getString("Dbname");
  115. password = getConfig().getString("Dbpass");
  116. port = getConfig().getInt("Dbport");
  117. table = getConfig().getString("Table");
  118.  
  119. try{
  120.  
  121. synchronized (this){
  122. if(getConnection() != null && !getConnection().isClosed()){
  123. return;
  124. }
  125.  
  126. Class.forName("com.mysql.jdbc.Driver");
  127.  
  128. setConnection( DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password));
  129.  
  130. Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "MYSQL CONNECTED");
  131. }
  132. }catch(SQLException e){
  133. e.printStackTrace();
  134. }catch(ClassNotFoundException e){
  135. e.printStackTrace();
  136. }
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143. public void onDisable() {
  144.  
  145. saveConfig();
  146.  
  147.  
  148. }
  149.  
  150. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  151.  
  152.  
  153. if (cmd.getName().equalsIgnoreCase("rank") && sender instanceof Player) {
  154.  
  155. Player player = (Player) sender;
  156.  
  157.  
  158.  
  159. }
  160.  
  161. return false;
  162. }
  163. private boolean setupPermissions()
  164. {
  165. RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
  166. if (permissionProvider != null) {
  167. permission = permissionProvider.getProvider();
  168. }
  169. return (permission != null);
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement