From 5abab982c767259124377805abefe72db57f1f28 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Sun, 11 Aug 2024 11:47:03 +0100 Subject: [PATCH] Allow registering more generic detail providers Allow registering details providers matching any super type, not just the exact type. This is mostly useful for 1.21, where we can have providers for any DataComponentHolder, not just item stacks. --- .../api/detail/BasicItemDetailProvider.java | 23 ++++++++----------- .../api/detail/DetailRegistry.java | 2 +- .../impl/detail/DetailRegistryImpl.java | 4 ++-- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/projects/common-api/src/main/java/dan200/computercraft/api/detail/BasicItemDetailProvider.java b/projects/common-api/src/main/java/dan200/computercraft/api/detail/BasicItemDetailProvider.java index bf706f0db..c709b79f6 100644 --- a/projects/common-api/src/main/java/dan200/computercraft/api/detail/BasicItemDetailProvider.java +++ b/projects/common-api/src/main/java/dan200/computercraft/api/detail/BasicItemDetailProvider.java @@ -13,7 +13,7 @@ import java.util.Map; import java.util.Objects; /** - * An item detail provider for {@link ItemStack}'s whose {@link Item} has a specific type. + * An item detail provider for {@link ItemStack}s whose {@link Item} has a specific type. * * @param The type the stack's item must have. */ @@ -22,7 +22,7 @@ public abstract class BasicItemDetailProvider implements DetailProvider implements DetailProvider implements DetailProvider data, ItemStack stack, T item - ); + public abstract void provideDetails(Map data, ItemStack stack, T item); @Override - public void provideDetails(Map data, ItemStack stack) { + public final void provideDetails(Map data, ItemStack stack) { var item = stack.getItem(); if (!itemType.isInstance(item)) return; - // If `namespace` is specified, insert into a new data map instead of the existing one. - Map child = namespace == null ? data : new HashMap<>(); - - provideDetails(child, stack, itemType.cast(item)); - - if (namespace != null) { + if (namespace == null) { + provideDetails(data, stack, itemType.cast(item)); + } else { + Map child = new HashMap<>(); + provideDetails(child, stack, itemType.cast(item)); data.put(namespace, child); } } diff --git a/projects/common-api/src/main/java/dan200/computercraft/api/detail/DetailRegistry.java b/projects/common-api/src/main/java/dan200/computercraft/api/detail/DetailRegistry.java index 7c355e8b0..4f21ff81f 100644 --- a/projects/common-api/src/main/java/dan200/computercraft/api/detail/DetailRegistry.java +++ b/projects/common-api/src/main/java/dan200/computercraft/api/detail/DetailRegistry.java @@ -26,7 +26,7 @@ public interface DetailRegistry { * @param provider The detail provider to register. * @see DetailProvider */ - void addProvider(DetailProvider provider); + void addProvider(DetailProvider provider); /** * Compute basic details about an object. This is cheaper than computing all details operation, and so is suitable diff --git a/projects/common/src/main/java/dan200/computercraft/impl/detail/DetailRegistryImpl.java b/projects/common/src/main/java/dan200/computercraft/impl/detail/DetailRegistryImpl.java index 3f16b6893..31d42bba2 100644 --- a/projects/common/src/main/java/dan200/computercraft/impl/detail/DetailRegistryImpl.java +++ b/projects/common/src/main/java/dan200/computercraft/impl/detail/DetailRegistryImpl.java @@ -15,7 +15,7 @@ import java.util.*; * @param The type of object that this registry provides details for. */ public class DetailRegistryImpl implements DetailRegistry { - private final Collection> providers = new ArrayList<>(); + private final Collection> providers = new ArrayList<>(); private final DetailProvider basic; public DetailRegistryImpl(DetailProvider basic) { @@ -24,7 +24,7 @@ public class DetailRegistryImpl implements DetailRegistry { } @Override - public synchronized void addProvider(DetailProvider provider) { + public synchronized void addProvider(DetailProvider provider) { Objects.requireNonNull(provider, "provider cannot be null"); if (!providers.contains(provider)) providers.add(provider); }