Advertisement
Guest User

MySQL

a guest
Jun 26th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.40 KB | None | 0 0
  1. public static Connection con;
  2.  
  3. public static String ip = "185.230.161.168";
  4. public static String port = "3306";
  5. public static String dbname = "Smash";
  6. public static String user = "Smash";
  7. public static String password = "hdgdl#1234";
  8. public static String cointable = "Coins";
  9. public static String statstable = "Stats";
  10.  
  11. public static HashMap<Integer, String> rang = new HashMap<Integer, String>();
  12.  
  13. public static void connect(){
  14. if(!isConnected()){
  15. try {
  16. con = DriverManager.getConnection("jdbc:mysql://" + ip +":" + port +"/" + dbname + "?autoReconnect=true", user, password);
  17. if(isConnected()){
  18. Bukkit.getConsoleSender().sendMessage("§aMySQL Verbindung hergestellt.");
  19. createTable();
  20. }
  21. } catch (SQLException e) {
  22. // TODO Auto-generated catch block
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27.  
  28. public static boolean isConnected(){
  29. return (con != null);
  30. }
  31.  
  32. public static void disconnect() {
  33. if(isConnected()){
  34. try {
  35. con.close();
  36. Bukkit.getConsoleSender().sendMessage("§cDie MySQL Verbindung wurde getrennt!");
  37. } catch (SQLException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43.  
  44. public static void createTable(){
  45. try {
  46. PreparedStatement ps = con.prepareStatement("CREATE TABLE IF NOT EXISTS Coins (UUID VARCHAR(64), Playername VARCHAR(32), Coins int(15));");
  47. ps.executeUpdate();
  48. } catch (SQLException e) {
  49. e.printStackTrace();
  50. }
  51.  
  52. try {
  53. PreparedStatement ps = con.prepareStatement("CREATE TABLE IF NOT EXISTS Stats (UUID VARCHAR(64),Playername VARCHAR(32), Kills int(11), Deaths int(11), Rounds int(11));");
  54. ps.executeUpdate();
  55. } catch (SQLException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59.  
  60. public static boolean UserExists(String UUID){
  61. try {
  62. PreparedStatement ps = MySQL.con.prepareStatement("SELECT Coins FROM " + cointable + " WHERE UUID = ?");
  63. ps.setString(1, UUID);
  64. ResultSet rs = ps.executeQuery();
  65. return rs.next();
  66. } catch (SQLException e) {
  67. e.printStackTrace();
  68. }
  69. return false;
  70. }
  71.  
  72. public static boolean UUIDExists(String UUID){
  73. try {
  74. PreparedStatement ps = MySQL.con.prepareStatement("SELECT UUID FROM " + statstable + " WHERE UUID = ?");
  75. ps.setString(1, UUID);
  76. ResultSet rs = ps.executeQuery();
  77. return rs.next();
  78. } catch (SQLException e) {
  79. e.printStackTrace();
  80. }
  81. return false;
  82. }
  83.  
  84. public static void createUser(String uuid, Player p){
  85. try {
  86. PreparedStatement ps = MySQL.con.prepareStatement("Insert INTO " + statstable + " (UUID,Playername,Kills,Deaths,Rounds) VALUES (?,?,?,?,?)");
  87. ps.setString(1, uuid);
  88. ps.setString(2, p.getName());
  89. ps.setInt(3, 0);
  90. ps.setInt(4, 0);
  91. ps.setInt(5, 0);
  92. ps.executeUpdate();
  93. } catch (SQLException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97.  
  98.  
  99. public static void addTode(String UUID, int amount, String playername){
  100. int tode = getTode(UUID);
  101.  
  102. amount+=tode;
  103. if(UUIDExists(UUID)){
  104. try {
  105. PreparedStatement ps = MySQL.con.prepareStatement("UPDATE " + statstable + " SET Deaths = ? WHERE UUID = ?");
  106. ps.setInt(1, amount);
  107. ps.setString(2, UUID);
  108. ps.executeUpdate();
  109. } catch (SQLException e) {
  110. e.printStackTrace();
  111. }
  112. }
  113. }
  114. public static void addKIlls(String UUID, int amount, String playername){
  115. int kills = getKills(UUID);
  116.  
  117. amount+=kills;
  118. if(UUIDExists(UUID)){
  119. try {
  120. PreparedStatement ps = MySQL.con.prepareStatement("UPDATE " + statstable + " SET Kills = ? WHERE UUID = ?");
  121. ps.setInt(1, amount);
  122. ps.setString(2, UUID);
  123. ps.executeUpdate();
  124. } catch (SQLException e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. }
  129. public static void removeTode(String uuid, int amount,String playername){
  130. int tode = getTode(uuid);
  131. if(UUIDExists(uuid)){
  132. try {
  133. PreparedStatement ps = MySQL.con.prepareStatement("UPDATE " + statstable + " SET Deaths = ? WHERE UUID = ?");
  134. ps.setInt(1, tode-=amount);
  135. ps.setString(2, uuid);
  136. ps.executeUpdate();
  137. } catch (SQLException e) {
  138. e.printStackTrace();
  139. }
  140. }
  141. }
  142. public static void removeKills(String uuid, int amount,String playername){
  143. int kills = getKills(uuid);
  144. if(UUIDExists(uuid)){
  145. try {
  146. PreparedStatement ps = MySQL.con.prepareStatement("UPDATE " + statstable + " SET Kills = ? WHERE UUID = ?");
  147. ps.setInt(1, kills-=amount);
  148. ps.setString(2, uuid);
  149. ps.executeUpdate();
  150. } catch (SQLException e) {
  151. e.printStackTrace();
  152. }
  153. }
  154. }
  155. public static Integer getTode(String uuid){
  156. try {
  157. PreparedStatement ps = MySQL.con.prepareStatement("Select Deaths From " + statstable + " Where UUID = ?");
  158. ps.setString(1, uuid);
  159. ResultSet rs = ps.executeQuery();
  160. while(rs.next()){
  161. return rs.getInt("Deaths");
  162. }
  163. } catch (SQLException e) {
  164. e.printStackTrace();
  165. }
  166. return 0;
  167. }
  168. public static Integer getKills(String uuid){
  169. int it = 0;
  170. if(UUIDExists(uuid)){
  171. try {
  172. PreparedStatement ps = MySQL.con.prepareStatement("Select * From " + statstable + " Where UUID = ?");
  173. ps.setString(1, uuid);
  174. ResultSet rs = ps.executeQuery();
  175.  
  176. if(rs.next()){
  177. it = rs.getInt("Kills");
  178. }
  179. } catch (SQLException e) {
  180. e.printStackTrace();
  181. }
  182. return it;
  183. }
  184. return 0;
  185. }
  186. public static String getPlayername(String UUID){
  187. try {
  188. PreparedStatement ps = MySQL.con.prepareStatement("Select Playername From " + statstable + " Where UUID = ?");
  189. ps.setString(1, UUID);
  190. ResultSet rs = ps.executeQuery();
  191. while(rs.next()){
  192. return rs.getString("Playername");
  193. }
  194. } catch (SQLException e) {
  195. e.printStackTrace();
  196. }
  197. return null;
  198. }
  199.  
  200. public static Integer getCoins(String uuid){
  201. try {
  202. PreparedStatement ps = MySQL.con.prepareStatement("Select Coins From " + cointable + " Where UUID = ?");
  203. ps.setString(1, uuid);
  204. ResultSet rs = ps.executeQuery();
  205. while(rs.next()){
  206. return rs.getInt("Coins");
  207. }
  208. } catch (SQLException e) {
  209. e.printStackTrace();
  210. }
  211. return 0;
  212. }
  213.  
  214. public static void addCoins(String uuid, int amount, Player p ){
  215. int coins = getCoins(uuid);
  216.  
  217. amount+=coins;
  218. if(UserExists(uuid)){
  219. try {
  220. PreparedStatement ps = MySQL.con.prepareStatement("UPDATE " + cointable + " SET Coins = ? WHERE UUID = ?");
  221. ps.setInt(1, amount);
  222. ps.setString(2, uuid);
  223. ps.executeUpdate();
  224. } catch (SQLException e) {
  225. e.printStackTrace();
  226. }
  227. }else{
  228. try {
  229. PreparedStatement ps = MySQL.con.prepareStatement("Insert INTO " + cointable + " (UUID,Playername,Coins) VALUES (?,?,?)");
  230. ps.setString(1, uuid);
  231. ps.setString(2, p.getName());
  232. ps.setInt(3, amount);
  233. ps.executeUpdate();
  234. } catch (SQLException e) {
  235. e.printStackTrace();
  236. }
  237. }
  238. }
  239.  
  240. public static void removeCoins(String uuid, int amount,String playername){
  241. int coins = getCoins(uuid);
  242. if(UserExists(uuid)){
  243. try {
  244. PreparedStatement ps = MySQL.con.prepareStatement("UPDATE " + cointable + " SET Coins = ? WHERE UUID = ?");
  245. ps.setInt(1, coins-=amount);
  246. ps.setString(2, uuid);
  247. ps.executeUpdate();
  248. } catch (SQLException e) {
  249. e.printStackTrace();
  250. }
  251. }else{
  252. try {
  253. PreparedStatement ps = MySQL.con.prepareStatement("Insert INTO " + cointable + " (UUID,Coins) VALUES (?,?)");
  254. ps.setString(1, uuid);
  255. ps.setInt(2, amount);
  256. ps.executeUpdate();
  257. } catch (SQLException e) {
  258. e.printStackTrace();
  259. }
  260. }
  261. }
  262.  
  263.  
  264.  
  265. public static void set(){
  266. try {
  267. PreparedStatement ps = MySQL.con.prepareStatement("SELECT UUID FROM " + statstable + " ORDER BY KILLS DESC LIMIT 10");
  268. ResultSet rs = ps.executeQuery();
  269.  
  270. int i = 0;
  271.  
  272. while(rs.next()){
  273. i++;
  274. rang.put(i, rs.getString("UUID"));
  275. }
  276. } catch (SQLException e) {
  277. e.printStackTrace();
  278. }
  279. if(InteractListener.cfg.getString("Kopf-1") != null && InteractListener.cfg.getString("Kopf-2") != null && InteractListener.cfg.getString("Kopf-3") != null && InteractListener.cfg.getString("Kopf-4") != null && InteractListener.cfg.getString("Kopf-5") != null && InteractListener.cfg.getString("Kopf-6") != null && InteractListener.cfg.getString("Kopf-7") != null && InteractListener.cfg.getString("Kopf-8") != null && InteractListener.cfg.getString("Kopf-9") != null && InteractListener.cfg.getString("Kopf-10") != null){
  280. Location loc = new Location(Bukkit.getWorld(InteractListener.cfg.getString("Kopf-1" + ".World")), InteractListener.cfg.getInt("Kopf-1" + ".X"), InteractListener.cfg.getInt("Kopf-1" + ".Y"), InteractListener.cfg.getInt("Kopf-1" + ".Z"));
  281. Location loc2 = new Location(Bukkit.getWorld(InteractListener.cfg.getString("Kopf-2" + ".World")), InteractListener.cfg.getInt("Kopf-2" + ".X"), InteractListener.cfg.getInt("Kopf-2" + ".Y"), InteractListener.cfg.getInt("Kopf-2" + ".Z"));
  282. Location loc3 = new Location(Bukkit.getWorld(InteractListener.cfg.getString("Kopf-3" + ".World")), InteractListener.cfg.getInt("Kopf-3" + ".X"), InteractListener.cfg.getInt("Kopf-3" + ".Y"), InteractListener.cfg.getInt("Kopf-3" + ".Z"));
  283. Location loc4 = new Location(Bukkit.getWorld(InteractListener.cfg.getString("Kopf-4" + ".World")), InteractListener.cfg.getInt("Kopf-4" + ".X"), InteractListener.cfg.getInt("Kopf-4" + ".Y"), InteractListener.cfg.getInt("Kopf-4" + ".Z"));
  284. Location loc5 = new Location(Bukkit.getWorld(InteractListener.cfg.getString("Kopf-5" + ".World")), InteractListener.cfg.getInt("Kopf-5" + ".X"), InteractListener.cfg.getInt("Kopf-5" + ".Y"), InteractListener.cfg.getInt("Kopf-5" + ".Z"));
  285. Location loc6 = new Location(Bukkit.getWorld(InteractListener.cfg.getString("Kopf-6" + ".World")), InteractListener.cfg.getInt("Kopf-6" + ".X"), InteractListener.cfg.getInt("Kopf-6" + ".Y"), InteractListener.cfg.getInt("Kopf-6" + ".Z"));
  286. Location loc7 = new Location(Bukkit.getWorld(InteractListener.cfg.getString("Kopf-7" + ".World")), InteractListener.cfg.getInt("Kopf-7" + ".X"), InteractListener.cfg.getInt("Kopf-7" + ".Y"), InteractListener.cfg.getInt("Kopf-7" + ".Z"));
  287. Location loc8 = new Location(Bukkit.getWorld(InteractListener.cfg.getString("Kopf-8" + ".World")), InteractListener.cfg.getInt("Kopf-8" + ".X"), InteractListener.cfg.getInt("Kopf-8" + ".Y"), InteractListener.cfg.getInt("Kopf-8" + ".Z"));
  288. Location loc9 = new Location(Bukkit.getWorld(InteractListener.cfg.getString("Kopf-9" + ".World")), InteractListener.cfg.getInt("Kopf-9" + ".X"), InteractListener.cfg.getInt("Kopf-9" + ".Y"), InteractListener.cfg.getInt("Kopf-9" + ".Z"));
  289. Location loc10 = new Location(Bukkit.getWorld(InteractListener.cfg.getString("Kopf-10" + ".World")), InteractListener.cfg.getInt("Kopf-10" + ".X"), InteractListener.cfg.getInt("Kopf-10" + ".Y"), InteractListener.cfg.getInt("Kopf-10" + ".Z"));
  290.  
  291. String face = InteractListener.cfg.getString("Kopf-1" + ".Richtung");
  292.  
  293. List<Location> locs = new ArrayList<>();
  294. locs.add(loc);
  295. locs.add(loc2);
  296. locs.add(loc3);
  297. locs.add(loc4);
  298. locs.add(loc5);
  299. locs.add(loc6);
  300. locs.add(loc7);
  301. locs.add(loc8);
  302. locs.add(loc9);
  303. locs.add(loc10);
  304.  
  305. for(int in = 0; in < locs.size(); in++){
  306. int id = in+1;
  307. if(getKills(rang.get(id)) > 0){
  308. locs.get(in).getBlock().setType(Material.SKULL);
  309. Skull s = (Skull) locs.get(in).getBlock().getState();
  310. s.setSkullType(SkullType.PLAYER);
  311. s.setRotation(BlockFace.WEST);
  312. String name = getPlayername(rang.get(id));
  313. s.setOwner(name);
  314. s.update();
  315.  
  316. Location ort = new Location(locs.get(id).getWorld(), locs.get(id).getX(), locs.get(id).getY() -1, locs.get(id).getZ());
  317.  
  318. if(ort.getBlock().getState() instanceof Sign){
  319. BlockState b = ort.getBlock().getState();
  320. Sign si = (Sign) b;
  321.  
  322. si.setLine(0, "Platz #" + id);
  323. si.setLine(1, name);
  324. si.setLine(2, "");
  325. if(getKills(rang.get(id)) == 1){
  326. si.setLine(3, getKills(rang.get(id)) + " Kill");
  327. }else if(getKills(rang.get(id)) > 1){
  328. si.setLine(3, getKills(rang.get(id)) + " Kills");
  329. }else{
  330. si.setLine(3, getKills(rang.get(id)) + " Kills");
  331. }
  332. si.update();
  333. }
  334. }
  335. }
  336. }else{
  337. Bukkit.getConsoleSender().sendMessage("Bitte Stats Köpfe setzen!");
  338. }
  339. }
  340.  
  341. public static void delete(String uuid){
  342. if(UserExists(uuid)){
  343. try {
  344. PreparedStatement ps = MySQL.con.prepareStatement("DELETE FROM " + cointable + " WHERE UUID = ?");
  345. ps.setString(1, uuid);
  346. ps.executeUpdate();
  347. } catch (SQLException e) {
  348. e.printStackTrace();
  349. }
  350. }
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement