From f5272e330d4af7ac256a32e0ef394f94f462917f Mon Sep 17 00:00:00 2001 From: alpha Date: Sun, 12 Nov 2023 15:07:41 -0600 Subject: [PATCH] Some model fixes --- gradle.properties | 2 +- .../entity/DefaultRetexturedBlockEntity.java | 2 +- .../block/entity/IRetexturedBlockEntity.java | 6 +-- .../block/entity/RetexturedBlockEntity.java | 7 +--- .../mantle/client/model/RetexturedModel.java | 42 ++++++++++--------- .../mantle/client/model/util/ModelHelper.java | 28 ++++++++++++- .../screen/book/element/StructureElement.java | 2 - .../mantle/util/RetexturedHelper.java | 2 +- 8 files changed, 56 insertions(+), 35 deletions(-) diff --git a/gradle.properties b/gradle.properties index 2e77841d..a76930c5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,7 +11,7 @@ minecraft_range=[1.18.2,1.20) # check these on https://fabricmc.net/versions.html loader_version=0.14.22 -fabric_version=0.87.0+1.20.1 +fabric_version=0.90.7+1.20.1 parchment_version=2021.12.19 # Build dependencies diff --git a/src/main/java/slimeknights/mantle/block/entity/DefaultRetexturedBlockEntity.java b/src/main/java/slimeknights/mantle/block/entity/DefaultRetexturedBlockEntity.java index 815edbf3..3079fda6 100644 --- a/src/main/java/slimeknights/mantle/block/entity/DefaultRetexturedBlockEntity.java +++ b/src/main/java/slimeknights/mantle/block/entity/DefaultRetexturedBlockEntity.java @@ -32,7 +32,7 @@ public DefaultRetexturedBlockEntity(BlockEntityType type, BlockPos pos, Block @Nonnull @Override - public IModelData getRenderAttachmentData() { + public IModelData getRenderData() { return this.data.get(); } diff --git a/src/main/java/slimeknights/mantle/block/entity/IRetexturedBlockEntity.java b/src/main/java/slimeknights/mantle/block/entity/IRetexturedBlockEntity.java index 1549538a..e06f87b4 100644 --- a/src/main/java/slimeknights/mantle/block/entity/IRetexturedBlockEntity.java +++ b/src/main/java/slimeknights/mantle/block/entity/IRetexturedBlockEntity.java @@ -1,6 +1,6 @@ package slimeknights.mantle.block.entity; -import net.fabricmc.fabric.api.rendering.data.v1.RenderAttachmentBlockEntity; +import net.fabricmc.fabric.api.blockview.v2.RenderDataBlockEntity; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; @@ -17,7 +17,7 @@ * * Use alongside {@link RetexturedBlock} and {@link slimeknights.mantle.item.RetexturedBlockItem}. See {@link DefaultRetexturedBlockEntity} for implementation. */ -public interface IRetexturedBlockEntity extends RenderAttachmentBlockEntity { +public interface IRetexturedBlockEntity extends RenderDataBlockEntity { /* Gets the Forge tile data for the tile entity */ default CompoundTag getTileData() { return ((BlockEntity)this).getCustomData(); @@ -62,5 +62,5 @@ default IModelData getRetexturedModelData() { } @Override - @Nullable IModelData getRenderAttachmentData(); + @Nullable IModelData getRenderData(); } diff --git a/src/main/java/slimeknights/mantle/block/entity/RetexturedBlockEntity.java b/src/main/java/slimeknights/mantle/block/entity/RetexturedBlockEntity.java index e9806d48..7a51c29c 100644 --- a/src/main/java/slimeknights/mantle/block/entity/RetexturedBlockEntity.java +++ b/src/main/java/slimeknights/mantle/block/entity/RetexturedBlockEntity.java @@ -1,14 +1,11 @@ package slimeknights.mantle.block.entity; import io.github.fabricators_of_create.porting_lib.common.util.Lazy; -import net.fabricmc.fabric.api.rendering.data.v1.RenderAttachmentBlockEntity; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; -import org.jetbrains.annotations.Nullable; import slimeknights.mantle.client.model.data.IModelData; -import slimeknights.mantle.client.model.data.SinglePropertyData; import slimeknights.mantle.util.RetexturedHelper; import javax.annotation.Nonnull; @@ -20,7 +17,7 @@ * @deprecated use {@link DefaultRetexturedBlockEntity} */ @Deprecated -public class RetexturedBlockEntity extends MantleBlockEntity implements IRetexturedBlockEntity, RenderAttachmentBlockEntity { +public class RetexturedBlockEntity extends MantleBlockEntity implements IRetexturedBlockEntity { /** Lazy value of model data as it will not change after first fetch */ private final Lazy data = Lazy.of(this::getRetexturedModelData); public RetexturedBlockEntity(BlockEntityType type, BlockPos pos, BlockState state) { @@ -29,7 +26,7 @@ public RetexturedBlockEntity(BlockEntityType type, BlockPos pos, BlockState s @Nonnull @Override - public IModelData getRenderAttachmentData() { + public IModelData getRenderData() { return data.get(); } diff --git a/src/main/java/slimeknights/mantle/client/model/RetexturedModel.java b/src/main/java/slimeknights/mantle/client/model/RetexturedModel.java index d869165a..0aa34450 100644 --- a/src/main/java/slimeknights/mantle/client/model/RetexturedModel.java +++ b/src/main/java/slimeknights/mantle/client/model/RetexturedModel.java @@ -8,6 +8,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonSyntaxException; import com.mojang.datafixers.util.Either; +import io.github.fabricators_of_create.porting_lib.models.CustomParticleIconModel; import io.github.fabricators_of_create.porting_lib.models.geometry.IGeometryLoader; import io.github.fabricators_of_create.porting_lib.models.geometry.IUnbakedGeometry; import lombok.AccessLevel; @@ -37,6 +38,7 @@ import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.state.BlockState; +import slimeknights.mantle.client.model.data.IModelData; import slimeknights.mantle.client.model.data.SinglePropertyData; import slimeknights.mantle.client.model.util.ColoredBlockModel; import slimeknights.mantle.client.model.util.DynamicBakedWrapper; @@ -151,7 +153,7 @@ public static Set getRetextured(JsonObject json) { } /** Baked variant of the model, used to swap out quads based on the texture */ - public static class Baked extends DynamicBakedWrapper { + public static class Baked extends DynamicBakedWrapper implements CustomParticleIconModel { /** Cache of texture name to baked model */ private final Map cache = new ConcurrentHashMap<>(); /* Properties for rebaking */ @@ -191,28 +193,30 @@ private BakedModel getCachedModel(Block block) { return cache.computeIfAbsent(ModelHelper.getParticleTexture(block), this::getRetexturedModel); } - // TODO: PORT -// @Override -// public TextureAtlasSprite getParticleIcon(IModelData data) { -// // if particle is retextured, fetch particle from the cached model -// if (retextured.contains("particle")) { -// Block block = data.getData(RetexturedHelper.BLOCK_PROPERTY); -// if (block != null) { -// return getCachedModel(block).getParticleIcon(data); -// } -// } -// return wrapped.getParticleIcon(data); -// } + @Override + public TextureAtlasSprite getParticleIcon(Object obj) { + // if particle is retextured, fetch particle from the cached model + if (retextured.contains("particle") && obj instanceof IModelData data) { + Block block = data.getData(RetexturedHelper.BLOCK_PROPERTY); + if (block != null) { + var cached = getCachedModel(block); + return cached instanceof CustomParticleIconModel cachedParticle ? cachedParticle.getParticleIcon(data) : cached.getParticleIcon(); + } + } + if (wrapped instanceof CustomParticleIconModel customParticleIconModel) + return customParticleIconModel.getParticleIcon(obj); + return wrapped.getParticleIcon(); + } @Override public void emitBlockQuads(BlockAndTintGetter blockView, BlockState state, BlockPos pos, Supplier randomSupplier, RenderContext context) { - if(blockView instanceof RenderAttachedBlockView renderAttachedBlockView && renderAttachedBlockView.getBlockEntityRenderAttachment(pos) instanceof SinglePropertyData data) { - Block block = (Block) data.getData(RetexturedHelper.BLOCK_PROPERTY); + if(blockView.getBlockEntityRenderData(pos) instanceof IModelData data) { + Block block = data.getData(RetexturedHelper.BLOCK_PROPERTY); if (block == null) { super.emitBlockQuads(blockView, state, pos, randomSupplier, context); return; } - ((FabricBakedModel)getCachedModel(block)).emitBlockQuads(blockView, state, pos, randomSupplier, context); + getCachedModel(block).emitBlockQuads(blockView, state, pos, randomSupplier, context); } else { super.emitBlockQuads(blockView, state, pos, randomSupplier, context); } @@ -233,7 +237,7 @@ public void emitItemQuads(ItemStack stack, Supplier randomSupplier } // if valid, use the block - ((FabricBakedModel)getCachedModel(block)).emitItemQuads(stack, randomSupplier, context); + getCachedModel(block).emitItemQuads(stack, randomSupplier, context); } @Override @@ -298,9 +302,7 @@ public BakedModel resolve(BakedModel originalModel, ItemStack stack, @Nullable C } // if valid, use the block - if (originalModel instanceof Baked model) - return model.getCachedModel(block); - return ((Baked) ((WrapperBakedModel) originalModel).getWrappedModel()).getCachedModel(block); + return ModelHelper.unwrap(originalModel, Baked.class).getCachedModel(block); } } } diff --git a/src/main/java/slimeknights/mantle/client/model/util/ModelHelper.java b/src/main/java/slimeknights/mantle/client/model/util/ModelHelper.java index a1ba3bd1..e925baf1 100644 --- a/src/main/java/slimeknights/mantle/client/model/util/ModelHelper.java +++ b/src/main/java/slimeknights/mantle/client/model/util/ModelHelper.java @@ -9,6 +9,7 @@ import com.mojang.math.Transformation; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import net.fabricmc.fabric.api.renderer.v1.model.WrapperBakedModel; import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.block.model.BakedQuad; @@ -68,7 +69,7 @@ public static T getBakedModel(BlockState state, Class if (minecraft == null) { return null; } - BakedModel baked = minecraft.getModelManager().getBlockModelShaper().getBlockModel(state); + BakedModel baked = unwrap(minecraft.getModelManager().getBlockModelShaper().getBlockModel(state), clazz); // map multipart and weighted random into the first variant if (baked instanceof MultiPartBakedModel) { baked = ((MultiPartBakedModel)baked).selectors.get(0).getRight(); @@ -97,7 +98,7 @@ public static T getBakedModel(ItemLike item, Class cla if (minecraft == null) { return null; } - BakedModel baked = minecraft.getItemRenderer().getItemModelShaper().getItemModel(item.asItem()); + BakedModel baked = unwrap(minecraft.getItemRenderer().getItemModelShaper().getItemModel(item.asItem()), clazz); if (clazz.isInstance(baked)) { return clazz.cast(baked); } @@ -218,4 +219,27 @@ public static int getRotation(JsonObject json, String key) { // return ((BakedQuadBuilder) this.parent).build(); // } // } + + /** + * Fully unwrap a model, i.e. return the innermost model. + */ + public static T unwrap(BakedModel model, Class modelClass) { + while (model instanceof WrapperBakedModel wrapper) { + if (modelClass.isAssignableFrom(model.getClass())) + return (T) model; + BakedModel wrapped = wrapper.getWrappedModel(); + + if (wrapped == null) { + return (T) model; + } else if (wrapped == model) { + throw new IllegalArgumentException("Model " + model + " is wrapping itself!"); + } else { + model = wrapped; + } + } + if (!modelClass.isAssignableFrom(model.getClass())) + throw new RuntimeException("Trying to unwrap " + model + " that isn't assignable to " + modelClass); + + return (T) model; + } } diff --git a/src/main/java/slimeknights/mantle/client/screen/book/element/StructureElement.java b/src/main/java/slimeknights/mantle/client/screen/book/element/StructureElement.java index 5fd56c38..d12885b8 100644 --- a/src/main/java/slimeknights/mantle/client/screen/book/element/StructureElement.java +++ b/src/main/java/slimeknights/mantle/client/screen/book/element/StructureElement.java @@ -3,7 +3,6 @@ import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.Tesselator; import com.mojang.math.Transformation; -import net.fabricmc.fabric.api.rendering.data.v1.RenderAttachmentBlockEntity; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; import net.minecraft.client.gui.GuiGraphics; @@ -11,7 +10,6 @@ import net.minecraft.client.renderer.block.BlockRenderDispatcher; import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.core.BlockPos; -import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate; import org.joml.Quaternionf; diff --git a/src/main/java/slimeknights/mantle/util/RetexturedHelper.java b/src/main/java/slimeknights/mantle/util/RetexturedHelper.java index 2a208600..18500bb4 100644 --- a/src/main/java/slimeknights/mantle/util/RetexturedHelper.java +++ b/src/main/java/slimeknights/mantle/util/RetexturedHelper.java @@ -97,7 +97,7 @@ public static void onTextureUpd if (level != null && level.isClientSide) { Block texture = self.getTexture(); texture = texture == Blocks.AIR ? null : texture; - IModelData data = self.getRetexturedModelData(); + IModelData data = self.getRenderData(); if (data.getData(BLOCK_PROPERTY) != texture) { data.setData(BLOCK_PROPERTY, texture); BlockState state = self.getBlockState();