Skip to content
This repository was archived by the owner on May 10, 2024. It is now read-only.

Commit 997210a

Browse files
committed
Retrieving ResultSets correctly
1 parent f53ba1b commit 997210a

File tree

7 files changed

+216
-129
lines changed

7 files changed

+216
-129
lines changed

commons/src/main/java/me/totalfreedom/totalfreedommod/admin/Admin.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
import me.totalfreedom.totalfreedommod.player.FPlayer;
55
import me.totalfreedom.totalfreedommod.rank.DisplayableGroup;
66
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
7-
import me.totalfreedom.totalfreedommod.util.FLog;
7+
import me.totalfreedom.totalfreedommod.sql.ResultSetProvider;
88
import me.totalfreedom.totalfreedommod.util.FUtil;
99
import org.apache.commons.lang.StringUtils;
1010
import org.bukkit.Bukkit;
1111
import org.bukkit.Server;
1212
import org.bukkit.entity.Player;
1313

14-
import java.sql.ResultSet;
15-
import java.sql.SQLException;
1614
import java.util.*;
1715

1816
public class Admin
@@ -32,23 +30,22 @@ public Admin(Player player)
3230
this.ips.add(FUtil.getIp(player));
3331
}
3432

35-
public Admin(ResultSet resultSet)
33+
public Admin(ResultSetProvider resultSet)
3634
{
37-
try
35+
if (resultSet.getString("uuid") == null)
3836
{
39-
this.uuid = UUID.fromString(resultSet.getString("uuid"));
40-
this.active = resultSet.getBoolean("active");
41-
this.rank = GroupProvider.fromArgument(resultSet.getString("rank")).getGroup();
42-
this.ips.clear();
43-
this.ips.addAll(FUtil.stringToList(resultSet.getString("ips")));
44-
this.lastLogin = new Date(resultSet.getLong("last_login"));
45-
this.commandSpy = resultSet.getBoolean("command_spy");
46-
this.potionSpy = resultSet.getBoolean("potion_spy");
47-
this.acFormat = resultSet.getString("ac_format");
48-
} catch (SQLException e)
49-
{
50-
FLog.severe("Failed to load admin: " + e.getMessage());
37+
throw new IllegalArgumentException("Failed to load admin, as the UUID is null.");
5138
}
39+
40+
this.uuid = UUID.fromString(resultSet.getString("uuid"));
41+
this.active = resultSet.getBoolean("active");
42+
this.rank = GroupProvider.fromArgument(resultSet.getString("rank")).getGroup();
43+
this.ips.clear();
44+
this.ips.addAll(FUtil.stringToList(resultSet.getString("ips")));
45+
this.lastLogin = new Date(resultSet.getLong("last_login"));
46+
this.commandSpy = resultSet.getBoolean("command_spy");
47+
this.potionSpy = resultSet.getBoolean("potion_spy");
48+
this.acFormat = resultSet.getString("ac_format");
5249
}
5350

5451
@Override
@@ -70,15 +67,15 @@ public String toString()
7067
public Map<String, Object> toSQLStorable()
7168
{
7269
HashMap<String, Object> map = new HashMap<>();
73-
map.put("uuid", uuid.toString());
74-
map.put("active", active);
75-
map.put("rank", rank.toString());
76-
map.put("ips", FUtil.listToString(ips));
77-
map.put("last_login", lastLogin.getTime());
78-
map.put("command_spy", commandSpy);
79-
map.put("potion_spy", potionSpy);
80-
map.put("ac_format", acFormat);
81-
return map;
70+
map.put("uuid", uuid.toString());
71+
map.put("active", active);
72+
map.put("rank", rank.toString());
73+
map.put("ips", FUtil.listToString(ips));
74+
map.put("last_login", lastLogin.getTime());
75+
map.put("command_spy", commandSpy);
76+
map.put("potion_spy", potionSpy);
77+
map.put("ac_format", acFormat);
78+
return map;
8279

8380
}
8481

commons/src/main/java/me/totalfreedom/totalfreedommod/admin/AdminList.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
import me.totalfreedom.totalfreedommod.FreedomService;
66
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
77
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
8+
import me.totalfreedom.totalfreedommod.sql.ResultSetProvider;
89
import me.totalfreedom.totalfreedommod.util.FLog;
910
import me.totalfreedom.totalfreedommod.util.FUtil;
1011
import net.kyori.adventure.text.Component;
1112
import org.bukkit.OfflinePlayer;
1213
import org.bukkit.command.CommandSender;
1314
import org.bukkit.entity.Player;
1415

15-
import java.sql.ResultSet;
1616
import java.sql.SQLException;
1717
import java.util.*;
1818
import java.util.concurrent.TimeUnit;
19+
import java.util.concurrent.atomic.AtomicInteger;
1920

2021
public class AdminList extends FreedomService
2122
{
@@ -45,11 +46,19 @@ public void load()
4546
allAdmins.clear();
4647
try
4748
{
48-
ResultSet adminSet = plugin.sql.getAdminList();
49-
while (adminSet.next())
50-
{
51-
tryAddAdmin(adminSet);
52-
}
49+
ResultSetProvider adminList = plugin.sql.getAdminList();
50+
AtomicInteger row = new AtomicInteger(1);
51+
adminList.getAllRowsResultSet().forEach(adminSet -> {
52+
try
53+
{
54+
tryAddAdmin(ResultSetProvider.fromRow(adminSet));
55+
row.set(row.get() + 1);
56+
} catch (Throwable e)
57+
{
58+
FLog.warning("An error occurred whilst reading the admin entry at row #" + row.get());
59+
FLog.warning(e);
60+
}
61+
});
5362
} catch (SQLException e)
5463
{
5564
FLog.severe("Failed to load admin list: " + e.getMessage());
@@ -59,17 +68,10 @@ public void load()
5968
FLog.info("Loaded " + allAdmins.size() + " admins (" + uuidTable.size() + " active, " + ipTable.size() + " IPs)");
6069
}
6170

62-
private void tryAddAdmin(ResultSet adminSet) throws SQLException
71+
private void tryAddAdmin(ResultSetProvider adminSet)
6372
{
64-
try
65-
{
6673
Admin admin = new Admin(adminSet);
6774
allAdmins.add(admin);
68-
} catch (Throwable ex)
69-
{
70-
FLog.warning("An error occurred whilst reading the admin entry at row #" + adminSet.getRow());
71-
FLog.warning(ex);
72-
}
7375
}
7476

7577
public void messageAllAdmins(Component message)
@@ -263,7 +265,7 @@ public void save(Admin admin)
263265
{
264266
try
265267
{
266-
ResultSet currentSave = plugin.sql.getAdminByUuid(admin.getUuid());
268+
ResultSetProvider currentSave = plugin.sql.getAdminByUuid(admin.getUuid());
267269
for (Map.Entry<String, Object> entry : admin.toSQLStorable().entrySet())
268270
{
269271
Object storedValue = plugin.sql.getValue(currentSave, entry.getKey(), entry.getValue());

commons/src/main/java/me/totalfreedom/totalfreedommod/banning/BanManager.java

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,8 @@
22

33
import com.google.common.collect.Maps;
44
import com.google.common.collect.Sets;
5-
import java.sql.ResultSet;
6-
import java.sql.SQLException;
7-
import java.util.ArrayList;
8-
import java.util.Collection;
9-
import java.util.Collections;
10-
import java.util.Date;
11-
import java.util.List;
12-
import java.util.Map;
13-
import java.util.Set;
14-
import java.util.UUID;
155
import me.totalfreedom.totalfreedommod.FreedomService;
6+
import me.totalfreedom.totalfreedommod.sql.ResultSetProvider;
167
import me.totalfreedom.totalfreedommod.util.FLog;
178
import me.totalfreedom.totalfreedommod.util.FUtil;
189
import org.bukkit.entity.Player;
@@ -21,6 +12,9 @@
2112
import org.bukkit.event.player.PlayerJoinEvent;
2213
import org.bukkit.event.player.PlayerLoginEvent;
2314

15+
import java.sql.SQLException;
16+
import java.util.*;
17+
2418
public class BanManager extends FreedomService
2519
{
2620

@@ -36,28 +30,24 @@ public void onStart()
3630
bans.clear();
3731
try
3832
{
39-
ResultSet banSet = plugin.sql.getBanList();
40-
{
41-
while (banSet.next())
33+
plugin.sql.getBanList().getAllRowsResultSet().forEach(row -> {
34+
ResultSetProvider banSet = ResultSetProvider.fromRow(row);
35+
String name = banSet.getString("name");
36+
UUID uuid = null;
37+
String strUUID = banSet.getString("uuid");
38+
if (strUUID != null)
4239
{
43-
String name = banSet.getString("name");
44-
UUID uuid = null;
45-
String strUUID = banSet.getString("uuid");
46-
if (strUUID != null)
47-
{
48-
uuid = UUID.fromString(strUUID);
49-
}
50-
List<String> ips = FUtil.stringToList(banSet.getString("ips"));
51-
String by = banSet.getString("by");
52-
Date at = new Date(banSet.getLong("at"));
53-
Date expires = new Date(banSet.getLong("expires"));
54-
String reason = banSet.getString("reason");
55-
Ban ban = new Ban(name, uuid, ips, by, at, expires, reason);
56-
bans.add(ban);
40+
uuid = UUID.fromString(strUUID);
5741
}
58-
}
59-
}
60-
catch (SQLException e)
42+
List<String> ips = FUtil.stringToList(banSet.getString("ips"));
43+
String by = banSet.getString("by");
44+
Date at = new Date(banSet.getLong("at"));
45+
Date expires = new Date(banSet.getLong("expires"));
46+
String reason = banSet.getString("reason");
47+
Ban ban = new Ban(name, uuid, ips, by, at, expires, reason);
48+
bans.add(ban);
49+
});
50+
} catch (SQLException e)
6151
{
6252
FLog.severe("Failed to load ban list: " + e.getMessage());
6353
}
@@ -186,8 +176,7 @@ public void addBan(Ban ban)
186176
if (ban.getUsername() != null && getByUsername(ban.getUsername()) != null)
187177
{
188178
removeBan(ban);
189-
}
190-
else
179+
} else
191180
{
192181

193182
for (String ip : ban.getIps())
@@ -267,8 +256,7 @@ public void onPlayerJoin(PlayerJoinEvent event)
267256
if (ban != null)
268257
{
269258
removeBan(ban);
270-
}
271-
else
259+
} else
272260
{
273261
ban = getByIp(FUtil.getIp(player));
274262
if (ban != null)

commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.google.common.collect.Lists;
55
import me.totalfreedom.totalfreedommod.api.ShopItem;
66
import me.totalfreedom.totalfreedommod.util.FConverter;
7+
import me.totalfreedom.totalfreedommod.sql.ResultSetProvider;
78
import me.totalfreedom.totalfreedommod.util.FLog;
89
import me.totalfreedom.totalfreedommod.util.FUtil;
910
import net.kyori.adventure.text.Component;
@@ -44,7 +45,7 @@ public class PlayerData
4445

4546
private Boolean inspect = false;
4647

47-
public PlayerData(ResultSet resultSet)
48+
public PlayerData(ResultSetProvider resultSet)
4849
{
4950
try
5051
{
@@ -71,7 +72,7 @@ public PlayerData(ResultSet resultSet)
7172
loginMessage = tempLoginMessage;
7273
//--
7374
inspect = resultSet.getBoolean("inspect");
74-
} catch (SQLException e)
75+
} catch (Throwable e)
7576
{
7677
FLog.severe("Failed to load player: " + e.getMessage());
7778
e.printStackTrace();

commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerList.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import me.totalfreedom.totalfreedommod.admin.Admin;
66
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
77
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
8+
import me.totalfreedom.totalfreedommod.sql.ResultSetProvider;
89
import me.totalfreedom.totalfreedommod.util.FLog;
910
import me.totalfreedom.totalfreedommod.util.FUtil;
1011
import org.bukkit.OfflinePlayer;
@@ -42,24 +43,17 @@ public FPlayer getPlayerSync(Player player)
4243

4344
public void loadMasterBuilders()
4445
{
45-
ResultSet resultSet = plugin.sql.getMasterBuilders();
46+
ResultSetProvider resultSet = plugin.sql.getMasterBuilders();
4647

4748
if (resultSet == null)
4849
{
4950
return;
5051
}
5152

52-
try
53-
{
54-
while (resultSet.next())
55-
{
56-
PlayerData playerData = load(resultSet);
57-
dataMap.put(playerData.getUuid(), playerData);
58-
}
59-
} catch (SQLException e)
60-
{
61-
FLog.severe("Failed to parse master builders: " + e.getMessage());
62-
}
53+
resultSet.getAllRowsResultSet().forEach(row -> {
54+
PlayerData playerData = load(ResultSetProvider.fromRow(row));
55+
dataMap.put(playerData.getUuid(), playerData);
56+
});
6357
}
6458

6559
public String getIp(OfflinePlayer player)
@@ -128,12 +122,13 @@ public PlayerData loadByIp(String ip)
128122
return load(plugin.sql.getPlayerByIp(ip));
129123
}
130124

131-
public PlayerData load(ResultSet resultSet)
125+
public PlayerData load(ResultSetProvider resultSet)
132126
{
133127
if (resultSet == null)
134128
{
135129
return null;
136130
}
131+
137132
return new PlayerData(resultSet);
138133
}
139134

@@ -149,7 +144,7 @@ public void save(PlayerData player)
149144
{
150145
try
151146
{
152-
ResultSet currentSave = plugin.sql.getPlayerByUuid(player.getUuid());
147+
ResultSetProvider currentSave = plugin.sql.getPlayerByUuid(player.getUuid());
153148
for (Map.Entry<String, Object> entry : player.toSQLStorable().entrySet())
154149
{
155150
Object storedValue = plugin.sql.getValue(currentSave, entry.getKey(), entry.getValue());

0 commit comments

Comments
 (0)