Make BlockApiLookup expose the API and context classes ()

* Make BlockApiLookup expose the API and context classes

* Test API and context classes in testmod
This commit is contained in:
Technici4n 2021-06-11 17:22:31 +02:00 committed by Player
parent 18ef9af8ec
commit 1d383bb698
4 changed files with 31 additions and 1 deletions
fabric-api-lookup-api-v1
build.gradle
src
main/java/net/fabricmc/fabric
api/lookup/v1/block
impl/lookup/block
testmod/java/net/fabricmc/fabric/test/lookup

View file

@ -1,5 +1,5 @@
archivesBaseName = "fabric-api-lookup-api-v1"
version = getSubprojectVersion(project, "1.0.2")
version = getSubprojectVersion(project, "1.1.0")
moduleDependencies(project, [
'fabric-api-base',

View file

@ -217,6 +217,16 @@ public interface BlockApiLookup<A, C> {
*/
void registerFallback(BlockApiProvider<A, C> fallbackProvider);
/**
* Return the API class of this lookup.
*/
Class<A> apiClass();
/**
* Return the context class of this lookup.
*/
Class<C> contextClass();
@FunctionalInterface
interface BlockApiProvider<A, C> {
/**

View file

@ -48,12 +48,14 @@ public final class BlockApiLookupImpl<A, C> implements BlockApiLookup<A, C> {
}
private final Class<A> apiClass;
private final Class<C> contextClass;
private final ApiProviderMap<Block, BlockApiProvider<A, C>> providerMap = ApiProviderMap.create();
private final List<BlockApiProvider<A, C>> fallbackProviders = new CopyOnWriteArrayList<>();
@SuppressWarnings("unchecked")
private BlockApiLookupImpl(Class<?> apiClass, Class<?> contextClass) {
this.apiClass = (Class<A>) apiClass;
this.contextClass = (Class<C>) contextClass;
}
@Nullable
@ -172,6 +174,16 @@ public final class BlockApiLookupImpl<A, C> implements BlockApiLookup<A, C> {
fallbackProviders.add(fallbackProvider);
}
@Override
public Class<A> apiClass() {
return apiClass;
}
@Override
public Class<C> contextClass() {
return contextClass;
}
@Nullable
public BlockApiProvider<A, C> getProvider(Block block) {
return providerMap.get(block);

View file

@ -83,6 +83,14 @@ public class FabricApiLookupTest implements ModInitializer {
BlockApiLookup<Void, Void> wrongInsertable = BlockApiLookup.get(new Identifier("testmod:item_insertable"), Void.class, Void.class);
wrongInsertable.registerFallback((world, pos, state, be, nocontext) -> null);
}, "The registry should have prevented creation of another instance with different classes, but same id.");
if (insertable2.apiClass() != ItemInsertable.class) {
throw new AssertionError("Incorrect API class was returned.");
}
if (insertable2.contextClass() != Direction.class) {
throw new AssertionError("Incorrect context class was returned.");
}
}
private static void testSelfRegistration() {