generated from neoforged/MDK
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try adding the BookcaseGeometryLoader (it doesn't work yet)
- Loading branch information
1 parent
b221b27
commit 1b96719
Showing
5 changed files
with
288 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
...java/com/github/minecraftschurlimods/bibliocraft/client/model/BookcaseGeometryLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
46 changes: 0 additions & 46 deletions
46
src/main/java/com/github/minecraftschurlimods/bibliocraft/client/model/BookcaseModel.java
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
src/main/resources/assets/bibliocraft/models/block/bookcase/base.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.