You can now place cloud pillars horizontally

This commit is contained in:
Sollace 2024-03-25 00:41:42 +00:00
parent 9ef3a946f6
commit a6b8e6fe93
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
2 changed files with 75 additions and 31 deletions

View file

@ -1,79 +1,115 @@
package com.minelittlepony.unicopia.block.cloud;
import java.util.Map;
import java.util.function.Function;
import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.EquineContext;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.PillarBlock;
import net.minecraft.block.ShapeContext;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.Util;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Direction.AxisDirection;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.WorldAccess;
public class CloudPillarBlock extends CloudBlock {
private static final BooleanProperty NORTH = Properties.NORTH;
private static final BooleanProperty SOUTH = Properties.SOUTH;
public static final EnumProperty<Direction.Axis> AXIS = Properties.AXIS;
private static final BooleanProperty TOP = Properties.NORTH;
private static final BooleanProperty BOTTOM = Properties.SOUTH;
private static final Map<Direction, BooleanProperty> DIRECTION_PROPERTIES = Map.of(
Direction.UP, NORTH,
Direction.DOWN, SOUTH
Direction.UP, TOP, Direction.DOWN, BOTTOM,
Direction.SOUTH, TOP, Direction.NORTH, BOTTOM,
Direction.EAST, TOP, Direction.WEST, BOTTOM
);
private static final VoxelShape CORE_SHAPE = Block.createCuboidShape(1, 0, 1, 15, 16, 15);
private static final VoxelShape FOOT_SHAPE = Block.createCuboidShape(0, 0, 0, 16, 5, 16);
private static final VoxelShape CAP_SHAPE = FOOT_SHAPE.offset(0, 11F / 16F, 0);
private static final VoxelShape[] SHAPES = new VoxelShape[] {
CORE_SHAPE,
VoxelShapes.union(CORE_SHAPE, FOOT_SHAPE),
VoxelShapes.union(CORE_SHAPE, CAP_SHAPE),
VoxelShapes.union(CORE_SHAPE, FOOT_SHAPE, CAP_SHAPE)
private static final Function<Direction.Axis, VoxelShape[]> SHAPES = Util.memoize(axis -> {
int[] offsets = { axis.choose(1, 0, 0), axis.choose(0, 1, 0), axis.choose(0, 0, 1) };
float capOffset = 11F / 16F;
VoxelShape core = Block.createCuboidShape(
axis.choose(0, 1, 1), axis.choose(1, 0, 1), axis.choose(1, 1, 0),
16 - axis.choose(0, 1, 1), 16 - axis.choose(1, 0, 1), 16 - axis.choose(1, 1, 0)
);
VoxelShape foot = Block.createCuboidShape(0, 0, 0, 16 - (11 * offsets[0]), 16 - (11 * offsets[1]), 16 - (11 * offsets[2]));
VoxelShape cap = foot.offset(capOffset * offsets[0], capOffset * offsets[1], capOffset * offsets[2]);
return new VoxelShape[] {
core,
VoxelShapes.union(core, foot),
VoxelShapes.union(core, cap),
VoxelShapes.union(core, cap, foot)
};
// [0,0] [0,1]
// [1,0] [1,1]
});
public CloudPillarBlock(Settings settings) {
super(settings, false);
setDefaultState(getDefaultState().with(NORTH, true).with(SOUTH, true));
setDefaultState(getDefaultState().with(TOP, true).with(BOTTOM, true).with(AXIS, Direction.Axis.Y));
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(NORTH, SOUTH);
builder.add(AXIS, TOP, BOTTOM);
}
@Override
protected VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context, EquineContext equineContext) {
return SHAPES[(state.get(NORTH) ? 0 : 2) + (state.get(SOUTH) ? 0 : 1)];
var axis = state.get(AXIS);
int[] offsets = { axis.choose(1, 0, 0), axis.choose(0, 1, 0), axis.choose(0, 0, 1) };
float capOffset = 11F / 16F;
VoxelShape core = Block.createCuboidShape(
axis.choose(0, 1, 1), axis.choose(1, 0, 1), axis.choose(1, 1, 0),
16 - axis.choose(0, 1, 1), 16 - axis.choose(1, 0, 1), 16 - axis.choose(1, 1, 0)
);
VoxelShape foot = Block.createCuboidShape(0, 0, 0, 16 - (11 * offsets[0]), 16 - (11 * offsets[1]), 16 - (11 * offsets[2]));
VoxelShape cap = foot.offset(capOffset * offsets[0], capOffset * offsets[1], capOffset * offsets[2]);
var temp = new VoxelShape[] {
core,
VoxelShapes.union(core, foot),
VoxelShapes.union(core, cap),
VoxelShapes.union(core, cap, foot)
};
return temp[(state.get(TOP) ? 0 : 2) + (state.get(BOTTOM) ? 0 : 1)];
//return SHAPES.apply(state.get(AXIS))[(state.get(TOP) ? 0 : 2) + (state.get(BOTTOM) ? 0 : 1)];
}
@Override
@Nullable
protected BlockState getPlacementState(ItemPlacementContext placementContext, EquineContext equineContext) {
BlockPos pos = placementContext.getBlockPos();
BlockState state = super.getPlacementState(placementContext, equineContext);
for (var property : DIRECTION_PROPERTIES.entrySet()) {
state = state.with(property.getValue(), placementContext.getWorld().getBlockState(pos.offset(property.getKey())).isOf(this));
}
return state;
Direction.Axis axis = placementContext.getSide().getAxis();
Direction upDirection = Direction.get(AxisDirection.POSITIVE, axis);
Direction downDirection = Direction.get(AxisDirection.NEGATIVE, axis);
BlockState above = placementContext.getWorld().getBlockState(pos.offset(upDirection));
BlockState below = placementContext.getWorld().getBlockState(pos.offset(downDirection));
return super.getPlacementState(placementContext, equineContext)
.with(DIRECTION_PROPERTIES.get(upDirection), above.isOf(this) && above.get(AXIS) == axis)
.with(DIRECTION_PROPERTIES.get(downDirection), below.isOf(this) && below.get(AXIS) == axis)
.with(AXIS, axis);
}
@Deprecated
@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
if (direction.getAxis() == Direction.Axis.Y) {
return state.with(DIRECTION_PROPERTIES.get(direction), neighborState.isOf(this));
if (direction.getAxis() == state.get(AXIS)) {
return state.with(DIRECTION_PROPERTIES.get(direction), neighborState.isOf(this) && neighborState.get(AXIS) == state.get(AXIS));
}
return state;
}
@Override
public BlockState rotate(BlockState state, BlockRotation rotation) {
return PillarBlock.changeRotation(state, rotation);
}
}

View file

@ -381,9 +381,17 @@ public class UBlockStateModelGenerator extends BlockStateModelGenerator {
Identifier middle = BlockModels.TEMPLATE_PILLAR.upload(pillar, textures, modelCollector);
Identifier end = BlockModels.TEMPLATE_PILLAR_END.upload(pillar, textures, modelCollector);
blockStateCollector.accept(MultipartBlockStateSupplier.create(pillar)
.with(BlockStateVariant.create().put(MODEL, middle))
.with(When.create().set(Properties.NORTH, false), BlockStateVariant.create().put(MODEL, end).put(UVLOCK, true).put(X, R180))
.with(When.create().set(Properties.SOUTH, false), BlockStateVariant.create().put(MODEL, end))
.with(When.create().set(Properties.AXIS, Direction.Axis.X), BlockStateVariant.create().put(MODEL, middle).put(X, R90).put(Y, R90))
.with(When.create().set(Properties.AXIS, Direction.Axis.X).set(Properties.NORTH, false), BlockStateVariant.create().put(MODEL, end).put(X, R270).put(Y, R90))
.with(When.create().set(Properties.AXIS, Direction.Axis.X).set(Properties.SOUTH, false), BlockStateVariant.create().put(MODEL, end).put(X, R90).put(Y, R90))
.with(When.create().set(Properties.AXIS, Direction.Axis.Y), BlockStateVariant.create().put(MODEL, middle))
.with(When.create().set(Properties.AXIS, Direction.Axis.Y).set(Properties.NORTH, false), BlockStateVariant.create().put(MODEL, end).put(X, R180))
.with(When.create().set(Properties.AXIS, Direction.Axis.Y).set(Properties.SOUTH, false), BlockStateVariant.create().put(MODEL, end))
.with(When.create().set(Properties.AXIS, Direction.Axis.Z), BlockStateVariant.create().put(MODEL, middle).put(X, R90))
.with(When.create().set(Properties.AXIS, Direction.Axis.Z).set(Properties.NORTH, false), BlockStateVariant.create().put(MODEL, end).put(X, R90))
.with(When.create().set(Properties.AXIS, Direction.Axis.Z).set(Properties.SOUTH, false), BlockStateVariant.create().put(MODEL, end).put(X, R270))
);
ItemModels.TEMPLATE_PILLAR.upload(ModelIds.getItemModelId(pillar.asItem()), textures, modelCollector);
}