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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
package net.benjidial.nswp;
import org.bukkit.entity.Player;
import org.bukkit.Location;
import org.bukkit.Bukkit;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.UUID;
public class Database {
static Connection connection;
public static void connect(String path) throws SQLException {
connection = DriverManager.getConnection("jdbc:sqlite:" + path);
}
static String playerID(Player player) {
return player.getUniqueId().toString().replaceAll("-", "");
}
static String waypointTableName(Player player) {
return "waypoints_" + playerID(player);
}
public static void createWaypointTable(Player player) throws SQLException {
PreparedStatement statement = connection.prepareStatement(
"CREATE TABLE IF NOT EXISTS " + waypointTableName(player) + " (" +
"name TEXT PRIMARY KEY, " +
"world TEXT NOT NULL, " +
"x REAL NOT NULL, " +
"y REAL NOT NULL, " +
"z REAL NOT NULL, " +
"pitch REAL NOT NULL, " +
"yaw REAL NOT NULL" +
")"
);
statement.executeUpdate();
}
static void createWBacks() throws SQLException {
PreparedStatement statement = connection.prepareStatement(
"CREATE TABLE IF NOT EXISTS wbacks (" +
"player_uuid TEXT PRIMARY KEY, " +
"world TEXT NOT NULL, " +
"x REAL NOT NULL, " +
"y REAL NOT NULL, " +
"z REAL NOT NULL, " +
"pitch REAL NOT NULL, " +
"yaw REAL NOT NULL" +
")"
);
statement.executeUpdate();
}
public static Location getWBack(Player player) throws SQLException {
PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM wbacks WHERE player_uuid = ?"
);
statement.setString(1, playerID(player));
ResultSet results = statement.executeQuery();
if (!results.next())
return null;
return cursorToLocation(results);
}
public static void setWBack(Player player, Location location) throws SQLException {
PreparedStatement statement = connection.prepareStatement(
"REPLACE INTO wbacks(player_uuid, world, x, y, z, pitch, yaw) VALUES(?, ?, ?, ?, ?, ?, ?)"
);
statement.setString(1, playerID(player));
statement.setString(2, location.getWorld().getName());
statement.setDouble(3, location.getX());
statement.setDouble(4, location.getY());
statement.setDouble(5, location.getZ());
statement.setDouble(6, location.getPitch());
statement.setDouble(7, location.getYaw());
statement.executeUpdate();
}
static Location cursorToLocation(ResultSet results) throws SQLException {
return new Location(
Bukkit.getWorld(results.getString("world")),
results.getDouble("x"),
results.getDouble("y"),
results.getDouble("z"),
(float)results.getDouble("yaw"),
(float)results.getDouble("pitch")
);
}
static Waypoint cursorToWaypoint(ResultSet results) throws SQLException {
return new Waypoint(
results.getString("name"),
cursorToLocation(results)
);
}
public static ArrayList<Waypoint> searchWaypoints(Player player, String searchString) throws SQLException {
PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM " + waypointTableName(player) + " WHERE name LIKE ? ESCAPE '!' ORDER BY world, name ASC"
);
statement.setString(1, "%" + searchString.replace("!", "!!").replace("_", "!_") + "%");
ResultSet results = statement.executeQuery();
ArrayList<Waypoint> list = new ArrayList<Waypoint>();
while (results.next())
list.add(cursorToWaypoint(results));
return list;
}
public static Waypoint lookupWaypoint(Player player, String name) throws SQLException {
PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM " + waypointTableName(player) + " WHERE name = ?"
);
statement.setString(1, name);
ResultSet results = statement.executeQuery();
if (!results.next())
return null;
return cursorToWaypoint(results);
}
public static void addWaypoint(Player player, Waypoint waypoint) throws SQLException {
if (lookupWaypoint(player, waypoint.name) != null)
player.sendMessage("There is already a waypoint with that name.");
else {
PreparedStatement statement = connection.prepareStatement(
"INSERT INTO " + waypointTableName(player) + "(name, world, x, y, z, pitch, yaw) " +
"VALUES(?, ?, ?, ?, ?, ?, ?)"
);
statement.setString(1, waypoint.name);
statement.setString(2, waypoint.location.getWorld().getName());
statement.setDouble(3, waypoint.location.getX());
statement.setDouble(4, waypoint.location.getY());
statement.setDouble(5, waypoint.location.getZ());
statement.setDouble(6, waypoint.location.getPitch());
statement.setDouble(7, waypoint.location.getYaw());
statement.executeUpdate();
player.sendMessage("Waypoint added.");
}
}
public static void deleteWaypoint(Player player, String name) throws SQLException {
PreparedStatement statement = connection.prepareStatement(
"DELETE FROM " + waypointTableName(player) + " WHERE name = ?"
);
statement.setString(1, name);
if (statement.executeUpdate() == 0)
player.sendMessage("No waypoint with that name.");
else
player.sendMessage("Waypoint deleted.");
}
public static void createTPATable() throws SQLException {
PreparedStatement statement = connection.prepareStatement(
"CREATE TABLE IF NOT EXISTS tpa_allowed (" +
"from TEXT NOT NULL, " +
"to TEXT NOT NULL" +
")"
);
statement.executeUpdate();
}
public static boolean isTPAAllowed(Player from, Player to) throws SQLException {
PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM tpa_allowed WHERE from = ? AND to = ?"
);
statement.setString(1, playerID(from));
statement.setString(2, playerID(to));
ResultSet results = statement.executeQuery();
return results.next();
}
public static void allowTPA(Player from, Player to) throws SQLException {
PreparedStatement statement = connection.prepareStatement(
"INSERT INTO tpa_allowed(from, to) VALUES(?, ?)"
);
statement.setString(1, playerID(from));
statement.setString(2, playerID(to));
statement.executeUpdate();
}
public static int disallowTPA(String fromName, Player to) throws SQLException {
//this is kinda scrungled but i think it should work.
PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM tpa_allowed WHERE to = ?"
);
statement.setString(1, playerID(to));
ResultSet table = statement.executeQuery();
int removed = 0;
while (table.next()) {
String fromID = table.getString("from");
if (Bukkit.getOfflinePlayer(UUID.fromString(fromID)).getName() == fromName) {
PreparedStatement deleteStatement = connection.prepareStatement(
"DELETE FROM tpa_allowed WHERE from = ? AND to = ?"
);
deleteStatement.setString(1, fromID);
deleteStatement.setString(2, playerID(to));
deleteStatement.executeUpdate();
++removed;
}
}
return removed;
}
public static ArrayList<String> getOnlineTPADisallowedTo(Player to) throws SQLException {
ArrayList<String> results = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers())
if (!isTPAAllowed(player, to))
results.add(player.getName());
return results;
}
public static ArrayList<String> getTPAAllowedTo(Player to) throws SQLException {
PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM tpa_allowed WHERE to = ?"
);
statement.setString(1, playerID(to));
ResultSet table = statement.executeQuery();
ArrayList<String> results = new ArrayList<>();
while (table.next()) {
String name = Bukkit.getOfflinePlayer(UUID.fromString(table.getString("from"))).getName();
//there is no way to disallow a player whose name is not know. but, to allow a player,
//they have to be online, and so their name should be known.
if (name != null)
results.add(name);
}
return results;
}
public static ArrayList<String> getOnlineTPAAllowedFrom(Player from) throws SQLException {
ArrayList<String> results = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers())
if (isTPAAllowed(from, player))
results.add(player.getName());
return results;
}
}
|