Skip to content

Commit

Permalink
try adding the BookcaseGeometryLoader (it doesn't work yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
IchHabeHunger54 committed Dec 2, 2023
1 parent b221b27 commit 1b96719
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.github.minecraftschurlimods.bibliocraft.client;

import com.github.minecraftschurlimods.bibliocraft.Bibliocraft;
import com.github.minecraftschurlimods.bibliocraft.block.BCBlock;
import com.github.minecraftschurlimods.bibliocraft.block.bookcase.BookcaseBlock;
import com.github.minecraftschurlimods.bibliocraft.client.model.BookcaseModel;
import com.github.minecraftschurlimods.bibliocraft.client.model.BookcaseGeometryLoader;
import com.github.minecraftschurlimods.bibliocraft.client.screen.BookcaseScreen;
import com.github.minecraftschurlimods.bibliocraft.init.BCBlocks;
import com.github.minecraftschurlimods.bibliocraft.init.BCMenuTypes;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.renderer.block.BlockModelShaper;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
Expand All @@ -26,14 +26,14 @@ public final class ClientHandler {
public static final class ModBus {
@SubscribeEvent
static void clientSetup(FMLClientSetupEvent event) {
MenuScreens.register(BCMenuTypes.BOOKCASE.get(), BookcaseScreen::new);
event.enqueueWork(() -> MenuScreens.register(BCMenuTypes.BOOKCASE.get(), BookcaseScreen::new));
}

@SubscribeEvent
static void modifyBakingResult(ModelEvent.ModifyBakingResult event) {
Map<ResourceLocation, BakedModel> models = event.getModels();
for (BookcaseBlock block : BCBlocks.BOOKCASE.values()) {
bakeBCBlock(models, block, BookcaseModel::new);
bakeBlock(models, block, BookcaseGeometryLoader.BookcaseBakedModel::new);
}
}

Expand All @@ -44,7 +44,12 @@ static void registerAdditional(ModelEvent.RegisterAdditional event) {
}
}

private static void bakeBCBlock(Map<ResourceLocation, BakedModel> models, BCBlock block, Function<BakedModel, ? extends BakedModelWrapper<BakedModel>> modelFactory) {
@SubscribeEvent
static void registerGeometryLoaders(ModelEvent.RegisterGeometryLoaders event) {
event.register("bookcase", BookcaseGeometryLoader.INSTANCE);
}

private static void bakeBlock(Map<ResourceLocation, BakedModel> models, Block block, Function<BakedModel, ? extends BakedModelWrapper<BakedModel>> modelFactory) {
for (BlockState state : block.getStateDefinition().getPossibleStates()) {
models.computeIfPresent(BlockModelShaper.stateToModelLocation(state), ($, model) -> modelFactory.apply(model));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package com.github.minecraftschurlimods.bibliocraft.client.model;

import com.github.minecraftschurlimods.bibliocraft.block.bookcase.BookcaseBlock;
import com.github.minecraftschurlimods.bibliocraft.block.bookcase.BookcaseBlockEntity;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mojang.math.Transformation;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.ItemOverrides;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.BlockModelRotation;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.resources.model.ModelBaker;
import net.minecraft.client.resources.model.ModelState;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.client.model.BakedModelWrapper;
import net.neoforged.neoforge.client.model.IDynamicBakedModel;
import net.neoforged.neoforge.client.model.SimpleModelState;
import net.neoforged.neoforge.client.model.data.ModelData;
import net.neoforged.neoforge.client.model.geometry.IGeometryBakingContext;
import net.neoforged.neoforge.client.model.geometry.IGeometryLoader;
import net.neoforged.neoforge.client.model.geometry.IUnbakedGeometry;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class BookcaseGeometryLoader implements IGeometryLoader<BookcaseGeometryLoader.BookcaseGeometry> {
public static final BookcaseGeometryLoader INSTANCE = new BookcaseGeometryLoader();

private BookcaseGeometryLoader() {
}

@Override
public BookcaseGeometry read(JsonObject jsonObject, JsonDeserializationContext context) throws JsonParseException {
BlockModel[] books = new BlockModel[16];
for (int i = 0; i < 16; i++) {
String book = "book_" + i;
books[i] = context.deserialize(GsonHelper.getAsJsonObject(jsonObject, book), BlockModel.class);
}
return new BookcaseGeometry(context.deserialize(GsonHelper.getAsJsonObject(jsonObject, "base"), BlockModel.class), books);
}

public static class BookcaseGeometry implements IUnbakedGeometry<BookcaseGeometry> {
private final BlockModel base;
private final BlockModel[] books;

public BookcaseGeometry(BlockModel base, BlockModel[] books) {
this.base = base;
this.books = books;
}

@Override
public BakedModel bake(IGeometryBakingContext context, ModelBaker baker, Function<Material, TextureAtlasSprite> spriteGetter, ModelState modelState, ItemOverrides overrides, ResourceLocation modelLocation) {
BakedModel[] directionalBase = new BakedModel[4];
BakedModel[][] directionalBooks = new BakedModel[4][16];
for (int i = 0; i < directionalBooks.length; i++) {
directionalBase[i] = base.bake(baker, base, spriteGetter, BlockModelRotation.by(0, i * 90), modelLocation, context.useBlockLight());
for (int j = 0; j < directionalBooks[i].length; j++) {
directionalBooks[i][j] = books[j].bake(baker, books[j], spriteGetter, BlockModelRotation.by(0, i * 90), modelLocation, context.useBlockLight());
}
}
return new BookcaseDynamicModel(context.useAmbientOcclusion(), context.isGui3d(), context.useBlockLight(), spriteGetter.apply(base.getMaterial("particle")), directionalBase, directionalBooks);
}

@Override
public void resolveParents(Function<ResourceLocation, UnbakedModel> modelGetter, IGeometryBakingContext context) {
base.resolveParents(modelGetter);
for (BlockModel book : books) {
book.resolveParents(modelGetter);
}
}
}

public static class BookcaseDynamicModel implements IDynamicBakedModel {
private final boolean useAmbientOcclusion;
private final boolean isGui3d;
private final boolean usesBlockLight;
private final TextureAtlasSprite particle;
private final BakedModel[] base;
private final BakedModel[][] books;

public BookcaseDynamicModel(boolean useAmbientOcclusion, boolean isGui3d, boolean usesBlockLight, TextureAtlasSprite particle, BakedModel[] base, BakedModel[][] books) {
this.useAmbientOcclusion = useAmbientOcclusion;
this.isGui3d = isGui3d;
this.usesBlockLight = usesBlockLight;
this.particle = particle;
this.base = base;
this.books = books;
}

@Override
public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, RandomSource rand, ModelData extraData, @Nullable RenderType renderType) {
int sideIndex = side == null ? 0 : switch (side) {
case NORTH -> 0;
case EAST -> 1;
case SOUTH -> 2;
case WEST -> 3;
default -> 0;
};
List<BakedQuad> quads = new ArrayList<>(base[sideIndex].getQuads(state, side, rand, extraData, renderType));
for (int i = 0; i < books[sideIndex].length; i++) {
Boolean book = extraData.get(BookcaseBlockEntity.MODEL_PROPERTIES.get(i));
if (book == null || !book) continue;
quads.addAll(books[sideIndex][i].getQuads(state, side, rand, extraData, renderType));
}
return quads;
}

@Override
public boolean useAmbientOcclusion() {
return useAmbientOcclusion;
}

@Override
public boolean isGui3d() {
return isGui3d;
}

@Override
public boolean usesBlockLight() {
return usesBlockLight;
}

@Override
public boolean isCustomRenderer() {
return false;
}

@Override
public TextureAtlasSprite getParticleIcon() {
return particle;
}

@Override
public ItemOverrides getOverrides() {
return ItemOverrides.EMPTY;
}
}

public static class BookcaseBakedModel extends BakedModelWrapper<BakedModel> {
public BookcaseBakedModel(BakedModel originalModel) {
super(originalModel);
}

@Override
public ModelData getModelData(BlockAndTintGetter level, BlockPos pos, BlockState state, ModelData modelData) {
if (state.getBlock() instanceof BookcaseBlock) {
BlockEntity blockEntity = level.getBlockEntity(pos);
if (!(blockEntity instanceof BookcaseBlockEntity be)) return modelData;
ModelData.Builder builder = ModelData.builder();
for (int i = 0; i < BookcaseBlockEntity.MODEL_PROPERTIES.size(); i++) {
builder.with(BookcaseBlockEntity.MODEL_PROPERTIES.get(i), !be.getItem(i).isEmpty());
}
return builder.build();
}
return modelData;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"parent": "minecraft:block/block",
"textures": {
"0": "#texture",
"particle": "#texture"
},
"elements": [
{
"name": "bottom",
"from": [1, 0, 8],
"to": [15, 1, 15],
"faces": {
"north": {"uv": [1, 15, 15, 16], "texture": "#0"},
"east": {"uv": [0, 0, 7, 1], "texture": "#missing"},
"south": {"uv": [0, 0, 14, 1], "texture": "#missing"},
"west": {"uv": [0, 0, 7, 1], "texture": "#missing"},
"up": {"uv": [1, 8, 15, 15], "texture": "#0"},
"down": {"uv": [1, 1, 15, 8], "texture": "#0"}
}
},
{
"name": "middle",
"from": [1, 7, 8],
"to": [15, 9, 15],
"faces": {
"north": {"uv": [1, 7, 15, 9], "texture": "#0"},
"east": {"uv": [0, 0, 7, 2], "texture": "#missing"},
"south": {"uv": [0, 0, 14, 2], "texture": "#missing"},
"west": {"uv": [0, 0, 7, 2], "texture": "#missing"},
"up": {"uv": [1, 8, 15, 15], "texture": "#0"},
"down": {"uv": [1, 1, 15, 8], "texture": "#0"}
}
},
{
"name": "top",
"from": [1, 15, 8],
"to": [15, 16, 15],
"faces": {
"north": {"uv": [1, 0, 15, 1], "texture": "#0"},
"east": {"uv": [0, 0, 7, 1], "texture": "#missing"},
"south": {"uv": [0, 0, 14, 1], "texture": "#missing"},
"west": {"uv": [0, 0, 7, 1], "texture": "#missing"},
"up": {"uv": [1, 8, 15, 15], "texture": "#0"},
"down": {"uv": [1, 1, 15, 8], "texture": "#0"}
}
},
{
"name": "left",
"from": [15, 0, 8],
"to": [16, 16, 15],
"faces": {
"north": {"uv": [0, 0, 1, 16], "texture": "#0"},
"east": {"uv": [1, 0, 8, 16], "texture": "#0"},
"south": {"uv": [0, 0, 1, 16], "texture": "#missing"},
"west": {"uv": [8, 0, 15, 16], "texture": "#0"},
"up": {"uv": [15, 8, 16, 15], "texture": "#0"},
"down": {"uv": [15, 1, 16, 8], "texture": "#0"}
}
},
{
"name": "right",
"from": [0, 0, 8],
"to": [1, 16, 15],
"faces": {
"north": {"uv": [15, 0, 16, 16], "texture": "#0"},
"east": {"uv": [1, 0, 8, 16], "texture": "#0"},
"south": {"uv": [0, 0, 1, 16], "texture": "#missing"},
"west": {"uv": [8, 0, 15, 16], "texture": "#0"},
"up": {"uv": [0, 8, 1, 15], "texture": "#0"},
"down": {"uv": [0, 1, 1, 8], "texture": "#0"}
}
},
{
"name": "back",
"from": [0, 0, 15],
"to": [16, 16, 16],
"faces": {
"north": {"uv": [0, 0, 16, 16], "texture": "#0"},
"east": {"uv": [0, 0, 1, 16], "texture": "#0"},
"south": {"uv": [0, 0, 16, 16], "texture": "#0"},
"west": {"uv": [15, 0, 16, 16], "texture": "#0"},
"up": {"uv": [0, 15, 16, 16], "texture": "#0"},
"down": {"uv": [0, 0, 16, 1], "texture": "#0"}
}
}
]
}
Loading

0 comments on commit 1b96719

Please sign in to comment.