| 1 | package net.minecraft.client.resources.model;
|
| 2 |
|
| 3 | import com.google.common.collect.HashMultimap;
|
| 4 | import com.google.common.collect.Multimap;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.util.List;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.renderer.block.dispatch.BlockStateModelPart;
|
| 10 | import net.minecraft.client.renderer.block.dispatch.ModelState;
|
| 11 | import net.minecraft.client.renderer.texture.TextureAtlas;
|
| 12 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 13 | import net.minecraft.client.resources.model.geometry.BakedQuad;
|
| 14 | import net.minecraft.client.resources.model.geometry.QuadCollection;
|
| 15 | import net.minecraft.client.resources.model.sprite.Material;
|
| 16 | import net.minecraft.client.resources.model.sprite.TextureSlots;
|
| 17 | import net.minecraft.core.Direction;
|
| 18 | import net.minecraft.resources.Identifier;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 | import org.slf4j.Logger;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public record SimpleModelWrapper(QuadCollection quads, boolean useAmbientOcclusion, Material.Baked particleMaterial) implements BlockStateModelPart {
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 |
|
| 26 | public static BlockStateModelPart bake(final ModelBaker modelBakery, final Identifier location, final ModelState state) {
|
| 27 | ResolvedModel model = modelBakery.getModel(location);
|
| 28 | TextureSlots textureSlots = model.getTopTextureSlots();
|
| 29 | boolean hasAmbientOcclusion = model.getTopAmbientOcclusion();
|
| 30 | Material.Baked particleMaterial = model.resolveParticleMaterial(textureSlots, modelBakery);
|
| 31 | QuadCollection geometry = model.bakeTopGeometry(textureSlots, modelBakery, state);
|
| 32 | Multimap<Identifier, Identifier> forbiddenSprites = findNonBlockSprites(geometry);
|
| 33 | if (forbiddenSprites != null) {
|
| 34 | LOGGER.warn("Rejecting block model {}, since it contains sprites from outside of supported atlas: {}", location, forbiddenSprites);
|
| 35 | return modelBakery.missingBlockModelPart();
|
| 36 | } else {
|
| 37 | return new SimpleModelWrapper(geometry, hasAmbientOcclusion, particleMaterial);
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | public static @Nullable Multimap<Identifier, Identifier> findNonBlockSprites(final QuadCollection geometry) {
|
| 42 | Multimap<Identifier, Identifier> forbiddenSprites = null;
|
| 43 |
|
| 44 | for (BakedQuad bakedQuad : geometry.getAll()) {
|
| 45 | TextureAtlasSprite sprite = bakedQuad.materialInfo().sprite();
|
| 46 | if (!sprite.atlasLocation().equals(TextureAtlas.LOCATION_BLOCKS)) {
|
| 47 | if (forbiddenSprites == null) {
|
| 48 | forbiddenSprites = HashMultimap.create();
|
| 49 | }
|
| 50 |
|
| 51 | forbiddenSprites.put(sprite.atlasLocation(), sprite.contents().name());
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | return forbiddenSprites;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public List<BakedQuad> getQuads(final @Nullable Direction direction) {
|
| 60 | return this.quads.getQuads(direction);
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public int materialFlags() {
|
| 65 | return this.quads.materialFlags();
|
| 66 | }
|
| 67 | }
|
| 68 |
|