Adds more options around getting islands and caching (#2394)

Sometimes, there is a need to get an island once but not cache it. For
example, when loading addons they may need to scan all the islands, but
not have them cached.
This commit is contained in:
tastybento 2024-06-01 08:37:37 -07:00 committed by GitHub
parent 475f6372e2
commit 01dcd6ecc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 2 deletions

View File

@ -1657,7 +1657,7 @@ public class IslandsManager {
/**
* Try to get an island by its unique id
*
* @param uniqueId - unique id string
* @param uniqueId - unique id of island
* @return optional island
* @since 1.3.0
*/
@ -1666,6 +1666,29 @@ public class IslandsManager {
return Optional.ofNullable(islandCache.getIslandById(uniqueId));
}
/**
* Try to get an island by its unique id. If you are needing to load all the islands to check something
* but do not need to have them cached, then use this method and set cache to false.
*
* @param uniqueId - unique id of island
* @param cache - if false, island will not be cached if it is not already
* @return optional island
* @since 2.4.0
*/
@NonNull
public Optional<Island> getIslandById(String uniqueId, boolean cache) {
return Optional.ofNullable(islandCache.getIslandById(uniqueId, cache));
}
/**
* Returns if this is a known island uniqueId. Will not load the island from the database if it is not loaded already.
* @param uniqueId - unique id of island
* @return true if this island exists
*/
public boolean isIslandId(String uniqueId) {
return islandCache.isIslandId(uniqueId);
}
/**
* Resets all flags to gamemode config.yml default
*

View File

@ -430,7 +430,29 @@ public class IslandCache {
@Nullable
public Island getIslandById(@NonNull String uniqueId) {
// Load from cache or database
return islandsById.computeIfAbsent(uniqueId, handler::loadObject);
return getIslandById(uniqueId, true);
}
/**
* Get the island by unique id
*
* @param uniqueId unique id of the Island.
* @param cache if true, then the Island will be cached if it is not already
* @return island or null if none found
* @since 2.4.0
*/
@Nullable
public Island getIslandById(@NonNull String uniqueId, boolean cache) {
Island island = islandsById.get(uniqueId);
if (island != null) {
return island;
}
island = handler.loadObject(uniqueId);
if (cache && island != null) {
islandsById.put(uniqueId, island);
}
return island;
}
/**
@ -485,4 +507,13 @@ public class IslandCache {
return islandsByUUID.getOrDefault(uniqueId, Collections.emptySet()).stream().map(this::getIslandById).toList();
}
/**
* Returns if this is a known island uniqueId. Will not load the island from the database if it is not loaded already.
* @param uniqueId - unique id of island
* @return true if this island exists
*/
public boolean isIslandId(String uniqueId) {
return this.islandsById.containsKey(uniqueId);
}
}