Move can-place modem logic to one place

This should make future changes easier. Closes #1769.
This commit is contained in:
Jonathan Coates 2024-04-01 13:55:44 +01:00
parent b9ba2534a4
commit 0f623c2cca
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
3 changed files with 20 additions and 3 deletions

View File

@ -4,7 +4,10 @@
package dan200.computercraft.shared.peripheral.modem;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
@ -22,4 +25,17 @@ public static VoxelShape getBounds(Direction facing) {
var direction = facing.ordinal();
return direction < BOXES.length ? BOXES[direction] : Shapes.block();
}
/**
* Determine if a block can support a modem.
*
* @param level The current level.
* @param pos The position of the adjacent block.
* @param side The side the modem will be placed against.
* @return Whether this block can support a modem.
*/
public static boolean canSupport(LevelReader level, BlockPos pos, Direction side) {
// TODO(1.20.4): Check the side is a full-block instead.
return Block.canSupportCenter(level, pos, side);
}
}

View File

@ -6,6 +6,7 @@
import dan200.computercraft.annotations.ForgeOverride;
import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.peripheral.modem.ModemShapes;
import dan200.computercraft.shared.platform.PlatformHelper;
import dan200.computercraft.shared.util.WaterloggableHelpers;
import dan200.computercraft.shared.util.WorldUtil;
@ -183,7 +184,7 @@ public BlockState updateShape(BlockState state, Direction side, BlockState other
// Pop our modem if needed.
var dir = state.getValue(MODEM).getFacing();
if (dir != null && dir.equals(side) && !canSupportCenter(level, otherPos, side.getOpposite())) {
if (dir != null && dir.equals(side) && !ModemShapes.canSupport(level, otherPos, side.getOpposite())) {
// If we've no cable, follow normal Minecraft logic and just remove the block.
if (!state.getValue(CABLE)) return getFluidState(state).createLegacyBlock();
@ -212,7 +213,7 @@ public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
var facing = state.getValue(MODEM).getFacing();
if (facing == null) return true;
return canSupportCenter(world, pos.relative(facing), facing.getOpposite());
return ModemShapes.canSupport(world, pos.relative(facing), facing.getOpposite());
}
@Nullable

View File

@ -75,7 +75,7 @@ public BlockState updateShape(BlockState state, Direction side, BlockState other
@Deprecated
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
var facing = state.getValue(FACING);
return canSupportCenter(world, pos.relative(facing), facing.getOpposite());
return ModemShapes.canSupport(world, pos.relative(facing), facing.getOpposite());
}
@Nullable