Skip to content

Commit

Permalink
Add caching for roles to avoid reading the files all the time
Browse files Browse the repository at this point in the history
  • Loading branch information
ceze88 committed Jul 7, 2024
1 parent 8425d99 commit 54688b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/craftaro/skyblock/island/Island.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class Island {
private final List<IslandLocation> islandLocations = new ArrayList<>();
private final Map<UUID, IslandCoop> coopPlayers = new HashMap<>();
private final Set<UUID> whitelistedPlayers = new HashSet<>();
private final Map<IslandRole, Set<UUID>> roleCache = new HashMap<>();

private UUID islandUUID;
private UUID ownerUUID;
Expand Down Expand Up @@ -599,6 +600,11 @@ public boolean isAlwaysLoaded() {
}

public Set<UUID> getRole(IslandRole role) {

if (roleCache.containsKey(role)) {
return new HashSet<>(roleCache.get(role)); // Return a copy to avoid external modification
}

Set<UUID> islandRoles = new HashSet<>();

if (role == IslandRole.OWNER) {
Expand All @@ -615,6 +621,8 @@ public Set<UUID> getRole(IslandRole role) {
}
}

roleCache.put(role, islandRoles);

return islandRoles;
}

Expand Down Expand Up @@ -673,6 +681,9 @@ public boolean setRole(IslandRole role, UUID uuid) {

getVisit().setMembers(getRole(IslandRole.MEMBER).size() + getRole(IslandRole.OPERATOR).size() + 1);

//Update role cache
roleCache.remove(role);

return true;
}
}
Expand All @@ -699,6 +710,9 @@ public boolean removeRole(IslandRole role, UUID uuid) {

getVisit().setMembers(getRole(IslandRole.MEMBER).size() + getRole(IslandRole.OPERATOR).size() + 1);

//Update role cache
roleCache.remove(role);

return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ public void onPlayerInteract(PlayerInteractEvent event) {
return;
}

Optional<XMaterial> material = block == null ? Optional.empty() : CompatibleMaterial.getMaterial(block.getType());

// Check permissions.
if (!this.plugin.getPermissionManager().processPermission(event, player, island)) {
return;
}

Optional<XMaterial> material = block == null ? Optional.empty() : CompatibleMaterial.getMaterial(block.getType());

if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
final Optional<XMaterial> blockType = CompatibleMaterial.getMaterial(event.getClickedBlock().getType());
final XMaterial heldType;
Expand Down

0 comments on commit 54688b7

Please sign in to comment.