Repository navigation
Harden item and effect compatibility #300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ff00749
Harden reflective Nexo item integration
BenCodez 57c1102
Centralize effect compatibility parsing
BenCodez 1a49cc4
Normalize boss bar compatibility values
BenCodez c53a15d
Test custom item and effect compatibility
BenCodez 8ed4bfb
Share Nexo reflection cache across item lookups
BenCodez 422a74b
Test shared Nexo reflection cache
BenCodez 5a1e040
Avoid retaining Nexo class loaders from weak cache
BenCodez 6c808bf
Test weak Nexo cache values
BenCodez 088793d
Keep production Nexo reflection cache stable
BenCodez d4bce69
Cover stable default Nexo cache lifetime
BenCodez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 101 additions & 10 deletions
111
AdvancedCore/src/main/java/com/bencodez/advancedcore/api/item/NexoItemHandle.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,120 @@ | ||
|
|
||
| package com.bencodez.advancedcore.api.item; | ||
|
|
||
| import java.lang.ref.WeakReference; | ||
| import java.lang.reflect.Method; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.WeakHashMap; | ||
|
|
||
| import org.bukkit.inventory.ItemStack; | ||
|
|
||
| import com.bencodez.advancedcore.AdvancedCorePlugin; | ||
|
|
||
| /** | ||
| * Reflection-backed Nexo integration kept isolated from the rest of ItemBuilder. | ||
| */ | ||
| public class NexoItemHandle { | ||
| private static final Map<String, ReflectionCache> DEFAULT_CACHES = new HashMap<>(); | ||
| private static final Map<ClassLoader, Map<String, WeakReference<ReflectionCache>>> CACHES = new WeakHashMap<>(); | ||
|
|
||
| private final ReflectionCache cache; | ||
|
|
||
| public NexoItemHandle() { | ||
| this(NexoItemHandle.class.getClassLoader(), "com.nexomc.nexo.api.NexoItems", "com.nexomc.nexo.items.ItemBuilder"); | ||
| } | ||
|
|
||
| protected NexoItemHandle(ClassLoader classLoader, String apiClassName, String builderClassName) { | ||
| ClassLoader effectiveClassLoader = classLoader == null ? NexoItemHandle.class.getClassLoader() : classLoader; | ||
| cache = sharedCache(effectiveClassLoader, apiClassName, builderClassName); | ||
| } | ||
|
|
||
| public ItemStack getItem(String item) { | ||
| if (item == null || item.isEmpty()) { | ||
| return null; | ||
| } | ||
| try { | ||
| Class<?> nexoItemsClass = Class.forName("com.nexomc.nexo.api.NexoItems"); | ||
| Method itemFromIdMethod = nexoItemsClass.getMethod("itemFromId", String.class); | ||
| Object itemBuilder = itemFromIdMethod.invoke(null, item); | ||
| if (itemBuilder != null) { | ||
| Class<?> itemBuilderClass = Class.forName("com.nexomc.nexo.items.ItemBuilder"); | ||
| Method buildMethod = itemBuilderClass.getMethod("build"); | ||
| Object builtItem = buildMethod.invoke(itemBuilder); | ||
| ensureMethods(); | ||
| Object itemBuilder = cache.itemFromIdMethod.invoke(null, item); | ||
| if (itemBuilder == null) { | ||
| return null; | ||
| } | ||
| Object builtItem = cache.buildMethod.invoke(itemBuilder); | ||
| if (builtItem instanceof ItemStack) { | ||
| return (ItemStack) builtItem; | ||
| } | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| logFailureOnce("Nexo build() returned an unexpected type: " | ||
| + (builtItem == null ? "null" : builtItem.getClass().getName()), null); | ||
| } catch (ReflectiveOperationException | LinkageError | RuntimeException e) { | ||
| logFailureOnce("Unable to use the installed Nexo item API", e); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private void ensureMethods() throws ReflectiveOperationException { | ||
| if (cache.itemFromIdMethod != null && cache.buildMethod != null) { | ||
| return; | ||
| } | ||
| synchronized (cache) { | ||
| if (cache.itemFromIdMethod == null) { | ||
| Class<?> nexoItemsClass = Class.forName(cache.apiClassName, true, cache.classLoader); | ||
| cache.itemFromIdMethod = nexoItemsClass.getMethod("itemFromId", String.class); | ||
| } | ||
| if (cache.buildMethod == null) { | ||
| Class<?> itemBuilderClass = Class.forName(cache.builderClassName, true, cache.classLoader); | ||
| cache.buildMethod = itemBuilderClass.getMethod("build"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void logFailureOnce(String message, Throwable throwable) { | ||
| synchronized (cache) { | ||
| if (cache.compatibilityFailureLogged) { | ||
| return; | ||
| } | ||
| cache.compatibilityFailureLogged = true; | ||
| } | ||
| AdvancedCorePlugin plugin = AdvancedCorePlugin.getInstance(); | ||
| if (plugin != null) { | ||
| plugin.getLogger().warning(message); | ||
| if (throwable != null) { | ||
| plugin.debug(throwable); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static synchronized ReflectionCache sharedCache(ClassLoader classLoader, String apiClassName, | ||
| String builderClassName) { | ||
| String key = apiClassName + '\0' + builderClassName; | ||
| if (classLoader == NexoItemHandle.class.getClassLoader()) { | ||
| return DEFAULT_CACHES.computeIfAbsent(key, | ||
| ignored -> new ReflectionCache(classLoader, apiClassName, builderClassName)); | ||
| } | ||
|
|
||
| Map<String, WeakReference<ReflectionCache>> classLoaderCaches = CACHES.computeIfAbsent(classLoader, | ||
| ignored -> new HashMap<>()); | ||
| WeakReference<ReflectionCache> reference = classLoaderCaches.get(key); | ||
| ReflectionCache existing = reference == null ? null : reference.get(); | ||
| if (existing != null) { | ||
| return existing; | ||
| } | ||
|
|
||
| ReflectionCache created = new ReflectionCache(classLoader, apiClassName, builderClassName); | ||
| classLoaderCaches.put(key, new WeakReference<>(created)); | ||
| return created; | ||
| } | ||
|
|
||
| private static final class ReflectionCache { | ||
| private final ClassLoader classLoader; | ||
| private final String apiClassName; | ||
| private final String builderClassName; | ||
| private volatile Method itemFromIdMethod; | ||
| private volatile Method buildMethod; | ||
| private boolean compatibilityFailureLogged; | ||
|
|
||
| private ReflectionCache(ClassLoader classLoader, String apiClassName, String builderClassName) { | ||
| this.classLoader = classLoader; | ||
|
BenCodez marked this conversation as resolved.
Resolved
|
||
| this.apiClassName = apiClassName; | ||
| this.builderClassName = builderClassName; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.