Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions libraries/blockentities-rendering/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Block Entities Rendering API

The Block Entities Rendering API provides events and utilities for registering and working with block entity renderers.

## Registering Custom Block Entity Renderers

Block entity renderer registration should be done in a listener to the `REGISTER_BLOCK_ENTITY_RENDERERS` event.

An example is shown below.

```java
package com.example;

import net.ornithemc.osl.blockentities.api.client.BlockEntityRenderingEvents;
import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer;

public class ExampleInitializer implements ClientModInitializer {

@Override
public void initClient() {
BlockEntityRenderingEvents.REGISTER_BLOCK_ENTITY_RENDERERS.register(registry -> {
registry.register(CookieBlockEntity.class, CookieBlockRenderer::new);
});
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpModule(project)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
environment = client
min_mc_version = a1.0.1_01
max_mc_version = 1.14.4

minecraft_version = 1.7.2
raven_build = 1
sparrow_build = 1
nests_build = 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.ornithemc.osl.blockentities.api.client;

import java.util.function.Supplier;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;

/**
* A wrapper around the {@linkplain BlockEntityRenderDispatcher} renderers map.
*/
@FunctionalInterface
public interface BlockEntityRendererRegistry {

/**
* Registers the given block entity renderer for the given block entity type.
*
* @param <T> the block entity type for which to register the renderer.
* @param type the block entity type class.
* @param factory the factory for the block entity renderer to register.
*/
<T extends BlockEntity> void register(Class<T> type, Supplier<BlockEntityRenderer<? super T>> factory);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.ornithemc.osl.blockentities.api.client;

import java.util.function.Consumer;

import net.ornithemc.osl.core.api.events.Event;

/**
* Events related to the block entities rendering lifecycle.
*/
public final class BlockEntityRenderingEvents {

/**
* This event is invoked upon client start-up, after Vanilla block entity renderer registration is finished.
*
* <p>
* Custom block entity renderer registration should be done in a listener of this event.
*
* <p>
* Listeners to this event should be registered in your mod's entrypoint,
* and can be done as follows:
*
* <pre>
* {@code
* BlockEntityRenderingEvents.REGISTER_BLOCK_ENTITY_RENDERERS.register(registry -> {
* registry.register(CookieBlockEntity.class, CookieBlockRenderer::new);
* });
* }
* </pre>
*
* @see BlockEntityRendererRegistry
*/
public static final Event<Consumer<BlockEntityRendererRegistry>> REGISTER_BLOCK_ENTITY_RENDERERS = Event.consumer();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package net.ornithemc.osl.blockentities.impl.mixin.client;

import java.util.Map;
import java.util.function.Supplier;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;

import net.ornithemc.osl.blockentities.api.client.BlockEntityRenderingEvents;

@Mixin(BlockEntityRenderDispatcher.class)
public class BlockEntityRenderDispatcherMixin {

@Shadow @Final
private Map<Class<? extends BlockEntity>, BlockEntityRenderer<? extends BlockEntity>> renderers;

@Inject(
method = "<init>",
at = @At(
value = "INVOKE",
target = "Ljava/util/Map;values()Ljava/util/Collection;"
)
)
private void osl$blockentities$registerBlockEntityRenderers(CallbackInfo ci) {
BlockEntityRenderingEvents.REGISTER_BLOCK_ENTITY_RENDERERS.invoker().accept(this::register);
}

@Unique
private <T extends BlockEntity> void register(Class<T> type, Supplier<BlockEntityRenderer<? super T>> factory) {
this.renderers.put(type, factory.get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"schemaVersion": 1,
"id": "osl-blockentities-rendering",
"version": "0.1.0+mca1.0.1_01-mc1.14.4",
"environment": "client",
"mixins": [
"osl.blockentities-rendering.mixins.json"
],
"depends": {
"fabricloader": "\u003e\u003d0.18.0",
"minecraft": "\u003e\u003d1.0.0-alpha.0.1.1 \u003c\u003d1.14.4",
"osl-core": "\u003e\u003d0.9.0",
"osl-entrypoints": "\u003e\u003d0.5.0"
},
"name": "OSL Block Entities Rendering",
"description": "Block Entities Rendering API and events.",
"authors": [
"OrnitheMC"
],
"contact": {
"homepage": "https://ornithemc.net/",
"issues": "https://github.com/OrnitheMC/ornithe-standard-libraries/issues",
"sources": "https://github.com/OrnitheMC/ornithe-standard-libraries"
},
"license": "Apache-2.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"required": true,
"minVersion": "0.8",
"package": "net.ornithemc.osl.blockentities.impl.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
],
"client": [
"client.BlockEntityRenderDispatcherMixin"
],
"server": [
],
"injectors": {
"defaultRequire": 1
}
}
1 change: 1 addition & 0 deletions libraries/blockentities-rendering/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpLibrary(project)
6 changes: 6 additions & 0 deletions libraries/blockentities-rendering/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library_id = blockentities-rendering
library_name = Block Entities Rendering
library_description = Block Entities Rendering API and events.
library_version = 0.1.0

osl_dependencies = core:0.9.0,entrypoints:0.5.0
42 changes: 42 additions & 0 deletions libraries/blockentities/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Block Entities API

The Block Entities API provides events and utilities for registering and working with block entities.

## Registering Custom Block Entity Types

Block entity type registration should be done in a listener to the `REGISTER_BLOCK_ENTITY_TYPES` event. The `BlockEntityTypeRegistry` class provides utility methods for registering block entity types.

An example is shown below.

```java
package com.example;

import net.ornithemc.osl.blockentities.api.BlockEntityEvents;
import net.ornithemc.osl.entrypoints.api.ModInitializer;

public class ExampleInitializer implements ModInitializer {

@Override
public void init() {
BlockEntityEvents.REGISTER_BLOCK_ENTITY_TYPES.register(ExampleBlockEntityTypes::init);
}
}
```

```java
package com.example;

import net.minecraft.block.entity.BlockEntityType;

import net.ornithemc.osl.blockentities.api.BlockEntityTypeRegistry;
import net.ornithemc.osl.blockentities.api.BlockEntityTypes;
import net.ornithemc.osl.core.util.NamespacedIdentifiers;

public final class ExampleBlockEntityTypes {

public static final BlockEntityType<CookieBlockEntity> COOKIE = BlockEntityTypeRegistry.register(NamespacedIdentifiers.from("example", "cookie"), BlockEntityTypes.builder(CookieBlockEntity::new, ExampleBlocks.COOKIE));

public static void init() {
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpModule(project)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
min_mc_version = 1.13-pre5
max_mc_version = 18w31a

minecraft_version = 1.13
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.ornithemc.osl.blockentities.api;

import net.ornithemc.osl.core.api.events.Event;

/**
* Events related to the block entities lifecycle.
*/
public final class BlockEntityEvents {

/**
* This event is invoked upon game start-up, after Vanilla block entity registration is finished.
*
* <p>
* Custom block entity type registration should be done in a listener of this event.
* Helper methods for registering block entity types can be found in the {@linkplain
* BlockEntityTypeRegistry} class.
*
* <p>
* Listeners to this event should be registered in your mod's entrypoint,
* and can be done as follows:
*
* <pre>
* {@code
* BlockEntityEvents.REGISTER_BLOCK_ENTITY_TYPES.register(() -> {
* BlockEntityTypeRegistry.register(NamespacedIdentifiers.from("example", "cookie"), BlockEntityTypes.builder(CookieBlockEntity::new));
* });
* }
* </pre>
*
* @see BlockEntityTypeRegistry
* @see BlockEntityTypes
*/
public static final Event<Runnable> REGISTER_BLOCK_ENTITY_TYPES = Event.runnable();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package net.ornithemc.osl.blockentities.api;

import java.util.Set;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;

import net.ornithemc.osl.blockentities.impl.BlockEntityTypeRegistryImpl;
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.registries.api.registry.Registry;
import net.ornithemc.osl.registries.api.registry.ResourceKey;

/**
* Public access to the Block Entity Types registry.
*/
public final class BlockEntityTypeRegistry {

public static final Registry<BlockEntityType<?>> REGISTRY = BlockEntityTypeRegistryImpl.REGISTRY;

/**
* @return the numerical ID assigned to the given block entity type.
*/
public static int getId(BlockEntityType<?> type) {
return BlockEntityTypeRegistryImpl.getId(type);
}

/**
* @return the namespaced ID assigned to the given block entity type.
*/
public static NamespacedIdentifier getIdentifier(BlockEntityType<?> type) {
return BlockEntityTypeRegistryImpl.getIdentifier(type);
}

/**
* @return the resource key assigned to the given block entity type.
*/
public static ResourceKey<BlockEntityType<?>> getKey(BlockEntityType<?> type) {
return BlockEntityTypeRegistryImpl.getKey(type);
}

/**
* @return the block entity type mapped to the given numerical ID.
*/
public static BlockEntityType<?> getBlockEntityType(int id) {
return BlockEntityTypeRegistryImpl.getBlockEntityType(id);
}

/**
* @return the block entity type mapped to the given namespaced ID.
*/
public static BlockEntityType<?> getBlockEntityType(NamespacedIdentifier identifier) {
return BlockEntityTypeRegistryImpl.getBlockEntityType(identifier);
}

/**
* @return the block entity type mapped to the given resource key.
*/
public static BlockEntityType<?> getBlockEntityType(ResourceKey<BlockEntityType<?>> key) {
return BlockEntityTypeRegistryImpl.getBlockEntityType(key);
}

/**
* @return a set containing all namespaced IDs in the registry.
*/
public static Set<NamespacedIdentifier> identifierSet() {
return BlockEntityTypeRegistryImpl.identifierSet();
}

/**
* @return a set containing all resource keys in the registry.
*/
public static Set<ResourceKey<BlockEntityType<?>>> keySet() {
return BlockEntityTypeRegistryImpl.keySet();
}

/**
* @param <T> the block entity type.
* @param identifier the namespaced ID of the block entity type.
* @param type the builder for the block entity type to register.
* @return the registered block entity type.
*/
public static <T extends BlockEntity> BlockEntityType<T> register(NamespacedIdentifier identifier, BlockEntityType.Builder<T> type) {
return BlockEntityTypeRegistryImpl.register(identifier, type);
}

/**
* @param <T> the block entity type.
* @param key the resource key of the block entity type.
* @param type the builder for the block entity type to register.
* @return the registered block entity type.
*/
public static <T extends BlockEntity> BlockEntityType<T> register(ResourceKey<BlockEntityType<?>> key, BlockEntityType.Builder<T> type) {
return BlockEntityTypeRegistryImpl.register(key, type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.ornithemc.osl.blockentities.api;

import java.util.function.Supplier;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;

/**
* Utilities for constructing and modifying block entity types.
*/
public final class BlockEntityTypes {

/**
* @return a new block entity type builder with the given block entity factory.
*/
public static <T extends BlockEntity> BlockEntityType.Builder<T> builder(Supplier<? extends T> factory) {
return BlockEntityType.Builder.of(factory);
}
}
Loading
Loading