Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. package fr.enoviah.brumalore;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.command.CommandExecutor;
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.event.EventHandler;
  7. import org.bukkit.event.Listener;
  8. import org.bukkit.event.player.PlayerInteractEntityEvent;
  9. import org.bukkit.plugin.java.JavaPlugin;
  10.  
  11. import java.sql.*;
  12. import java.util.UUID;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. public final class Brumalore extends JavaPlugin implements Listener, CommandExecutor {
  17.  
  18. public static Brumalore instance;
  19.  
  20. Connection con = null;
  21. Statement st = null;
  22. ResultSet rs = null;
  23.  
  24. String url = "jdbc:mysql://ssh.enoviah.fr:3306/burmazur";
  25. String user = "testorder";
  26. String password = "abcdabcd";
  27.  
  28. public static Brumalore getInstance() {
  29. return (instance);
  30. }
  31.  
  32. @Override
  33. public void onEnable() {
  34. this.instance = this;
  35. this.getLogger().info("Enabled.");
  36. Bukkit.getServer().getPluginManager().registerEvents(this, this);
  37. getCommand("lore").setExecutor(new CommandLore());
  38. initConnector();
  39. }
  40.  
  41. @Override
  42. public void onDisable() {
  43. this.getLogger().info("Disabled.");
  44. }
  45.  
  46. @EventHandler
  47. public void PlayerRightClick(PlayerInteractEntityEvent event) {
  48. if (event.getRightClicked() instanceof Player) {
  49. Player clicked = (Player) event.getRightClicked();
  50. printLore(event.getRightClicked().getUniqueId(), event.getPlayer());
  51. }
  52. }
  53.  
  54. public void createLore(String lore, UUID uuid) {
  55. try {
  56. lore = lore.replaceAll("&", "§");
  57. if (isPresent(uuid.toString()) == false) {
  58. st.executeUpdate("INSERT INTO `burmazur`.`brumalore` (`UUID`, `lore`) VALUES (\"" + uuid.toString() + "\",\"" + lore + "\")");
  59. } else
  60. st.executeUpdate("UPDATE `burmazur`.`brumalore` SET `lore` = \"" + lore + "\" WHERE UUID = \"" + uuid.toString() + "\"");
  61. initConnector();
  62. } catch (SQLException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66.  
  67. private boolean isPresent(String uuid) {
  68. if (this.rs != null) {
  69. try {
  70. while (this.rs.next()) {
  71. if (this.rs.getString(2).equals(uuid)) {
  72. return (true);
  73. }
  74. }
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. try {
  80. this.rs.beforeFirst();
  81. } catch (SQLException e) {
  82. e.printStackTrace();
  83. }
  84. return (false);
  85. }
  86.  
  87. private void initConnector() {
  88. try {
  89. // table : brumalore ; content : (String UUID, String Text)
  90. con = DriverManager.getConnection(url, user, password);
  91. st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  92. ResultSet.CONCUR_READ_ONLY);
  93. rs = st.executeQuery("SELECT * FROM brumalore");
  94. } catch (SQLException ex) {
  95. Logger lgr = this.getLogger();
  96. lgr.log(Level.SEVERE, ex.getMessage(), ex);
  97. }
  98. }
  99.  
  100. private void printLore(UUID uuid, Player p) {
  101. boolean sended = false;
  102. if (this.rs != null) {
  103. try {
  104. while (this.rs.next()) {
  105. if (this.rs.getString(2).equals(uuid.toString())) {
  106. p.sendMessage(this.rs.getString(3));
  107. sended = true;
  108. break;
  109. }
  110. }
  111.  
  112. if (!sended)
  113. p.sendMessage("No Lore.");
  114. } catch (SQLException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. try {
  119. this.rs.beforeFirst();
  120. } catch (SQLException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement