Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.konsi.blockregen2;
- import java.sql.*;
- import org.bukkit.Bukkit;
- import org.bukkit.CropState;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- public class SQLManager {
- public Connection connection;
- private BlockRegen2 plugin;
- public SQLManager(final BlockRegen2 plugin) {
- this.plugin = plugin;
- }
- private void openConnection(){
- try {
- 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());
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void closeConnection(){
- try {
- this.connection.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void initDatabase(){
- this.openConnection();
- try {
- 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`)) ;");
- sql.execute();
- sql.close();
- } catch (Exception e) {
- e.printStackTrace();
- return;
- } finally {
- closeConnection();
- }
- }
- public void removeItem(int id){
- try{
- PreparedStatement sql = connection.prepareStatement("DELETE FROM `BlockRegen2-Blocks` WHERE `ID`=?;");
- sql.setInt(1, id);
- sql.execute();
- sql.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- public void insertBlock(String name, int x, int y, int z, String world, int respawntime){
- openConnection();
- System.out.print("One "+name+" mined in "+world);
- try{
- PreparedStatement sql = connection.prepareStatement("INSERT INTO `BlockRegen2-Blocks` (`id`, `name`, `respawntime`, `x`, `y`, `z`, `world`) VALUES (?,?,?,?,?,?,?);");
- sql.setInt(1, nextID());
- sql.setString(2, name);
- sql.setInt(3, respawntime);
- sql.setInt(4, x);
- sql.setInt(5, y);
- sql.setInt(6, z);
- sql.setString(7, world);
- sql.execute();
- sql.close();
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- closeConnection();
- }
- }
- @SuppressWarnings("deprecation")
- public void check(){
- openConnection();
- try{
- PreparedStatement sql1 = connection.prepareStatement("SELECT * FROM `BlockRegen2-Blocks`;");
- ResultSet rs1 = sql1.executeQuery();
- while(rs1.next()){
- PreparedStatement sql3 = connection.prepareStatement("UPDATE `BlockRegen2-Blocks` SET `respawntime`=? WHERE `id`=?;");
- sql3.setInt(1, rs1.getInt("respawntime") - 4);
- sql3.setInt(2, rs1.getInt("id"));
- sql3.executeUpdate();
- if(rs1.getInt("respawntime") - 4 <= 0){
- Location loc = new Location(Bukkit.getWorld(rs1.getString("world")), rs1.getInt("x"), rs1.getInt("y"), rs1.getInt("z"));
- Block block = loc.getBlock();
- String name =rs1.getString("name");
- if(name.equals("ironore")){
- plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
- public void run() {
- block.setType(Material.IRON_ORE);
- }
- }, 1);
- }
- if(name.equals("goldore")){
- plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
- public void run() {
- block.setType(Material.GOLD_ORE);
- }
- }, 1);
- }
- if(name.equals("redstoneore")){
- plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
- public void run() {
- block.setType(Material.REDSTONE_ORE);
- }
- }, 1);
- }
- if(name.equals("diamondore")){
- plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
- public void run() {
- block.setType(Material.DIAMOND_ORE);
- }
- }, 1);
- }
- if(name.equals("coalore")){
- plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
- public void run() {
- block.setType(Material.COAL_ORE);
- }
- }, 1);
- }
- if(name.equals("coalore")){
- plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
- public void run() {
- block.setType(Material.COAL_ORE);
- }
- }, 1);
- }
- if(name.equals("blueberry")){
- block.setType(Material.CROPS);
- block.setData(CropState.GERMINATED.getData());
- }
- if(name.equals("strawberry")){
- block.setType(Material.CROPS);
- block.setData(CropState.SMALL.getData());
- }
- if (name.equals("strawberry")){
- block.setType(Material.CROPS);
- block.setData(CropState.TALL.getData());
- }
- this.removeItem(rs1.getInt("id"));
- }
- sql3.close();
- }
- rs1.close();
- sql1.close();
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- closeConnection();
- }
- }
- public int nextID(){
- int data = 0;
- try{
- PreparedStatement sql = connection.prepareStatement("SELECT `ID` FROM `BlockRegen2-Blocks` ORDER BY `ID` DESC LIMIT 1;");
- ResultSet rs = sql.executeQuery();
- if(rs.next()){
- data = rs.getInt("id") + 1;
- }
- rs.close();
- sql.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- return data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement