Post init API enhancement, automatic render registry (WIP)

This commit is contained in:
paulevsGitch 2021-07-22 23:38:32 +03:00
parent 50ccbace6d
commit 551bf6865c
4 changed files with 43 additions and 18 deletions

View file

@ -1,8 +1,19 @@
package ru.bclib.api; package ru.bclib.api;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.world.level.block.Block;
import ru.bclib.blocks.BaseChestBlock;
import ru.bclib.blocks.BaseSignBlock;
import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.client.render.BaseChestBlockEntityRenderer;
import ru.bclib.client.render.BaseSignBlockEntityRenderer;
import ru.bclib.interfaces.PostInitable; import ru.bclib.interfaces.PostInitable;
import ru.bclib.interfaces.RenderLayerProvider;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -14,16 +25,38 @@ public class PostInitAPI {
postInitFunctions.add(function); postInitFunctions.add(function);
} }
public static void postInit() { public static void postInit(boolean isClient) {
if (postInitFunctions == null) { if (postInitFunctions == null) {
return; return;
} }
postInitFunctions.forEach(function -> function.accept(null)); postInitFunctions.forEach(function -> function.accept(null));
Registry.BLOCK.forEach(block -> { Registry.BLOCK.forEach(block -> {
if (block instanceof PostInitable) { processBlockCommon(block);
((PostInitable) block).postInit(); if (isClient) {
processBlockClient(block);
} }
}); });
postInitFunctions = null; postInitFunctions = null;
} }
@Environment(EnvType.CLIENT)
private static void processBlockClient(Block block) {
if (block instanceof RenderLayerProvider) {
BCLRenderLayer layer = ((RenderLayerProvider) block).getRenderLayer();
if (layer == BCLRenderLayer.CUTOUT) BlockRenderLayerMap.INSTANCE.putBlock(block, RenderType.cutout());
else if (layer == BCLRenderLayer.TRANSLUCENT) BlockRenderLayerMap.INSTANCE.putBlock(block, RenderType.translucent());
}
if (block instanceof BaseChestBlock) {
BaseChestBlockEntityRenderer.registerRenderLayer(block);
}
else if (block instanceof BaseSignBlock) {
BaseSignBlockEntityRenderer.registerRenderLayer(block);
}
}
private static void processBlockCommon(Block block) {
if (block instanceof PostInitable) {
((PostInitable) block).postInit();
}
}
} }

View file

@ -16,19 +16,6 @@ public class BCLibClient implements ClientModInitializer {
public void onInitializeClient() { public void onInitializeClient() {
ModIntegrationAPI.registerAll(); ModIntegrationAPI.registerAll();
BaseBlockEntityRenders.register(); BaseBlockEntityRenders.register();
registerRenderLayers(); PostInitAPI.postInit(true);
PostInitAPI.postInit();
}
private void registerRenderLayers() {
RenderType cutout = RenderType.cutout();
RenderType translucent = RenderType.translucent();
Registry.BLOCK.forEach(block -> {
if (block instanceof RenderLayerProvider) {
BCLRenderLayer layer = ((RenderLayerProvider) block).getRenderLayer();
if (layer == BCLRenderLayer.CUTOUT) BlockRenderLayerMap.INSTANCE.putBlock(block, cutout);
else if (layer == BCLRenderLayer.TRANSLUCENT) BlockRenderLayerMap.INSTANCE.putBlock(block, translucent);
}
});
} }
} }

View file

@ -0,0 +1,5 @@
package ru.bclib.interfaces;
public interface TileEntityRenderProvider {
}

View file

@ -10,6 +10,6 @@ public class BCLibServer implements DedicatedServerModInitializer {
@Override @Override
public void onInitializeServer() { public void onInitializeServer() {
ModIntegrationAPI.registerAll(); ModIntegrationAPI.registerAll();
PostInitAPI.postInit(); PostInitAPI.postInit(false);
} }
} }