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

92 lines
3.4 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;
import net.minecraft.block.Block;
2021-03-01 20:14:50 +01:00
import net.minecraft.block.BlockState;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.Direction;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
2021-03-01 20:14:50 +01:00
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import java.util.ArrayList;
import java.util.List;
2021-03-01 20:14:50 +01:00
public class BlockLampPowerer extends FullyDirectionalBlock {
public BlockLampPowerer() {
2021-03-01 20:14:50 +01:00
super(ActuallyBlocks.defaultPickProps(0));
}
@Override
2021-03-01 20:14:50 +01:00
public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) {
2016-05-19 20:05:12 +02:00
this.updateLamp(worldIn, pos);
}
@Override
public void onPlace(BlockState state, World world, BlockPos pos, BlockState oldState, boolean isMoving) {
2016-01-08 13:31:58 +01:00
this.updateLamp(world, pos);
}
2019-05-02 09:10:29 +02:00
private void updateLamp(World 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<>());
}
}
2019-05-02 09:10:29 +02:00
private void updateLampsAtPos(World 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);
}
}
2019-05-02 09:10:29 +02:00
private void updateSurrounding(World 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
2021-03-01 20:14:50 +01:00
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
switch (state.getValue(FACING)) {
2021-03-01 20:14:50 +01:00
case EAST:
return Shapes.LampPowererShapes.SHAPE_E;
case SOUTH:
return Shapes.LampPowererShapes.SHAPE_S;
case WEST:
return Shapes.LampPowererShapes.SHAPE_W;
default:
return Shapes.LampPowererShapes.SHAPE_N;
}
2016-02-01 17:49:55 +01:00
}
}