Change models loading (WIP)

This commit is contained in:
Aleksey 2021-05-15 22:22:33 +03:00
parent de8baa4b83
commit 2910df3078
8 changed files with 129 additions and 41 deletions

View file

@ -2,6 +2,7 @@ package ru.betterend.blocks.basis;
import java.io.Reader; import java.io.Reader;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.MultiVariant; import net.minecraft.client.renderer.block.model.MultiVariant;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
@ -26,8 +27,12 @@ public class BarkBlock extends EndPillarBlock {
} }
@Override @Override
public MultiVariant getModelVariant(BlockState blockState) { public BlockModel getBlockModel(BlockState blockState) {
return null;
}
@Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
return null; return null;
} }

View file

@ -4,8 +4,10 @@ import java.io.Reader;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import com.mojang.math.Transformation;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.MultiVariant; import net.minecraft.client.renderer.block.model.MultiVariant;
import net.minecraft.client.renderer.block.model.Variant;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -34,12 +36,12 @@ public class BlockBase extends Block implements BlockModelProvider {
@Override @Override
public String getModelString(String block) { public String getModelString(String block) {
ResourceLocation blockId = Registry.BLOCK.getKey(this); ResourceLocation blockId = Registry.BLOCK.getKey(this);
return Patterns.createJson(Patterns.BLOCK_BASE, blockId.getPath(), blockId.getPath()); return Patterns.createBlockSimple(blockId.getPath());
} }
@Override @Override
public BlockModel getModel() { public BlockModel getModel() {
return getModelVariant(defaultBlockState()); return getBlockModel(defaultBlockState());
} }
@Override @Override
@ -48,7 +50,15 @@ public class BlockBase extends Block implements BlockModelProvider {
} }
@Override @Override
public MultiVariant getModelVariant(BlockState blockState) { public BlockModel getBlockModel(BlockState blockState) {
return BlockModel.fromString(getModelString("")); ResourceLocation blockId = Registry.BLOCK.getKey(this);
String pattern = Patterns.createBlockSimple(blockId.getPath());
return BlockModelProvider.createBlockModel(blockId, pattern);
}
@Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
Variant variant = new Variant(resourceLocation, Transformation.identity(), false, 1);
return new MultiVariant(Collections.singletonList(variant));
} }
} }

View file

@ -24,8 +24,6 @@ import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import ru.betterend.blocks.BlockProperties; import ru.betterend.blocks.BlockProperties;
import ru.betterend.patterns.BlockModelProvider; import ru.betterend.patterns.BlockModelProvider;
import ru.betterend.patterns.Patterns; import ru.betterend.patterns.Patterns;
@ -91,38 +89,22 @@ public class EndAnvilBlock extends AnvilBlock implements BlockModelProvider {
@Override @Override
public BlockModel getModel() { public BlockModel getModel() {
return null; return getBlockModel(defaultBlockState());
} }
@Override @Override
public Pair<ResourceLocation, BlockModel> getBlockModel(BlockState blockState) { public BlockModel getBlockModel(BlockState blockState) {
Direction facing = blockState.getValue(FACING);
int destruction = blockState.getValue(DESTRUCTION); int destruction = blockState.getValue(DESTRUCTION);
ResourceLocation blockId = Registry.BLOCK.getKey(this); ResourceLocation blockId = Registry.BLOCK.getKey(this);
ResourceLocation modelId = createModelId(blockId, facing, destruction);
Map<String, String> map = Maps.newHashMap(); Map<String, String> map = Maps.newHashMap();
map.put("%anvil%", blockId.getPath()); map.put("%anvil%", blockId.getPath());
map.put("%top%", "_top_" + destruction); map.put("%top%", "_top_" + destruction);
String jsonString = Patterns.createJson(Patterns.BLOCK_ANVIL, map); String pattern = Patterns.createJson(Patterns.BLOCK_ANVIL, map);
BlockModel blockModel = BlockModel.fromString(jsonString); return BlockModelProvider.createBlockModel(blockId, pattern);
return Pair.of(modelId, blockModel);
} }
@Override @Override
public MultiVariant getModelVariant(BlockState blockState) { public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
Direction facing = blockState.getValue(FACING); return BlockModelProvider.createFacingModel(resourceLocation, blockState.getValue(FACING));
int destruction = blockState.getValue(DESTRUCTION);
ResourceLocation blockId = Registry.BLOCK.getKey(this);
ResourceLocation modelId = createModelId(blockId, facing, destruction);
Transformation transform = new Transformation(null, facing.getRotation(), null, null);
Variant variant = new Variant(modelId, transform, false, 1);
return new MultiVariant(Collections.singletonList(variant));
}
protected ResourceLocation createModelId(ResourceLocation blockId, Direction facing, int destruction) {
return new ResourceLocation(blockId.getNamespace(),
blockId.getPath() + "/" + facing + "/destruction_" + destruction);
} }
} }

View file

@ -5,6 +5,8 @@ import java.util.List;
import java.util.Random; import java.util.Random;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.MultiVariant;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
@ -103,8 +105,34 @@ public class EndBarrelBlock extends BarrelBlock implements BlockModelProvider {
return Patterns.createJson(Patterns.BLOCK_BOTTOM_TOP, texture, texture); return Patterns.createJson(Patterns.BLOCK_BOTTOM_TOP, texture, texture);
} }
@Override
public BlockModel getModel() {
return getBlockModel(defaultBlockState());
}
@Override @Override
public ResourceLocation statePatternId() { public ResourceLocation statePatternId() {
return Patterns.STATE_BARREL; return Patterns.STATE_BARREL;
} }
@Override
public BlockModel getBlockModel(BlockState blockState) {
ResourceLocation blockId = Registry.BLOCK.getKey(this);
String texture = blockId.getPath();
String pattern;
if (blockState.getValue(OPEN)) {
pattern = Patterns.createJson(Patterns.BLOCK_BARREL_OPEN, texture, texture);
} else {
pattern = Patterns.createJson(Patterns.BLOCK_BOTTOM_TOP, texture, texture);
}
if (pattern != null) {
return BlockModelProvider.createBlockModel(blockId, pattern);
}
return null;
}
@Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
return BlockModelProvider.createFacingModel(resourceLocation, blockState.getValue(FACING));
}
} }

View file

@ -4,8 +4,13 @@ import java.io.Reader;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import com.mojang.math.Transformation;
import com.mojang.math.Vector3f;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.MultiVariant;
import net.minecraft.client.renderer.block.model.Variant;
import net.minecraft.core.Direction;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -16,7 +21,7 @@ import net.minecraft.world.level.storage.loot.LootContext;
import ru.betterend.patterns.BlockModelProvider; import ru.betterend.patterns.BlockModelProvider;
import ru.betterend.patterns.Patterns; import ru.betterend.patterns.Patterns;
public abstract class EndPillarBlock extends RotatedPillarBlock implements BlockModelProvider { public class EndPillarBlock extends RotatedPillarBlock implements BlockModelProvider {
public EndPillarBlock(Properties settings) { public EndPillarBlock(Properties settings) {
super(settings); super(settings);
} }
@ -43,12 +48,43 @@ public abstract class EndPillarBlock extends RotatedPillarBlock implements Block
@Override @Override
public String getModelString(String block) { public String getModelString(String block) {
String texture = Registry.BLOCK.getKey(this).getPath(); return createBlockPattern();
return Patterns.createJson(Patterns.BLOCK_PILLAR, texture, texture);
} }
@Override @Override
public ResourceLocation statePatternId() { public ResourceLocation statePatternId() {
return Patterns.STATE_PILLAR; return Patterns.STATE_PILLAR;
} }
@Override
public BlockModel getBlockModel(BlockState blockState) {
ResourceLocation blockId = Registry.BLOCK.getKey(this);
BlockModel model = BlockModel.fromString(createBlockPattern());
ResourceLocation modelLoc = new ResourceLocation(blockId.getNamespace(), "blocks/" + blockId.getPath());
model.name = modelLoc.toString();
return model;
}
@Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
Direction.Axis axis = blockState.getValue(AXIS);
Transformation transform = Transformation.identity();
switch (axis) {
case X: {
transform = new Transformation(null, Vector3f.ZP.rotationDegrees(90), null, null);
break;
}
case Z: {
transform = new Transformation(null, Vector3f.XP.rotationDegrees(90), null, null);
break;
}
}
Variant variant = new Variant(resourceLocation, transform, false, 1);
return new MultiVariant(Collections.singletonList(variant));
}
private String createBlockPattern() {
String texture = Registry.BLOCK.getKey(this).getPath();
return Patterns.createBlockPillar(texture);
}
} }

View file

@ -79,12 +79,14 @@ public abstract class ModelLoaderMixin {
Block block = Registry.BLOCK.get(clearLoc); Block block = Registry.BLOCK.get(clearLoc);
if (block instanceof BlockModelProvider) { if (block instanceof BlockModelProvider) {
block.getStateDefinition().getPossibleStates().forEach(blockState -> { block.getStateDefinition().getPossibleStates().forEach(blockState -> {
MultiVariant modelVariant = ((BlockModelProvider) block).getModelVariant(blockState); ModelResourceLocation stateLoc = BlockModelShaper.stateToModelLocation(clearLoc, blockState);
Pair<ResourceLocation, BlockModel> modelData = ((BlockModelProvider) block).getBlockModel(blockState); MultiVariant modelVariant = ((BlockModelProvider) block).getModelVariant(stateLoc, blockState);
if (modelVariant != null) { BlockModel blockModel = ((BlockModelProvider) block).getBlockModel(blockState);
ModelResourceLocation stateLoc = BlockModelShaper.stateToModelLocation(clearLoc, blockState); if (modelVariant != null && blockModel != null) {
cacheAndQueueDependencies(stateLoc, modelVariant); cacheAndQueueDependencies(stateLoc, modelVariant);
unbakedCache.put(modelData.getLeft(), modelData.getRight()); unbakedCache.put(stateLoc, blockModel);
} else {
BetterEnd.LOGGER.warning("Error loading models for {}", clearLoc);
} }
}); });
info.cancel(); info.cancel();

View file

@ -1,16 +1,32 @@
package ru.betterend.patterns; package ru.betterend.patterns;
import java.io.Reader; import java.io.Reader;
import java.util.Collections;
import com.mojang.math.Transformation;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.MultiVariant; import net.minecraft.client.renderer.block.model.MultiVariant;
import net.minecraft.client.renderer.block.model.Variant;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import org.apache.commons.lang3.tuple.Pair;
public interface BlockModelProvider extends ModelProvider { public interface BlockModelProvider extends ModelProvider {
String getStatesPattern(Reader data); String getStatesPattern(Reader data);
ResourceLocation statePatternId(); ResourceLocation statePatternId();
Pair<ResourceLocation, BlockModel> getBlockModel(BlockState blockState); BlockModel getBlockModel(BlockState blockState);
MultiVariant getModelVariant(BlockState blockState); MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState);
static BlockModel createBlockModel(ResourceLocation blockId, String pattern) {
BlockModel model = BlockModel.fromString(pattern);
ResourceLocation modelLoc = new ResourceLocation(blockId.getNamespace(), "blocks/" + blockId.getPath());
model.name = modelLoc.toString();
return model;
}
static MultiVariant createFacingModel(ResourceLocation resourceLocation, Direction facing) {
Transformation transform = new Transformation(null, facing.getRotation(), null, null);
Variant variant = new Variant(resourceLocation, transform, false, 1);
return new MultiVariant(Collections.singletonList(variant));
}
} }

View file

@ -12,6 +12,7 @@ import java.util.stream.Collectors;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.ResourceManager;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -122,6 +123,13 @@ public class Patterns {
return createJson(ITEM_GENERATED, name); return createJson(ITEM_GENERATED, name);
} }
public static String createBlockSimple(String name) {
return Patterns.createJson(Patterns.BLOCK_BASE, name, name);
}
public static String createBlockPillar(String name) {
return Patterns.createJson(Patterns.BLOCK_PILLAR, name, name);
}
public static String createJson(Reader data, String parent, String block) { public static String createJson(Reader data, String parent, String block) {
try (BufferedReader buffer = new BufferedReader(data)) { try (BufferedReader buffer = new BufferedReader(data)) {
return buffer.lines().collect(Collectors.joining()) return buffer.lines().collect(Collectors.joining())
@ -160,4 +168,5 @@ public class Patterns {
return ""; return "";
} }
} }
} }