Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.18.2 dev #18

Merged
merged 15 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Create Railways Navigator (Minecraft Create Mod Addon)
![Logo](https://github.com/MisterJulsen/Create-Train-Navigator/blob/1.18.2-Multiloader/icon_256px.png)
![Logo](https://github.com/MisterJulsen/Create-Train-Navigator/blob/1.18.2/icon_256px.png)

Get a list possible train connections in your world from one station to another using the Create Railways Navigator.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,30 @@ public BlockState getStateForPlacement(BlockPlaceContext context) {
BlockPos clickedPos = context.getClickedPos();
BlockPos placedOnPos = clickedPos.relative(face.getOpposite());
Level level = context.getLevel();
BlockState blockState = level.getBlockState(placedOnPos);
BlockState otherState = level.getBlockState(placedOnPos);
BlockState stateForPlacement = this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());

if ((blockState.getBlock() != this) || (context.getPlayer() != null && context.getPlayer().isShiftKeyDown())) {
stateForPlacement = super.getStateForPlacement(context).setValue(FACING, context.getHorizontalDirection().getOpposite());
if ((otherState.getBlock() != this) || (context.getPlayer() != null && context.getPlayer().isShiftKeyDown())) {
stateForPlacement = getDefaultPlacementState(context, stateForPlacement, otherState);
} else { // Clicked on existing block
Direction otherFacing = blockState.getValue(FACING);
stateForPlacement = stateForPlacement.setValue(FACING, otherFacing);
stateForPlacement = appendOnPlace(context, stateForPlacement, otherState);
}

return updateColumn(level, clickedPos, stateForPlacement, true);
}

public BlockState appendOnPlace(BlockPlaceContext context, BlockState state, BlockState other) {
Direction otherFacing = other.getValue(FACING);
state = state
.setValue(FACING, otherFacing)
;
return state;
}

public BlockState getDefaultPlacementState(BlockPlaceContext context, BlockState state, BlockState other) {
return super.getStateForPlacement(context).setValue(FACING, context.getHorizontalDirection().getOpposite());
}

protected BlockState updateColumn(Level level, BlockPos pos, BlockState state, boolean present) {
MutableBlockPos currentPos = new MutableBlockPos();
Axis axis = getConnectionAxis(state);
Expand Down Expand Up @@ -152,8 +163,8 @@ public void onPlace(BlockState pState, Level pLevel, BlockPos pPos, BlockState p

public <T extends Comparable<T>> BlockState getPropertyFromNeighbours(BlockState pState, Level pLevel, BlockPos pPos, Property<T> property) {
Direction leftDirection = pState.getValue(HorizontalDirectionalBlock.FACING).getClockWise();
BlockPos relPos = pPos.relative(leftDirection);
BlockState newState = null;
BlockPos relPos = pPos.relative(leftDirection);
if ((newState = getPropertyFromNeighbour(pState, pLevel, pPos, relPos, property)) != null) {
return newState;
}
Expand All @@ -173,7 +184,7 @@ public <T extends Comparable<T>> BlockState getPropertyFromNeighbours(BlockState
}

public <T extends Comparable<T>> BlockState getPropertyFromNeighbour(BlockState pState, Level pLevel, BlockPos pPos, BlockPos relPos, Property<T> property) {
if (pState.getBlock() == pLevel.getBlockState(relPos).getBlock()) {
if (canConnectWithBlock(pLevel, pState, pLevel.getBlockState(relPos))) {
return pState.setValue(property, pLevel.getBlockState(relPos).getValue(property));
}
return null;
Expand Down Expand Up @@ -247,10 +258,10 @@ protected boolean canConnect(LevelAccessor level, BlockPos pos, BlockState state
return other.getBlock() == this && state.getValue(FACING) == other.getValue(FACING);
}

public boolean canConnectWithBlock(BlockGetter level, BlockPos selfPos, BlockPos otherPos) {
return level.getBlockState(selfPos).getBlock() instanceof AbstractAdvancedDisplayBlock && level.getBlockState(otherPos).getBlock() instanceof AbstractAdvancedDisplayBlock &&
level.getBlockState(selfPos).getBlock() == level.getBlockState(otherPos).getBlock() &&
level.getBlockState(selfPos).getValue(FACING) == level.getBlockState(otherPos).getValue(FACING)
public boolean canConnectWithBlock(BlockGetter level, BlockState selfState, BlockState otherState) {
return selfState.getBlock() instanceof AbstractAdvancedDisplayBlock && otherState.getBlock() instanceof AbstractAdvancedDisplayBlock &&
selfState.getBlock() == otherState.getBlock() &&
selfState.getValue(FACING) == otherState.getValue(FACING)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,23 @@ public BlockState getStateForPlacement(BlockPlaceContext context) {
BlockPos clickedPos = context.getClickedPos();
BlockPos placedOnPos = clickedPos.relative(face.getOpposite());
Level level = context.getLevel();
BlockState blockState = level.getBlockState(placedOnPos);
BlockState otherState = level.getBlockState(placedOnPos);
BlockState stateForPlacement = this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());

if ((blockState.getBlock() != this) || (context.getPlayer() != null && context.getPlayer().isShiftKeyDown())) {
stateForPlacement = super.getStateForPlacement(context).setValue(FACING, context.getHorizontalDirection().getOpposite());
if ((otherState.getBlock() != this) || (context.getPlayer() != null && context.getPlayer().isShiftKeyDown())) {
stateForPlacement = getDefaultPlacementState(context, stateForPlacement, otherState);
stateForPlacement = getPropertyFromNeighbours(stateForPlacement, level, clickedPos, SIDE);
} else { // Clicked on existing block
Direction otherFacing = blockState.getValue(FACING);
stateForPlacement = stateForPlacement.setValue(FACING, otherFacing).setValue(SIDE, blockState.getValue(SIDE));
stateForPlacement = appendOnPlace(context, stateForPlacement, otherState);
}

return updateColumn(level, clickedPos, stateForPlacement, true);
}

public BlockState appendOnPlace(BlockPlaceContext context, BlockState state, BlockState other) {
state = super.appendOnPlace(context, state, other)
.setValue(SIDE, other.getValue(SIDE))
;
return state;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.world.item.context.BlockPlaceContext;
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.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition.Builder;
Expand Down Expand Up @@ -75,45 +76,59 @@ public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos,
}

@Override
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
BlockState stateForPlacement = super.getStateForPlacement(pContext);
Direction direction = pContext.getClickedFace();
Direction looking = pContext.getHorizontalDirection();
public BlockState getDefaultPlacementState(BlockPlaceContext context, BlockState state, BlockState other) {
BlockState stateForPlacement = super.getDefaultPlacementState(context, state, other);
Direction direction = context.getClickedFace();
Direction looking = context.getHorizontalDirection();
Axis axis = looking.getAxis();
AxisDirection axisDirection = looking.getAxisDirection();

double xzPos = 0.5f;
if (axis == Axis.X) {
xzPos = pContext.getClickLocation().x - pContext.getClickedPos().getX();
xzPos = context.getClickLocation().x - context.getClickedPos().getX();
} else if (axis == Axis.Z) {
xzPos = pContext.getClickLocation().z - pContext.getClickedPos().getZ();
xzPos = context.getClickLocation().z - context.getClickedPos().getZ();
}

EBlockAlignment zAlign = EBlockAlignment.CENTER;

if (direction == pContext.getPlayer().getDirection().getOpposite() || (axisDirection == AxisDirection.POSITIVE ? xzPos > 0.66666666D : xzPos < 0.33333333D)) {
if (direction == context.getPlayer().getDirection().getOpposite() || (axisDirection == AxisDirection.POSITIVE ? xzPos > 0.66666666D : xzPos < 0.33333333D)) {
zAlign = EBlockAlignment.POSITIVE;
} else if (direction == pContext.getPlayer().getDirection() || (axisDirection == AxisDirection.POSITIVE ? xzPos < 0.33333333D : xzPos > 0.66666666D)) {
} else if (direction == context.getPlayer().getDirection() || (axisDirection == AxisDirection.POSITIVE ? xzPos < 0.33333333D : xzPos > 0.66666666D)) {
zAlign = EBlockAlignment.NEGATIVE;
}

return stateForPlacement
.setValue(Z_ALIGN, zAlign)
;
}
}

@Override
public BlockState appendOnPlace(BlockPlaceContext context, BlockState state, BlockState other) {
return super.appendOnPlace(context, state, other)
.setValue(Z_ALIGN, other.getValue(Z_ALIGN))
;
}

@Override
protected void createBlockStateDefinition(Builder<Block, BlockState> pBuilder) {
super.createBlockStateDefinition(pBuilder.add(Z_ALIGN));
}

@Override
public boolean canConnectWithBlock(BlockGetter level, BlockPos selfPos, BlockPos otherPos) {
return super.canConnectWithBlock(level, selfPos, otherPos) &&
level.getBlockState(selfPos).getValue(Z_ALIGN) == level.getBlockState(otherPos).getValue(Z_ALIGN)
public boolean canConnectWithBlock(BlockGetter level, BlockState selfState, BlockState otherState) {
return super.canConnectWithBlock(level, selfState, otherState) &&
selfState.getValue(Z_ALIGN) == otherState.getValue(Z_ALIGN)
;
}

@Override
protected boolean canConnect(LevelAccessor level, BlockPos pos, BlockState state, BlockState other) {
return super.canConnect(level, pos, state, other) &&
state.getValue(Z_ALIGN) == other.getValue(Z_ALIGN)
;
}

@Override
public Pair<Float, Float> getRenderAspectRatio(Level level, BlockState blockState, BlockPos pos) {
return Pair.of(1.0F, 1.0F);
Expand Down
Loading