· 6 years ago · Jul 08, 2019, 06:20 PM
1package com.systly.coinsapi.mysql;
2
3import com.systly.coinsapi.Coins;
4import com.systly.core.Core;
5import lombok.Getter;
6import org.bukkit.entity.Player;
7
8import java.sql.*;
9import java.util.UUID;
10
11public class MySQL {
12
13 @Getter
14 private Connection connection;
15 @Getter
16 private boolean isConnected = connection != null;
17
18 private void connect(){
19 if (!isConnected()) {
20 try {
21 connection = DriverManager.getConnection("jdbc:mysql://" + Core.getInstance().getMySQLCredential().getHostname()
22 + ":" + Core.getInstance().getMySQLCredential().getPort() + "/" + Coins.getInstance().getCoinsConfiguration().getDatabaseName()
23 + "," + Core.getInstance().getMySQLCredential().getUser() + "," + Core.getInstance().getMySQLCredential().getPassword());
24 } catch (final SQLException exception) {
25 exception.printStackTrace();
26 }
27 }
28 }
29
30 private void disconnect(){
31 if (isConnected()) {
32 try {
33 connection.close();
34 } catch (final SQLException exception) {
35 exception.printStackTrace();
36 }
37 }
38 }
39
40 private void createTable(){
41 if (isConnected()) {
42 try {
43 final PreparedStatement preparedStatement = connection.prepareStatement(
44 "CREATE TABLE IF NOT EXISTS " + Coins.getInstance().getCoinsConfiguration().getDatabaseName()
45 + " (Spielername VARCHAR(100), UUID VARCHAR(100), Coins INT(100))");
46 preparedStatement.executeQuery();
47 preparedStatement.close();
48 } catch (final SQLException exception) {
49 exception.printStackTrace();
50 }
51 }
52 }
53
54 private boolean playerExists(final UUID uuid){
55 try {
56 final PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM " +
57 Coins.getInstance().getCoinsConfiguration().getDatabaseName() + " WHERE UUID = ?");
58 preparedStatement.setString(1, uuid.toString());
59 final ResultSet resultSet = preparedStatement.executeQuery();
60 final boolean exists = resultSet.next();
61 resultSet.close();
62 preparedStatement.close();
63 return exists;
64 } catch (final Exception exception) {
65 exception.printStackTrace();
66 return false;
67 }
68 }
69
70 private void registerPlayer(final UUID uuid, final String name){
71 if (!playerExists(uuid)) {
72 try {
73 final PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO " +
74 Coins.getInstance().getCoinsConfiguration().getDatabaseName()
75 + " (Playername, UUID, Coins) VALUES (?, ?, ?)");
76 preparedStatement.setString(1, name);
77 preparedStatement.setString(2, uuid.toString());
78 preparedStatement.setInt(3, Coins.getInstance().getCoinsConfiguration().getStartCoins());
79 preparedStatement.execute();
80 preparedStatement.close();
81 } catch (final Exception exception) {
82 exception.printStackTrace();
83 }
84 }
85 }
86
87 private int getCoins(final Player player){
88 try {
89 final PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM " +
90 Coins.getInstance().getCoinsConfiguration().getDatabaseName() + " WHERE UUID = ?");
91 preparedStatement.setString(1, player.getUniqueId().toString());
92 final ResultSet resultSet = preparedStatement.executeQuery();
93 resultSet.next();
94 int coins = resultSet.getInt("Coins");
95 resultSet.close();
96 preparedStatement.close();
97 return coins;
98 } catch (final Exception exception) {
99 exception.printStackTrace();
100 return -1;
101 }
102 }
103
104 private int getCoins(final UUID uuid){
105 try {
106 final PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM " +
107 Coins.getInstance().getCoinsConfiguration().getDatabaseName() + " WHERE UUID = ?");
108 preparedStatement.setString(1, uuid.toString());
109 final ResultSet resultSet = preparedStatement.executeQuery();
110 resultSet.next();
111 int coins = resultSet.getInt("Coins");
112 resultSet.close();
113 preparedStatement.close();
114 return coins;
115 } catch (final Exception exception) {
116 exception.printStackTrace();
117 return -1;
118 }
119 }
120
121 public void setCoins(final Player player, int coins){
122 try {
123 final PreparedStatement preparedStatement = connection.prepareStatement("UPDATE " +
124 Coins.getInstance().getCoinsConfiguration().getDatabaseName()
125 + " SET Coins = ? WHERE UUID = ?");
126 preparedStatement.setInt(1, coins);
127 preparedStatement.setString(2, player.getUniqueId().toString());
128 preparedStatement.executeUpdate();
129 preparedStatement.close();
130 } catch (Exception var3) {
131 var3.printStackTrace();
132 }
133 }
134
135
136 public void setCoins(final UUID uuid, int coins){
137 try {
138 final PreparedStatement preparedStatement = connection.prepareStatement("UPDATE " +
139 Coins.getInstance().getCoinsConfiguration().getDatabaseName()
140 + " SET Coins = ? WHERE UUID = ?");
141 preparedStatement.setInt(1, coins);
142 preparedStatement.setString(2, uuid.toString());
143 preparedStatement.executeUpdate();
144 preparedStatement.close();
145 } catch (Exception var3) {
146 var3.printStackTrace();
147 }
148 }
149
150 public void addCoins(final Player player, int coins){
151 setCoins(player, getCoins(player) + coins);
152 }
153
154 public void addCoins(final UUID uuid, int coins){
155 setCoins(uuid, getCoins(uuid) + coins);
156 }
157
158 public void removeCoins(final Player player, int coins){
159 setCoins(player, getCoins(player) + coins);
160 }
161
162 public void removeCoins(final UUID uuid, int coins){
163 setCoins(uuid, getCoins(uuid) + coins);
164 }
165}