ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLampController.java

92 lines
3.5 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("BlockLampPowerer.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.blocks;
2021-03-01 20:14:50 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.base.FullyDirectionalBlock;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockGetter;
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.properties.BlockStateProperties;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import java.util.ArrayList;
import java.util.List;
2021-11-24 01:17:37 +01:00
public class BlockLampController extends FullyDirectionalBlock {
2021-11-24 01:17:37 +01:00
public BlockLampController() {
2024-03-02 21:23:08 +01:00
super(ActuallyBlocks.defaultPickProps());
}
@Override
2024-03-02 21:23:08 +01:00
public void neighborChanged(BlockState state, Level worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) {
2016-05-19 20:05:12 +02:00
this.updateLamp(worldIn, pos);
}
@Override
2024-03-02 21:23:08 +01:00
public void onPlace(BlockState state, Level world, BlockPos pos, BlockState oldState, boolean isMoving) {
2016-01-08 13:31:58 +01:00
this.updateLamp(world, pos);
}
2024-03-02 21:23:08 +01:00
private void updateLamp(Level world, BlockPos pos) {
if (!world.isClientSide) {
2021-02-26 22:15:48 +01:00
BlockState state = world.getBlockState(pos);
BlockPos coords = pos.relative(WorldUtil.getDirectionByPistonRotation(state));
this.updateLampsAtPos(world, coords, world.getBestNeighborSignal(pos) > 0, new ArrayList<>());
}
}
2024-03-02 21:23:08 +01:00
private void updateLampsAtPos(Level world, BlockPos pos, boolean powered, List<BlockPos> updatedAlready) {
2021-02-26 22:15:48 +01:00
BlockState state = world.getBlockState(pos);
Block block = state.getBlock();
2019-05-02 09:10:29 +02:00
if (block instanceof BlockColoredLamp) {
if (state.getValue(BlockStateProperties.LIT) && !powered) {
world.setBlockAndUpdate(pos, state.setValue(BlockStateProperties.LIT, false));
2021-03-01 20:14:50 +01:00
}
if (!state.getValue(BlockStateProperties.LIT) && powered) {
world.setBlockAndUpdate(pos, state.setValue(BlockStateProperties.LIT, true));
}
this.updateSurrounding(world, pos, powered, updatedAlready);
}
}
2024-03-02 21:23:08 +01:00
private void updateSurrounding(Level world, BlockPos pos, boolean powered, List<BlockPos> updatedAlready) {
for (Direction side : Direction.values()) {
BlockPos offset = pos.relative(side);
2019-05-02 09:10:29 +02:00
if (!updatedAlready.contains(offset)) {
updatedAlready.add(pos);
this.updateLampsAtPos(world, offset, powered, updatedAlready);
}
}
}
2015-12-19 10:30:39 +01:00
@Override
2024-03-02 21:23:08 +01:00
public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
switch (state.getValue(FACING)) {
2021-03-01 20:14:50 +01:00
case EAST:
2024-03-02 21:23:08 +01:00
return VoxelShapes.LampPowererShapes.SHAPE_E;
2021-03-01 20:14:50 +01:00
case SOUTH:
2024-03-02 21:23:08 +01:00
return VoxelShapes.LampPowererShapes.SHAPE_S;
2021-03-01 20:14:50 +01:00
case WEST:
2024-03-02 21:23:08 +01:00
return VoxelShapes.LampPowererShapes.SHAPE_W;
2021-03-01 20:14:50 +01:00
default:
2024-03-02 21:23:08 +01:00
return VoxelShapes.LampPowererShapes.SHAPE_N;
2021-03-01 20:14:50 +01:00
}
2016-02-01 17:49:55 +01:00
}
}