Skip to content

Commit

Permalink
Registering blockstates from helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Fureniku committed Jan 13, 2024
1 parent a1a0f98 commit c489491
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ public void assignHelper(HelperBase helper) {

@Override
protected void createBlockState(StateDefinition.Builder<Block, BlockState> builder) {
Debug.Log("[1234] create blockstate");
/*if (_toggleHelper != null) builder.add(_toggleHelper.TOGGLED);
if (_rotationHelper != null) builder.add(_rotationHelper.DIRECTION);
if (_connectHorizontalHelper != null) */
builder.add(ConnectHorizontalHelper.NORTH, ConnectHorizontalHelper.EAST, ConnectHorizontalHelper.SOUTH, ConnectHorizontalHelper.WEST);
for (int i = 0; i < _helpers.size(); i++) {
HelperBase helper = _helpers.get(i);
if (helper instanceof HelperBlockstate) {
builder = ((HelperBlockstate) helper).addDefaultState(builder);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.world.level.block.AirBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.shapes.VoxelShape;
Expand Down Expand Up @@ -60,6 +61,15 @@ public HelperType getType() {
return HelperType.CONNECTION_HORIZONTAL;
}

@Override
public StateDefinition.Builder<Block, BlockState> addDefaultState(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(NORTH);
builder.add(EAST);
builder.add(SOUTH);
builder.add(WEST);
return builder;
}

@Override
public void generateBlockstate(TextureSet[] resources, String modelDir, String modelName, Block block, MetroBlockStateProvider blockStateProvider) {
String connectedName = _connectedModelName != null ? _connectedModelName : modelName + "_connection";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.fureniku.metropolis.blocks.decorative.helpers;

import com.fureniku.metropolis.enums.HelperType;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;

public abstract class HelperBase {

public abstract BlockState setDefaultState(BlockState state);
public abstract HelperType getType();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.Property;

public abstract class HelperBlockstate extends HelperBase {

public abstract void generateBlockstate(TextureSet[] resources, String modelDir, String modelName, Block block, MetroBlockStateProvider blockStateProvider);
public abstract BlockState getPlacementState(BlockPlaceContext context, BlockState currentState, Block block);
public abstract StateDefinition.Builder<Block, BlockState> addDefaultState(StateDefinition.Builder<Block, BlockState> builder);

public BlockState setState(BlockState state, Property prop, Comparable value) {
return state.setValue(prop, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.world.level.block.Block;
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.DirectionProperty;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.neoforged.neoforge.client.model.generators.BlockModelBuilder;
Expand Down Expand Up @@ -39,6 +40,12 @@ public HelperType getType() {
return HelperType.ROTATION;
}

@Override
public StateDefinition.Builder<Block, BlockState> addDefaultState(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(DIRECTION);
return builder;
}

@Override
public void generateBlockstate(TextureSet[] resources, String modelDir, String modelName, Block block, MetroBlockStateProvider blockStateProvider) {
BlockModelBuilder bmb = blockStateProvider.prepareModels(block, modelDir, modelName, resources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.shapes.VoxelShape;
Expand Down Expand Up @@ -59,6 +60,12 @@ public BlockState setDefaultState(BlockState state) {
return state;
}

@Override
public StateDefinition.Builder<Block, BlockState> addDefaultState(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(TOGGLED);
return builder;
}

@Override
public HelperType getType() {
return HelperType.TOGGLE;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/fureniku/metropolis/utils/ShapeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class ShapeUtils {

private static final Object2IntMap<BlockState> stateToIndex = new Object2IntOpenHashMap<>();

public static VoxelShape makeShape(float size, float height) {
return Block.box(size, 0, size, 16-size, height, 16-size);
}

/**
* Create an array of voxelshapes for a non-standard shaped collider. Simple constructor for very simple uniform custom shapes
* @param size The expanding point from the center for each segment of the block (e.g. 2 will make a segment 4 wide)
Expand Down

0 comments on commit c489491

Please sign in to comment.