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