Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 KB | None | 0 0
  1. public class Coins
  2. implements CommandExecutor
  3. {
  4. public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args)
  5. {
  6. if ((cmd.getName().equalsIgnoreCase("coins")) &&
  7. ((sender instanceof Player)))
  8. {
  9. final Player p = (Player)sender;
  10. if (args.length == 0) {
  11. p.sendMessage(Coin.prefix + "�7Du hast derzeit �6" + Coin.getCoins(p) + " �7Coins");
  12. }
  13. if (args.length == 1)
  14. {
  15. String name = args[0];
  16. final Player t = Bukkit.getServer().getPlayerExact(name);
  17. if (t != null)
  18. {
  19. p.sendMessage(Coin.prefix + " �7Datenbank wird ausgelesen...");
  20. Bukkit.getScheduler().scheduleAsyncDelayedTask(Coin.plugin, new Runnable()
  21. {
  22. public void run()
  23. {
  24. p.sendMessage(Coin.prefix + "�e" + t.getName() + " �7hat derzeit �6" + Coin.getCoins(p) + " �7Coins");
  25. }
  26. }, 35L);
  27. }
  28. else
  29. {
  30. OfflinePlayer oplayer = Bukkit.getOfflinePlayer(name);
  31. UUID uuid = oplayer.getUniqueId();
  32. if (Coin.isPlayerExisting(uuid)) {
  33. p.sendMessage(Coin.prefix + "�e" + UUIDFetcher.getName(uuid) + " �7hat derzeit �6" + Coin.getCoins(t) + " �7Coins");
  34. } else {
  35. p.sendMessage(Coin.prefix + "�cDer angegebene Spieler ist der Datenbank unbekannt");
  36. }
  37. }
  38. }
  39. }
  40. if (cmd.getName().equalsIgnoreCase("addcoins")) {
  41. if (args.length == 2)
  42. {
  43. if (sender.hasPermission("lostdos.addcoins"))
  44. {
  45. Player t = Bukkit.getPlayer(args[0]);
  46. int i = Integer.valueOf(args[1]).intValue();
  47. if (t != null)
  48. {
  49. sender.sendMessage(Coin.prefix + "�7Du hast dem Spieler �e" + t.getName() + " �c" + i + " �7Coins geschenkt");
  50. if ((sender instanceof Player)) {
  51. t.sendMessage(Coin.prefix + "�7Du hast �c" + args[1] + " �7Coins von �e" + sender.getName() + " �7erhalten");
  52. }
  53. Coin.addCoins(t, i);
  54. }
  55. else
  56. {
  57. sender.sendMessage(Coin.prefix + "�cDieser Spieler ist nicht Online");
  58. }
  59. }
  60. else
  61. {
  62. sender.sendMessage(Coin.prefix + Coin.noperm);
  63. }
  64. }
  65. else {
  66. sender.sendMessage(Coin.prefix + "�cVerwende�8: �c/addcoins <�eSpieler�c> <�eAnzahl�c>");
  67. }
  68. }
  69. if (cmd.getName().equalsIgnoreCase("pay")) {
  70. if (args.length == 2)
  71. {
  72. Player t = Bukkit.getPlayer(args[0]);
  73. int i = Integer.valueOf(args[1]).intValue();
  74. if (t != null) {
  75. sender.sendMessage(Coin.prefix + "�7Du hast dem Spieler �e" + t.getName() + " �c" + i + " �7Coins bezahlt");
  76. } else {
  77. sender.sendMessage(Coin.prefix + "�cDieser Spieler ist nicht Online");
  78. }
  79. }
  80. else
  81. {
  82. sender.sendMessage(Coin.prefix + "�cVerwende�8: �c/pay <�eSpieler�c> <�eAnzahl�c>");
  83. }
  84. }
  85. return false;
  86. }
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. public class AsyncPlayerPreLoginListener
  98. implements Listener
  99. {
  100. @EventHandler
  101. public void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent e)
  102. {
  103. UUID uuid = e.getUniqueId();
  104. String name = e.getName();
  105. if (e.getLoginResult() == AsyncPlayerPreLoginEvent.Result.ALLOWED)
  106. {
  107. Coin.registerPlayer(uuid, name);
  108. Coin.setPlayerName(uuid, name);
  109. }
  110. }
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. public class Coin
  122. extends JavaPlugin
  123. {
  124. public static Coin plugin;
  125. public static File file = new File("plugins/Coins", "config.yml");
  126. public static FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  127. public static String prefix = "�8[�eCoins�8] ";
  128. public static String noperm = "�7Der gew�nschte Befehl ist uns nicht bekannt";
  129. public static String unknowncmd = "�cDieser Befehl existiert nicht.";
  130. public static String host = cfg.getString("MySQL.Host");
  131. public static String port = cfg.getString("MySQL.Port");
  132. public static String database = cfg.getString("MySQL.Datenbank");
  133. public static String username = cfg.getString("MySQL.Benutzername");
  134. public static String password = cfg.getString("MySQL.Passwort");
  135. public static Connection connection;
  136.  
  137. public void onEnable()
  138. {
  139. plugin = this;
  140. saveDefaultConfig();
  141. reloadConfig();
  142. registerCommands();
  143. registerListener();
  144. Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
  145. {
  146. public void run() {}
  147. }, 12000L, 12000L);
  148. connect();
  149. createTableIfNotExists();
  150. Bukkit.getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable()
  151. {
  152. public void run() {}
  153. }, 12000L, 12000L);
  154. }
  155.  
  156. public void onDisable() {}
  157.  
  158. public static void registerCommands()
  159. {
  160. plugin.getCommand("coins").setExecutor(new Coins());
  161. plugin.getCommand("addcoins").setExecutor(new Coins());
  162. }
  163.  
  164. public static void registerListener()
  165. {
  166. Bukkit.getPluginManager().registerEvents(new AsyncPlayerPreLoginListener(), plugin);
  167. }
  168.  
  169. public static Connection getConnection()
  170. {
  171. return connection;
  172. }
  173.  
  174. public static boolean isConnected()
  175. {
  176. return connection != null;
  177. }
  178.  
  179. public static void connect()
  180. {
  181. if (!isConnected()) {
  182. try
  183. {
  184. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  185. }
  186. catch (SQLException ex)
  187. {
  188. ex.printStackTrace();
  189. }
  190. }
  191. }
  192.  
  193. public static void disconnect()
  194. {
  195. if (isConnected()) {
  196. try
  197. {
  198. connection.close();
  199. }
  200. catch (SQLException ex)
  201. {
  202. ex.printStackTrace();
  203. }
  204. }
  205. }
  206.  
  207. public static void createTableIfNotExists()
  208. {
  209. if (isConnected()) {
  210. try
  211. {
  212. PreparedStatement ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS Coins (Spielername VARCHAR(100), UUID VARCHAR(100), Coins INT(100))");
  213. ps.executeUpdate();
  214. ps.close();
  215. }
  216. catch (SQLException ex)
  217. {
  218. ex.printStackTrace();
  219. }
  220. }
  221. }
  222.  
  223. public static boolean isPlayerExisting(Player p)
  224. {
  225. try
  226. {
  227. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Coins WHERE UUID = ?");
  228. ps.setString(1, p.getUniqueId().toString());
  229. ResultSet result = ps.executeQuery();
  230. boolean isExisting = result.next();
  231. result.close();
  232. ps.close();
  233. return isExisting;
  234. }
  235. catch (Exception ex)
  236. {
  237. ex.printStackTrace();
  238. }
  239. return false;
  240. }
  241.  
  242. public static boolean isPlayerExisting(UUID uuid)
  243. {
  244. try
  245. {
  246. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Coins WHERE UUID = ?");
  247. ps.setString(1, uuid.toString());
  248. ResultSet result = ps.executeQuery();
  249. boolean isExisting = result.next();
  250. result.close();
  251. ps.close();
  252. return isExisting;
  253. }
  254. catch (Exception ex)
  255. {
  256. ex.printStackTrace();
  257. }
  258. return false;
  259. }
  260.  
  261. public static void registerPlayer(UUID uuid, String name)
  262. {
  263. if (isPlayerExisting(uuid)) {
  264. return;
  265. }
  266. try
  267. {
  268. PreparedStatement ps = connection.prepareStatement("INSERT INTO Coins (Spielername, UUID, Coins) VALUES (?, ?, ?)");
  269. ps.setString(1, name);
  270. ps.setString(2, uuid.toString());
  271. ps.setInt(3, 0);
  272. ps.execute();
  273. ps.close();
  274. }
  275. catch (Exception ex)
  276. {
  277. ex.printStackTrace();
  278. }
  279. }
  280.  
  281. public static void setPlayerName(UUID uuid, String name)
  282. {
  283. try
  284. {
  285. PreparedStatement ps = connection.prepareStatement("UPDATE Coins SET Spielername = ? WHERE UUID = ?");
  286. ps.setString(1, name);
  287. ps.setString(2, uuid.toString());
  288. ps.executeUpdate();
  289. ps.close();
  290. }
  291. catch (Exception ex)
  292. {
  293. ex.printStackTrace();
  294. }
  295. }
  296.  
  297. public static int getCoins(Player p)
  298. {
  299. try
  300. {
  301. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Coins WHERE UUID = ?");
  302. ps.setString(1, p.getUniqueId().toString());
  303. ResultSet result = ps.executeQuery();
  304. result.next();
  305. int coins = result.getInt("Coins");
  306. result.close();
  307. ps.close();
  308. return coins;
  309. }
  310. catch (Exception ex)
  311. {
  312. ex.printStackTrace();
  313. }
  314. return -1;
  315. }
  316.  
  317. public static int getCoins(UUID uuid)
  318. {
  319. try
  320. {
  321. PreparedStatement ps = connection.prepareStatement("SELECT * FROM Coins WHERE UUID = ?");
  322. ps.setString(1, uuid.toString());
  323. ResultSet result = ps.executeQuery();
  324. result.next();
  325. int coins = result.getInt("Coins");
  326. result.close();
  327. ps.close();
  328. return coins;
  329. }
  330. catch (Exception ex)
  331. {
  332. ex.printStackTrace();
  333. }
  334. return -1;
  335. }
  336.  
  337. public static void setCoins(Player p, int coins)
  338. {
  339. try
  340. {
  341. PreparedStatement ps = connection.prepareStatement("UPDATE Coins SET Coins = ? WHERE UUID = ?");
  342. ps.setInt(1, coins);
  343. ps.setString(2, p.getUniqueId().toString());
  344. ps.executeUpdate();
  345. ps.close();
  346. }
  347. catch (Exception ex)
  348. {
  349. ex.printStackTrace();
  350. }
  351. }
  352.  
  353. public static void setCoins(UUID uuid, int coins)
  354. {
  355. try
  356. {
  357. PreparedStatement ps = connection.prepareStatement("UPDATE Coins SET Coins = ? WHERE UUID = ?");
  358. ps.setInt(1, coins);
  359. ps.setString(2, uuid.toString());
  360. ps.executeUpdate();
  361. ps.close();
  362. }
  363. catch (Exception ex)
  364. {
  365. ex.printStackTrace();
  366. }
  367. }
  368.  
  369. public static void addCoins(Player p, int coins)
  370. {
  371. setCoins(p, getCoins(p) + coins);
  372. }
  373.  
  374. public static void addCoins(UUID uuid, int coins)
  375. {
  376. setCoins(uuid, getCoins(uuid) + coins);
  377. }
  378.  
  379. public static void removeCoins(Player p, int coins)
  380. {
  381. setCoins(p, getCoins(p) - coins);
  382. }
  383.  
  384. public static void removeCoins(UUID uuid, int coins)
  385. {
  386. setCoins(uuid, getCoins(uuid) - coins);
  387. }
  388. }
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396. // INTERNAL ERROR //
  397.  
  398.  
  399.  
  400.  
  401. MySQL:
  402. Host: host
  403. Port: port
  404. Datenbank: database
  405. Benutzername: username
  406. Passwort: password
  407.  
  408.  
  409.  
  410.  
  411. name: Coins
  412. version: 1.0
  413. author: JailBreaker
  414. main: eu.lostdos.Coins.Coin
  415.  
  416. commands:
  417. coins:
  418. description: Zeigt die Coins eines Spielers an.
  419. addcoins:
  420. description: Vergibt Spielern Coin.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement