· 9 years ago · Jan 26, 2017, 06:00 PM
1package tk.hunterpvp.mysql;
2
3import java.sql.ResultSet;
4import java.sql.SQLException;
5import java.sql.Statement;
6import java.util.logging.Level;
7
8import org.bukkit.entity.Player;
9
10import tk.hunterpvp.Main;
11
12public class MySQLManager {
13
14 private static MySQL db;
15 public Main plugin;
16
17 public MySQLManager(Main plugin2) {
18 this.plugin = plugin2;
19 }
20
21 public void setupRanking() throws SQLException {
22 if (Main.plugin.getConfig().getBoolean("mysql.ativo")) {
23 db = new MySQL(this.plugin, Main.plugin.getConfig().getString("mysql.host-name"),
24 Main.plugin.getConfig().getString("mysql.porta"),
25 Main.plugin.getConfig().getString("mysql.database"),
26 Main.plugin.getConfig().getString("mysql.usuario"),
27 Main.plugin.getConfig().getString("mysql.senha"));
28 db.openConnection();
29 Statement statement = db.getConnection().createStatement();
30 statement.executeUpdate(
31 "CREATE TABLE IF NOT EXISTS `hunterpvps` (`nome` varchar(32), `kills` int, `deaths` int, `xp` int, `rank` varchar(32))");
32
33 plugin.database = true;
34 }
35 }
36
37 public void closeDB() {
38 db.closeConnection();
39 }
40
41 public static void join(Player p) throws SQLException {
42 if (Main.plugin.getConfig().getBoolean("mysql.ativo")) {
43 String name = p.getName();
44 if (!db.checkConnection()) {
45 db.openConnection();
46 }
47 Statement s = db.getConnection().createStatement();
48
49 ResultSet rs = s.executeQuery("SELECT * FROM hunterpvps WHERE `nome`='" + name + "';");
50 if (rs.next()) {
51 return;
52 }
53 s.executeUpdate("INSERT INTO hunterpvps (`nome`, `kills`, `deaths`, `xp`, `rank`) VALUES ('" + name
54 + "', '0','0', '100', '§7Noob');");
55 Main.plugin.getLogger().log(Level.INFO,
56 "Os dados do jogador " + name + " foram inseridos com sucesso na database !");
57 }
58 }
59
60 public static int getKills(Player p) throws SQLException {
61 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
62 return 0;
63 }
64 String name = p.getName();
65 if (!db.checkConnection()) {
66 db.openConnection();
67 }
68 Statement s = db.getConnection().createStatement();
69 ResultSet rs = s.executeQuery("SELECT * FROM hunterpvps WHERE `nome`='" + name + "';");
70 if (!rs.next()) {
71 return 0;
72 }
73 int retorno = rs.getInt("kills");
74
75 return retorno;
76 }
77
78 public static int getDeaths(Player p) throws SQLException {
79 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
80 return 0;
81 }
82 String name = p.getName();
83 if (!db.checkConnection()) {
84 db.openConnection();
85 }
86 Statement s = db.getConnection().createStatement();
87 ResultSet rs = s.executeQuery("SELECT * FROM hunterpvps WHERE `nome`='" + name + "';");
88 if (!rs.next()) {
89 return 0;
90 }
91 int retorno = rs.getInt("deaths");
92
93 return retorno;
94 }
95
96 public static int getXP(Player p) throws SQLException {
97 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
98 return 0;
99 }
100 String name = p.getName();
101 if (!db.checkConnection()) {
102 db.openConnection();
103 }
104 Statement s = db.getConnection().createStatement();
105 ResultSet rs = s.executeQuery("SELECT * FROM hunterpvps WHERE `nome`='" + name + "';");
106 if (!rs.next()) {
107 return 0;
108 }
109 int retorno = rs.getInt("xp");
110
111 return retorno;
112 }
113
114 public static String getRank(Player p) throws SQLException {
115 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
116 return "§7Noob";
117 }
118 String name = p.getName();
119 if (!db.checkConnection()) {
120 db.openConnection();
121 }
122 Statement s = db.getConnection().createStatement();
123 ResultSet rs = s.executeQuery("SELECT * FROM hunterpvps WHERE `nome`='" + name + "';");
124 if (!rs.next()) {
125 return "§7Noob";
126 }
127 String retorno = rs.getString("rank");
128
129 return retorno;
130 }
131
132 public static void getTop3Kills() {
133 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
134 } else {
135 if (!db.checkConnection()) {
136 db.openConnection();
137 }
138 Statement s = null;
139 try {
140 s = db.getConnection().createStatement();
141 } catch (SQLException e) {
142 e.printStackTrace();
143 }
144 ResultSet rs = null;
145 try {
146 rs = s.executeQuery("SELECT * FROM hunterpvps ORDER BY kills DESC LIMIT 3");
147 } catch (SQLException e) {
148 e.printStackTrace();
149 }
150 try {
151 while (rs.next()) {
152 Main.topkills.put(rs.getString("nome"), rs.getInt("kills"));
153 }
154 } catch (SQLException e) {
155 e.printStackTrace();
156 }
157 }
158 }
159
160 //
161 // Sets...
162 //
163
164 public static void setKills(Player p, int quantidade) throws SQLException {
165 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
166 Main.plugin.getLogger().log(Level.WARNING, "Nao foi possivel salvar as Kills do jogador " + p.getName()
167 + " na database " + "porque o MySQL esta desativado !");
168 return;
169 }
170 String name = p.getName();
171 if (!db.checkConnection()) {
172 db.openConnection();
173 }
174 Statement s = db.getConnection().createStatement();
175 s.executeUpdate("UPDATE hunterpvps SET `kills`='" + quantidade + "' WHERE `nome`='" + name + "';");
176 }
177
178 public static void setDeaths(Player p, int quantidade) throws SQLException {
179 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
180 Main.plugin.getLogger().log(Level.WARNING, "Nao foi possivel salvar as Deaths do jogador " + p.getName()
181 + " na database " + "porque o MySQL esta desativado !");
182 return;
183 }
184 String name = p.getName();
185 if (!db.checkConnection()) {
186 db.openConnection();
187 }
188 Statement s = db.getConnection().createStatement();
189 s.executeUpdate("UPDATE hunterpvps SET `deaths`='" + quantidade + "' WHERE `nome`='" + name + "';");
190 }
191
192 public static void setXP(Player p, int quantidade) throws SQLException {
193 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
194 Main.plugin.getLogger().log(Level.WARNING, "Nao foi possivel salvar o XP do jogador " + p.getName()
195 + " na database " + "porque o MySQL esta desativado !");
196 return;
197 }
198 String name = p.getName();
199 if (!db.checkConnection()) {
200 db.openConnection();
201 }
202 Statement s = db.getConnection().createStatement();
203 s.executeUpdate("UPDATE hunterpvps SET `xp`='" + quantidade + "' WHERE `nome`='" + name + "';");
204 }
205
206 public static void setRank(Player p, String rank) throws SQLException {
207 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
208 Main.plugin.getLogger().log(Level.WARNING, "Nao foi possivel salvar o Rank do jogador " + p.getName()
209 + " na database " + "porque o MySQL esta desativado !");
210 return;
211 }
212 String name = p.getName();
213 if (!db.checkConnection()) {
214 db.openConnection();
215 }
216 Statement s = db.getConnection().createStatement();
217 s.executeUpdate("UPDATE hunterpvps SET `rank`='" + rank + "' WHERE `nome`='" + name + "';");
218 }
219
220}