Skip to content

Commit

Permalink
sync MetadataStoreBase & PermissibleBase
Browse files Browse the repository at this point in the history
Good night
  • Loading branch information
iamnoksio committed Jan 31, 2023
1 parent e32b6e8 commit 9a7436e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
40 changes: 25 additions & 15 deletions nPaper-API/src/main/java/org/bukkit/metadata/MetadataStoreBase.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.bukkit.metadata;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.Validate;
import org.bukkit.plugin.Plugin;

import java.util.*;

public abstract class MetadataStoreBase<T> {
private Map<String, Map<Plugin, MetadataValue>> metadataMap = new HashMap<String, Map<Plugin, MetadataValue>>();
private Map<String, Map<Plugin, MetadataValue>> metadataMap = new ConcurrentHashMap<String, Map<Plugin, MetadataValue>>();

/**
* Adds a metadata value to an object. Each metadata value is owned by a
Expand Down Expand Up @@ -40,7 +46,9 @@ public synchronized void setMetadata(T subject, String metadataKey, MetadataValu
entry = new WeakHashMap<Plugin, MetadataValue>(1);
metadataMap.put(key, entry);
}
entry.put(owningPlugin, newMetadataValue);
synchronized (entry) {
entry.put(owningPlugin, newMetadataValue);
}
}

/**
Expand All @@ -53,14 +61,14 @@ public synchronized void setMetadata(T subject, String metadataKey, MetadataValu
* requested value.
* @see MetadataStore#getMetadata(Object, String)
*/
public synchronized List<MetadataValue> getMetadata(T subject, String metadataKey) {
public List<MetadataValue> getMetadata(T subject, String metadataKey) {
String key = disambiguate(subject, metadataKey);
if (metadataMap.containsKey(key)) {
Collection<MetadataValue> values = metadataMap.get(key).values();
Map<Plugin, MetadataValue> entry = metadataMap.get(key);
if (entry != null) {
Collection<MetadataValue> values = entry.values();
return Collections.unmodifiableList(new ArrayList<MetadataValue>(values));
} else {
return Collections.emptyList();
}
return Collections.emptyList();
}

/**
Expand All @@ -71,7 +79,7 @@ public synchronized List<MetadataValue> getMetadata(T subject, String metadataKe
* @param metadataKey the unique metadata key being queried.
* @return the existence of the metadataKey within subject.
*/
public synchronized boolean hasMetadata(T subject, String metadataKey) {
public boolean hasMetadata(T subject, String metadataKey) {
String key = disambiguate(subject, metadataKey);
return metadataMap.containsKey(key);
}
Expand All @@ -87,17 +95,19 @@ public synchronized boolean hasMetadata(T subject, String metadataKey) {
* org.bukkit.plugin.Plugin)
* @throws IllegalArgumentException If plugin is null
*/
public synchronized void removeMetadata(T subject, String metadataKey, Plugin owningPlugin) {
public void removeMetadata(T subject, String metadataKey, Plugin owningPlugin) {
Validate.notNull(owningPlugin, "Plugin cannot be null");
String key = disambiguate(subject, metadataKey);
Map<Plugin, MetadataValue> entry = metadataMap.get(key);
if (entry == null) {
return;
}

entry.remove(owningPlugin);
if (entry.isEmpty()) {
metadataMap.remove(key);
synchronized (entry) {
entry.remove(owningPlugin);
if (entry.isEmpty()) {
metadataMap.remove(key);
}
}
}

Expand All @@ -110,7 +120,7 @@ public synchronized void removeMetadata(T subject, String metadataKey, Plugin ow
* @see MetadataStore#invalidateAll(org.bukkit.plugin.Plugin)
* @throws IllegalArgumentException If plugin is null
*/
public synchronized void invalidateAll(Plugin owningPlugin) {
public void invalidateAll(Plugin owningPlugin) {
Validate.notNull(owningPlugin, "Plugin cannot be null");
for (Map<Plugin, MetadataValue> values : metadataMap.values()) {
if (values.containsKey(owningPlugin)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ public PermissibleBase(ServerOperator opable) {
public boolean isOp() {
if (opable == null) {
return false;
} else {
return opable.isOp();
}
return opable.isOp();
}

public void setOp(boolean value) {
Expand All @@ -49,15 +48,13 @@ public boolean isPermissionSet(String name) {
if (name == null) {
throw new IllegalArgumentException("Permission name cannot be null");
}

return permissions.containsKey(name.toLowerCase());
}

public boolean isPermissionSet(Permission perm) {
if (perm == null) {
throw new IllegalArgumentException("Permission cannot be null");
}

return isPermissionSet(perm.getName());
}

Expand All @@ -68,17 +65,16 @@ public boolean hasPermission(String inName) {

String name = inName.toLowerCase();

if (isPermissionSet(name)) {
return permissions.get(name).getValue();
} else {
Permission perm = Bukkit.getServer().getPluginManager().getPermission(name);
PermissionAttachmentInfo info = permissions.get(name);
if (info != null) {
return info.getValue();
}
Permission perm = Bukkit.getServer().getPluginManager().getPermission(name);

if (perm != null) {
return perm.getDefault().getValue(isOp());
} else {
return Permission.DEFAULT_PERMISSION.getValue(isOp());
}
if (perm != null) {
return perm.getDefault().getValue(isOp());
}
return Permission.DEFAULT_PERMISSION.getValue(isOp());
}

public boolean hasPermission(Permission perm) {
Expand All @@ -88,13 +84,14 @@ public boolean hasPermission(Permission perm) {

String name = perm.getName().toLowerCase();

if (isPermissionSet(name)) {
return permissions.get(name).getValue();
PermissionAttachmentInfo info = permissions.get(name);
if (info != null) {
return info.getValue();
}
return perm.getDefault().getValue(isOp());
}

public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value) {
public synchronized PermissionAttachment addAttachment(Plugin plugin, String name, boolean value) {
if (name == null) {
throw new IllegalArgumentException("Permission name cannot be null");
} else if (plugin == null) {
Expand All @@ -111,7 +108,7 @@ public PermissionAttachment addAttachment(Plugin plugin, String name, boolean va
return result;
}

public PermissionAttachment addAttachment(Plugin plugin) {
public synchronized PermissionAttachment addAttachment(Plugin plugin) {
if (plugin == null) {
throw new IllegalArgumentException("Plugin cannot be null");
} else if (!plugin.isEnabled()) {
Expand All @@ -126,7 +123,7 @@ public PermissionAttachment addAttachment(Plugin plugin) {
return result;
}

public void removeAttachment(PermissionAttachment attachment) {
public synchronized void removeAttachment(PermissionAttachment attachment) {
if (attachment == null) {
throw new IllegalArgumentException("Attachment cannot be null");
}
Expand All @@ -145,7 +142,7 @@ public void removeAttachment(PermissionAttachment attachment) {
}
}

public void recalculatePermissions() {
public synchronized void recalculatePermissions() {
clearPermissions();
Set<Permission> defaults = Bukkit.getServer().getPluginManager().getDefaultPermissions(isOp());
Bukkit.getServer().getPluginManager().subscribeToDefaultPerms(isOp(), parent);
Expand Down Expand Up @@ -192,7 +189,7 @@ private void calculateChildPermissions(Map<String, Boolean> children, boolean in
}
}

public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks) {
public synchronized PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks) {
if (name == null) {
throw new IllegalArgumentException("Permission name cannot be null");
} else if (plugin == null) {
Expand All @@ -210,7 +207,7 @@ public PermissionAttachment addAttachment(Plugin plugin, String name, boolean va
return result;
}

public PermissionAttachment addAttachment(Plugin plugin, int ticks) {
public synchronized PermissionAttachment addAttachment(Plugin plugin, int ticks) {
if (plugin == null) {
throw new IllegalArgumentException("Plugin cannot be null");
} else if (!plugin.isEnabled()) {
Expand All @@ -228,7 +225,7 @@ public PermissionAttachment addAttachment(Plugin plugin, int ticks) {
}
}

public Set<PermissionAttachmentInfo> getEffectivePermissions() {
public synchronized Set<PermissionAttachmentInfo> getEffectivePermissions() {
return new HashSet<PermissionAttachmentInfo>(permissions.values());
}

Expand Down

0 comments on commit 9a7436e

Please sign in to comment.