Skip to content

Commit 2bfa67e

Browse files
committed
[block entities] implement basic rendering API
1 parent 9bf18d0 commit 2bfa67e

11 files changed

Lines changed: 208 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Block Entities Rendering API
2+
3+
The Block Entities Rendering API provides events and utilities for registering and working with block entity renderers.
4+
5+
## Registering Custom Block Entity Renderers
6+
7+
Block entity renderer registration should be done in a listener to the `REGISTER_BLOCK_ENTITY_RENDERERS` event.
8+
9+
An example is shown below.
10+
11+
```java
12+
package com.example;
13+
14+
import net.ornithemc.osl.blockentities.api.client.BlockEntityRenderingEvents;
15+
import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer;
16+
17+
public class ExampleInitializer implements ClientModInitializer {
18+
19+
@Override
20+
public void initClient() {
21+
BlockEntityRenderingEvents.REGISTER_BLOCK_ENTITY_RENDERERS.register(registry -> {
22+
registry.register(CookieBlockEntity.class, CookieBlockRenderer::new);
23+
});
24+
}
25+
}
26+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
setUpModule(project)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
environment = client
2+
min_mc_version = a1.0.1_01
3+
max_mc_version = 1.14.4
4+
5+
minecraft_version = 1.7.2
6+
raven_build = 1
7+
sparrow_build = 1
8+
nests_build = 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.ornithemc.osl.blockentities.api.client;
2+
3+
import java.util.function.Supplier;
4+
5+
import net.minecraft.block.entity.BlockEntity;
6+
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
7+
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
8+
9+
/**
10+
* A wrapper around the {@linkplain BlockEntityRenderDispatcher} renderers map.
11+
*/
12+
@FunctionalInterface
13+
public interface BlockEntityRendererRegistry {
14+
15+
/**
16+
* Registers the given block entity renderer for the given block entity type.
17+
*
18+
* @param <T> the block entity type for which to register the renderer.
19+
* @param type the block entity type class.
20+
* @param factory the factory for the block entity renderer to register.
21+
*/
22+
<T extends BlockEntity> void register(Class<T> type, Supplier<BlockEntityRenderer<? super T>> factory);
23+
24+
/**
25+
* Registers the given block entity renderer for the given block entity type.
26+
* <p>
27+
* NOTE: this method only exists because generic type signatures are stripped
28+
* from all Minecraft jars from 1.8.2-pre4 and below, which makes using
29+
* {@linkplain #register(Class, Supplier)} impossible without special patches to
30+
* bring these generic type signatures back. Progress on this work is being
31+
* made, but has not yet been completed. In the meantime, this method exists to
32+
* allow block entity renderers to be registered without compile time type
33+
* safety checks.
34+
*
35+
* @param <T> the block entity type for which to register the renderer.
36+
* @param type the block entity type class.
37+
* @param factory the factory for the block entity renderer to register.
38+
*
39+
* @deprecated use {@linkplain #register(Class, Supplier)} if possible
40+
*/
41+
@SuppressWarnings({ "unchecked", "rawtypes" })
42+
@Deprecated
43+
default <T extends BlockEntity> void registerUnsafe(Class<T> type, Supplier<BlockEntityRenderer> factory) {
44+
this.register(type, (Supplier) factory);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net.ornithemc.osl.blockentities.api.client;
2+
3+
import java.util.function.Consumer;
4+
5+
import net.ornithemc.osl.core.api.events.Event;
6+
7+
/**
8+
* Events related to the block entities rendering lifecycle.
9+
*/
10+
public final class BlockEntityRenderingEvents {
11+
12+
/**
13+
* This event is invoked upon client start-up, after Vanilla block entity renderer registration is finished.
14+
*
15+
* <p>
16+
* Custom block entity renderer registration should be done in a listener of this event.
17+
*
18+
* <p>
19+
* Listeners to this event should be registered in your mod's entrypoint,
20+
* and can be done as follows:
21+
*
22+
* <pre>
23+
* {@code
24+
* BlockEntityRenderingEvents.REGISTER_BLOCK_ENTITY_RENDERERS.register(registry -> {
25+
* registry.register(CookieBlockEntity.class, CookieBlockRenderer::new);
26+
* });
27+
* }
28+
* </pre>
29+
*
30+
* @see BlockEntityRendererRegistry
31+
*/
32+
public static final Event<Consumer<BlockEntityRendererRegistry>> REGISTER_BLOCK_ENTITY_RENDERERS = Event.consumer();
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package net.ornithemc.osl.blockentities.impl.mixin.client;
2+
3+
import java.util.Map;
4+
import java.util.function.Supplier;
5+
6+
import org.spongepowered.asm.mixin.Final;
7+
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.Shadow;
9+
import org.spongepowered.asm.mixin.Unique;
10+
import org.spongepowered.asm.mixin.injection.At;
11+
import org.spongepowered.asm.mixin.injection.Inject;
12+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
13+
14+
import net.minecraft.block.entity.BlockEntity;
15+
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
16+
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
17+
18+
import net.ornithemc.osl.blockentities.api.client.BlockEntityRenderingEvents;
19+
20+
@Mixin(BlockEntityRenderDispatcher.class)
21+
public class BlockEntityRenderDispatcherMixin {
22+
23+
@Shadow @Final
24+
private Map<Class<? extends BlockEntity>, BlockEntityRenderer<? extends BlockEntity>> renderers;
25+
26+
@Inject(
27+
method = "<init>",
28+
at = @At(
29+
value = "INVOKE",
30+
target = "Ljava/util/Map;values()Ljava/util/Collection;"
31+
)
32+
)
33+
private void osl$blockentities$registerBlockEntityRenderers(CallbackInfo ci) {
34+
BlockEntityRenderingEvents.REGISTER_BLOCK_ENTITY_RENDERERS.invoker().accept(this::register);
35+
}
36+
37+
@Unique
38+
private <T extends BlockEntity> void register(Class<T> type, Supplier<BlockEntityRenderer<? super T>> factory) {
39+
this.renderers.put(type, factory.get());
40+
}
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"schemaVersion": 1,
3+
"id": "osl-blockentities-rendering",
4+
"version": "0.1.0+mca1.0.1_01-mc1.14.4",
5+
"environment": "client",
6+
"mixins": [
7+
"osl.blockentities-rendering.mixins.json"
8+
],
9+
"depends": {
10+
"fabricloader": "\u003e\u003d0.18.0",
11+
"minecraft": "\u003e\u003d1.0.0-alpha.0.1.1 \u003c\u003d1.14.4",
12+
"osl-core": "\u003e\u003d0.9.0",
13+
"osl-entrypoints": "\u003e\u003d0.5.0"
14+
},
15+
"name": "OSL Block Entities Rendering",
16+
"description": "Block Entities Rendering API and events.",
17+
"authors": [
18+
"OrnitheMC"
19+
],
20+
"contact": {
21+
"homepage": "https://ornithemc.net/",
22+
"issues": "https://github.com/OrnitheMC/ornithe-standard-libraries/issues",
23+
"sources": "https://github.com/OrnitheMC/ornithe-standard-libraries"
24+
},
25+
"license": "Apache-2.0"
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"required": true,
3+
"minVersion": "0.8",
4+
"package": "net.ornithemc.osl.blockentities.impl.mixin",
5+
"compatibilityLevel": "JAVA_8",
6+
"mixins": [
7+
],
8+
"client": [
9+
"client.BlockEntityRenderDispatcherMixin"
10+
],
11+
"server": [
12+
],
13+
"injectors": {
14+
"defaultRequire": 1
15+
}
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
setUpLibrary(project)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
library_id = blockentities-rendering
2+
library_name = Block Entities Rendering
3+
library_description = Block Entities Rendering API and events.
4+
library_version = 0.1.0
5+
6+
osl_dependencies = core:0.9.0,entrypoints:0.5.0

0 commit comments

Comments
 (0)