Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.25 KB | None | 0 0
  1. package main;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.UUID;
  9. import java.util.concurrent.TimeUnit;
  10.  
  11. import Level.LevelCommand;
  12. import commands.BanCMD;
  13. import commands.PingCommand;
  14. import net.md_5.bungee.api.ProxyServer;
  15. import net.md_5.bungee.api.connection.ProxiedPlayer;
  16. import net.md_5.bungee.api.event.PlayerDisconnectEvent;
  17. import net.md_5.bungee.api.event.ServerConnectEvent;
  18. import net.md_5.bungee.api.plugin.Listener;
  19. import net.md_5.bungee.api.plugin.Plugin;
  20. import net.md_5.bungee.event.EventHandler;
  21. import onlinetime.OnlineCmd;
  22. import rank.Rank;
  23. import rank.RankCommand;
  24. import report.ReportCmd;
  25. import report.ReportReason;
  26. import teleporter.TeleportCmd;
  27.  
  28. public class MySQL extends Plugin implements Listener{
  29.  
  30. private static String HOST = "localhost";
  31. private static String PORT = "3306";
  32. private static String USER = "root";
  33. private static String PASSWORD = "hPThDW9nD8uQuJST";
  34. private static String DATABASE = "MC-Server";
  35. public static Connection con;
  36.  
  37. public static String Prefix = "§b§l▋ §9§lCerekCraft §b➤ §7";
  38. public static String Fehler = "§c§l▋ §4§lFehler §c➤ §7";
  39. public static String noperm = Fehler + "Du hast keinen Zugriff auf diesen Befehl!";getPluginManager().registerListener(this, this);
  40. ProxyServer.getInstance().getPluginManager().registerListener(this, this);
  41. ProxyServer.getInstance().getPluginManager().registerCommand(this, new RankCommand("rank"));
  42. ProxyServer.getInstance().getPluginManager().registerCommand(this, new LevelCommand("level"));
  43. ProxyServer.getInstance().getPluginManager().registerCommand(this, new PingCommand("ping"));
  44. ProxyServer.getInstance().getPluginManager().registerCommand(this, new OnlineCmd("onlinetime"));
  45. ProxyServer.getInstance().getPluginManager().registerCommand(this, new TeleportCmd("gtp"));
  46. ProxyServer.getInstance().getPluginManager().registerCommand(this, new ReportCmd("report"));
  47. ProxyServer.getInstance().getPluginManager().registerCommand(this, new BanCMD("ban"));
  48.  
  49. ProxyServer.getInstance().getPluginManager().registerListener(this, this);
  50.  
  51.  
  52.  
  53. connect();
  54. start();
  55. startReport();
  56. Nick.start();
  57. reconnect();
  58.  
  59. AddOnlineTime();
  60. }
  61.  
  62. public void onDisable() {
  63. disconnect();
  64. }
  65.  
  66. private void AddOnlineTime(){
  67. ProxyServer.getInstance().getScheduler().schedule(this, new Runnable() {
  68.  
  69. @Override
  70. public void run() {
  71.  
  72. for(ProxiedPlayer p : ProxyServer.getInstance().getPlayers()){
  73. setOnlineTime(p.getUniqueId(), getOnlineTime(p.getUniqueId()) + 1);
  74. LevelCommand.addXP(p.getUniqueId(), 25);
  75. }
  76. AddOnlineTime();
  77. }
  78. }, 1, TimeUnit.MINUTES);
  79. }
  80.  
  81. private void reconnect(){
  82. ProxyServer.getInstance().getScheduler().schedule(this, new Runnable() {
  83.  
  84. @Override
  85. public void run() {
  86. disconnect();
  87. connect();
  88. reconnect();
  89. }
  90. }, 5, TimeUnit.MINUTES);
  91. }
  92.  
  93. @EventHandler
  94. public void onJoin(ServerConnectEvent e){
  95. if(!containsPlayer(e.getPlayer().getUniqueId())){
  96. update(e.getPlayer().getUniqueId(), e.getPlayer().getName(), Rank.Spieler.toString(), Rank.Spieler.toString(), 100, 1, 0, "UNKNOWN", 0, true);
  97. }else{
  98. setName(e.getPlayer().getUniqueId(), e.getPlayer().getName());
  99. setOnline(e.getPlayer().getUniqueId(), true);
  100. }
  101.  
  102. if(!Nick.containsPlayer(e.getPlayer().getUniqueId())){
  103. Nick.update(e.getPlayer().getUniqueId(), false, false, "UNKNOWN", "UNKNOWN");
  104. }
  105.  
  106. e.getPlayer().addGroups(getRank(e.getPlayer().getUniqueId()).toString());
  107.  
  108. }
  109.  
  110. @EventHandler
  111. public void onJeave(PlayerDisconnectEvent e){
  112. setOnline(e.getPlayer().getUniqueId(), false);
  113. }
  114.  
  115.  
  116.  
  117.  
  118. @SuppressWarnings("deprecation")
  119. public static void connect() {
  120.  
  121. try {
  122. con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":" + PORT + "/" + DATABASE, USER, PASSWORD);
  123. ProxyServer.getInstance().getConsole().sendMessage("�9MySQL �8| �7Erfolgreich mit der Datenbank �e" + DATABASE + " �7verbunden...");
  124. } catch (SQLException e) {
  125. ProxyServer.getInstance().getConsole().sendMessage("�9MySQL �8| �cKonnte nicht zu �4" + DATABASE + " �cverbinden...");
  126. }
  127. }
  128.  
  129. @SuppressWarnings("deprecation")
  130. public static void disconnect() {
  131. if (isConnected()) {
  132. try {
  133. con.close();
  134. ProxyServer.getInstance().getConsole().sendMessage("�9MySQL �8| �7Erfolgreich von Datenbank �e" + DATABASE + " �7getrennt...");
  135. } catch (SQLException e) {
  136. ProxyServer.getInstance().getConsole().sendMessage("�9MySQL �8| �cKonnte nicht von �4" + DATABASE + " �ctrennen...");
  137. }
  138. }
  139. }
  140.  
  141. private static void startReport(){
  142. try {
  143. PreparedStatement ps = MySQL.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS Reports (TargetUUID varchar(100) NOT NULL,"
  144. + " FromUUID varchar(100) NOT NULL,"
  145. + " Reason varchar(100) NOT NULL)");
  146. ps.execute();
  147. } catch (SQLException e) {
  148. e.printStackTrace();
  149. }
  150. }
  151.  
  152. public static void addReport(UUID From, UUID To, ReportReaso
  153. public static void update(String qry) {
  154.  
  155. if (isConnected()) {
  156.  
  157. try {
  158. con.createStatement().executeUpdate(qry);
  159. } catch (SQLException e) {
  160. e.printStackTrace();
  161. }
  162. }
  163. }
  164.  
  165. public static ResultSet getResult(String qry) {
  166.  
  167. if (isConnected()) {
  168.  
  169. try {
  170. return con.createStatement().executeQuery(qry);
  171. } catch (SQLException e) {
  172. e.printStackTrace();
  173. }
  174. }
  175. return null;
  176. }
  177. n reason){
  178.  
  179. try {
  180. PreparedStatement ps = MySQL.getConnection()
  181. .prepareStatement("INSERT INTO Reports (TargetUUID, Fromeason) VALUES (?, ?, ?)");
  182. ps.setString(1, To.toString());
  183. ps.setString(2, From.toString());
  184. ps.setString(3, reason.toString());
  185. ps.execute();
  186. } catch (SQLException e) {
  187. e.printStackTrace();
  188. }
  189. }
  190.  
  191. public static boolean isConnected() {
  192. return (con == null ? false : true);
  193. }
  194.  
  195. public static Connection getConnection() {
  196. return con;
  197. }
  198.  
  199. public static void start() {
  200. try {
  201. PreparedStatement ps = MySQL.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS Spieler (UUID varchar(100) NOT NULL,"
  202. + " Name varchar(100) NOT NULL,"
  203. + " Rang varchar(100) NOT NULL,"
  204. + " RangDisplay varchar(100) NOT NULL,"
  205. + " Coins int NOT NULL,"
  206. + " Level int NOT NULL,"
  207. + " XP int NOT NULL,"
  208. + " OnlineTime int NOT NULL,"
  209. + " Server varchar(100) NOT NULL,"
  210. + " Online boolean NOT NULL DEFAULT false)");
  211. ps.execute();
  212. } catch (SQLException e) {
  213. e.printStackTrace();
  214. }
  215. }
  216.  
  217. public static void setOnlineTime(UUID uuid, int name){
  218. if(containsPlayer(uuid)){
  219. try {
  220. PreparedStatement ps = MySQL.getConnection().prepareStatement("UPDATE Spieler SET OnlineTime = ? WHERE UUID = ?");
  221. ps.setInt(1, name);
  222. ps.setString(2, uuid.toString());
  223. ps.executeUpdate();
  224. } catch (SQLException e) {
  225. e.printStackTrace();
  226. }
  227. }
  228. }
  229.  
  230. public static void setServer(UUID uuid, String name){
  231. if(containsPlayer(uuid)){
  232. try {
  233. PreparedStatement ps = MySQL.getConnection().prepareStatement("UPDATE Spieler SET Server = ? WHERE UUID = ?");
  234. ps.setString(1, name);
  235. ps.setString(2, uuid.toString());
  236. ps.executeUpdate();
  237. } catch (SQLException e) {
  238. e.printStackTrace();
  239. }
  240. }
  241. }
  242.  
  243. public static UUID getUUID(String name){
  244. try {
  245. PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT UUID FROM Spieler WHERE Name = ?");
  246. ps.setString(1, name);
  247. ResultSet rs = ps.executeQuery();
  248. while(rs.next()){
  249. return UUID.fromString(rs.getString("UUID"));
  250. }
  251. } catch (SQLException e) {
  252. e.printStackTrace();
  253. }
  254. return null;
  255. }
  256.  
  257. public static String getServer(UUID uuid){
  258. if(containsPlayer(uuid)){
  259.  
  260. try {
  261. PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT Server FROM Spieler WHERE UUID = ?");
  262. ps.setString(1, uuid.toString());
  263. ResultSet rs = ps.executeQuery();
  264. while(rs.next()){
  265. return rs.getString("Server");
  266. }
  267. } catch (SQLException e) {
  268. e.printStackTrace();
  269. }
  270. }
  271. return null;
  272. }
  273.  
  274. public static Integer getOnlineTime(UUID uuid){
  275. if(containsPlayer(uuid)){
  276.  
  277. try {
  278. PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT OnlineTime FROM Spieler WHERE UUID = ?");
  279. ps.setString(1, uuid.toString());
  280. ResultSet rs = ps.executeQuery();
  281. while(rs.next()){
  282. return rs.getInt("OnlineTime");
  283. }
  284. } catch (SQLException e) {
  285. e.printStackTrace();
  286. }
  287. }
  288. return null;
  289. }
  290.  
  291.  
  292. public static boolean containsPlayer(UUID uuid) {
  293. try {
  294. PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT UUID FROM Spieler WHERE UUID = ?");
  295. ps.setString(1, uuid.toString());
  296. ResultSet rs = ps.executeQuery();
  297. return rs.next();
  298. } catch (SQLException e) {
  299. e.printStackTrace();
  300. }
  301. return false;
  302. }
  303.  
  304.  
  305. public static String getName(UUID uuid){
  306. if(containsPlayer(uuid)){
  307.  
  308. try {
  309. PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT Name FROM Spieler WHERE UUID = ?");
  310. ps.setString(1, uuid.toString());
  311. ResultSet rs = ps.executeQuery();
  312. while(rs.next()){
  313. return rs.getString("Name");
  314. }
  315. } catch (SQLException e) {
  316. e.printStackTrace();
  317. }
  318. }
  319. return null;
  320. }
  321.  
  322. public static Rank getRank(UUID uuid){
  323. if(containsPlayer(uuid)){
  324.  
  325. try {
  326. PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT Rang FROM Spieler WHERE UUID = ?");
  327. ps.setString(1, uuid.toString());
  328. ResultSet rs = ps.executeQuery();
  329. while(rs.next()){
  330. return Rank.valueOf(rs.getString("Rang"));
  331. }
  332. } catch (SQLException e) {
  333. e.printStackTrace();
  334. }
  335. }
  336. return null;
  337. }
  338.  
  339. public static Rank getRankDisplay(UUID uuid){
  340. if(containsPlayer(uuid)){
  341.  
  342. try {
  343. PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT RangDisplay FROM Spieler WHERE UUID = ?");
  344. ps.setString(1, uuid.toString());
  345. ResultSet rs = ps.executeQuery();
  346. while(rs.next()){
  347. return Rank.valueOf(rs.getString("RangDisplay"));
  348. }
  349. } catch (SQLException e) {
  350. e.printStackTrace();
  351. }
  352. }
  353. return null;
  354. }
  355.  
  356. public static Integer getCoins(UUID uuid){
  357. if(containsPlayer(uuid)){
  358.  
  359. try {
  360. PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT Coins FROM Spieler WHERE UUID = ?");
  361. ps.setString(1, uuid.toString());
  362. ResultSet rs = ps.executeQuery();
  363. while(rs.next()){
  364. return rs.getInt("Coins");
  365. }
  366. } catch (SQLException e) {
  367. e.printStackTrace();
  368. }
  369. }
  370. return 0;
  371. }
  372.  
  373. public static Integer getLevel(UUID uuid){
  374. if(containsPlayer(uuid)){
  375.  
  376. try {
  377. PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT Level FROM Spieler WHERE UUID = ?");
  378. ps.setString(1, uuid.toString());
  379. ResultSet rs = ps.executeQuery();
  380. while(rs.next()){
  381. return rs.getInt("Level");
  382. }
  383. } catch (SQLException e) {
  384. e.printStackTrace();
  385. }
  386. }
  387. return 0;
  388. }
  389.  
  390. public static Integer getXP(UUID uuid){
  391. if(containsPlayer(uuid)){
  392.  
  393. try {
  394. PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT XP FROM Spieler WHERE UUID = ?");
  395. ps.setString(1, uuid.toString());
  396. ResultSet rs = ps.executeQuery();
  397. while(rs.next()){
  398. return rs.getInt("XP");
  399. }
  400. } catch (SQLException e) {
  401. e.printStackTrace();
  402. }
  403. }
  404. return 0;
  405. }
  406.  
  407. public static boolean isOnline(UUID uuid){
  408. if(containsPlayer(uuid)){
  409.  
  410. try {
  411. PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT Online FROM Spieler WHERE UUID = ?");
  412. ps.setString(1, uuid.toString());
  413. ResultSet rs = ps.executeQuery();
  414. while(rs.next()){
  415. return rs.getBoolean("Online");
  416. }
  417. } catch (SQLException e) {
  418. e.printStackTrace();
  419. }
  420. }
  421. return false;
  422. }
  423.  
  424. public static void setName(UUID uuid, String name){
  425. if(containsPlayer(uuid)){
  426. try {
  427. PreparedStatement ps = MySQL.getConnection().prepareStatement("UPDATE Spieler SET Name = ? WHERE UUID = ?");
  428. ps.setString(1, name);
  429. ps.setString(2, uuid.toString());
  430. ps.executeUpdate();
  431. } catch (SQLException e) {
  432. e.printStackTrace();
  433. }
  434. }
  435. }
  436.  
  437. public static void setRank(UUID uuid, String rank){
  438. if(containsPlayer(uuid)){
  439. try {
  440. PreparedStatement ps = MySQL.getConnection().prepareStatement("UPDATE Spieler SET Rang = ? WHERE UUID = ?");
  441. ps.setString(1, rank);
  442. ps.setString(2, uuid.toString());
  443. ps.executeUpdate();
  444. } catch (SQLException e) {
  445. e.printStackTrace();
  446. }
  447. }
  448. }
  449.  
  450. public static void setRankDisplay(UUID uuid, String rank){
  451. if(containsPlayer(uuid)){
  452. try {
  453. PreparedStatement ps = MySQL.getConnection().prepareStatement("UPDATE Spieler SET RangDisplay = ? WHERE UUID = ?");
  454. ps.setString(1, rank);
  455. ps.setString(2, uuid.toString());
  456. ps.executeUpdate();
  457. } catch (SQLException e) {
  458. e.printStackTrace();
  459. }
  460. }
  461. }
  462.  
  463. public static void setCoins(UUID uuid, int coins){
  464. if(containsPlayer(uuid)){
  465. try {
  466. PreparedStatement ps = MySQL.getConnection().prepareStatement("UPDATE Spieler SET Coins = ? WHERE UUID = ?");
  467. ps.setInt(1, coins);
  468. ps.setString(2, uuid.toString());
  469. ps.executeUpdate();
  470. } catch (SQLException e) {
  471. e.printStackTrace();
  472. }
  473. }
  474. }
  475.  
  476. public static void setLevel(UUID uuid, int level){
  477. if(containsPlayer(uuid)){
  478. try {
  479. PreparedStatement ps = MySQL.getConnection().prepareStatement("UPDATE Spieler SET Level = ? WHERE UUID = ?");
  480. ps.setInt(1, level);
  481. ps.setString(2, uuid.toString());
  482. ps.executeUpdate();
  483. } catch (SQLException e) {
  484. e.printStackTrace();
  485. }
  486. }
  487. }
  488.  
  489. public static void setXP(UUID uuid, int XP){
  490. if(containsPlayer(uuid)){
  491. try {
  492. PreparedStatement ps = MySQL.getConnection().prepareStatement("UPDATE Spieler SET XP = ? WHERE UUID = ?");
  493. ps.setInt(1, XP);
  494. ps.setString(2, uuid.toString());
  495. ps.executeUpdate();
  496. } catch (SQLException e) {
  497. e.printStackTrace();
  498. }
  499. }
  500. }
  501.  
  502. public static void setOnline(UUID uuid, boolean online){
  503. if(containsPlayer(uuid)){
  504. try {
  505. PreparedStatement ps = MySQL.getConnection().prepareStatement("UPDATE Spieler SET Online = ? WHERE UUID = ?");
  506. ps.setBoolean(1, online);
  507. ps.setString(2, uuid.toString());
  508. ps.executeUpdate();
  509. } catch (SQLException e) {
  510. e.printStackTrace();
  511. }
  512. }
  513. }
  514.  
  515. public static void update(UUID uuid, String Name, String Rang, String RangDisplay, int Coins, int Level, int XP, String Server, int Time, boolean Online) {
  516.  
  517. try {
  518.  
  519. if (containsPlayer(uuid)) {
  520.  
  521. PreparedStatement ps = MySQL.getConnection().prepareStatement("UPDATE Spieler SET Name = ? WHERE UUID = ?");
  522. ps.setString(1, Name);
  523. ps.setString(2, uuid.toString());
  524. ps.executeUpdate();
  525.  
  526. PreparedStatement ps2 = MySQL.getConnection().prepareStatement("UPDATE Spieler SET Rang = ? WHERE UUID = ?");
  527. ps2.setString(1, Rang);
  528. ps2.setString(2, uuid.toString());
  529. ps2.executeUpdate();
  530.  
  531. PreparedStatement ps9 = MySQL.getConnection().prepareStatement("UPDATE Spieler SET RangDisplay = ? WHERE UUID = ?");
  532. ps9.setString(1, RangDisplay);
  533. ps9.setString(2, uuid.toString());
  534. ps9.executeUpdate();
  535.  
  536. PreparedStatement ps3 = MySQL.getConnection().prepareStatement("UPDATE Spieler SET Coins = ? WHERE UUID = ?");
  537. ps3.setInt(1, Coins);
  538. ps3.setString(2, uuid.toString());
  539. ps3.executeUpdate();
  540.  
  541. PreparedStatement ps4 = MySQL.getConnection().prepareStatement("UPDATE Spieler SET Level = ? WHERE UUID = ?");
  542. ps4.setInt(1, Level);
  543. ps4.setString(2, uuid.toString());
  544. ps4.executeUpdate();
  545.  
  546. PreparedStatement ps5 = MySQL.getConnection().prepareStatement("UPDATE Spieler SET XP = ? WHERE UUID = ?");
  547. ps5.setInt(1, XP);
  548. ps5.setString(2, uuid.toString());
  549. ps5.executeUpdate();
  550.  
  551. PreparedStatement ps8 = MySQL.getConnection().prepareStatement("UPDATE Spieler SET Server = ? WHERE UUID = ?");
  552. ps8.setString(1, Server);
  553. ps8.setString(2, uuid.toString());
  554. ps8.executeUpdate();
  555.  
  556. PreparedStatement ps11 = MySQL.getConnection().prepareStatement("UPDATE Spieler SET OnlineTime = ? WHERE UUID = ?");
  557. ps11.setInt(1, Time);
  558. ps11.setString(2, uuid.toString());
  559. ps11.executeUpdate();
  560.  
  561. PreparedStatement ps10 = MySQL.getConnection().prepareStatement("UPDATE Spieler SET Online = ? WHERE UUID = ?");
  562. ps10.setBoolean(1, Online);
  563. ps10.setString(2, uuid.toString());
  564. ps10.executeUpdate();
  565.  
  566. } else {
  567. PreparedStatement ps = MySQL.getConnection()
  568. .prepareStatement("INSERT INTO Spieler (UUID, Name, Rang, RangDisplay, Coins, Level, XP, OnlineTime, Server, Online) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  569. ps.setString(1, uuid.toString());
  570. ps.setString(2, Name);
  571. ps.setString(3, Rang);
  572. ps.setString(4, RangDisplay);
  573. ps.setInt(5, Coins);
  574. ps.setInt(6, Level);
  575. ps.setInt(7, XP);
  576. ps.setInt(8, Time);
  577. ps.setString(9, Server);
  578. ps.setBoolean(10, Online);
  579. ps.execute();
  580. }
  581.  
  582. } catch (SQLException e) {
  583. e.printStackTrace();
  584. }
  585.  
  586. }
  587.  
  588. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement