/
HomeData.java
231 lines (216 loc) · 5.9 KB
/
HomeData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.asylumsw.bukkit.waypoints;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Location;
/**
*
* @author jonathan
*/
public class HomeData {
public final static String HOME_TABLE = "CREATE TABLE `homeList` ("
+ "`id` INTEGER PRIMARY KEY,"
+ "`player` varchar(255) NOT NULL UNIQUE,"
+ "`x` int NOT NULL, `y` int NOT NULL, `z` int NOT NULL,"
+ "`pitch` smallint NOT NULL,"
+ "`yaw` smallint NOT NULL,"
+ "`world` varchar(255) NOT NULL )";
public static void initTable() {
if (!tableExists()) {
createTable();
}
}
public static HashMap<String, Warp> getHomes() {
HashMap<String, Warp> homeList = new HashMap<String, Warp>();
Connection conn = null;
Statement statement = null;
ResultSet set = null;
Logger log = Logger.getLogger("Minecraft");
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(Waypoints.DATABASE);
statement = conn.createStatement();
set = statement.executeQuery("SELECT player,x,y,z,pitch,yaw,world FROM homeList");
while (set.next()) {
String player = set.getString("player");
Warp home = new Warp(player, set.getInt("x"), set.getInt("y"),
set.getInt("z"), set.getInt("yaw"), set.getInt("pitch"),
set.getString("world"), Waypoint.HOME);
homeList.put(player, home);
}
}
catch (SQLException ex) {
log.log(Level.SEVERE, "[WP:HOMES]: Load exception.");
log.log(Level.INFO, ex.getMessage());
}
catch (ClassNotFoundException e) {
log.log(Level.SEVERE, "[WP:HOMES]: Error loading org.sqlite.JDBC");
}
finally {
try {
if (statement != null) {
statement.close();
}
if (set != null) {
set.close();
}
if (conn != null) {
conn.close();
}
}
catch (SQLException ex) {
log.log(Level.SEVERE, "[WP:HOMES]: Load Exception (on close)");
}
}
return homeList;
}
private static boolean tableExists() {
Connection conn = null;
ResultSet rs = null;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(Waypoints.DATABASE);
DatabaseMetaData dbm = conn.getMetaData();
rs = dbm.getTables(null, null, "homeList", null);
return (!rs.next() ? false : true);
}
catch (SQLException ex) {
Logger log = Logger.getLogger("Minecraft");
log.log(Level.SEVERE, "[WP:HOMES]: Table check exception");
return false;
}
catch (ClassNotFoundException ex2) {
Logger log = Logger.getLogger("Minecraft");
log.log(Level.SEVERE, "[WP:HOMES]: Class Not Found Exception");
return false;
}
finally {
try {
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
catch (SQLException ex) {
Logger log = Logger.getLogger("Minecraft");
log.log(Level.SEVERE, "[WP:HOMES]: Table Check Exception (on closing)");
}
}
}
private static void createTable() {
Connection conn = null;
Statement st = null;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(Waypoints.DATABASE);
st = conn.createStatement();
st.executeUpdate(HOME_TABLE);
}
catch (SQLException e) {
Logger log = Logger.getLogger("Minecraft");
log.log(Level.SEVERE, "[WP:HOMES]: Create Table Exception", e);
}
catch (ClassNotFoundException e) {
Logger log = Logger.getLogger("Minecraft");
log.log(Level.SEVERE, "[WP:HOMES]: Error loading org.sqlite.JDBC");
}
finally {
try {
if (conn != null) {
conn.close();
}
if (st != null) {
st.close();
}
}
catch (SQLException e) {
Logger log = Logger.getLogger("Minecraft");
log.log(Level.SEVERE, "[WP:HOMES]: Could not create the table (on close)");
}
}
}
public static boolean addHome(Warp loc) {
Connection conn = null;
PreparedStatement ps = null;
Logger log = Logger.getLogger("Minecraft");
boolean success = false;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(Waypoints.DATABASE);
ps = conn.prepareStatement("INSERT INTO homeList (player, x, y, z, pitch, yaw, world) "
+ "VALUES (?,?,?,?,?,?,?)");
ps.setString(1, loc.getName());
ps.setInt(2, loc.getX());
ps.setInt(3, loc.getY());
ps.setInt(4, loc.getZ());
ps.setInt(5, (int) loc.getPitch());
ps.setInt(6, (int) loc.getYaw());
ps.setString(7, loc.getWorldName());
ps.executeUpdate();
success = true;
}
catch (SQLException ex) {
log.log(Level.SEVERE, "[WP:HOMES]: Warp Insert Exception", ex);
}
catch (ClassNotFoundException ex2) {
log.log(Level.SEVERE, "[WP:HOMES]: Error loading org.sqlite.JDBC");
}
finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
}
catch (SQLException ex) {
log.log(Level.SEVERE, "[WP:HOMES]: Warp Insert Exception (on close)", ex);
}
}
return success;
}
public static boolean deleteHome(String playerName) {
Connection conn = null;
PreparedStatement ps = null;
Logger log = Logger.getLogger("Minecraft");
boolean success = false;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(Waypoints.DATABASE);
ps = conn.prepareStatement("DELETE FROM homeList WHERE player = ?");
ps.setString(1, playerName);
ps.executeUpdate();
success = true;
}
catch (SQLException ex) {
log.log(Level.SEVERE, "[WP:HOMES]: Warp Delete Exception", ex);
}
catch (ClassNotFoundException ex2) {
log.log(Level.SEVERE, "[WP:HOMES]: Error loading org.sqlite.JDBC");
}
finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
}
catch (SQLException ex) {
log.log(Level.SEVERE, "[WP:HOMES]: Warp Insert Exception (on close)", ex);
}
}
return success;
}
}