Advertisement
Guest User

Untitled

a guest
Feb 6th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.20 KB | None | 0 0
  1. package at.coins.coins;
  2. import net.md_5.bungee.api.connection.ProxiedPlayer;
  3.  
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. public class CoinsAPI {
  7.  
  8. public static void createTable(){
  9. try{
  10. PreparedStatement ps = MySQL.getStatement("CREATE TABLE IF NOT EXISTS Coins (Spielername VARCHAR(100), UUID VARCHAR(100), Coins INT(100))");
  11. ps.executeUpdate();
  12. }catch(Exception ex){
  13. ex.printStackTrace();
  14. }
  15. }
  16.  
  17. public static void register(ProxiedPlayer p){
  18. try{
  19. PreparedStatement ps = MySQL.getStatement("INSERT INTO Coins (Spielername, UUID, Coins) VALUES (?, ?, ?)");
  20. ps.setString(1, p.getName());
  21. ps.setString(2, p.getUniqueId().toString());
  22. ps.setInt(3, 0);
  23. ps.executeUpdate();
  24. ps.close();
  25. }catch(Exception ex){
  26. ex.printStackTrace();
  27. }
  28. }
  29.  
  30. public static boolean isRegistered(ProxiedPlayer p){
  31. try{
  32. PreparedStatement ps = MySQL.getStatement("SELECT * FROM Coins WHERE UUID= ?");
  33. ps.setString(1, p.getUniqueId().toString());
  34. ResultSet rs = ps.executeQuery();
  35. boolean user = rs.next();
  36. rs.close();
  37. rs.close();
  38. return user;
  39. }catch(Exception ex){
  40. ex.printStackTrace();
  41. }
  42. return false;
  43. }
  44.  
  45. public static boolean isRegistered(String name){
  46. try{
  47. PreparedStatement ps = MySQL.getStatement("SELECT * FROM Coins WHERE Spielername= ?");
  48. ps.setString(1, name);
  49. ResultSet rs = ps.executeQuery();
  50. boolean user = rs.next();
  51. rs.close();
  52. rs.close();
  53. return user;
  54. }catch(Exception ex){
  55. ex.printStackTrace();
  56. }
  57. return false;
  58. }
  59.  
  60. public static int getCoins(ProxiedPlayer p){
  61. try{
  62. PreparedStatement ps = MySQL.getStatement("SELECT * FROM Coins WHERE UUID= ?");
  63. ps.setString(1, p.getUniqueId().toString());
  64. ResultSet rs = ps.executeQuery();
  65. rs.next();
  66. int coins = rs.getInt("Coins");
  67. rs.close();
  68. ps.close();
  69. return coins;
  70. }catch(Exception ex){
  71. ex.printStackTrace();
  72. }
  73. return -1;
  74. }
  75.  
  76. public static void setCoins(ProxiedPlayer p, int coins){
  77. try{
  78. PreparedStatement ps = MySQL.getStatement("UPDATE Coins SET Coins= ? WHERE UUID= ?");
  79. ps.setInt(1, coins);
  80. ps.setString(2, p.getUniqueId().toString());
  81. ps.executeUpdate();
  82. ps.close();
  83. }catch(Exception ex){
  84. ex.printStackTrace();
  85. }
  86. }
  87.  
  88. public static void addCoins(ProxiedPlayer p, int coins){
  89. try{
  90. PreparedStatement ps = MySQL.getStatement("UPDATE Coins SET Coins= ? WHERE UUID= ?");
  91. ps.setInt(1, getCoins(p) + coins);
  92. ps.setString(2, p.getUniqueId().toString());
  93. ps.executeUpdate();
  94. ps.close();
  95. }catch(Exception ex){
  96. ex.printStackTrace();
  97. }
  98. }
  99.  
  100. public static void removeCoins(ProxiedPlayer p, int coins){
  101. try{
  102. PreparedStatement ps = MySQL.getStatement("UPDATE Coins SET Coins= ? WHERE UUID= ?");
  103. ps.setInt(1, getCoins(p) - coins);
  104. ps.setString(2, p.getUniqueId().toString());
  105. ps.executeUpdate();
  106. ps.close();
  107. }catch(Exception ex){
  108. ex.printStackTrace();
  109. }
  110. }
  111.  
  112. public static int getCoins(String name){
  113. try{
  114. PreparedStatement ps = MySQL.getStatement("SELECT * FROM Coins WHERE Spielername= ?");
  115. ps.setString(1, name);
  116. ResultSet rs = ps.executeQuery();
  117. rs.next();
  118. int coins = rs.getInt("Coins");
  119. rs.close();
  120. ps.close();
  121. return coins;
  122. }catch(Exception ex){
  123. ex.printStackTrace();
  124. }
  125. return -1;
  126. }
  127.  
  128. public static void setCoins(String name, int coins){
  129. try{
  130. PreparedStatement ps = MySQL.getStatement("UPDATE Coins SET Coins= ? WHERE Spielername= ?");
  131. ps.setInt(1, coins);
  132. ps.setString(2, name);
  133. ps.executeUpdate();
  134. ps.close();
  135. }catch(Exception ex){
  136. ex.printStackTrace();
  137. }
  138. }
  139.  
  140. public static void addCoins(String name, int coins){
  141. try{
  142. PreparedStatement ps = MySQL.getStatement("UPDATE Coins SET Coins= ? WHERE Spielername= ?");
  143. ps.setInt(1, getCoins(name) + coins);
  144. ps.setString(2, name);
  145. ps.executeUpdate();
  146. ps.close();
  147. }catch(Exception ex){
  148. ex.printStackTrace();
  149. }
  150. }
  151.  
  152. public static void removeCoins(String name, int coins){
  153. try{
  154. PreparedStatement ps = MySQL.getStatement("UPDATE Coins SET Coins= ? WHERE Spielername= ?");
  155. ps.setInt(1, getCoins(name) - coins);
  156. ps.setString(2, name);
  157. ps.executeUpdate();
  158. ps.close();
  159. }catch(Exception ex){
  160. ex.printStackTrace();
  161. }
  162. }
  163.  
  164. }
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. package at.coins.coins;
  172.  
  173. import net.md_5.bungee.api.CommandSender;
  174. import net.md_5.bungee.api.connection.ProxiedPlayer;
  175. import net.md_5.bungee.api.plugin.Command;
  176.  
  177. public class CoinsCMD extends Command {
  178.  
  179. public CoinsCMD(String name) {
  180. super(name);
  181. }
  182.  
  183. @Override
  184. public void execute(CommandSender cs, String[] args) {
  185. if(!(cs instanceof ProxiedPlayer)){
  186. }
  187.  
  188.  
  189.  
  190. ProxiedPlayer p = (ProxiedPlayer)cs;
  191. if(args.length == 0){
  192. int coins = CoinsAPI.getCoins(p);
  193. p.sendMessage(Main.PREFIX + "§bDu hast aktuell §9" + coins + " §bCoins.");
  194. } else if(args.length == 1){
  195. if(cs.hasPermission("server.coins")) {
  196. if(args[0].equalsIgnoreCase("add")){
  197. p.sendMessage(Main.PREFIX + "§c/coins add <Spieler> <Anzahl>");
  198. }else if(args[0].equalsIgnoreCase("remove")){
  199. p.sendMessage(Main.PREFIX + "§c/coins remove <Spieler> <Anzahl>");
  200. }else if(args[0].equalsIgnoreCase("set")){
  201. p.sendMessage(Main.PREFIX + "§c/coins set <Spieler> <Anzahl>");
  202. }else{
  203. String name = args[0];
  204. if(CoinsAPI.isRegistered(name)){
  205. int coins = CoinsAPI.getCoins(name);
  206. p.sendMessage(Main.PREFIX + "§bDer Spieler §9" + name + "§b hat aktuell §9" + coins + " §bCoins.");
  207. }else{
  208. p.sendMessage(Main.PREFIX + "§cDieser Spieler ist nicht registriert.");
  209. }
  210. }
  211. } else {
  212. ((ProxiedPlayer) cs).sendMessage(Main.NOPERM);
  213. }
  214.  
  215. }else if(args.length == 2){
  216. if(cs.hasPermission("server.coins")) {
  217. if(args[0].equalsIgnoreCase("add")){
  218. p.sendMessage(Main.PREFIX + "§c/coins add <Spieler> <Anzahl>");
  219. }else if(args[0].equalsIgnoreCase("remove")){
  220. p.sendMessage(Main.PREFIX + "§c/coins remove <Spieler> <Anzahl>");
  221. }else if(args[0].equalsIgnoreCase("set")){
  222. p.sendMessage(Main.PREFIX + "§c/coins set <Spieler> <Anzahl>");
  223. }else{
  224. p.sendMessage(Main.PREFIX + "§c/coins add|remove|set <Spieler> <Anzahl>");
  225. }
  226. } else {
  227. ((ProxiedPlayer) cs).sendMessage(Main.NOPERM);
  228. }
  229.  
  230. }else if(args.length == 3){
  231. if(cs.hasPermission("server.coins")) {
  232. if(args[0].equalsIgnoreCase("add")){
  233. String name = args[1];
  234. if(CoinsAPI.isRegistered(name)){
  235. int coins = Integer.valueOf(args[2]);
  236. CoinsAPI.addCoins(name, coins);
  237. p.sendMessage(Main.PREFIX + "§bDu hast dem Spieler §9" + name + "§b erfolgreich §9" + coins + " §bCoins hinzugefügt.");
  238. }
  239. }else if(args[0].equalsIgnoreCase("remove")){
  240. String name = args[1];
  241. if(CoinsAPI.isRegistered(name)){
  242. int coins = Integer.valueOf(args[2]);
  243. CoinsAPI.removeCoins(name, coins);
  244. p.sendMessage(Main.PREFIX + "§bDu hast dem Spieler §9" + name + "§b erfolgreich §9" + coins + " §bCoins abgezogen.");
  245. }
  246. }else if(args[0].equalsIgnoreCase("set")){
  247. String name = args[1];
  248. if(CoinsAPI.isRegistered(name)){
  249. int coins = Integer.valueOf(args[2]);
  250. CoinsAPI.setCoins(name, coins);
  251. p.sendMessage(Main.PREFIX + "§bDu hast die Coins von §9" + name + "§b erfolgreich auf §9" + coins + " §bCoins gesetzt.");
  252. }
  253. }else{
  254. p.sendMessage(Main.PREFIX + "§c/coins add|remove|set <Spieler> <Anzahl>");
  255. }
  256. } else {
  257. ((ProxiedPlayer) cs).sendMessage(Main.NOPERM);
  258. }
  259.  
  260. }
  261.  
  262.  
  263.  
  264. }
  265. }
  266.  
  267.  
  268.  
  269.  
  270.  
  271. package at.coins.coins;
  272.  
  273. import net.md_5.bungee.api.connection.ProxiedPlayer;
  274. import net.md_5.bungee.api.event.ServerSwitchEvent;
  275. import net.md_5.bungee.api.plugin.Listener;
  276. import net.md_5.bungee.event.EventHandler;
  277.  
  278. public class JoinEVENT implements Listener {
  279.  
  280. @EventHandler
  281. public void onJoin(ServerSwitchEvent e) {
  282. ProxiedPlayer p = e.getPlayer();
  283. if(!CoinsAPI.isRegistered(p)){
  284. CoinsAPI.register(p);
  285. }
  286. }
  287.  
  288. }
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296. package at.coins.coins;
  297.  
  298. import net.md_5.bungee.api.ProxyServer;
  299. import net.md_5.bungee.api.plugin.Plugin;
  300.  
  301. public class Main extends Plugin {
  302.  
  303. public static String PREFIX = "§8[§1COINS§8] ";
  304. public static String NOPERM = PREFIX + "§cDu hast zu wenig Rechte!";
  305.  
  306. @Override
  307. public void onEnable() {
  308. ProxyServer.getInstance().getConsole().sendMessage(Main.PREFIX + "§aGESTARTET : §2CODER: sowas");
  309. MySQL.connect();
  310. CoinsAPI.createTable();
  311. register();
  312. }
  313.  
  314. @Override
  315. public void onDisable() {
  316. ProxyServer.getInstance().getConsole().sendMessage(Main.PREFIX + "§4GESTOPPT");
  317. MySQL.disconnect();
  318. }
  319. public void register() {
  320. ProxyServer.getInstance().getPluginManager().registerCommand(this, new CoinsCMD("coins"));
  321. ProxyServer.getInstance().getPluginManager().registerListener(this, new JoinEVENT());
  322. }
  323.  
  324.  
  325.  
  326. }
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333. package at.coins.coins;
  334. import net.md_5.bungee.api.ProxyServer;
  335.  
  336. import java.sql.Connection;
  337. import java.sql.DriverManager;
  338. import java.sql.PreparedStatement;
  339. import java.sql.ResultSet;
  340. import java.sql.SQLException;
  341. public class MySQL {
  342.  
  343. public static String host = "localhost";
  344. public static String port = "3306";
  345. public static String database = "coins";
  346. public static String username = "root";
  347. public static String password = "1234";
  348.  
  349. public static Connection con;
  350.  
  351. public static boolean isConnected(){
  352. return con != null;
  353. }
  354.  
  355. public static void connect(){
  356. if(!isConnected()){
  357. try {
  358. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
  359. ProxyServer.getInstance().getConsole().sendMessage(Main.PREFIX + "§aEs konnte erfolgreich mit der Datenbank verbunden werden");
  360. } catch (SQLException e) {
  361. ProxyServer.getInstance().getConsole().sendMessage(Main.PREFIX + "§cEs konnte nicht mit der Datenbank verbunden werden");
  362. }
  363. }
  364. }
  365.  
  366. public static void disconnect(){
  367. try {
  368. con.close();
  369. ProxyServer.getInstance().getConsole().sendMessage(Main.PREFIX + "§aDie Verbindung zur Datenbank konnte erfolgreich geschlossen werden");
  370. } catch (SQLException e) {
  371. ProxyServer.getInstance().getConsole().sendMessage(Main.PREFIX + "§cDie Verbindung zur Datenbank konnte nicht geschlossen werden");
  372. }
  373. }
  374.  
  375. public static PreparedStatement getStatement(String sql){
  376. if(isConnected()){
  377. PreparedStatement ps;
  378. try {
  379. ps = con.prepareStatement(sql);
  380. return ps;
  381. } catch (SQLException e) {
  382. e.printStackTrace();
  383. }
  384. }
  385. return null;
  386. }
  387.  
  388. public static ResultSet getResult(String sql){
  389. if(isConnected()){
  390. PreparedStatement ps;
  391. ResultSet rs;
  392. try {
  393. ps = getStatement(sql);
  394. rs = ps.executeQuery();
  395. return rs;
  396. } catch (SQLException e) {
  397. e.printStackTrace();
  398. }
  399. }
  400. return null;
  401. }
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement