· 8 years ago · Oct 06, 2017, 03:46 AM
1package com.amoebaman.BattleVaultAssault;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.sql.SQLException;
8import java.sql.Statement;
9
10import org.bukkit.Location;
11
12//Literally 90% of this code was taken from zonedabone, but I'm learning from it :P
13public class MySQLController {
14
15 public Connection connection;
16 public final String new_table_vault_info = "CREATE TABLE `test`.`vault_info` (`id` INT NOT NULL AUTO_INCREMENT ,`name` VARCHAR(45) NOT NULL ,`attrespx` INT NOT NULL ,`attrespy` INT NOT NULL ,`attrespz` INT NOT NULL ,`defrespx` INT NOT NULL ,`defrespy` INT NOT NULL ,`defrespz` INT NOT NULL ,`capblockx` INT NOT NULL ,`capblocky` INT NOT NULL ,`capblockz` INT NOT NULL ,`vault_infocol` INT NOT NULL ,PRIMARY KEY (`id`) ,UNIQUE INDEX `name_UNIQUE` (`name` ASC) );";
17 public String url;
18 public String port;
19 public String username;
20 public String password;
21 public BattleVaultAssault plugin;
22 public boolean debug;
23
24 public MySQLController(String url, String port, String username, String password, BattleVaultAssault plugin, boolean debug){
25 this.url = url;
26 this.port = port;
27 this.username = username;
28 this.password = password;
29 this.plugin = plugin;
30 this.debug = debug;
31 load();
32 }
33
34 public boolean load(){
35 try {
36 Class.forName("com.mysql.jdbc.Driver");
37 if (debug) System.out.println("Got Driver");
38 }
39 catch (ClassNotFoundException e1) {
40 System.err.println("Failed getting driver");
41 e1.printStackTrace();
42 return false;
43 }
44 String strStmt = "CREATE DATABASE IF NOT EXISTS vault_info";
45 try {
46 connection = DriverManager.getConnection("jdbc:mysql://"+url+":" + port, username, password);
47 Statement st = connection.createStatement();
48 st.executeUpdate(strStmt);
49 if (debug) System.out.println("Creating database");
50 }
51 catch (SQLException e) {
52 System.err.println("Failed creating database: " + strStmt);
53 e.printStackTrace();
54 return false;
55 }
56 try {
57 connection = DriverManager.getConnection("jdbc:mysql://"+url+":" + port +"/battlesnitch", username, password);
58 } catch (SQLException e) {
59 e.printStackTrace();
60 return false;
61 }
62 try {
63 connection.setAutoCommit(false);
64 }
65 catch (SQLException e) {
66 e.printStackTrace();
67 }
68 return true;
69 }
70
71 public void addVault(Vault vault){
72 try{
73 PreparedStatement ps = connection.prepareStatement("INSERT INTO vault_info (name,attrespx,attrespy,attrespz,defrespx,defrespy,defrespz,capblockx,capblocky,capblockz) (?,?,?,?,?,?,?,?,?,?");
74 ps.setString(1, vault.name);
75 ps.setInt(2, (int) vault.attackerRespawn.x);
76 ps.setInt(3, (int) vault.attackerRespawn.y);
77 ps.setInt(4, (int) vault.attackerRespawn.z);
78 ps.setInt(5, (int) vault.defenderRespawn.x);
79 ps.setInt(6, (int) vault.defenderRespawn.y);
80 ps.setInt(7, (int) vault.defenderRespawn.z);
81 ps.setInt(8, (int) vault.captureBlock.x);
82 ps.setInt(9, (int) vault.captureBlock.y);
83 ps.setInt(10, (int) vault.captureBlock.z);
84 ps.execute();
85 connection.commit();
86 }
87 catch(SQLException e){
88 e.printStackTrace();
89 }
90 }
91
92 public Vault getVault(int id){
93 try{
94 PreparedStatement ps = connection.prepareStatement("SELECT id FROM vault_info WHERE id = ?");
95 ps.setInt(1, id);
96 ResultSet rs = ps.executeQuery();
97 return new Vault(rs.getString("name"),
98 new Location(plugin.world, rs.getInt("attrespx"), rs.getInt("attrespy"), rs.getInt("attrespz")),
99 new Location(plugin.world, rs.getInt("defrespx"), rs.getInt("defrespy"), rs.getInt("defrespz")),
100 new Location(plugin.world, rs.getInt("capblockx"), rs.getInt("capblocky"), rs.getInt("capblockz")));
101 }
102 catch(SQLException e){
103 e.printStackTrace();
104 }
105 return null;
106 }
107
108 public int getNumVaults(){
109 try{
110 PreparedStatement ps = connection.prepareStatement("SELECT id FROM vault_info WHERE id = 1");
111 ResultSet rs = ps.executeQuery();
112 rs.last();
113 return rs.getInt("id");
114 }
115 catch(SQLException e){
116 e.printStackTrace();
117 }
118 return 0;
119 }
120
121 public void removeVault(int id) {
122 try{
123 PreparedStatement ps = connection.prepareStatement("DELETE FROM vault_info WHERE id = ?");
124 ps.setInt(1, id);
125 ps.execute();
126 connection.commit();
127 }
128 catch(SQLException e){
129 e.printStackTrace();
130 }
131 }
132
133}