Advertisement
Guest User

Untitled

a guest
Feb 4th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. package com.lupe.basicland.tokeny;
  2.  
  3. import java.io.File;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.util.concurrent.TimeUnit;
  11.  
  12. import org.bukkit.Bukkit;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.event.EventHandler;
  15. import org.bukkit.event.Listener;
  16. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
  17. import org.bukkit.plugin.Plugin;
  18. import org.bukkit.plugin.java.JavaPlugin;
  19.  
  20.  
  21.  
  22.  
  23. public class main extends JavaPlugin implements Listener{
  24.  
  25. public static String username;
  26. public static String password;
  27. public static int port;
  28. public static String url;
  29. public static String table;
  30. public static String database;
  31. public static String host;
  32. public static String notenoughtokens;
  33. static Connection connection;
  34. public static int playerbalance;
  35. public void onEnable() {
  36. String root = this.getDataFolder().getAbsolutePath();
  37. File file =new File(root+"/config.yml");
  38. if(file.exists()){
  39.  
  40. host = this.getConfig().getString("options.host");
  41. notenoughtokens = this.getConfig().getString("messages.not_enough_tokens");
  42. notenoughtokens = notenoughtokens.replace("&", "ยง");
  43. System.out.println("Host nalezen!");
  44. port = this.getConfig().getInt("options.port");
  45. System.out.println("port nalezen!");
  46. password = this.getConfig().getString("options.password");
  47. System.out.println("heslo nalezeno!");
  48. username = this.getConfig().getString("options.username");
  49. System.out.println("uzivatel nalezen!");
  50. database = this.getConfig().getString("options.database");
  51. System.out.println("databaze nalezena!");
  52. table = this.getConfig().getString("options.table");
  53. System.out.println("tabulka nalezena!");
  54. url = ("jdbc:mysql://"+host+":"+port);
  55. getServer().getPluginManager().registerEvents((Listener)this, (Plugin)this);
  56.  
  57. try {
  58. Class.forName("com.mysql.jdbc.Driver");
  59. } catch (ClassNotFoundException e) {
  60. e.printStackTrace();
  61. System.err.println("Nebyl nalezen driver pro mysql.");
  62. }
  63. try {
  64. connection = DriverManager.getConnection(url,username,password);
  65. System.out.println("Uspesne napojeno na databazi!");
  66. } catch (SQLException e) {
  67. e.printStackTrace();
  68. }
  69. }else{
  70. System.out.println("Creating new config.yml...");
  71. this.saveDefaultConfig();
  72. }
  73. }
  74.  
  75. @EventHandler
  76. public void checkoncommand(PlayerCommandPreprocessEvent event) throws InterruptedException {
  77. String command = event.getMessage();
  78. command = command.substring(1);
  79. command = command.replace(" ", "_");
  80. if (this.getConfig().getInt(command+".price") != 0) {
  81. String commandtoexecute = this.getConfig().getString(command+".run_command");
  82. String player = ""+event.getPlayer().getName();
  83. commandtoexecute = commandtoexecute.replaceAll("%player%", player);
  84. event.setCancelled(true);
  85. String price = ""+this.getConfig().getInt(command+".price");
  86. String UUID = event.getPlayer().getUniqueId().toString();
  87. url = ("jdbc:mysql://"+host+":"+port);
  88. int pricefinal = Integer.parseInt(price);
  89. if (!price.equals("0")) {
  90. try {
  91. Class.forName("com.mysql.jdbc.Driver");
  92. } catch (ClassNotFoundException e) {
  93. e.printStackTrace();
  94. System.err.println("Nebyl nalezen driver pro mysql.");
  95. }
  96. try {
  97. connection = DriverManager.getConnection(url,username,password);
  98. } catch (SQLException e) {
  99. e.printStackTrace();
  100. }
  101. String sql = "select balance from "+database+"."+table+" where account_id='"+UUID+"'";
  102. try {
  103. PreparedStatement stmt = connection.prepareStatement(sql);
  104. ResultSet results = stmt.executeQuery(sql);
  105. results.next();
  106. playerbalance = results.getInt(1);
  107.  
  108. } catch (SQLException e) {
  109. e.printStackTrace();
  110. };
  111. if (playerbalance >= pricefinal) {
  112. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), commandtoexecute);
  113. TimeUnit.MILLISECONDS.sleep(20);
  114. String sqll = "update "+database+"."+table+" set balance="+(playerbalance-pricefinal)+" where account_id='"+UUID+"'";
  115. try {
  116. PreparedStatement stmt = connection.prepareStatement(sqll);
  117. stmt.executeUpdate(sqll);
  118.  
  119. } catch (SQLException e) {
  120. e.printStackTrace();
  121. }
  122. }
  123. else {
  124. event.getPlayer().sendMessage(notenoughtokens);
  125. }
  126.  
  127. }
  128.  
  129. }
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement