Skip to content

Commit 470623b

Browse files
committed
Adjust Cache so it's easier to work with, no functionality changes
1 parent aa62af9 commit 470623b

File tree

7 files changed

+85
-61
lines changed

7 files changed

+85
-61
lines changed

src/main/java/simplexity/simplehomes/commands/DeleteHome.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.jetbrains.annotations.Nullable;
1111
import simplexity.simplehomes.Home;
1212
import simplexity.simplehomes.configs.LocaleHandler;
13+
import simplexity.simplehomes.saving.Cache;
1314
import simplexity.simplehomes.saving.SQLHandler;
1415

1516
import java.util.ArrayList;
@@ -42,14 +43,13 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
4243
//Probably had to do with how I was originally putting it in but whatever, I'm doing it here now.
4344
Component parsedHomeDeleteMessage = LocaleHandler.getInstance().locationResolver(homeRequested,
4445
LocaleHandler.getInstance().getHomeDeleted());
45-
SQLHandler.getInstance().deleteHome(player.getUniqueId(), homeName);
46+
Cache.getInstance().removeHomeByName(player.getUniqueId(), homeName);
4647
player.sendMessage(parsedHomeDeleteMessage);
4748
return true;
4849

4950
}
5051

5152

52-
5353
//Handle logic to get the home requested in the arguments
5454
private boolean shouldDelete(Home homeRequested, Player player, String homeName) {
5555
if (homeRequested == null) {

src/main/java/simplexity/simplehomes/commands/HomeCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import simplexity.simplehomes.SimpleHomes;
1717
import simplexity.simplehomes.configs.ConfigHandler;
1818
import simplexity.simplehomes.configs.LocaleHandler;
19+
import simplexity.simplehomes.saving.Cache;
1920
import simplexity.simplehomes.saving.SQLHandler;
2021

2122
import java.util.ArrayList;
@@ -36,7 +37,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
3637
sender.sendRichMessage(LocaleHandler.getInstance().getMustBePlayer());
3738
return false;
3839
}
39-
List<Home> playerHomesList = SQLHandler.getInstance().getHomes(player.getUniqueId());
40+
List<Home> playerHomesList = Cache.getInstance().getPlayerHomes(player.getUniqueId());
4041
//Check for lockout
4142
if (CommandUtils.isLockedOut(player)) {
4243
player.sendRichMessage(LocaleHandler.getInstance().getCannotUseCommand(),

src/main/java/simplexity/simplehomes/commands/HomeList.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import simplexity.simplehomes.Home;
1212
import simplexity.simplehomes.SimpleHomes;
1313
import simplexity.simplehomes.configs.LocaleHandler;
14-
import simplexity.simplehomes.saving.SQLHandler;
14+
import simplexity.simplehomes.saving.Cache;
1515

1616
import java.util.List;
1717

@@ -25,7 +25,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
2525
sender.sendRichMessage(LocaleHandler.getInstance().getMustBePlayer());
2626
return false;
2727
}
28-
List<Home> playerHomes = SQLHandler.getInstance().getHomes(player.getUniqueId());
28+
List<Home> playerHomes = Cache.getInstance().getPlayerHomes(player.getUniqueId());
2929
//Check for lockout
3030
if (CommandUtils.isLockedOut(player)) {
3131
player.sendRichMessage(LocaleHandler.getInstance().getCannotUseCommand(),

src/main/java/simplexity/simplehomes/commands/SetHome.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import simplexity.simplehomes.Home;
1010
import simplexity.simplehomes.configs.ConfigHandler;
1111
import simplexity.simplehomes.configs.LocaleHandler;
12-
import simplexity.simplehomes.saving.SQLHandler;
12+
import simplexity.simplehomes.saving.Cache;
1313

1414
import java.util.List;
1515

@@ -21,7 +21,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
2121
sender.sendRichMessage(LocaleHandler.getInstance().getMustBePlayer());
2222
return false;
2323
}
24-
List<Home> playerHomes = SQLHandler.getInstance().getHomes(player.getUniqueId());
24+
List<Home> playerHomes = Cache.getInstance().getPlayerHomes(player.getUniqueId());
2525
if (!canSetMoreHomes(player, playerHomes)) {
2626
sender.sendRichMessage(LocaleHandler.getInstance().getCannotSetMoreHomes(),
2727
Placeholder.parsed("value", String.valueOf(CommandUtils.maxHomesPermission(player))));
@@ -58,7 +58,7 @@ private String getHomeName(String[] args) {
5858
}
5959

6060
private void handleSetHome(Player player, String homeName) {
61-
SQLHandler.getInstance().setHome(player.getUniqueId(), player.getLocation(), homeName);
61+
Cache.getInstance().setPlayerHome(player.getUniqueId(), player.getLocation(), homeName);
6262
player.sendRichMessage(LocaleHandler.getInstance().getHomeSet(), Placeholder.unparsed("name", homeName));
6363
}
6464

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package simplexity.simplehomes.listeners;
22

3-
import org.bukkit.entity.Player;
43
import org.bukkit.event.EventHandler;
54
import org.bukkit.event.Listener;
65
import org.bukkit.event.player.PlayerQuitEvent;
7-
import simplexity.simplehomes.saving.SQLHandler;
6+
import simplexity.simplehomes.saving.Cache;
87

98
import java.util.UUID;
109

1110
public class PlayerLeaveListener implements Listener {
1211
@EventHandler
1312
public void onPlayerLeave(PlayerQuitEvent event) {
1413
UUID uuid = event.getPlayer().getUniqueId();
15-
SQLHandler.getInstance().removePlayerFromCache(uuid);
14+
Cache.getInstance().removePlayerFromCache(uuid);
1615
}
1716
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package simplexity.simplehomes.saving;
2+
3+
import org.bukkit.Location;
4+
import simplexity.simplehomes.Home;
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.UUID;
10+
11+
public class Cache {
12+
13+
private static Cache instance;
14+
15+
public Cache() {
16+
}
17+
18+
public static Cache getInstance() {
19+
if (instance == null) instance = new Cache();
20+
return instance;
21+
}
22+
23+
24+
private final HashMap<UUID, List<Home>> cachedHomes = new HashMap<>();
25+
26+
public List<Home> getPlayerHomes(UUID playerUuid) {
27+
// Get cached homes if they're already cached, and returned
28+
if (cachedHomes.containsKey(playerUuid)) return cachedHomes.get(playerUuid);
29+
// Otherwise, query the database, and cache the results, or create a blank array list and add it if there is no
30+
// results. Return the newly cached data.
31+
List<Home> savedHomes = SQLHandler.getInstance().getHomes(playerUuid);
32+
if (savedHomes == null || savedHomes.isEmpty()) savedHomes = new ArrayList<>();
33+
cachedHomes.put(playerUuid, savedHomes);
34+
return savedHomes;
35+
}
36+
37+
public void setPlayerHome(UUID playerUuid, Location location, String name) {
38+
List<Home> playerHomes = getPlayerHomes(playerUuid);
39+
Home existingHome = getHomeFromList(playerHomes, name);
40+
if (existingHome != null) {
41+
playerHomes.remove(existingHome);
42+
}
43+
Home newHome = new Home(name, location);
44+
playerHomes.add(newHome);
45+
cachedHomes.put(playerUuid, playerHomes);
46+
SQLHandler.getInstance().setHome(playerUuid, location, name);
47+
}
48+
49+
public void removeHomeByName(UUID playerUuid, String homeName) {
50+
List<Home> playerHomes = getPlayerHomes(playerUuid);
51+
Home homeToRemove = getHomeFromList(playerHomes, homeName);
52+
if (homeToRemove == null) return;
53+
SQLHandler.getInstance().deleteHome(playerUuid, homeName);
54+
playerHomes.remove(homeToRemove);
55+
cachedHomes.put(playerUuid, playerHomes);
56+
}
57+
58+
public void removePlayerFromCache(UUID playerUuid) {
59+
cachedHomes.remove(playerUuid);
60+
}
61+
62+
@SuppressWarnings("JavaExistingMethodCanBeUsed")
63+
private Home getHomeFromList(List<Home> homes, String homeName) {
64+
for (Home home : homes) {
65+
if (home.name().equalsIgnoreCase(homeName)) {
66+
return home;
67+
}
68+
}
69+
return null;
70+
}
71+
}

src/main/java/simplexity/simplehomes/saving/SQLHandler.java

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.sql.SQLException;
1313
import java.sql.Statement;
1414
import java.util.ArrayList;
15-
import java.util.HashMap;
1615
import java.util.List;
1716
import java.util.UUID;
1817
import java.util.logging.Logger;
@@ -25,8 +24,6 @@ public class SQLHandler {
2524
private SQLHandler() {
2625
}
2726

28-
private static final HashMap<UUID, List<Home>> cachedHomes = new HashMap<>();
29-
3027
private static SQLHandler instance;
3128

3229
public static SQLHandler getInstance() {
@@ -59,9 +56,6 @@ world_name VARCHAR(255),
5956
}
6057

6158
public List<Home> getHomes(UUID uuid) {
62-
if (cachedHomes.containsKey(uuid)) {
63-
return cachedHomes.get(uuid);
64-
}
6559
List<Home> homes = new ArrayList<>();
6660
String query = "SELECT * FROM homes WHERE player_uuid = ?";
6761
try (PreparedStatement statement = getConnection().prepareStatement(query)) {
@@ -82,7 +76,6 @@ public List<Home> getHomes(UUID uuid) {
8276
)
8377
));
8478
}
85-
cachedHomes.put(uuid, homes);
8679
return homes;
8780
} catch (SQLException e) {
8881
logger.severe("Failed to get homes");
@@ -97,7 +90,7 @@ public List<Home> getHomes(UUID uuid) {
9790
return null;
9891
}
9992

100-
public boolean deleteHome(UUID uuid, String homeName) {
93+
public void deleteHome(UUID uuid, String homeName) {
10194
// Prepare the SQL statement to check if the home exists
10295
String checkIfExistsQuery = "SELECT * FROM homes WHERE player_uuid = ? AND home_name = ?";
10396
try (PreparedStatement homeExists = getConnection().prepareStatement(checkIfExistsQuery)) {
@@ -111,20 +104,16 @@ public boolean deleteHome(UUID uuid, String homeName) {
111104
deleteStatement.setString(2, homeName);
112105
deleteStatement.executeUpdate();
113106
}
114-
} else {
115-
return false; // Nothing to delete, return false
116107
}
117108
}
118109
} catch (SQLException e) {
119110
logger.severe("Failed to delete home");
120111
e.printStackTrace();
121-
return false; // Error occurred while deleting home
122112
}
123-
updateCache(uuid);
124-
return false;
113+
// home deleted successfully
125114
}
126115

127-
public boolean setHome(UUID uuid, Location location, String homeName) {
116+
public void setHome(UUID uuid, Location location, String homeName) {
128117
String insertQuery = "REPLACE INTO homes " +
129118
"(player_uuid_and_name, player_uuid, home_name, world_uuid, location_x, location_y, location_z, yaw, pitch) " +
130119
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
@@ -143,45 +132,9 @@ public boolean setHome(UUID uuid, Location location, String homeName) {
143132
logger.severe("Failed to set home");
144133
logger.severe("Error occurred at Home Insert.");
145134
e.printStackTrace();
146-
return false; // Error occurred while setting home
147-
}
148-
updateCache(uuid);
149-
return true; // Home set successfully
150-
}
151-
152-
private void updateCache(UUID uuid) {
153-
List<Home> homes = new ArrayList<>();
154-
String query = "SELECT * FROM homes WHERE player_uuid = ?";
155-
try (PreparedStatement statement = getConnection().prepareStatement(query)) {
156-
statement.setString(1, uuid.toString());
157-
try (ResultSet resultSet = statement.executeQuery()) {
158-
while (resultSet.next()) {
159-
String worldUUIDString = resultSet.getString("world_uuid");
160-
UUID worldUUID = UUID.fromString(worldUUIDString);
161-
homes.add(new Home(
162-
resultSet.getString("home_name"),
163-
new Location(
164-
SimpleHomes.getInstance().getServer().getWorld(worldUUID),
165-
resultSet.getDouble("location_x"),
166-
resultSet.getDouble("location_y"),
167-
resultSet.getDouble("location_z"),
168-
resultSet.getFloat("yaw"),
169-
resultSet.getFloat("pitch")
170-
)
171-
));
172-
}
173-
cachedHomes.put(uuid, homes);
174-
}
175-
} catch (SQLException e) {
176-
logger.severe("Failed to update cache");
177-
e.printStackTrace();
178135
}
179136
}
180137

181-
public void removePlayerFromCache(UUID uuid) {
182-
cachedHomes.remove(uuid);
183-
}
184-
185138
private Connection getConnection() throws SQLException {
186139
if (connection == null || connection.isClosed() || !connection.isValid(2)) {
187140
connection = sqlOrSqlLite();

0 commit comments

Comments
 (0)