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); 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 @Override
public String getStatesPattern(Reader data) { public String getStatesPattern(Reader data) {
ResourceLocation blockId = Registry.BLOCK.getKey(this); ResourceLocation blockId = Registry.BLOCK.getKey(this);
@ -27,13 +21,9 @@ public class BarkBlock extends EndPillarBlock {
} }
@Override @Override
public BlockModel getBlockModel(BlockState blockState) { protected String createBlockPattern() {
return null; ResourceLocation blockId = Registry.BLOCK.getKey(this);
} return Patterns.createJson(Patterns.BLOCK_BASE, getName(blockId), blockId.getPath());
@Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
return null;
} }
private String getName(ResourceLocation blockId) { private String getName(ResourceLocation blockId) {

View file

@ -40,8 +40,8 @@ public class BlockBase extends Block implements BlockModelProvider {
} }
@Override @Override
public BlockModel getModel() { public BlockModel getModel(ResourceLocation blockId) {
return getBlockModel(defaultBlockState()); return getBlockModel(blockId, defaultBlockState());
} }
@Override @Override
@ -50,15 +50,13 @@ public class BlockBase extends Block implements BlockModelProvider {
} }
@Override @Override
public BlockModel getBlockModel(BlockState blockState) { public BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
ResourceLocation blockId = Registry.BLOCK.getKey(this);
String pattern = Patterns.createBlockSimple(blockId.getPath()); String pattern = Patterns.createBlockSimple(blockId.getPath());
return BlockModelProvider.createBlockModel(blockId, pattern); return BlockModelProvider.createBlockModel(blockId, pattern);
} }
@Override @Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) { public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
Variant variant = new Variant(resourceLocation, Transformation.identity(), false, 1); return BlockModelProvider.createBlockSimple(resourceLocation);
return new MultiVariant(Collections.singletonList(variant));
} }
} }

View file

@ -88,14 +88,13 @@ public class EndAnvilBlock extends AnvilBlock implements BlockModelProvider {
} }
@Override @Override
public BlockModel getModel() { public BlockModel getModel(ResourceLocation blockId) {
return getBlockModel(defaultBlockState()); return getBlockModel(blockId, defaultBlockState());
} }
@Override @Override
public BlockModel getBlockModel(BlockState blockState) { public BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
int destruction = blockState.getValue(DESTRUCTION); int destruction = blockState.getValue(DESTRUCTION);
ResourceLocation blockId = Registry.BLOCK.getKey(this);
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);

View file

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

View file

@ -5,6 +5,9 @@ import java.util.Collections;
import java.util.List; import java.util.List;
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.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;
@ -38,16 +41,33 @@ public class EndChainBlock extends ChainBlock implements BlockModelProvider, IRe
public String getModelString(String block) { public String getModelString(String block) {
ResourceLocation blockId = Registry.BLOCK.getKey(this); ResourceLocation blockId = Registry.BLOCK.getKey(this);
if (block.contains("item")) { 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()); return Patterns.createJson(Patterns.BLOCK_CHAIN, blockId.getPath(), blockId.getPath());
} }
@Override
public BlockModel getModel(ResourceLocation blockId) {
return BlockModelProvider.createItemModel(blockId.getPath());
}
@Override @Override
public ResourceLocation statePatternId() { public ResourceLocation statePatternId() {
return Patterns.STATE_CHAIN; 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 @Override
public ERenderLayer getRenderLayer() { public ERenderLayer getRenderLayer() {
return ERenderLayer.CUTOUT; return ERenderLayer.CUTOUT;

View file

@ -4,6 +4,8 @@ import java.io.Reader;
import java.util.List; import java.util.List;
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.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;
@ -21,9 +23,7 @@ public class EndChestBlock extends ChestBlock implements BlockModelProvider {
private final Block parent; private final Block parent;
public EndChestBlock(Block source) { public EndChestBlock(Block source) {
super(FabricBlockSettings.copyOf(source).noOcclusion(), () -> { super(FabricBlockSettings.copyOf(source).noOcclusion(), () -> EndBlockEntities.CHEST);
return EndBlockEntities.CHEST;
});
this.parent = source; this.parent = source;
} }
@ -57,9 +57,27 @@ public class EndChestBlock extends ChestBlock implements BlockModelProvider {
} }
return Patterns.createJson(Patterns.BLOCK_EMPTY, parentId.getPath()); 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 @Override
public ResourceLocation statePatternId() { public ResourceLocation statePatternId() {
return Patterns.STATE_SIMPLE; 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 java.util.List;
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.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;
@ -37,9 +39,24 @@ public class EndComposterBlock extends ComposterBlock implements BlockModelProvi
String blockName = blockId.getPath(); String blockName = blockId.getPath();
return Patterns.createJson(Patterns.BLOCK_COMPOSTER, blockName); return Patterns.createJson(Patterns.BLOCK_COMPOSTER, blockName);
} }
@Override
public BlockModel getModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState());
}
@Override @Override
public ResourceLocation statePatternId() { public ResourceLocation statePatternId() {
return Patterns.STATE_COMPOSTER; 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 java.util.List;
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.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;
@ -49,9 +51,37 @@ public class EndCraftingTableBlock extends CraftingTableBlock implements BlockMo
} }
}); });
} }
@Override
public BlockModel getModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState());
}
@Override @Override
public ResourceLocation statePatternId() { public ResourceLocation statePatternId() {
return Patterns.STATE_SIMPLE; 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 @Override
public BlockModel getModel() { public BlockModel getModel(ResourceLocation blockId) {
return null; return getBlockModel(blockId, defaultBlockState());
} }
@Override @Override
@ -57,8 +57,7 @@ public class EndPillarBlock extends RotatedPillarBlock implements BlockModelProv
} }
@Override @Override
public BlockModel getBlockModel(BlockState blockState) { public BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
ResourceLocation blockId = Registry.BLOCK.getKey(this);
BlockModel model = BlockModel.fromString(createBlockPattern()); BlockModel model = BlockModel.fromString(createBlockPattern());
ResourceLocation modelLoc = new ResourceLocation(blockId.getNamespace(), "blocks/" + blockId.getPath()); ResourceLocation modelLoc = new ResourceLocation(blockId.getNamespace(), "blocks/" + blockId.getPath());
model.name = modelLoc.toString(); model.name = modelLoc.toString();
@ -67,23 +66,10 @@ public class EndPillarBlock extends RotatedPillarBlock implements BlockModelProv
@Override @Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) { public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
Direction.Axis axis = blockState.getValue(AXIS); return BlockModelProvider.createRotatedModel(resourceLocation, 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() { protected String createBlockPattern() {
String texture = Registry.BLOCK.getKey(this).getPath(); String texture = Registry.BLOCK.getKey(this).getPath();
return Patterns.createBlockPillar(texture); return Patterns.createBlockPillar(texture);
} }

View file

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

View file

@ -4,6 +4,7 @@ import java.io.Reader;
import java.util.Collections; import java.util.Collections;
import com.mojang.math.Transformation; 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.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.client.renderer.block.model.Variant;
@ -14,9 +15,14 @@ import net.minecraft.world.level.block.state.BlockState;
public interface BlockModelProvider extends ModelProvider { public interface BlockModelProvider extends ModelProvider {
String getStatesPattern(Reader data); String getStatesPattern(Reader data);
ResourceLocation statePatternId(); ResourceLocation statePatternId();
BlockModel getBlockModel(BlockState blockState); BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState);
MultiVariant getModelVariant(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) { static BlockModel createBlockModel(ResourceLocation blockId, String pattern) {
BlockModel model = BlockModel.fromString(pattern); BlockModel model = BlockModel.fromString(pattern);
ResourceLocation modelLoc = new ResourceLocation(blockId.getNamespace(), "blocks/" + blockId.getPath()); ResourceLocation modelLoc = new ResourceLocation(blockId.getNamespace(), "blocks/" + blockId.getPath());
@ -24,9 +30,30 @@ public interface BlockModelProvider extends ModelProvider {
return model; 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) { static MultiVariant createFacingModel(ResourceLocation resourceLocation, Direction facing) {
Transformation transform = new Transformation(null, facing.getRotation(), null, null); Transformation transform = new Transformation(null, facing.getRotation(), null, null);
Variant variant = new Variant(resourceLocation, transform, false, 1); Variant variant = new Variant(resourceLocation, transform, false, 1);
return new MultiVariant(Collections.singletonList(variant)); 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; package ru.betterend.patterns;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.resources.ResourceLocation;
public interface ModelProvider { public interface ModelProvider {
String getModelString(String name); String getModelString(String name);
BlockModel getModel(); BlockModel getModel(ResourceLocation resourceLocation);
} }