· 9 years ago · Jan 26, 2017, 06:10 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 plugin.database = true;
33 statement.close();
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 rs.close();
58 s.close();
59 }
60 }
61
62 public static int getKills(Player p) throws SQLException {
63 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
64 return 0;
65 }
66 String name = p.getName();
67 if (!db.checkConnection()) {
68 db.openConnection();
69 }
70 Statement s = db.getConnection().createStatement();
71 ResultSet rs = s.executeQuery("SELECT * FROM hunterpvps WHERE `nome`='" + name + "';");
72 if (!rs.next()) {
73 return 0;
74 }
75 int retorno = rs.getInt("kills");
76 rs.close();
77 s.close();
78 return retorno;
79 }
80
81 public static int getDeaths(Player p) throws SQLException {
82 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
83 return 0;
84 }
85 String name = p.getName();
86 if (!db.checkConnection()) {
87 db.openConnection();
88 }
89 Statement s = db.getConnection().createStatement();
90 ResultSet rs = s.executeQuery("SELECT * FROM hunterpvps WHERE `nome`='" + name + "';");
91 if (!rs.next()) {
92 return 0;
93 }
94 int retorno = rs.getInt("deaths");
95 rs.close();
96 s.close();
97 return retorno;
98 }
99
100 public static int getXP(Player p) throws SQLException {
101 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
102 return 0;
103 }
104 String name = p.getName();
105 if (!db.checkConnection()) {
106 db.openConnection();
107 }
108 Statement s = db.getConnection().createStatement();
109 ResultSet rs = s.executeQuery("SELECT * FROM hunterpvps WHERE `nome`='" + name + "';");
110 if (!rs.next()) {
111 return 0;
112 }
113 int retorno = rs.getInt("xp");
114 rs.close();
115 s.close();
116 return retorno;
117 }
118
119 public static String getRank(Player p) throws SQLException {
120 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
121 return "§7Noob";
122 }
123 String name = p.getName();
124 if (!db.checkConnection()) {
125 db.openConnection();
126 }
127 Statement s = db.getConnection().createStatement();
128 ResultSet rs = s.executeQuery("SELECT * FROM hunterpvps WHERE `nome`='" + name + "';");
129 if (!rs.next()) {
130 return "§7Noob";
131 }
132 String retorno = rs.getString("rank");
133 rs.close();
134 s.close();
135 return retorno;
136 }
137
138 public static void getTop3Kills() {
139 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
140 } else {
141 if (!db.checkConnection()) {
142 db.openConnection();
143 }
144 Statement s = null;
145 try {
146 s = db.getConnection().createStatement();
147 } catch (SQLException e) {
148 e.printStackTrace();
149 }
150 ResultSet rs = null;
151 try {
152 rs = s.executeQuery("SELECT * FROM hunterpvps ORDER BY kills DESC LIMIT 3");
153 } catch (SQLException e) {
154 e.printStackTrace();
155 }
156 try {
157 while (rs.next()) {
158 Main.topkills.put(rs.getString("nome"), rs.getInt("kills"));
159 rs.close();
160 s.close();
161 }
162 } catch (SQLException e) {
163 e.printStackTrace();
164 }
165 }
166 }
167
168 //
169 // Sets...
170 //
171
172 public static void setKills(Player p, int quantidade) throws SQLException {
173 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
174 Main.plugin.getLogger().log(Level.WARNING, "Nao foi possivel salvar as Kills do jogador " + p.getName()
175 + " na database " + "porque o MySQL esta desativado !");
176 return;
177 }
178 String name = p.getName();
179 if (!db.checkConnection()) {
180 db.openConnection();
181 }
182 Statement s = db.getConnection().createStatement();
183 s.executeUpdate("UPDATE hunterpvps SET `kills`='" + quantidade + "' WHERE `nome`='" + name + "';");
184 s.close();
185 }
186
187 public static void setDeaths(Player p, int quantidade) throws SQLException {
188 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
189 Main.plugin.getLogger().log(Level.WARNING, "Nao foi possivel salvar as Deaths do jogador " + p.getName()
190 + " na database " + "porque o MySQL esta desativado !");
191 return;
192 }
193 String name = p.getName();
194 if (!db.checkConnection()) {
195 db.openConnection();
196 }
197 Statement s = db.getConnection().createStatement();
198 s.executeUpdate("UPDATE hunterpvps SET `deaths`='" + quantidade + "' WHERE `nome`='" + name + "';");
199 s.close();
200 }
201
202 public static void setXP(Player p, int quantidade) throws SQLException {
203 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
204 Main.plugin.getLogger().log(Level.WARNING, "Nao foi possivel salvar o XP do jogador " + p.getName()
205 + " na database " + "porque o MySQL esta desativado !");
206 return;
207 }
208 String name = p.getName();
209 if (!db.checkConnection()) {
210 db.openConnection();
211 }
212 Statement s = db.getConnection().createStatement();
213 s.executeUpdate("UPDATE hunterpvps SET `xp`='" + quantidade + "' WHERE `nome`='" + name + "';");
214 s.close();
215 }
216
217 public static void setRank(Player p, String rank) throws SQLException {
218 if (!Main.plugin.getConfig().getBoolean("mysql.ativo")) {
219 Main.plugin.getLogger().log(Level.WARNING, "Nao foi possivel salvar o Rank do jogador " + p.getName()
220 + " na database " + "porque o MySQL esta desativado !");
221 return;
222 }
223 String name = p.getName();
224 if (!db.checkConnection()) {
225 db.openConnection();
226 }
227 Statement s = db.getConnection().createStatement();
228 s.executeUpdate("UPDATE hunterpvps SET `rank`='" + rank + "' WHERE `nome`='" + name + "';");
229 s.close();
230 }
231
232}