Advertisement
Guest User

Untitled

a guest
Sep 20th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. package me.konsi.blockregen2;
  2.  
  3. import java.sql.*;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.CropState;
  7. import org.bukkit.Location;
  8. import org.bukkit.Material;
  9. import org.bukkit.block.Block;
  10.  
  11. public class SQLManager {
  12. public Connection connection;
  13. private BlockRegen2 plugin;
  14.  
  15. public SQLManager(final BlockRegen2 plugin) {
  16. this.plugin = plugin;
  17. }
  18. private void openConnection(){
  19. try {
  20. this.connection = DriverManager.getConnection("jdbc:mysql://" + this.plugin.getConfig().getString("MySQL.host") + ":" + this.plugin.getConfig().getString("MySQL.port") + "/" + this.plugin.getConfig().getString("MySQL.database"), new StringBuilder().append(this.plugin.getConfig().getString("MySQL.user")).toString(), new StringBuilder().append(this.plugin.getConfig().getString("MySQL.password")).toString());
  21. }
  22. catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. public void closeConnection(){
  27. try {
  28. this.connection.close();
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. public void initDatabase(){
  34. this.openConnection();
  35. try {
  36. final PreparedStatement sql = connection.prepareStatement("CREATE TABLE IF NOT EXISTS `BlockRegen2-Blocks` (`ID` INT NOT NULL UNIQUE, `name` varchar(100), `respawntime` INT, `x` INT, `y` INT, `z` INT, `world` VARCHAR(255), PRIMARY KEY (`ID`)) ;");
  37. sql.execute();
  38. sql.close();
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. return;
  42. } finally {
  43. closeConnection();
  44. }
  45. }
  46. public void removeItem(int id){
  47. try{
  48. PreparedStatement sql = connection.prepareStatement("DELETE FROM `BlockRegen2-Blocks` WHERE `ID`=?;");
  49. sql.setInt(1, id);
  50. sql.execute();
  51. sql.close();
  52. }catch(Exception e){
  53. e.printStackTrace();
  54. }
  55. }
  56.  
  57. public void insertBlock(String name, int x, int y, int z, String world, int respawntime){
  58. openConnection();
  59. System.out.print("One "+name+" mined in "+world);
  60. try{
  61. PreparedStatement sql = connection.prepareStatement("INSERT INTO `BlockRegen2-Blocks` (`id`, `name`, `respawntime`, `x`, `y`, `z`, `world`) VALUES (?,?,?,?,?,?,?);");
  62. sql.setInt(1, nextID());
  63. sql.setString(2, name);
  64. sql.setInt(3, respawntime);
  65. sql.setInt(4, x);
  66. sql.setInt(5, y);
  67. sql.setInt(6, z);
  68. sql.setString(7, world);
  69. sql.execute();
  70. sql.close();
  71. }catch(Exception e){
  72. e.printStackTrace();
  73. }finally{
  74. closeConnection();
  75. }
  76. }
  77.  
  78. @SuppressWarnings("deprecation")
  79. public void check(){
  80. openConnection();
  81. try{
  82. PreparedStatement sql1 = connection.prepareStatement("SELECT * FROM `BlockRegen2-Blocks`;");
  83. ResultSet rs1 = sql1.executeQuery();
  84. while(rs1.next()){
  85. PreparedStatement sql3 = connection.prepareStatement("UPDATE `BlockRegen2-Blocks` SET `respawntime`=? WHERE `id`=?;");
  86. sql3.setInt(1, rs1.getInt("respawntime") - 4);
  87. sql3.setInt(2, rs1.getInt("id"));
  88. sql3.executeUpdate();
  89. if(rs1.getInt("respawntime") - 4 <= 0){
  90. Location loc = new Location(Bukkit.getWorld(rs1.getString("world")), rs1.getInt("x"), rs1.getInt("y"), rs1.getInt("z"));
  91. Block block = loc.getBlock();
  92. String name =rs1.getString("name");
  93. if(name.equals("ironore")){
  94. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
  95. public void run() {
  96. block.setType(Material.IRON_ORE);
  97. }
  98. }, 1);
  99. }
  100. if(name.equals("goldore")){
  101. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
  102. public void run() {
  103. block.setType(Material.GOLD_ORE);
  104. }
  105. }, 1);
  106. }
  107. if(name.equals("redstoneore")){
  108. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
  109. public void run() {
  110. block.setType(Material.REDSTONE_ORE);
  111. }
  112. }, 1);
  113. }
  114. if(name.equals("diamondore")){
  115. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
  116. public void run() {
  117. block.setType(Material.DIAMOND_ORE);
  118. }
  119. }, 1);
  120. }
  121. if(name.equals("coalore")){
  122. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
  123. public void run() {
  124. block.setType(Material.COAL_ORE);
  125. }
  126. }, 1);
  127. }
  128. if(name.equals("coalore")){
  129. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
  130. public void run() {
  131. block.setType(Material.COAL_ORE);
  132. }
  133. }, 1);
  134. }
  135. if(name.equals("blueberry")){
  136. block.setType(Material.CROPS);
  137. block.setData(CropState.GERMINATED.getData());
  138. }
  139. if(name.equals("strawberry")){
  140. block.setType(Material.CROPS);
  141. block.setData(CropState.SMALL.getData());
  142. }
  143. if (name.equals("strawberry")){
  144. block.setType(Material.CROPS);
  145. block.setData(CropState.TALL.getData());
  146. }
  147. this.removeItem(rs1.getInt("id"));
  148. }
  149. sql3.close();
  150. }
  151. rs1.close();
  152. sql1.close();
  153. }catch(Exception e){
  154. e.printStackTrace();
  155. }finally{
  156. closeConnection();
  157. }
  158. }
  159.  
  160. public int nextID(){
  161. int data = 0;
  162. try{
  163. PreparedStatement sql = connection.prepareStatement("SELECT `ID` FROM `BlockRegen2-Blocks` ORDER BY `ID` DESC LIMIT 1;");
  164. ResultSet rs = sql.executeQuery();
  165. if(rs.next()){
  166. data = rs.getInt("id") + 1;
  167. }
  168. rs.close();
  169. sql.close();
  170. }catch(Exception e){
  171. e.printStackTrace();
  172. }
  173. return data;
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement