Refactor cached data away from cache

This commit is contained in:
William committed 2023-03-21 19:23:45 +00:00
1 parent b095c92854
commit a959aa9519
14 files changed
+166 -373

No files matched your search

@@ -11,13 +11,48 @@ import net.william278.huskhomes.user.User;
import net.william278.huskhomes.util.ValidationException;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.*;
public class HomesManager {
private final HuskHomes plugin;
private final List<Home> publicHomes;
private final Map<String, List<Home>> userHomes;
protected HomesManager(@NotNull HuskHomes plugin) {
this.plugin = plugin;
this.publicHomes = plugin.getDatabase().getPublicHomes();
this.userHomes = new HashMap<>();
plugin.runAsync(() -> plugin.getOnlineUsers()
.forEach(this::cacheUserHomes));
}
/**
* Cached user homes - maps a username to a list of their homes
*/
@NotNull
public Map<String, List<String>> getUserHomes() {
return userHomes.entrySet().stream()
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue().stream().map(Home::getName).toList()),
HashMap::putAll);
}
public void cacheUserHomes(@NotNull User user) {
userHomes.put(user.getUuid().toString(), plugin.getDatabase().getHomes(user));
}
public void removeUserHomes(@NotNull User user) {
userHomes.remove(user.getUuid().toString());
}
/**
* Cached public homes - maps a username to a list of their public homes
*/
@NotNull
public Map<String, List<String>> getPublicHomes() {
return publicHomes.stream()
.collect(HashMap::new, (m, e) -> m.put(e.getOwner().getUsername(), List.of(e.getName())),
HashMap::putAll);
}
public void createHome(@NotNull User owner, @NotNull String name, @NotNull Position position,
@@ -18,21 +18,15 @@ import java.util.*;
*/
public class RequestsManager {
@NotNull
private final HuskHomes plugin;
/**
* Map of user UUIDs to a queue of received teleport requests
*/
// Map of user UUIDs to a queue of received teleport requests
private final Map<UUID, Deque<TeleportRequest>> requests;
/**
* Set of users who are ignoring tpa requests
*/
// Set of users who are ignoring tpa requests
private final Set<UUID> ignoringRequests;
public RequestsManager(@NotNull HuskHomes implementation) {
this.plugin = implementation;
public RequestsManager(@NotNull HuskHomes plugin) {
this.plugin = plugin;
this.requests = new HashMap<>();
this.ignoringRequests = new HashSet<>();
}
@@ -173,8 +167,6 @@ public class RequestsManager {
*
* @param request The {@link TeleportRequest} to send
* @param recipient The online recipient of the request
* @return the {@link TeleportRequest} that was sent if it could be sent, otherwise an empty optional if it was not
* sent because the recipient is ignoring requests or is vanished ({@link OnlineUser#isVanished()})
*/
public void sendLocalTeleportRequest(@NotNull TeleportRequest request, @NotNull OnlineUser recipient) {
request.setRecipientName(recipient.getUsername());
@@ -196,7 +188,7 @@ public class RequestsManager {
// Add the request and display a message to the recipient
addTeleportRequest(request, recipient);
plugin.getLocales().getLocale((request.getType() == TeleportRequest.Type.TPA ? "tpa" : "tpahere")
+ "_request_received", request.getRequesterName())
+ "_request_received", request.getRequesterName())
.ifPresent(recipient::sendMessage);
plugin.getLocales().getLocale("teleport_request_buttons", request.getRequesterName())
.ifPresent(recipient::sendMessage);
@@ -7,13 +7,24 @@ import net.william278.huskhomes.position.Warp;
import net.william278.huskhomes.util.ValidationException;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
public class WarpsManager {
private final HuskHomes plugin;
private final List<Warp> warps;
protected WarpsManager(@NotNull HuskHomes plugin) {
this.plugin = plugin;
this.warps = plugin.getDatabase().getWarps();
}
/**
* Cached warp names
*/
@NotNull
public List<String> getWarps() {
return warps.stream().map(Warp::getName).toList();
}
public void createWarp(@NotNull String name, @NotNull Position position, boolean overwrite) throws ValidationException {