Skip to content

Commit

Permalink
Make code thread safe - 2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
virustotalop committed Apr 11, 2022
1 parent f3168a2 commit 701a660
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'com.clubobsidian'
version = '2.0.1'
version = '2.0.2'

processResources {
from(sourceSets.main.resources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
Expand All @@ -24,19 +24,18 @@

public final class PermissionManager implements Listener {

private Map<UUID, Map<String, PermissionNode>> userPermissionCache;
private PermissionPlugin plugin;
private final Map<UUID, Map<String, PermissionNode>> userPermissionCache = new ConcurrentHashMap<>();
private final PermissionPlugin plugin;

public PermissionManager() {
this.userPermissionCache = new HashMap<>();
this.plugin = this.findUpdater();
}

public boolean hasPermission(Player player, String permission) {
UUID uuid = player.getUniqueId();
Map<String, PermissionNode> nodes = this.userPermissionCache.get(uuid);
if(nodes == null) {
nodes = new HashMap<>();
nodes = new ConcurrentHashMap<>();
this.userPermissionCache.put(uuid, nodes);
}
PermissionNode node = nodes.get(permission);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package com.clubobsidian.foundry.permission;

import java.util.concurrent.atomic.AtomicBoolean;

public class PermissionNode {

private final String permission;
private boolean hasPermission;
private final AtomicBoolean hasPermission;

public PermissionNode(String permission, boolean hasPermission) {
this.permission = permission;
this.hasPermission = hasPermission;
this.hasPermission = new AtomicBoolean(hasPermission);
}

public String getPermission() {
return this.permission;
}

public boolean hasPermission() {
return this.hasPermission;
return this.hasPermission.get();
}

public void setHasPermission(boolean hasPermission) {
this.hasPermission = hasPermission;
this.hasPermission.set(hasPermission);
}
}

0 comments on commit 701a660

Please sign in to comment.