- Modified connectivity behaviour of Girders
- Girders now support and connect to lanterns and chains
This commit is contained in:
simibubi 2022-07-03 16:34:35 +02:00
parent 58263f9c06
commit 9b9e252bac
4 changed files with 179 additions and 77 deletions

View File

@ -1,5 +1,6 @@
package com.simibubi.create.content.curiosities.girder;
import static net.minecraft.world.level.block.FaceAttachedHorizontalDirectionalBlock.FACE;
import static net.minecraft.world.level.block.state.properties.BlockStateProperties.WATERLOGGED;
import java.util.Random;
@ -25,6 +26,7 @@ import com.simibubi.create.foundation.utility.placement.PlacementHelpers;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Direction.Axis;
import net.minecraft.core.Direction.AxisDirection;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
@ -34,11 +36,14 @@ import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.ChainBlock;
import net.minecraft.world.level.block.LanternBlock;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.SimpleWaterloggedBlock;
@ -46,13 +51,17 @@ import net.minecraft.world.level.block.WallBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition.Builder;
import net.minecraft.world.level.block.state.properties.AttachFace;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
public class GirderBlock extends Block implements SimpleWaterloggedBlock, IWrenchable {
@ -63,10 +72,12 @@ public class GirderBlock extends Block implements SimpleWaterloggedBlock, IWrenc
public static final BooleanProperty Z = BooleanProperty.create("z");
public static final BooleanProperty TOP = BooleanProperty.create("top");
public static final BooleanProperty BOTTOM = BooleanProperty.create("bottom");
public static final EnumProperty<Axis> AXIS = BlockStateProperties.AXIS;
public GirderBlock(Properties p_49795_) {
super(p_49795_);
registerDefaultState(defaultBlockState().setValue(WATERLOGGED, false)
.setValue(AXIS, Axis.Y)
.setValue(TOP, false)
.setValue(BOTTOM, false)
.setValue(X, false)
@ -75,7 +86,13 @@ public class GirderBlock extends Block implements SimpleWaterloggedBlock, IWrenc
@Override
protected void createBlockStateDefinition(Builder<Block, BlockState> pBuilder) {
super.createBlockStateDefinition(pBuilder.add(X, Z, TOP, BOTTOM, WATERLOGGED));
super.createBlockStateDefinition(pBuilder.add(X, Z, TOP, BOTTOM, AXIS, WATERLOGGED));
}
@Override
@SuppressWarnings("deprecation")
public VoxelShape getBlockSupportShape(BlockState pState, BlockGetter pReader, BlockPos pPos) {
return Shapes.or(super.getBlockSupportShape(pState, pReader, pPos), AllShapes.EIGHT_VOXEL_POLE.get(Axis.Y));
}
@Override
@ -92,19 +109,20 @@ public class GirderBlock extends Block implements SimpleWaterloggedBlock, IWrenc
.setValue(BOTTOM, pState.getValue(BOTTOM))
.setValue(GirderEncasedShaftBlock.HORIZONTAL_AXIS, pState.getValue(X) || pHit.getDirection()
.getAxis() == Axis.Z ? Axis.Z : Axis.X));
pLevel.playSound(null, pPos, SoundEvents.NETHERITE_BLOCK_HIT, SoundSource.BLOCKS, 0.5f, 1.25f);
if (!pLevel.isClientSide && !pPlayer.isCreative()) {
itemInHand.shrink(1);
if (itemInHand.isEmpty())
pPlayer.setItemInHand(pHand, ItemStack.EMPTY);
}
return InteractionResult.SUCCESS;
}
if (AllItems.WRENCH.isIn(itemInHand) && !pPlayer.isSteppingCarefully()) {
if (GirderWrenchBehavior.handleClick(pLevel, pPos, pState, pHit))
return InteractionResult.sidedSuccess(pLevel.isClientSide);
return InteractionResult.FAIL;
}
@ -121,6 +139,11 @@ public class GirderBlock extends Block implements SimpleWaterloggedBlock, IWrenc
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : Fluids.EMPTY.defaultFluidState();
}
@Override
public InteractionResult onWrenched(BlockState state, UseOnContext context) {
return InteractionResult.PASS;
}
@Override
public void tick(BlockState p_60462_, ServerLevel p_60463_, BlockPos p_60464_, Random p_60465_) {
Block.updateOrDestroy(p_60462_, Block.updateFromNeighbourShapes(p_60462_, p_60463_, p_60464_), p_60463_,
@ -133,12 +156,29 @@ public class GirderBlock extends Block implements SimpleWaterloggedBlock, IWrenc
if (state.getValue(WATERLOGGED))
world.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(world));
Axis axis = direction.getAxis();
Property<Boolean> updateProperty =
axis == Axis.X ? X : axis == Axis.Z ? Z : direction == Direction.UP ? TOP : BOTTOM;
if (!isConnected(world, pos, state, direction) && !isConnected(world, pos, state, direction.getOpposite()))
state = state.setValue(updateProperty, false);
if (direction.getAxis() != Axis.Y) {
if (state.getValue(AXIS) != direction.getAxis()) {
Property<Boolean> updateProperty =
axis == Axis.X ? X : axis == Axis.Z ? Z : direction == Direction.UP ? TOP : BOTTOM;
if (!isConnected(world, pos, state, direction)
&& !isConnected(world, pos, state, direction.getOpposite()))
state = state.setValue(updateProperty, false);
}
} else if (state.getValue(AXIS) != Axis.Y) {
if (world.getBlockState(pos.above())
.getBlockSupportShape(world, pos.above())
.isEmpty())
state = state.setValue(TOP, false);
if (world.getBlockState(pos.below())
.getBlockSupportShape(world, pos.below())
.isEmpty())
state = state.setValue(BOTTOM, false);
}
for (Direction d : Iterate.directionsInAxis(axis))
state = updateState(world, pos, state, d);
return state;
}
@ -151,6 +191,7 @@ public class GirderBlock extends Block implements SimpleWaterloggedBlock, IWrenc
BlockState state = super.getStateForPlacement(context);
state = state.setValue(X, face.getAxis() == Axis.X);
state = state.setValue(Z, face.getAxis() == Axis.Z);
state = state.setValue(AXIS, face.getAxis());
for (Direction d : Iterate.directions)
state = updateState(level, pos, state, d);
@ -166,7 +207,9 @@ public class GirderBlock extends Block implements SimpleWaterloggedBlock, IWrenc
if (axis.isVertical())
return updateVerticalProperty(level, pos, state, updateProperty, sideState, d);
if (sideState.getBlock() instanceof GirderEncasedShaftBlock
if (state.getValue(AXIS) == axis)
state = state.setValue(updateProperty, true);
else if (sideState.getBlock() instanceof GirderEncasedShaftBlock
&& sideState.getValue(GirderEncasedShaftBlock.HORIZONTAL_AXIS) != axis)
state = state.setValue(updateProperty, true);
else if (sideState.getBlock() == state.getBlock() && sideState.getValue(updateProperty))
@ -206,20 +249,37 @@ public class GirderBlock extends Block implements SimpleWaterloggedBlock, IWrenc
public static BlockState updateVerticalProperty(LevelAccessor level, BlockPos pos, BlockState state,
Property<Boolean> updateProperty, BlockState sideState, Direction d) {
if (isGirder(sideState) && isXGirder(sideState) == isZGirder(sideState))
state = state.setValue(updateProperty, true);
boolean canAttach = false;
if (state.hasProperty(AXIS) && state.getValue(AXIS) == Axis.Y)
canAttach = true;
else if (isGirder(sideState) && isXGirder(sideState) == isZGirder(sideState))
canAttach = true;
else if (isGirder(sideState))
state = state.setValue(updateProperty, true);
canAttach = true;
else if (sideState.hasProperty(WallBlock.UP) && sideState.getValue(WallBlock.UP))
state = state.setValue(updateProperty, true);
canAttach = true;
else if (sideState.getBlock() instanceof NixieTubeBlock && NixieTubeBlock.getFacing(sideState) == d)
state = state.setValue(updateProperty, true);
canAttach = true;
else if (sideState.getBlock() instanceof FlapDisplayBlock)
state = state.setValue(updateProperty, true);
else if (sideState.getBlock() instanceof PlacardBlock && PlacardBlock.connectedDirection(sideState) == d)
state = state.setValue(updateProperty, true);
canAttach = true;
else if (sideState.getBlock() instanceof LanternBlock
&& (d == Direction.DOWN) == (sideState.getValue(LanternBlock.HANGING)))
canAttach = true;
else if (sideState.getBlock() instanceof ChainBlock && sideState.getValue(ChainBlock.AXIS) == Axis.Y)
canAttach = true;
else if (sideState.hasProperty(FACE)) {
if (sideState.getValue(FACE) == AttachFace.CEILING && d == Direction.DOWN)
canAttach = true;
else if (sideState.getValue(FACE) == AttachFace.FLOOR && d == Direction.UP)
canAttach = true;
} else if (sideState.getBlock() instanceof PlacardBlock && PlacardBlock.connectedDirection(sideState) == d)
canAttach = true;
else if (isFacingBracket(level, pos, d))
state = state.setValue(updateProperty, true);
canAttach = true;
if (canAttach)
return state.setValue(updateProperty, true);
return state;
}
@ -280,6 +340,9 @@ public class GirderBlock extends Block implements SimpleWaterloggedBlock, IWrenc
@Override
public BlockState rotate(BlockState state, Rotation rot) {
state = state.setValue(AXIS,
rot.rotate(Direction.fromAxisAndDirection(state.getValue(AXIS), AxisDirection.POSITIVE))
.getAxis());
if (rot.rotate(Direction.EAST)
.getAxis() == Axis.X)
return state;

View File

@ -30,6 +30,7 @@ import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
public class GirderEncasedShaftBlock extends HorizontalAxisKineticBlock
@ -52,12 +53,19 @@ public class GirderEncasedShaftBlock extends HorizontalAxisKineticBlock
return AllShapes.GIRDER_BEAM_SHAFT.get(pState.getValue(HORIZONTAL_AXIS));
}
@Override
@SuppressWarnings("deprecation")
public VoxelShape getBlockSupportShape(BlockState pState, BlockGetter pReader, BlockPos pPos) {
return Shapes.or(super.getBlockSupportShape(pState, pReader, pPos), AllShapes.EIGHT_VOXEL_POLE.get(Axis.Y));
}
@Override
public BlockState getRotatedBlockState(BlockState originalState, Direction targetedFace) {
return AllBlocks.METAL_GIRDER.getDefaultState()
.setValue(WATERLOGGED, originalState.getValue(WATERLOGGED))
.setValue(GirderBlock.X, originalState.getValue(HORIZONTAL_AXIS) == Axis.Z)
.setValue(GirderBlock.Z, originalState.getValue(HORIZONTAL_AXIS) == Axis.X)
.setValue(GirderBlock.AXIS, originalState.getValue(HORIZONTAL_AXIS) == Axis.X ? Axis.Z : Axis.X)
.setValue(GirderBlock.BOTTOM, originalState.getValue(BOTTOM))
.setValue(GirderBlock.TOP, originalState.getValue(TOP));
}
@ -96,7 +104,10 @@ public class GirderEncasedShaftBlock extends HorizontalAxisKineticBlock
Property<Boolean> updateProperty = direction == Direction.UP ? TOP : BOTTOM;
if (direction.getAxis()
.isVertical()) {
state = state.setValue(updateProperty, false);
if (world.getBlockState(pos.relative(direction))
.getBlockSupportShape(world, pos.relative(direction))
.isEmpty())
state = state.setValue(updateProperty, false);
return GirderBlock.updateVerticalProperty(world, pos, state, updateProperty, neighbourState, direction);
}

View File

@ -67,7 +67,8 @@ public class GirderPlacementHelper implements IPlacementHelper {
private BlockState withAxis(BlockState state, Axis axis) {
if (state.getBlock() instanceof GirderBlock)
return state.setValue(GirderBlock.X, axis == Axis.X)
.setValue(GirderBlock.Z, axis == Axis.Z);
.setValue(GirderBlock.Z, axis == Axis.Z)
.setValue(GirderBlock.AXIS, axis);
if (state.getBlock() instanceof GirderEncasedShaftBlock && axis.isHorizontal())
return state.setValue(GirderEncasedShaftBlock.HORIZONTAL_AXIS, axis == Axis.X ? Axis.Z : Axis.X);
return state;

View File

@ -19,12 +19,12 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Direction.Axis;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
@ -58,25 +58,42 @@ public class GirderWrenchBehavior {
return;
Vec3 center = VecHelper.getCenterOf(pos);
Vec3 edge = center.add(Vec3.atLowerCornerOf(dirPair.getFirst().getNormal()).scale(0.4));
Direction.Axis[] axes = Arrays.stream(Iterate.axes).filter(axis -> axis != dirPair.getFirst().getAxis()).toArray(Direction.Axis[]::new);
Vec3 edge = center.add(Vec3.atLowerCornerOf(dirPair.getFirst()
.getNormal())
.scale(0.4));
Direction.Axis[] axes = Arrays.stream(Iterate.axes)
.filter(axis -> axis != dirPair.getFirst()
.getAxis())
.toArray(Direction.Axis[]::new);
double normalMultiplier = dirPair.getSecond() == Action.PAIR ? 3 : 1;
double normalMultiplier = dirPair.getSecond() == Action.PAIR ? 4 : 1;
Vec3 corner1 = edge
.add(Vec3.atLowerCornerOf(Direction.fromAxisAndDirection(axes[0], Direction.AxisDirection.POSITIVE).getNormal()).scale(0.3))
.add(Vec3.atLowerCornerOf(Direction.fromAxisAndDirection(axes[1], Direction.AxisDirection.POSITIVE).getNormal()).scale(0.3))
.add(Vec3.atLowerCornerOf(dirPair.getFirst().getNormal()).scale(0.1 * normalMultiplier));
.add(Vec3.atLowerCornerOf(Direction.fromAxisAndDirection(axes[0], Direction.AxisDirection.POSITIVE)
.getNormal())
.scale(0.3))
.add(Vec3.atLowerCornerOf(Direction.fromAxisAndDirection(axes[1], Direction.AxisDirection.POSITIVE)
.getNormal())
.scale(0.3))
.add(Vec3.atLowerCornerOf(dirPair.getFirst()
.getNormal())
.scale(0.1 * normalMultiplier));
normalMultiplier = dirPair.getSecond() == Action.HORIZONTAL ? 9 : 1;
normalMultiplier = dirPair.getSecond() == Action.HORIZONTAL ? 9 : 2;
Vec3 corner2 = edge
.add(Vec3.atLowerCornerOf(Direction.fromAxisAndDirection(axes[0], Direction.AxisDirection.NEGATIVE).getNormal()).scale(0.3))
.add(Vec3.atLowerCornerOf(Direction.fromAxisAndDirection(axes[1], Direction.AxisDirection.NEGATIVE).getNormal()).scale(0.3))
.add(Vec3.atLowerCornerOf(dirPair.getFirst().getOpposite().getNormal()).scale(0.1 * normalMultiplier));
.add(Vec3.atLowerCornerOf(Direction.fromAxisAndDirection(axes[0], Direction.AxisDirection.NEGATIVE)
.getNormal())
.scale(0.3))
.add(Vec3.atLowerCornerOf(Direction.fromAxisAndDirection(axes[1], Direction.AxisDirection.NEGATIVE)
.getNormal())
.scale(0.3))
.add(Vec3.atLowerCornerOf(dirPair.getFirst()
.getOpposite()
.getNormal())
.scale(0.1 * normalMultiplier));
CreateClient.OUTLINER.showAABB("girderWrench", new AABB(corner1, corner2))
.lineWidth(1 / 32f)
.colored(new Color(127, 127, 127));
.lineWidth(1 / 32f)
.colored(new Color(127, 127, 127));
}
@Nullable
@ -86,16 +103,19 @@ public class GirderWrenchBehavior {
if (validDirections.isEmpty())
return null;
List<Direction> directions = IPlacementHelper.orderedByDistance(pos, result.getLocation(), validDirections.stream().map(Pair::getFirst).toList());
List<Direction> directions = IPlacementHelper.orderedByDistance(pos, result.getLocation(),
validDirections.stream()
.map(Pair::getFirst)
.toList());
if (directions.isEmpty())
return null;
Direction dir = directions.get(0);
return validDirections.stream()
.filter(pair -> pair.getFirst() == dir)
.findFirst()
.orElseGet(() -> Pair.of(dir, Action.SINGLE));
.filter(pair -> pair.getFirst() == dir)
.findFirst()
.orElseGet(() -> Pair.of(dir, Action.SINGLE));
}
public static List<Pair<Direction, Action>> getValidDirections(BlockGetter level, BlockPos pos) {
@ -105,44 +125,47 @@ public class GirderWrenchBehavior {
return Collections.emptyList();
return Arrays.stream(Iterate.directions)
.<Pair<Direction, Action>>mapMulti((direction, consumer) -> {
BlockState other = level.getBlockState(pos.relative(direction));
.<Pair<Direction, Action>>mapMulti((direction, consumer) -> {
BlockState other = level.getBlockState(pos.relative(direction));
// up and down
if (direction.getAxis() == Direction.Axis.Y) {
//no other girder in target dir
if (!AllBlocks.METAL_GIRDER.has(other)) {
if (!blockState.getValue(GirderBlock.X) ^ !blockState.getValue(GirderBlock.Z))
consumer.accept(Pair.of(direction, Action.SINGLE));
return;
}
//this girder is a pole or cross
if (blockState.getValue(GirderBlock.X) == blockState.getValue(GirderBlock.Z))
return;
//other girder is a pole or cross
if (other.getValue(GirderBlock.X) == other.getValue(GirderBlock.Z))
return;
//toggle up/down connection for both
consumer.accept(Pair.of(direction, Action.PAIR));
if (!blockState.getValue(GirderBlock.X) && !blockState.getValue(GirderBlock.Z))
return;
// up and down
if (direction.getAxis() == Direction.Axis.Y) {
// no other girder in target dir
if (!AllBlocks.METAL_GIRDER.has(other)) {
if (!blockState.getValue(GirderBlock.X) ^ !blockState.getValue(GirderBlock.Z))
consumer.accept(Pair.of(direction, Action.SINGLE));
return;
}
// this girder is a pole or cross
if (blockState.getValue(GirderBlock.X) == blockState.getValue(GirderBlock.Z))
return;
// other girder is a pole or cross
if (other.getValue(GirderBlock.X) == other.getValue(GirderBlock.Z))
return;
// toggle up/down connection for both
consumer.accept(Pair.of(direction, Action.PAIR));
if (AllBlocks.METAL_GIRDER.has(other))
consumer.accept(Pair.of(direction, Action.HORIZONTAL));
return;
}
}).toList();
// if (AllBlocks.METAL_GIRDER.has(other))
// consumer.accept(Pair.of(direction, Action.HORIZONTAL));
})
.toList();
}
public static boolean handleClick(Level level, BlockPos pos, BlockState state, BlockHitResult result) {
Pair<Direction, Action> dirPair = getDirectionAndAction(result, level, pos);
if (dirPair == null)
return false;
if (level.isClientSide)
return true;
if (!state.getValue(GirderBlock.X) && !state.getValue(GirderBlock.Z))
return false;
Direction dir = dirPair.getFirst();
@ -150,35 +173,39 @@ public class GirderWrenchBehavior {
BlockState other = level.getBlockState(otherPos);
if (dir == Direction.UP) {
level.setBlock(pos, state.cycle(GirderBlock.TOP), 2 | 16);
if (dirPair.getSecond() == Action.PAIR && AllBlocks.METAL_GIRDER.has(other)) {
level.setBlock(otherPos, other.cycle(GirderBlock.BOTTOM), 2 | 16);
}
level.setBlock(pos, postProcess(state.cycle(GirderBlock.TOP)), 2 | 16);
if (dirPair.getSecond() == Action.PAIR && AllBlocks.METAL_GIRDER.has(other))
level.setBlock(otherPos, postProcess(other.cycle(GirderBlock.BOTTOM)), 2 | 16);
return true;
}
if (dir == Direction.DOWN) {
level.setBlock(pos, state.cycle(GirderBlock.BOTTOM), 2 | 16);
if (dirPair.getSecond() == Action.PAIR && AllBlocks.METAL_GIRDER.has(other)) {
level.setBlock(otherPos, other.cycle(GirderBlock.TOP), 2 | 16);
}
level.setBlock(pos, postProcess(state.cycle(GirderBlock.BOTTOM)), 2 | 16);
if (dirPair.getSecond() == Action.PAIR && AllBlocks.METAL_GIRDER.has(other))
level.setBlock(otherPos, postProcess(other.cycle(GirderBlock.TOP)), 2 | 16);
return true;
}
if (dirPair.getSecond() == Action.HORIZONTAL) {
BooleanProperty property = dir.getAxis() == Direction.Axis.X ? GirderBlock.X : GirderBlock.Z;
level.setBlock(pos, state.cycle(property), 2 | 16);
return true;
}
// if (dirPair.getSecond() == Action.HORIZONTAL) {
// BooleanProperty property = dir.getAxis() == Direction.Axis.X ? GirderBlock.X : GirderBlock.Z;
// level.setBlock(pos, state.cycle(property), 2 | 16);
//
// return true;
// }
return true;
}
private static BlockState postProcess(BlockState newState) {
if (newState.getValue(GirderBlock.TOP) && newState.getValue(GirderBlock.BOTTOM))
return newState;
if (newState.getValue(GirderBlock.AXIS) != Axis.Y)
return newState;
return newState.setValue(GirderBlock.AXIS, newState.getValue(GirderBlock.X) ? Axis.X : Axis.Z);
}
private enum Action {
SINGLE,
PAIR,
HORIZONTAL
SINGLE, PAIR, HORIZONTAL
}
}