Change models loading (WIP)

This commit is contained in:
Aleksey 2021-05-16 11:35:02 +03:00
parent 2910df3078
commit 60568cea18
12 changed files with 144 additions and 59 deletions

View file

@ -14,12 +14,6 @@ public class BarkBlock extends EndPillarBlock {
super(settings);
}
@Override
public String getModelString(String block) {
ResourceLocation blockId = Registry.BLOCK.getKey(this);
return Patterns.createJson(Patterns.BLOCK_BASE, getName(blockId), blockId.getPath());
}
@Override
public String getStatesPattern(Reader data) {
ResourceLocation blockId = Registry.BLOCK.getKey(this);
@ -27,13 +21,9 @@ public class BarkBlock extends EndPillarBlock {
}
@Override
public BlockModel getBlockModel(BlockState blockState) {
return null;
}
@Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
return null;
protected String createBlockPattern() {
ResourceLocation blockId = Registry.BLOCK.getKey(this);
return Patterns.createJson(Patterns.BLOCK_BASE, getName(blockId), blockId.getPath());
}
private String getName(ResourceLocation blockId) {

View file

@ -40,8 +40,8 @@ public class BlockBase extends Block implements BlockModelProvider {
}
@Override
public BlockModel getModel() {
return getBlockModel(defaultBlockState());
public BlockModel getModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState());
}
@Override
@ -50,15 +50,13 @@ public class BlockBase extends Block implements BlockModelProvider {
}
@Override
public BlockModel getBlockModel(BlockState blockState) {
ResourceLocation blockId = Registry.BLOCK.getKey(this);
public BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
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));
return BlockModelProvider.createBlockSimple(resourceLocation);
}
}

View file

@ -88,14 +88,13 @@ public class EndAnvilBlock extends AnvilBlock implements BlockModelProvider {
}
@Override
public BlockModel getModel() {
return getBlockModel(defaultBlockState());
public BlockModel getModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState());
}
@Override
public BlockModel getBlockModel(BlockState blockState) {
public BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
int destruction = blockState.getValue(DESTRUCTION);
ResourceLocation blockId = Registry.BLOCK.getKey(this);
Map<String, String> map = Maps.newHashMap();
map.put("%anvil%", blockId.getPath());
map.put("%top%", "_top_" + destruction);

View file

@ -106,8 +106,8 @@ public class EndBarrelBlock extends BarrelBlock implements BlockModelProvider {
}
@Override
public BlockModel getModel() {
return getBlockModel(defaultBlockState());
public BlockModel getModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState());
}
@Override
@ -116,8 +116,7 @@ public class EndBarrelBlock extends BarrelBlock implements BlockModelProvider {
}
@Override
public BlockModel getBlockModel(BlockState blockState) {
ResourceLocation blockId = Registry.BLOCK.getKey(this);
public BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
String texture = blockId.getPath();
String pattern;
if (blockState.getValue(OPEN)) {

View file

@ -5,6 +5,9 @@ import java.util.Collections;
import java.util.List;
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.Direction;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
@ -38,16 +41,33 @@ public class EndChainBlock extends ChainBlock implements BlockModelProvider, IRe
public String getModelString(String block) {
ResourceLocation blockId = Registry.BLOCK.getKey(this);
if (block.contains("item")) {
return Patterns.createJson(Patterns.ITEM_GENERATED, "item/" + blockId.getPath());
return Patterns.createItemGenerated(block);
}
return Patterns.createJson(Patterns.BLOCK_CHAIN, blockId.getPath(), blockId.getPath());
}
@Override
public BlockModel getModel(ResourceLocation blockId) {
return BlockModelProvider.createItemModel(blockId.getPath());
}
@Override
public ResourceLocation statePatternId() {
return Patterns.STATE_CHAIN;
}
@Override
public BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
String pattern = Patterns.createJson(Patterns.BLOCK_CHAIN, blockId.getPath(), blockId.getPath());
return BlockModelProvider.createBlockModel(blockId, pattern);
}
@Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
Direction.Axis axis = blockState.getValue(AXIS);
return BlockModelProvider.createRotatedModel(resourceLocation, axis);
}
@Override
public ERenderLayer getRenderLayer() {
return ERenderLayer.CUTOUT;

View file

@ -4,6 +4,8 @@ import java.io.Reader;
import java.util.List;
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.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
@ -21,9 +23,7 @@ public class EndChestBlock extends ChestBlock implements BlockModelProvider {
private final Block parent;
public EndChestBlock(Block source) {
super(FabricBlockSettings.copyOf(source).noOcclusion(), () -> {
return EndBlockEntities.CHEST;
});
super(FabricBlockSettings.copyOf(source).noOcclusion(), () -> EndBlockEntities.CHEST);
this.parent = source;
}
@ -57,9 +57,27 @@ public class EndChestBlock extends ChestBlock implements BlockModelProvider {
}
return Patterns.createJson(Patterns.BLOCK_EMPTY, parentId.getPath());
}
@Override
public BlockModel getModel(ResourceLocation blockId) {
String pattern = Patterns.createJson(Patterns.ITEM_CHEST, blockId.getPath());
return BlockModel.fromString(pattern);
}
@Override
public ResourceLocation statePatternId() {
return Patterns.STATE_SIMPLE;
}
@Override
public BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
ResourceLocation parentId = Registry.BLOCK.getKey(parent);
String pattern = Patterns.createJson(Patterns.BLOCK_EMPTY, parentId.getPath());
return BlockModel.fromString(pattern);
}
@Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
return BlockModelProvider.createBlockSimple(resourceLocation);
}
}

View file

@ -5,6 +5,8 @@ import java.util.Collections;
import java.util.List;
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.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
@ -37,9 +39,24 @@ public class EndComposterBlock extends ComposterBlock implements BlockModelProvi
String blockName = blockId.getPath();
return Patterns.createJson(Patterns.BLOCK_COMPOSTER, blockName);
}
@Override
public BlockModel getModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState());
}
@Override
public ResourceLocation statePatternId() {
return Patterns.STATE_COMPOSTER;
}
@Override
public BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
return null;
}
@Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
return null;
}
}

View file

@ -6,6 +6,8 @@ import java.util.HashMap;
import java.util.List;
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.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
@ -49,9 +51,37 @@ public class EndCraftingTableBlock extends CraftingTableBlock implements BlockMo
}
});
}
@Override
public BlockModel getModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState());
}
@Override
public ResourceLocation statePatternId() {
return Patterns.STATE_SIMPLE;
}
@Override
public BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
String blockName = blockId.getPath();
String pattern = Patterns.createJson(Patterns.BLOCK_SIDED, new HashMap<String, String>() {
private static final long serialVersionUID = 1L;
{
put("%particle%", blockName + "_front");
put("%down%", blockName + "_bottom");
put("%up%", blockName + "_top");
put("%north%", blockName + "_front");
put("%south%", blockName + "_side");
put("%west%", blockName + "_front");
put("%east%", blockName + "_side");
}
});
return BlockModel.fromString(pattern);
}
@Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
return BlockModelProvider.createBlockSimple(resourceLocation);
}
}

View file

@ -36,8 +36,8 @@ public class EndPillarBlock extends RotatedPillarBlock implements BlockModelProv
}
@Override
public BlockModel getModel() {
return null;
public BlockModel getModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState());
}
@Override
@ -57,8 +57,7 @@ public class EndPillarBlock extends RotatedPillarBlock implements BlockModelProv
}
@Override
public BlockModel getBlockModel(BlockState blockState) {
ResourceLocation blockId = Registry.BLOCK.getKey(this);
public BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
BlockModel model = BlockModel.fromString(createBlockPattern());
ResourceLocation modelLoc = new ResourceLocation(blockId.getNamespace(), "blocks/" + blockId.getPath());
model.name = modelLoc.toString();
@ -67,23 +66,10 @@ public class EndPillarBlock extends RotatedPillarBlock implements BlockModelProv
@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));
return BlockModelProvider.createRotatedModel(resourceLocation, blockState.getValue(AXIS));
}
private String createBlockPattern() {
protected String createBlockPattern() {
String texture = Registry.BLOCK.getKey(this).getPath();
return Patterns.createBlockPillar(texture);
}

View file

@ -62,7 +62,7 @@ public abstract class ModelLoaderMixin {
if (!resourceManager.hasResource(itemModelLoc)) {
Item item = Registry.ITEM.get(clearLoc);
if (item instanceof ModelProvider) {
BlockModel model = ((ModelProvider) item).getModel();
BlockModel model = ((ModelProvider) item).getModel(clearLoc);
if (model != null) {
model.name = itemLoc.toString();
} else {
@ -81,7 +81,7 @@ public abstract class ModelLoaderMixin {
block.getStateDefinition().getPossibleStates().forEach(blockState -> {
ModelResourceLocation stateLoc = BlockModelShaper.stateToModelLocation(clearLoc, blockState);
MultiVariant modelVariant = ((BlockModelProvider) block).getModelVariant(stateLoc, blockState);
BlockModel blockModel = ((BlockModelProvider) block).getBlockModel(blockState);
BlockModel blockModel = ((BlockModelProvider) block).getBlockModel(clearLoc, blockState);
if (modelVariant != null && blockModel != null) {
cacheAndQueueDependencies(stateLoc, modelVariant);
unbakedCache.put(stateLoc, blockModel);

View file

@ -4,6 +4,7 @@ import java.io.Reader;
import java.util.Collections;
import com.mojang.math.Transformation;
import com.mojang.math.Vector3f;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.MultiVariant;
import net.minecraft.client.renderer.block.model.Variant;
@ -14,9 +15,14 @@ import net.minecraft.world.level.block.state.BlockState;
public interface BlockModelProvider extends ModelProvider {
String getStatesPattern(Reader data);
ResourceLocation statePatternId();
BlockModel getBlockModel(BlockState blockState);
BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState);
MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState);
static BlockModel createItemModel(String name) {
String pattern = Patterns.createItemGenerated("item/" + name);
return BlockModel.fromString(pattern);
}
static BlockModel createBlockModel(ResourceLocation blockId, String pattern) {
BlockModel model = BlockModel.fromString(pattern);
ResourceLocation modelLoc = new ResourceLocation(blockId.getNamespace(), "blocks/" + blockId.getPath());
@ -24,9 +30,30 @@ public interface BlockModelProvider extends ModelProvider {
return model;
}
static MultiVariant createBlockSimple(ResourceLocation resourceLocation) {
Variant variant = new Variant(resourceLocation, Transformation.identity(), false, 1);
return new MultiVariant(Collections.singletonList(variant));
}
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));
}
static MultiVariant createRotatedModel(ResourceLocation resourceLocation, Direction.Axis rotation) {
Transformation transform = Transformation.identity();
switch (rotation) {
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));
}
}

View file

@ -1,8 +1,9 @@
package ru.betterend.patterns;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.resources.ResourceLocation;
public interface ModelProvider {
String getModelString(String name);
BlockModel getModel();
BlockModel getModel(ResourceLocation resourceLocation);
}