Skip to content

Commit

Permalink
Work on rotatables
Browse files Browse the repository at this point in the history
  • Loading branch information
Fureniku committed Nov 28, 2023
1 parent 5c098a4 commit e4fe9b4
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected BlockState getPlacementState(BlockPlaceContext context) {
* Create the blockstate. Override whenever we have a custom blockstate available on a block.
* @param builder
*/
protected void createBlockState(StateDefinition.Builder builder) {}
protected void createBlockState(StateDefinition.Builder<Block, BlockState> builder) {}

/**
* Get which shape should be used for the block. Override when blocks don't have a standard full cube.
Expand Down Expand Up @@ -127,7 +127,7 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
}

@Override
public void createBlockStateDefinition(StateDefinition.Builder builder) {
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
createBlockState(builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
public class MetroBlockDecorative extends MetroBlockBase {

private final VoxelShape BLOCK_SHAPE;
private TextureSet[] _resources;
private String _modelName;
protected TextureSet[] _resources;
protected String _modelName;

public MetroBlockDecorative(Properties props, String modelName, ResourceLocation resource) {
this(props, 16, 16, modelName, new TextureSet("texture", resource));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
package com.fureniku.metropolis.blocks;

import com.fureniku.metropolis.datagen.MetroBlockStateProvider;
import com.fureniku.metropolis.datagen.TextureSet;
import com.fureniku.metropolis.utils.Debug;
import net.minecraft.core.Direction;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.neoforged.neoforge.client.model.generators.BlockModelBuilder;
import net.neoforged.neoforge.registries.RegistryObject;

public class MetroBlockDecorativeRotatable extends MetroBlockBase {
/**
* Decorative non-full block which can be rotated
*/
public class MetroBlockDecorativeRotatable extends MetroBlockDecorative {

protected static final DirectionProperty DIRECTION = DirectionProperty.create("rotation", Direction.Plane.HORIZONTAL);
protected static final DirectionProperty DIRECTION = HorizontalDirectionalBlock.FACING;
private final VoxelShape BLOCK_SHAPE;

public MetroBlockDecorativeRotatable(Properties props) {
this(props, 16, 16);
}

public MetroBlockDecorativeRotatable(Properties props, float height) {
this(props, height, 16);
}

public MetroBlockDecorativeRotatable(Properties props, float height, float width) {
this(props, width, height, width);
}

public MetroBlockDecorativeRotatable(Properties props, VoxelShape shape) {
super(props);
/**
* Constructor for decorative blocks which have a specific shape. This shape is rotated automatically.
* @param props
* @param shape
*/
public MetroBlockDecorativeRotatable(Properties props, VoxelShape shape, String modelName, TextureSet... textures) {
super(props, modelName, textures);
BLOCK_SHAPE = shape;
this.registerDefaultState(this.defaultBlockState().setValue(DIRECTION, Direction.NORTH));
}

public MetroBlockDecorativeRotatable(Properties props, float sizeX, float sizeY, float sizeZ) {
super(props);
float insetX = (16-sizeX)/2;
float insetZ = (16-sizeZ)/2;
BLOCK_SHAPE = Block.box(insetX, 0, insetZ, 16-insetX, 16-sizeY, 16-insetZ);
this.registerDefaultState(this.defaultBlockState().setValue(DIRECTION, Direction.NORTH));
this.registerDefaultState(this.stateDefinition.any().setValue(DIRECTION, Direction.NORTH));
}

@Override
public void generateBlockState(RegistryObject<Block> blockRegistryObject, MetroBlockStateProvider blockStateProvider) {
blockStateProvider.simpleBlockWithItem(blockRegistryObject.get());
Block block = blockRegistryObject.get();
BlockModelBuilder bmb;
if (_modelName == null || _resources == null) {
bmb = blockStateProvider.getModelFilesWithTexture(block, "", "blocks/decorative/" + block.getName(), blockStateProvider.modLoc("blocks/decorative/" + block.getName()));
} else {
bmb = blockStateProvider.getModelFilesWithTexture(block, "", "blocks/decorative/" + _modelName, _resources[0].getTexture());
if (_resources.length > 1) {
for (int i = 1; i < _resources.length; i++) {
bmb = bmb.texture(_resources[i].getKey(), _resources[i].getTexture());
}
}
}
blockStateProvider.horizontalBlock(block, bmb);
}

@Override
Expand All @@ -55,7 +59,12 @@ protected BlockState getPlacementState(BlockPlaceContext context) {
}

@Override
protected void createBlockState(StateDefinition.Builder builder) {
protected void createBlockState(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(DIRECTION);
}

@Override
protected VoxelShape getShapeFromBlockState(BlockState pState) {
return BLOCK_SHAPE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.fureniku.metropolis.blocks;

import com.fureniku.metropolis.datagen.TextureSet;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.phys.shapes.VoxelShape;

public class MetroBlockDecorativeRotatableBuilder {

private BlockBehaviour.Properties props;
private VoxelShape blockShape = Block.box(0, 0, 0, 16, 16, 16);
private String modelName = null;
private TextureSet[] textures = null;

public MetroBlockDecorativeRotatableBuilder setProps(BlockBehaviour.Properties props) {
this.props = props;
return this;
}

public MetroBlockDecorativeRotatableBuilder setHeight(float height) {
return setShape(16, height, 16);
}

public MetroBlockDecorativeRotatableBuilder setWidth(float width) {
return setShape(width, 16, width);
}

public MetroBlockDecorativeRotatableBuilder setShape(float width, float height) {
return setShape(width, height, width);
}

public MetroBlockDecorativeRotatableBuilder setShape(float width, float height, float depth) {
float insetX = (16-width)/2;
float insetZ = (16-depth)/2;
return setShape(Block.box(insetX, 0, insetZ, 16-insetX, 16-height, 16-insetZ));
}

public MetroBlockDecorativeRotatableBuilder setShape(VoxelShape shape) {
this.blockShape = shape;
return this;
}

public MetroBlockDecorativeRotatableBuilder setModelName(String modelName) {
this.modelName = modelName;
return this;
}

public MetroBlockDecorativeRotatableBuilder setTextures(TextureSet... textures) {
this.textures = textures;
return this;
}

public MetroBlockDecorativeRotatableBuilder setTextures(ResourceLocation resource) {
return setTextures(new TextureSet("texture", resource));
}

public MetroBlockDecorativeRotatable build() {
return new MetroBlockDecorativeRotatable(props, blockShape, modelName, textures);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.neoforged.neoforge.client.model.generators.BlockModelBuilder;
import net.neoforged.neoforge.client.model.generators.BlockStateProvider;
import net.neoforged.neoforge.client.model.generators.ItemModelBuilder;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.neoforged.neoforge.client.model.generators.*;
import net.neoforged.neoforge.common.data.ExistingFileHelper;
import net.neoforged.neoforge.registries.ForgeRegistries;
import net.neoforged.neoforge.registries.RegistryObject;

import java.util.Collection;
import java.util.function.Function;

/**
* Metropolis Block State Provider
Expand All @@ -39,6 +40,10 @@ public void simpleBlockWithItem(Block block) {
simpleBlockWithItem(block, cubeAll(block));
}

public void horizontalBlock(Block block, ModelFile model) {
horizontalBlock(block, model, 180);
}

/**
* Get a modelled block, with a matching itemblock. Uses your blocks name to get the matching texture.
* @param block Your block
Expand Down

0 comments on commit e4fe9b4

Please sign in to comment.