diff --git a/paper-api/src/main/java/org/bukkit/metadata/MetadataStoreBase.java b/paper-api/src/main/java/org/bukkit/metadata/MetadataStoreBase.java index 6886a7003f..a0698522f0 100644 --- a/paper-api/src/main/java/org/bukkit/metadata/MetadataStoreBase.java +++ b/paper-api/src/main/java/org/bukkit/metadata/MetadataStoreBase.java @@ -12,7 +12,7 @@ import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; public abstract class MetadataStoreBase { - private Map> metadataMap = new HashMap>(); + private Map> metadataMap = new java.util.concurrent.ConcurrentHashMap>(); // Paper /** * Adds a metadata value to an object. Each metadata value is owned by a @@ -46,7 +46,9 @@ public abstract class MetadataStoreBase { entry = new WeakHashMap(1); metadataMap.put(key, entry); } - entry.put(owningPlugin, newMetadataValue); + synchronized (entry) { + entry.put(owningPlugin, newMetadataValue); + } } /** @@ -60,10 +62,11 @@ public abstract class MetadataStoreBase { * @see MetadataStore#getMetadata(Object, String) */ @NotNull - public synchronized List getMetadata(@NotNull T subject, @NotNull String metadataKey) { + public List getMetadata(@NotNull T subject, @NotNull String metadataKey) { // Paper String key = disambiguate(subject, metadataKey); - if (metadataMap.containsKey(key)) { - Collection values = metadataMap.get(key).values(); + Map entry = metadataMap.get(key); + if (entry != null) { + Collection values = entry.values(); return Collections.unmodifiableList(new ArrayList(values)); } else { return Collections.emptyList(); @@ -78,7 +81,7 @@ public abstract class MetadataStoreBase { * @param metadataKey the unique metadata key being queried. * @return the existence of the metadataKey within subject. */ - public synchronized boolean hasMetadata(@NotNull T subject, @NotNull String metadataKey) { + public boolean hasMetadata(@NotNull T subject, @NotNull String metadataKey) { // Paper String key = disambiguate(subject, metadataKey); return metadataMap.containsKey(key); } @@ -94,17 +97,18 @@ public abstract class MetadataStoreBase { * @see MetadataStore#removeMetadata(Object, String, * org.bukkit.plugin.Plugin) */ - public synchronized void removeMetadata(@NotNull T subject, @NotNull String metadataKey, @NotNull Plugin owningPlugin) { + public void removeMetadata(@NotNull T subject, @NotNull String metadataKey, @NotNull Plugin owningPlugin) { // Paper Preconditions.checkArgument(owningPlugin != null, "Plugin cannot be null"); String key = disambiguate(subject, metadataKey); Map 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); + } } } @@ -117,7 +121,7 @@ public abstract class MetadataStoreBase { * @throws IllegalArgumentException If plugin is null * @see MetadataStore#invalidateAll(org.bukkit.plugin.Plugin) */ - public synchronized void invalidateAll(@NotNull Plugin owningPlugin) { + public void invalidateAll(@NotNull Plugin owningPlugin) { // Paper Preconditions.checkArgument(owningPlugin != null, "Plugin cannot be null"); for (Map values : metadataMap.values()) { if (values.containsKey(owningPlugin)) {