From 54688b7b73016db71ef3d5cef72fce4500984784 Mon Sep 17 00:00:00 2001 From: ceze88 Date: Sun, 7 Jul 2024 17:48:35 +0200 Subject: [PATCH] Add caching for roles to avoid reading the files all the time --- .../java/com/craftaro/skyblock/island/Island.java | 14 ++++++++++++++ .../skyblock/listeners/InteractListeners.java | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/craftaro/skyblock/island/Island.java b/src/main/java/com/craftaro/skyblock/island/Island.java index 5b47886b..6b7a6145 100644 --- a/src/main/java/com/craftaro/skyblock/island/Island.java +++ b/src/main/java/com/craftaro/skyblock/island/Island.java @@ -61,6 +61,7 @@ public class Island { private final List islandLocations = new ArrayList<>(); private final Map coopPlayers = new HashMap<>(); private final Set whitelistedPlayers = new HashSet<>(); + private final Map> roleCache = new HashMap<>(); private UUID islandUUID; private UUID ownerUUID; @@ -599,6 +600,11 @@ public class Island { } public Set getRole(IslandRole role) { + + if (roleCache.containsKey(role)) { + return new HashSet<>(roleCache.get(role)); // Return a copy to avoid external modification + } + Set islandRoles = new HashSet<>(); if (role == IslandRole.OWNER) { @@ -615,6 +621,8 @@ public class Island { } } + roleCache.put(role, islandRoles); + return islandRoles; } @@ -673,6 +681,9 @@ public class Island { getVisit().setMembers(getRole(IslandRole.MEMBER).size() + getRole(IslandRole.OPERATOR).size() + 1); + //Update role cache + roleCache.remove(role); + return true; } } @@ -699,6 +710,9 @@ public class Island { getVisit().setMembers(getRole(IslandRole.MEMBER).size() + getRole(IslandRole.OPERATOR).size() + 1); + //Update role cache + roleCache.remove(role); + return true; } } diff --git a/src/main/java/com/craftaro/skyblock/listeners/InteractListeners.java b/src/main/java/com/craftaro/skyblock/listeners/InteractListeners.java index 3b4185e4..1bc84464 100644 --- a/src/main/java/com/craftaro/skyblock/listeners/InteractListeners.java +++ b/src/main/java/com/craftaro/skyblock/listeners/InteractListeners.java @@ -170,13 +170,13 @@ public class InteractListeners implements Listener { return; } - Optional material = block == null ? Optional.empty() : CompatibleMaterial.getMaterial(block.getType()); - // Check permissions. if (!this.plugin.getPermissionManager().processPermission(event, player, island)) { return; } + Optional material = block == null ? Optional.empty() : CompatibleMaterial.getMaterial(block.getType()); + if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { final Optional blockType = CompatibleMaterial.getMaterial(event.getClickedBlock().getType()); final XMaterial heldType;