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 d6546a9254..f46e1ec906 100644 --- a/paper-api/src/main/java/org/bukkit/metadata/MetadataStoreBase.java +++ b/paper-api/src/main/java/org/bukkit/metadata/MetadataStoreBase.java @@ -6,7 +6,7 @@ import org.bukkit.plugin.Plugin; import java.util.*; public abstract class MetadataStoreBase { - private Map> metadataMap = new HashMap>(); + private Map> metadataMap = new HashMap>(); /** * Adds a metadata value to an object. Each metadata value is owned by a specific{@link Plugin}. @@ -25,23 +25,16 @@ public abstract class MetadataStoreBase { * @throws IllegalArgumentException If value is null, or the owning plugin is null */ public synchronized void setMetadata(T subject, String metadataKey, MetadataValue newMetadataValue) { + Plugin owningPlugin = newMetadataValue.getOwningPlugin(); Validate.notNull(newMetadataValue, "Value cannot be null"); - Validate.notNull(newMetadataValue.getOwningPlugin(), "Plugin cannot be null"); + Validate.notNull(owningPlugin, "Plugin cannot be null"); String key = disambiguate(subject, metadataKey); - if (!metadataMap.containsKey(key)) { - metadataMap.put(key, new ArrayList()); + Map entry = metadataMap.get(key); + if (entry == null) { + entry = new WeakHashMap(1); + metadataMap.put(key, entry); } - // we now have a list of subject's metadata for the given metadata key. If newMetadataValue's owningPlugin - // is found in this list, replace the value rather than add a new one. - List metadataList = metadataMap.get(key); - for (int i = 0; i < metadataList.size(); i++) { - if (metadataList.get(i).getOwningPlugin().equals(newMetadataValue.getOwningPlugin())) { - metadataList.set(i, newMetadataValue); - return; - } - } - // we didn't find a duplicate...add the new metadata value - metadataList.add(newMetadataValue); + entry.put(owningPlugin, newMetadataValue); } /** @@ -56,7 +49,8 @@ public abstract class MetadataStoreBase { public synchronized List getMetadata(T subject, String metadataKey) { String key = disambiguate(subject, metadataKey); if (metadataMap.containsKey(key)) { - return Collections.unmodifiableList(metadataMap.get(key)); + Collection values = metadataMap.get(key).values(); + return Collections.unmodifiableList(new ArrayList(values)); } else { return Collections.emptyList(); } @@ -86,15 +80,14 @@ public abstract class MetadataStoreBase { public synchronized void removeMetadata(T subject, String metadataKey, Plugin owningPlugin) { Validate.notNull(owningPlugin, "Plugin cannot be null"); String key = disambiguate(subject, metadataKey); - List metadataList = metadataMap.get(key); - if (metadataList == null) return; - for (int i = 0; i < metadataList.size(); i++) { - if (metadataList.get(i).getOwningPlugin().equals(owningPlugin)) { - metadataList.remove(i); - if (metadataList.isEmpty()) { - metadataMap.remove(key); - } - } + Map entry = metadataMap.get(key); + if (entry == null) { + return; + } + + entry.remove(owningPlugin); + if (entry.isEmpty()) { + metadataMap.remove(key); } } @@ -108,11 +101,9 @@ public abstract class MetadataStoreBase { */ public synchronized void invalidateAll(Plugin owningPlugin) { Validate.notNull(owningPlugin, "Plugin cannot be null"); - for (List values : metadataMap.values()) { - for (MetadataValue value : values) { - if (value.getOwningPlugin().equals(owningPlugin)) { - value.invalidate(); - } + for (Map values : metadataMap.values()) { + if (values.containsKey(owningPlugin)) { + values.get(owningPlugin).invalidate(); } } } diff --git a/paper-api/src/main/java/org/bukkit/metadata/MetadataValueAdapter.java b/paper-api/src/main/java/org/bukkit/metadata/MetadataValueAdapter.java index c4b8b392aa..3b83380f56 100644 --- a/paper-api/src/main/java/org/bukkit/metadata/MetadataValueAdapter.java +++ b/paper-api/src/main/java/org/bukkit/metadata/MetadataValueAdapter.java @@ -1,5 +1,7 @@ package org.bukkit.metadata; +import java.lang.ref.WeakReference; + import org.apache.commons.lang.Validate; import org.bukkit.plugin.Plugin; import org.bukkit.util.NumberConversions; @@ -13,15 +15,15 @@ import org.bukkit.util.NumberConversions; * */ public abstract class MetadataValueAdapter implements MetadataValue { - protected final Plugin owningPlugin; + protected final WeakReference owningPlugin; protected MetadataValueAdapter(Plugin owningPlugin) { Validate.notNull(owningPlugin, "owningPlugin cannot be null"); - this.owningPlugin = owningPlugin; + this.owningPlugin = new WeakReference(owningPlugin); } public Plugin getOwningPlugin() { - return owningPlugin; + return owningPlugin.get(); } public int asInt() {