Skip to content

Commit

Permalink
migrated lamps to the new model system
Browse files Browse the repository at this point in the history
  • Loading branch information
UnlikePaladin committed Dec 23, 2023
1 parent 52fc69b commit 8e3a524
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,10 +35,10 @@ public static Identifier getItemModelId() {
}

public static final List<String> 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<String> STATIC_PARTS = new ArrayList<>() {{
Expand All @@ -48,11 +49,8 @@ public static Identifier getItemModelId() {
private static final Identifier PARENT = new Identifier("block/block");
public static final List<Identifier> 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));
Expand All @@ -64,38 +62,31 @@ public static Identifier getItemModelId() {
Map<WoodVariant, SpriteIdentifier> textureMap = new HashMap<>();
@Override
public Collection<SpriteIdentifier> getTextureDependencies(Function<Identifier, UnbakedModel> unbakedModelGetter, Set<Pair<String, String>> 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
public Collection<Identifier> getModelDependencies() {
return Collections.singleton(PARENT);
}

public static final Map<ModelBakeSettings, List<BakedModel>> CACHED_MODELS = new ConcurrentHashMap<>();
@Nullable
@Override
public BakedModel bake(ModelLoader loader, Function<SpriteIdentifier, Sprite> textureGetter, ModelBakeSettings rotationContainer, Identifier modelId) {
Map<WoodVariant, Map<String, BakedModel>> bakedModels = new LinkedHashMap<>();
List<String> 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<WoodVariant, Sprite> spriteMap = new HashMap<>();
for (Map.Entry<WoodVariant, SpriteIdentifier> spriteIdentifierEntry : textureMap.entrySet()) {
spriteMap.put(spriteIdentifierEntry.getKey(), textureGetter.apply(spriteIdentifierEntry.getValue()));
if (CACHED_MODELS.containsKey(rotationContainer))
return getBakedModel(rotationContainer, CACHED_MODELS.get(rotationContainer));

List<BakedModel> 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<WoodVariant, Sprite> textures, ModelBakeSettings settings, Map<WoodVariant, Map<String, BakedModel>> variantToModelMap, List<String> modelParts) {
public static BakedModel getBakedModel(ModelBakeSettings settings, List<BakedModel> modelParts) {
throw new RuntimeException("Method wasn't replaced correctly");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"credit": "By UnlikePaladin",
"textures": {
"texture": "#texture"
"texture": "minecraft:block/stripped_oak_log"
},
"elements": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"credit": "By UnlikePaladin",
"textures": {
"texture": "#texture"
"texture": "minecraft:block/stripped_oak_log"
},
"elements": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> modelParts;
private final Map<WoodVariant, Map<String, BakedModel>> bakedModels;
private final Map<WoodVariant, Sprite> textureMap;
public FabricBasicLampModel(Map<WoodVariant, Sprite> textures, ModelBakeSettings settings, Map<WoodVariant, Map<String, BakedModel>> bakedModels, List<String> 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<BakedModel> modelParts) {
super(settings, modelParts);
}

@Override
Expand All @@ -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
Expand All @@ -74,18 +71,41 @@ public void emitItemQuads(ItemStack stack, Supplier<Random> 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<Sprite> oakSprite = new ArrayList<>();
static List<Sprite> 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<WoodVariant, List<Sprite>> sprites = new HashMap<>();
static List<Sprite> 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<Sprite> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<WoodVariant, Sprite> textures, ModelBakeSettings settings, Map<WoodVariant, Map<String, BakedModel>> variantToModelMap, List<String> modelParts) {
return new FabricBasicLampModel(textures, settings, variantToModelMap, modelParts);
static Map<ModelBakeSettings, BakedModel> modelMap = new ConcurrentHashMap<>();
public static BakedModel getBakedModel(ModelBakeSettings settings, List<BakedModel> modelParts) {
if (modelMap.containsKey(settings))
return modelMap.get(settings);
BakedModel model = new FabricBasicLampModel(settings, modelParts);
modelMap.put(settings, model);
return model;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ public void pushTextureTransform(RenderContext context, List<Sprite> toReplace,
}
public void pushTextureTransform(RenderContext context, List<Sprite> toReplace, List<Sprite> 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;
Expand Down
Loading

0 comments on commit 8e3a524

Please sign in to comment.