Skip to content

Commit

Permalink
Adds more options around getting islands and caching (#2394)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tastybento committed Jun 1, 2024
1 parent 475f637 commit 01dcd6e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,7 @@ public static void updateIsland(Island island) {
/**
* 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
*/
Expand All @@ -1666,6 +1666,29 @@ public Optional<Island> getIslandById(String uniqueId) {
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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,29 @@ public void setOwner(@NonNull Island island, @Nullable UUID newOwnerUUID) {
@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;
}

/**
Expand Down Expand Up @@ -485,4 +507,13 @@ public Set<String> getAllIslandIds() {
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);
}

}

0 comments on commit 01dcd6e

Please sign in to comment.