From 8e3a5245733a253c284a01869be7ce933758081c Mon Sep 17 00:00:00 2001 From: UnlikePaladin <36827970+UnlikePaladin@users.noreply.github.com> Date: Sat, 23 Dec 2023 16:47:19 -0600 Subject: [PATCH] migrated lamps to the new model system --- .../basicLamp/UnbakedBasicLampModel.java | 45 +++---- .../data/materials/VariantRegistryBase.java | 2 +- .../assets/PFMBlockstateModelProvider.java | 7 - .../block/basic_lamp/basic_lamp_bottom.json | 4 +- .../block/basic_lamp/basic_lamp_middle.json | 2 +- .../block/basic_lamp/basic_lamp_single.json | 4 +- .../block/basic_lamp/basic_lamp_top.json | 2 +- .../fabric/FabricBasicLampModel.java | 76 +++++++---- .../fabric/UnbakedBasicLampModelImpl.java | 13 +- .../models/fabric/PFMFabricBakedModel.java | 26 ++-- .../basicLamp/forge/ForgeBasicLampModel.java | 122 ++++++++++++------ .../forge/UnbakedBasicLampModelImpl.java | 4 +- .../models/forge/PFMForgeBakedModel.java | 4 +- .../pfm/mixin/forge/PFMModelLoaderMixin.java | 2 +- 14 files changed, 186 insertions(+), 127 deletions(-) diff --git a/common/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/UnbakedBasicLampModel.java b/common/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/UnbakedBasicLampModel.java index b1695bf71..5a4b5edbb 100644 --- a/common/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/UnbakedBasicLampModel.java +++ b/common/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/UnbakedBasicLampModel.java @@ -15,6 +15,7 @@ import org.jetbrains.annotations.Nullable; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; public class UnbakedBasicLampModel implements UnbakedModel { @@ -34,10 +35,10 @@ public static Identifier getItemModelId() { } public static final List MODEL_PARTS_BASE = new ArrayList<>() {{ - add("block/basic_lamp/template_basic_lamp/template_basic_lamp_bottom"); - add("block/basic_lamp/template_basic_lamp/template_basic_lamp_middle"); - add("block/basic_lamp/template_basic_lamp/template_basic_lamp_single"); - add("block/basic_lamp/template_basic_lamp/template_basic_lamp_top"); + add("block/basic_lamp/basic_lamp_bottom"); + add("block/basic_lamp/basic_lamp_middle"); + add("block/basic_lamp/basic_lamp_single"); + add("block/basic_lamp/basic_lamp_top"); }}; public static final List STATIC_PARTS = new ArrayList<>() {{ @@ -48,11 +49,8 @@ public static Identifier getItemModelId() { private static final Identifier PARENT = new Identifier("block/block"); public static final List ALL_MODEL_IDS = new ArrayList<>() { { - for (WoodVariant variant : WoodVariantRegistry.getVariants()) { - for (String part : MODEL_PARTS_BASE) { - String newPart = part.replaceAll("template", variant.asString()); - add(new Identifier(PaladinFurnitureMod.MOD_ID, newPart)); - } + for (String part : MODEL_PARTS_BASE) { + add(new Identifier(PaladinFurnitureMod.MOD_ID, part)); } for (String part : STATIC_PARTS) { add(new Identifier(PaladinFurnitureMod.MOD_ID, part)); @@ -64,10 +62,7 @@ public static Identifier getItemModelId() { Map textureMap = new HashMap<>(); @Override public Collection getTextureDependencies(Function unbakedModelGetter, Set> unresolvedTextureReferences) { - for (WoodVariant variant : WoodVariantRegistry.getVariants()) { - textureMap.put(variant, new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, variant.getTexture(BlockType.STRIPPED_LOG))); - } - return textureMap.values(); + return Collections.emptyList(); } @Override @@ -75,27 +70,23 @@ public Collection getModelDependencies() { return Collections.singleton(PARENT); } + public static final Map> CACHED_MODELS = new ConcurrentHashMap<>(); @Nullable @Override public BakedModel bake(ModelLoader loader, Function textureGetter, ModelBakeSettings rotationContainer, Identifier modelId) { - Map> bakedModels = new LinkedHashMap<>(); - List parts = new ArrayList<>(MODEL_PARTS_BASE); - parts.addAll(STATIC_PARTS); - for (WoodVariant woodVariant : WoodVariantRegistry.getVariants()) { - bakedModels.put(woodVariant, new LinkedHashMap<>()); - for (String part : parts) { - bakedModels.get(woodVariant).put(part, loader.bake(new Identifier(PaladinFurnitureMod.MOD_ID, part.replaceAll("template", woodVariant.asString())), rotationContainer)); - } - } - Map spriteMap = new HashMap<>(); - for (Map.Entry spriteIdentifierEntry : textureMap.entrySet()) { - spriteMap.put(spriteIdentifierEntry.getKey(), textureGetter.apply(spriteIdentifierEntry.getValue())); + if (CACHED_MODELS.containsKey(rotationContainer)) + return getBakedModel(rotationContainer, CACHED_MODELS.get(rotationContainer)); + + List bakedModelList = new ArrayList<>(); + for (Identifier modelPart : ALL_MODEL_IDS) { + bakedModelList.add(loader.bake(modelPart, rotationContainer)); } - return getBakedModel(spriteMap, rotationContainer, bakedModels, parts); + CACHED_MODELS.put(rotationContainer, bakedModelList); + return getBakedModel(rotationContainer, bakedModelList); } @ExpectPlatform - public static BakedModel getBakedModel(Map textures, ModelBakeSettings settings, Map> variantToModelMap, List modelParts) { + public static BakedModel getBakedModel(ModelBakeSettings settings, List modelParts) { throw new RuntimeException("Method wasn't replaced correctly"); } } diff --git a/common/src/main/java/com/unlikepaladin/pfm/data/materials/VariantRegistryBase.java b/common/src/main/java/com/unlikepaladin/pfm/data/materials/VariantRegistryBase.java index c91364758..583e48568 100644 --- a/common/src/main/java/com/unlikepaladin/pfm/data/materials/VariantRegistryBase.java +++ b/common/src/main/java/com/unlikepaladin/pfm/data/materials/VariantRegistryBase.java @@ -33,7 +33,7 @@ protected void finalizeAndFreeze() { if (Objects.equals(entry.getNamespace(), modId)) { if (!linkedHashMap.containsKey(entry.getIdentifier())) { linkedHashMap.put(entry.getIdentifier(), entry); - }else if (entry != WoodVariantRegistry.OAK) { + }else if (entry.getIdentifier() != WoodVariantRegistry.OAK.getIdentifier()) { PaladinFurnitureMod.GENERAL_LOGGER.warn("Found block type with duplicate id ({}), skipping",entry.identifier); } } diff --git a/common/src/main/java/com/unlikepaladin/pfm/runtime/assets/PFMBlockstateModelProvider.java b/common/src/main/java/com/unlikepaladin/pfm/runtime/assets/PFMBlockstateModelProvider.java index 9954c7585..203dc7faa 100644 --- a/common/src/main/java/com/unlikepaladin/pfm/runtime/assets/PFMBlockstateModelProvider.java +++ b/common/src/main/java/com/unlikepaladin/pfm/runtime/assets/PFMBlockstateModelProvider.java @@ -215,13 +215,6 @@ public void registerCounters() { public void registerLamp() { Identifier modelID = ModelIds.getBlockModelId(PaladinFurnitureModBlocksItems.BASIC_LAMP); - for (WoodVariant variant : WoodVariantRegistry.getVariants()) { - Texture blockTexture = createRawBlockTexture(true, variant); - for (Model model : TEMPLATE_LAMP_ARRAY) { - Identifier id = new Identifier(modelID.getNamespace(), ModelIDS.get(model).getPath().replace("template_", "").replace("template", "").replaceAll("basic_lamp", variant.asString() + "_basic_lamp").replace("block/", "block/basic_lamp/").replace("//", "/")); - model.upload(id, blockTexture, this.modelCollector); - } - } this.blockStateCollector.accept(createSingleStateBlockState(PaladinFurnitureModBlocksItems.BASIC_LAMP, List.of(modelID))); PFMBlockstateModelProvider.modelPathMap.put(PaladinFurnitureModBlocksItems.BASIC_LAMP, UnbakedBasicLampModel.getItemModelId()); } diff --git a/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_bottom.json b/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_bottom.json index 89a63579d..22e877e18 100644 --- a/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_bottom.json +++ b/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_bottom.json @@ -1,8 +1,8 @@ { "credit": "By UnlikePaladin", "textures": { - "particle": "#texture", - "texture": "#texture" + "particle": "minecraft:block/stripped_oak_log", + "texture": "minecraft:block/stripped_oak_log" }, "elements": [ { diff --git a/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_middle.json b/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_middle.json index 71b84cbdf..d5c7a5e7b 100644 --- a/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_middle.json +++ b/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_middle.json @@ -1,7 +1,7 @@ { "credit": "By UnlikePaladin", "textures": { - "texture": "#texture" + "texture": "minecraft:block/stripped_oak_log" }, "elements": [ { diff --git a/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_single.json b/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_single.json index 5c9814db1..4df9971cb 100644 --- a/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_single.json +++ b/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_single.json @@ -1,8 +1,8 @@ { "credit": "By UnlikePaladin", "textures": { - "particle": "#texture", - "texture": "#texture" + "particle": "minecraft:block/stripped_oak_log", + "texture": "minecraft:block/stripped_oak_log" }, "elements": [ { diff --git a/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_top.json b/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_top.json index 09cb5b0f4..2c1260431 100644 --- a/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_top.json +++ b/common/src/main/resources/assets/pfm/models/block/basic_lamp/basic_lamp_top.json @@ -1,7 +1,7 @@ { "credit": "By UnlikePaladin", "textures": { - "texture": "#texture" + "texture": "minecraft:block/stripped_oak_log" }, "elements": [ { diff --git a/fabric/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/fabric/FabricBasicLampModel.java b/fabric/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/fabric/FabricBasicLampModel.java index 8f183d06d..96d43f404 100644 --- a/fabric/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/fabric/FabricBasicLampModel.java +++ b/fabric/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/fabric/FabricBasicLampModel.java @@ -3,6 +3,8 @@ import com.unlikepaladin.pfm.blocks.BasicLampBlock; import com.unlikepaladin.pfm.blocks.blockentities.LampBlockEntity; import com.unlikepaladin.pfm.blocks.models.AbstractBakedModel; +import com.unlikepaladin.pfm.blocks.models.fabric.PFMFabricBakedModel; +import com.unlikepaladin.pfm.data.materials.BlockType; import com.unlikepaladin.pfm.data.materials.WoodVariant; import com.unlikepaladin.pfm.data.materials.WoodVariantRegistry; import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel; @@ -13,27 +15,20 @@ import net.minecraft.client.render.model.ModelBakeSettings; import net.minecraft.client.render.model.json.ModelTransformation; import net.minecraft.client.texture.Sprite; +import net.minecraft.client.util.SpriteIdentifier; import net.minecraft.item.ItemStack; +import net.minecraft.screen.PlayerScreenHandler; import net.minecraft.state.property.Properties; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import net.minecraft.world.BlockRenderView; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; +import java.util.*; import java.util.function.Supplier; -public class FabricBasicLampModel extends AbstractBakedModel implements FabricBakedModel { - private final List modelParts; - private final Map> bakedModels; - private final Map textureMap; - public FabricBasicLampModel(Map textures, ModelBakeSettings settings, Map> bakedModels, List modelParts) { - super(settings, bakedModels.get(WoodVariantRegistry.OAK).values().stream().toList()); - this.modelParts = modelParts; - this.textureMap = textures; - this.bakedModels = bakedModels; +public class FabricBasicLampModel extends PFMFabricBakedModel { + public FabricBasicLampModel(ModelBakeSettings settings, List modelParts) { + super(settings, modelParts); } @Override @@ -51,21 +46,23 @@ public void emitBlockQuads(BlockRenderView blockView, BlockState state, BlockPos } boolean up = blockView.getBlockState(pos.up()).getBlock() instanceof BasicLampBlock; boolean down = blockView.getBlockState(pos.down()).getBlock() instanceof BasicLampBlock; + pushTextureTransform(context, getOakStrippedLogSprite(), getVariantStrippedLogSprite(variant)); if (up && down) { - ((FabricBakedModel)bakedModels.get(variant).get(modelParts.get(1))).emitBlockQuads(blockView, state, pos, randomSupplier, context); + ((FabricBakedModel)getTemplateBakedModels().get(1)).emitBlockQuads(blockView, state, pos, randomSupplier, context); } else if (up) { - ((FabricBakedModel)bakedModels.get(variant).get(modelParts.get(0))).emitBlockQuads(blockView, state, pos, randomSupplier, context); + ((FabricBakedModel)getTemplateBakedModels().get(0)).emitBlockQuads(blockView, state, pos, randomSupplier, context); } else if (down) { - ((FabricBakedModel)bakedModels.get(variant).get(modelParts.get(3))).emitBlockQuads(blockView, state, pos, randomSupplier, context); - ((FabricBakedModel)bakedModels.get(variant).get(modelParts.get(5+onOffset))).emitBlockQuads(blockView, state, pos, randomSupplier, context); - ((FabricBakedModel)bakedModels.get(variant).get(modelParts.get(4))).emitBlockQuads(blockView, state, pos, randomSupplier, context); + ((FabricBakedModel)getTemplateBakedModels().get(3)).emitBlockQuads(blockView, state, pos, randomSupplier, context); + ((FabricBakedModel)getTemplateBakedModels().get(5+onOffset)).emitBlockQuads(blockView, state, pos, randomSupplier, context); + ((FabricBakedModel)getTemplateBakedModels().get(4)).emitBlockQuads(blockView, state, pos, randomSupplier, context); } else { - ((FabricBakedModel)bakedModels.get(variant).get(modelParts.get(4))).emitBlockQuads(blockView, state, pos, randomSupplier, context); - ((FabricBakedModel)bakedModels.get(variant).get(modelParts.get(2))).emitBlockQuads(blockView, state, pos, randomSupplier, context); - ((FabricBakedModel)bakedModels.get(variant).get(modelParts.get(5+onOffset))).emitBlockQuads(blockView, state, pos, randomSupplier, context); + ((FabricBakedModel)getTemplateBakedModels().get(4)).emitBlockQuads(blockView, state, pos, randomSupplier, context); + ((FabricBakedModel)getTemplateBakedModels().get(2)).emitBlockQuads(blockView, state, pos, randomSupplier, context); + ((FabricBakedModel)getTemplateBakedModels().get(5+onOffset)).emitBlockQuads(blockView, state, pos, randomSupplier, context); } + context.popTransform(); } @Override @@ -74,18 +71,41 @@ public void emitItemQuads(ItemStack stack, Supplier randomSupplier, Rend if (stack.hasNbt()) { variant = WoodVariantRegistry.getVariant(Identifier.tryParse(stack.getSubNbt("BlockEntityTag").getString("variant"))); } - ((FabricBakedModel)bakedModels.get(variant).get(modelParts.get(4))).emitItemQuads(stack, randomSupplier, context); - ((FabricBakedModel)bakedModels.get(variant).get(modelParts.get(2))).emitItemQuads(stack, randomSupplier, context); - ((FabricBakedModel)bakedModels.get(variant).get(modelParts.get(5))).emitItemQuads(stack, randomSupplier, context); + pushTextureTransform(context, getOakStrippedLogSprite(), getVariantStrippedLogSprite(variant)); + ((FabricBakedModel)getTemplateBakedModels().get(4)).emitItemQuads(stack, randomSupplier, context); + ((FabricBakedModel)getTemplateBakedModels().get(2)).emitItemQuads(stack, randomSupplier, context); + ((FabricBakedModel)getTemplateBakedModels().get(5)).emitItemQuads(stack, randomSupplier, context); + context.popTransform(); } @Override - public Sprite getParticleSprite() { - return bakedModels.get(WoodVariantRegistry.OAK).get(modelParts.get(4)).getParticleSprite(); + public ModelTransformation getTransformation() { + return getTemplateBakedModels().get(2).getTransformation(); + } + + static List oakSprite = new ArrayList<>(); + static List getOakStrippedLogSprite() { + if (!oakSprite.isEmpty()) + return oakSprite; + Sprite wood = new SpriteIdentifier(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE, WoodVariantRegistry.OAK.getTexture(BlockType.STRIPPED_LOG)).getSprite(); + oakSprite.add(wood); + return oakSprite; + } + + static Map> sprites = new HashMap<>(); + static List getVariantStrippedLogSprite(WoodVariant variant) { + if (sprites.containsKey(variant)) + return sprites.get(variant); + + Sprite wood = new SpriteIdentifier(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE, variant.getTexture(BlockType.STRIPPED_LOG)).getSprite(); + List spriteList = new ArrayList<>(); + spriteList.add(wood); + sprites.put(variant, spriteList); + return spriteList; } @Override - public ModelTransformation getTransformation() { - return bakedModels.get(WoodVariantRegistry.OAK).get(modelParts.get(2)).getTransformation(); + public Sprite pfm$getParticle(BlockState state) { + return getTemplateBakedModels().get(4).getParticleSprite(); } } diff --git a/fabric/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/fabric/UnbakedBasicLampModelImpl.java b/fabric/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/fabric/UnbakedBasicLampModelImpl.java index 56b7187b2..3a7677f7e 100644 --- a/fabric/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/fabric/UnbakedBasicLampModelImpl.java +++ b/fabric/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/fabric/UnbakedBasicLampModelImpl.java @@ -1,16 +1,19 @@ package com.unlikepaladin.pfm.blocks.models.basicLamp.fabric; -import com.unlikepaladin.pfm.data.materials.WoodVariant; import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.ModelBakeSettings; -import net.minecraft.client.texture.Sprite; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class UnbakedBasicLampModelImpl { - - public static BakedModel getBakedModel(Map textures, ModelBakeSettings settings, Map> variantToModelMap, List modelParts) { - return new FabricBasicLampModel(textures, settings, variantToModelMap, modelParts); + static Map modelMap = new ConcurrentHashMap<>(); + public static BakedModel getBakedModel(ModelBakeSettings settings, List modelParts) { + if (modelMap.containsKey(settings)) + return modelMap.get(settings); + BakedModel model = new FabricBasicLampModel(settings, modelParts); + modelMap.put(settings, model); + return model; } } diff --git a/fabric/src/main/java/com/unlikepaladin/pfm/blocks/models/fabric/PFMFabricBakedModel.java b/fabric/src/main/java/com/unlikepaladin/pfm/blocks/models/fabric/PFMFabricBakedModel.java index 70bbf2f1c..0d621f44a 100644 --- a/fabric/src/main/java/com/unlikepaladin/pfm/blocks/models/fabric/PFMFabricBakedModel.java +++ b/fabric/src/main/java/com/unlikepaladin/pfm/blocks/models/fabric/PFMFabricBakedModel.java @@ -39,19 +39,21 @@ public void pushTextureTransform(RenderContext context, List toReplace, } public void pushTextureTransform(RenderContext context, List toReplace, List replacement, Identifier atlasId) { context.pushTransform(quad -> { - Sprite originalSprite = SpriteFinder.get(MinecraftClient.getInstance().getBakedModelManager().getAtlas(atlasId)).find(quad, 0); - Identifier keyId = originalSprite.getId(); - int textureIndex = IntStream.range(0, toReplace.size()) - .filter(i -> keyId.equals(toReplace.get(i).getId())) - .findFirst() - .orElse(-1); + if (replacement != null && toReplace != null ){ + Sprite originalSprite = SpriteFinder.get(MinecraftClient.getInstance().getBakedModelManager().getAtlas(atlasId)).find(quad, 0); + Identifier keyId = originalSprite.getId(); + int textureIndex = IntStream.range(0, toReplace.size()) + .filter(i -> keyId.equals(toReplace.get(i).getId())) + .findFirst() + .orElse(-1); - if (textureIndex != -1 && !toReplace.equals(replacement)) { - Sprite sprite = replacement.get(textureIndex); - for (int index = 0; index < 4; index++) { - float frameU = originalSprite.method_35804(quad.spriteU(index, 0)); - float frameV = originalSprite.method_35805(quad.spriteV(index, 0)); - quad.sprite(index, 0, sprite.getFrameU(frameU), sprite.getFrameV(frameV)); + if (textureIndex != -1 && !toReplace.equals(replacement)) { + Sprite sprite = replacement.get(textureIndex); + for (int index = 0; index < 4; index++) { + float frameU = originalSprite.method_35804(quad.spriteU(index, 0)); + float frameV = originalSprite.method_35805(quad.spriteV(index, 0)); + quad.sprite(index, 0, sprite.getFrameU(frameU), sprite.getFrameV(frameV)); + } } } return true; diff --git a/forge/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/forge/ForgeBasicLampModel.java b/forge/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/forge/ForgeBasicLampModel.java index 9e9512934..2f7cc4e1a 100644 --- a/forge/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/forge/ForgeBasicLampModel.java +++ b/forge/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/forge/ForgeBasicLampModel.java @@ -1,22 +1,24 @@ package com.unlikepaladin.pfm.blocks.models.basicLamp.forge; -import com.mojang.datafixers.util.Pair; import com.unlikepaladin.pfm.blocks.BasicLampBlock; import com.unlikepaladin.pfm.blocks.blockentities.LampBlockEntity; -import com.unlikepaladin.pfm.blocks.models.AbstractBakedModel; import com.unlikepaladin.pfm.blocks.models.forge.ModelBitSetProperty; +import com.unlikepaladin.pfm.blocks.models.forge.PFMForgeBakedModel; +import com.unlikepaladin.pfm.data.materials.BlockType; import com.unlikepaladin.pfm.data.materials.WoodVariant; import com.unlikepaladin.pfm.data.materials.WoodVariantRegistry; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; -import net.minecraft.client.render.RenderLayer; import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.BakedQuad; import net.minecraft.client.render.model.ModelBakeSettings; import net.minecraft.client.render.model.json.ModelTransformation; import net.minecraft.client.texture.Sprite; +import net.minecraft.client.util.SpriteIdentifier; import net.minecraft.item.ItemStack; +import net.minecraft.screen.PlayerScreenHandler; import net.minecraft.state.property.Properties; +import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.world.BlockRenderView; @@ -28,62 +30,94 @@ import java.util.*; -public class ForgeBasicLampModel extends AbstractBakedModel { - private final List modelParts; - private final Map> bakedModels; - private final Map textureMap; - public ForgeBasicLampModel(Map textures, ModelBakeSettings settings, Map> bakedModels, List modelParts) { - super(settings, bakedModels.get(WoodVariantRegistry.OAK).values().stream().toList()); - this.modelParts = modelParts; - this.textureMap = textures; - this.bakedModels = bakedModels; +public class ForgeBasicLampModel extends PFMForgeBakedModel { + public ForgeBasicLampModel(ModelBakeSettings settings, List modelParts) { + super(settings, modelParts); } public static ModelProperty CONNECTIONS = new ModelProperty<>(); public static ModelProperty VARIANT = new ModelProperty<>(); + @Override + public void appendProperties(ModelDataMap.Builder builder) { + super.appendProperties(builder); + builder.withProperty(CONNECTIONS); + builder.withProperty(VARIANT); + } + @NotNull @Override public IModelData getModelData(@NotNull BlockRenderView world, @NotNull BlockPos pos, @NotNull BlockState state, @NotNull IModelData tileData) { - ModelDataMap.Builder builder = new ModelDataMap.Builder(); - WoodVariant variant = WoodVariantRegistry.OAK; - BlockEntity entity = world.getBlockEntity(pos); - if (entity instanceof LampBlockEntity) { - variant = ((LampBlockEntity) entity).getVariant(); + if (state.getBlock() instanceof BasicLampBlock) { + ModelDataMap.Builder builder = new ModelDataMap.Builder(); + appendProperties(builder); + + IModelData data = builder.build(); + super.getModelData(world, pos, state, data); + + WoodVariant variant = WoodVariantRegistry.OAK; + BlockEntity entity = world.getBlockEntity(pos); + if (entity instanceof LampBlockEntity) { + variant = ((LampBlockEntity) entity).getVariant(); + } + BitSet set = new BitSet(); + set.set(0, world.getBlockState(pos.up()).getBlock() instanceof BasicLampBlock); + set.set(1, world.getBlockState(pos.down()).getBlock() instanceof BasicLampBlock); + data.setData(CONNECTIONS, new ModelBitSetProperty(set)); + data.setData(VARIANT, variant); + return data; } - BitSet set = new BitSet(); - set.set(0, world.getBlockState(pos.up()).getBlock() instanceof BasicLampBlock); - set.set(1, world.getBlockState(pos.down()).getBlock() instanceof BasicLampBlock); - builder.withInitial(CONNECTIONS, new ModelBitSetProperty(set)); - builder.withInitial(VARIANT, variant); - return builder.build(); + return tileData; + } + + static List oakSprite = new ArrayList<>(); + static List getOakStrippedLogSprite() { + if (!oakSprite.isEmpty()) + return oakSprite; + Sprite wood = new SpriteIdentifier(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE, WoodVariantRegistry.OAK.getTexture(BlockType.STRIPPED_LOG)).getSprite(); + oakSprite.add(wood); + return oakSprite; + } + + static Map> sprites = new HashMap<>(); + static List getVariantStrippedLogSprite(WoodVariant variant) { + if (sprites.containsKey(variant)) + return sprites.get(variant); + + Sprite wood = new SpriteIdentifier(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE, variant.getTexture(BlockType.STRIPPED_LOG)).getSprite(); + List spriteList = new ArrayList<>(); + spriteList.add(wood); + sprites.put(variant, spriteList); + return spriteList; } @NotNull @Override public List getQuads(@Nullable BlockState state, @Nullable Direction side, @NotNull Random rand, @NotNull IModelData extraData) { - List quads = new ArrayList<>(); if (state != null && extraData.getData(CONNECTIONS) != null && extraData.getData(CONNECTIONS).connections != null) { + List quads = new ArrayList<>(); int onOffset = state.get(Properties.LIT) ? 1 : 0; WoodVariant variant = extraData.getData(VARIANT); + Sprite sprite = new SpriteIdentifier(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE, variant.getTexture(BlockType.STRIPPED_LOG)).getSprite(); BitSet set = extraData.getData(CONNECTIONS).connections; if (set.get(0) && set.get(1)) { - quads.addAll(bakedModels.get(variant).get(modelParts.get(1)).getQuads(state, side, rand, extraData)); + quads.addAll(getTemplateBakedModels().get(1).getQuads(state, side, rand, extraData)); } else if (set.get(0)) { - quads.addAll(bakedModels.get(variant).get(modelParts.get(0)).getQuads(state, side, rand, extraData)); + quads.addAll(getTemplateBakedModels().get(0).getQuads(state, side, rand, extraData)); } else if (set.get(1)) { - quads.addAll(bakedModels.get(variant).get(modelParts.get(3)).getQuads(state, side, rand, extraData)); - quads.addAll(bakedModels.get(variant).get(modelParts.get(5+onOffset)).getQuads(state, side, rand, extraData)); - quads.addAll(bakedModels.get(variant).get(modelParts.get(4)).getQuads(state, side, rand, extraData)); + quads.addAll(getTemplateBakedModels().get(3).getQuads(state, side, rand, extraData)); + quads.addAll(getTemplateBakedModels().get(5+onOffset).getQuads(state, side, rand, extraData)); + quads.addAll(getTemplateBakedModels().get(4).getQuads(state, side, rand, extraData)); } else { - quads.addAll(bakedModels.get(variant).get(modelParts.get(4)).getQuads(state, side, rand, extraData)); - quads.addAll(bakedModels.get(variant).get(modelParts.get(2)).getQuads(state, side, rand, extraData)); - quads.addAll(bakedModels.get(variant).get(modelParts.get(5+onOffset)).getQuads(state, side, rand, extraData)); + quads.addAll(getTemplateBakedModels().get(4).getQuads(state, side, rand, extraData)); + quads.addAll(getTemplateBakedModels().get(2).getQuads(state, side, rand, extraData)); + quads.addAll(getTemplateBakedModels().get(5+onOffset).getQuads(state, side, rand, extraData)); } + return getQuadsWithTexture(quads, getOakStrippedLogSprite(), getVariantStrippedLogSprite(variant)); } - return quads; + return Collections.emptyList(); } @Override @@ -92,12 +126,28 @@ public boolean isBuiltin() { } @Override - public Sprite getParticleSprite() { - return bakedModels.get(WoodVariantRegistry.OAK).get(modelParts.get(4)).getParticleSprite(); + public Sprite getParticleIcon(@NotNull IModelData data) { + if (data != null && data.hasProperty(VARIANT)) { + return getVariantStrippedLogSprite(data.getData(VARIANT)).get(0); + } + return super.getParticleIcon(data); } @Override public ModelTransformation getTransformation() { - return bakedModels.get(WoodVariantRegistry.OAK).get(modelParts.get(2)).getTransformation(); + return getTemplateBakedModels().get(2).getTransformation(); + } + + @Override + public List getQuads(ItemStack stack, @Nullable BlockState state, @Nullable Direction face, Random random) { + List quads = new ArrayList<>(); + WoodVariant variant = WoodVariantRegistry.OAK; + if (stack.hasNbt()) { + variant = WoodVariantRegistry.getVariant(Identifier.tryParse(stack.getSubNbt("BlockEntityTag").getString("variant"))); + } + quads.addAll(getTemplateBakedModels().get(4).getQuads(state, face, random)); + quads.addAll(getTemplateBakedModels().get(2).getQuads(state, face, random)); + quads.addAll(getTemplateBakedModels().get(5).getQuads(state, face, random)); + return getQuadsWithTexture(quads, getOakStrippedLogSprite(), getVariantStrippedLogSprite(variant)); } } diff --git a/forge/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/forge/UnbakedBasicLampModelImpl.java b/forge/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/forge/UnbakedBasicLampModelImpl.java index 7b0bc95a0..3412042b3 100644 --- a/forge/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/forge/UnbakedBasicLampModelImpl.java +++ b/forge/src/main/java/com/unlikepaladin/pfm/blocks/models/basicLamp/forge/UnbakedBasicLampModelImpl.java @@ -9,7 +9,7 @@ import java.util.Map; public class UnbakedBasicLampModelImpl { - public static BakedModel getBakedModel(Map textures, ModelBakeSettings settings, Map> variantToModelMap, List modelParts) { - return new ForgeBasicLampModel(textures, settings, variantToModelMap, modelParts); + public static BakedModel getBakedModel(ModelBakeSettings settings, List modelParts) { + return new ForgeBasicLampModel(settings, modelParts); } } diff --git a/forge/src/main/java/com/unlikepaladin/pfm/blocks/models/forge/PFMForgeBakedModel.java b/forge/src/main/java/com/unlikepaladin/pfm/blocks/models/forge/PFMForgeBakedModel.java index 07a9190f8..6852ce7df 100644 --- a/forge/src/main/java/com/unlikepaladin/pfm/blocks/models/forge/PFMForgeBakedModel.java +++ b/forge/src/main/java/com/unlikepaladin/pfm/blocks/models/forge/PFMForgeBakedModel.java @@ -47,8 +47,8 @@ public IModelData getModelData(@NotNull BlockRenderView world, @NotNull BlockPos Map> separatedQuads = new ConcurrentHashMap<>(); public List getQuadsWithTexture(List quads, List toReplace, List replacements) { - if (toReplace.size() != replacements.size()) { - PaladinFurnitureMod.GENERAL_LOGGER.warn("Replacement list is not the same size, skipping transformation"); + if (replacements == null || toReplace == null || toReplace.size() != replacements.size()) { + PaladinFurnitureMod.GENERAL_LOGGER.warn("Replacement list is not the same size or was null, skipping transformation"); return quads; } if (toReplace.equals(replacements)) diff --git a/forge/src/main/java/com/unlikepaladin/pfm/mixin/forge/PFMModelLoaderMixin.java b/forge/src/main/java/com/unlikepaladin/pfm/mixin/forge/PFMModelLoaderMixin.java index 914033141..23ca4a6ce 100644 --- a/forge/src/main/java/com/unlikepaladin/pfm/mixin/forge/PFMModelLoaderMixin.java +++ b/forge/src/main/java/com/unlikepaladin/pfm/mixin/forge/PFMModelLoaderMixin.java @@ -172,7 +172,7 @@ else if (ModelHelper.containsIdentifier(UnbakedFreezerModel.FREEZER_MODEL_IDS.to this.modelsToBake.put(resourceId, model); ci.cancel(); } - else if (ModelHelper.containsIdentifier(UnbakedBasicLampModel.LAMP_MODEL_IDS.toArray(new Identifier[0]), modifiedId)){ + else if (UnbakedBasicLampModel.LAMP_MODEL_IDS.contains(modifiedId)){ UnbakedModel model = new UnbakedBasicLampModel(); this.unbakedModels.put(resourceId, model); this.modelsToBake.put(resourceId, model);