TheBrokenRail
b347a67a10
All checks were successful
EnergonRelics/pipeline/head This commit looks good
57 lines
2.3 KiB
Java
57 lines
2.3 KiB
Java
package com.thebrokenrail.energonrelics.block.entity.forcefield;
|
|
|
|
import com.thebrokenrail.energonrelics.api.block.entity.core.EnergyReceiverBlockEntity;
|
|
import com.thebrokenrail.energonrelics.api.energy.Action;
|
|
import com.thebrokenrail.energonrelics.block.forcefield.util.AbstractFieldBlock;
|
|
import com.thebrokenrail.energonrelics.block.forcefield.util.FieldProjectorBlock;
|
|
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
|
|
import net.minecraft.block.BlockState;
|
|
import net.minecraft.block.entity.BlockEntityType;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.util.math.Direction;
|
|
|
|
import java.util.function.Function;
|
|
|
|
public class FieldProjectorBlockEntity extends EnergyReceiverBlockEntity {
|
|
private final Function<BlockState, AbstractFieldBlock> block;
|
|
|
|
public FieldProjectorBlockEntity(BlockEntityType<?> type, Function<BlockState, AbstractFieldBlock> block) {
|
|
super(type);
|
|
this.block = block;
|
|
}
|
|
|
|
protected void setLastBlock(BlockPos pos, BlockState lastState) {
|
|
}
|
|
|
|
protected long getCost() {
|
|
return HardcodedConfig.FIELD_PROJECTOR_ENERGY_REQUIRED;
|
|
}
|
|
|
|
@Override
|
|
protected void energyTick() {
|
|
assert getWorld() != null;
|
|
|
|
addAction(Action.createBlockStatePropertyAction(getCost(), FieldProjectorBlock.POWERED, true, false));
|
|
if (getCachedState().get(FieldProjectorBlock.POWERED)) {
|
|
Direction facing = getCachedState().get(FieldProjectorBlock.FACING);
|
|
BlockState state = block.apply(getCachedState()).getDefaultState().with(AbstractFieldBlock.FACING, facing);
|
|
|
|
boolean valid = false;
|
|
for (int i = 1; i < HardcodedConfig.FIELD_MAX_SIZE + 1; i++) {
|
|
BlockPos targetPos = getPos().offset(facing, i);
|
|
BlockState targetState = getWorld().getBlockState(targetPos);
|
|
if (targetState.isAir() || (targetState != state && targetState.getBlock() instanceof AbstractFieldBlock && targetState.get(AbstractFieldBlock.FACING) == facing)) {
|
|
getWorld().setBlockState(targetPos, state);
|
|
} else if (targetState != state) {
|
|
setLastBlock(targetPos, targetState);
|
|
valid = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!valid) {
|
|
setLastBlock(null, null);
|
|
}
|
|
}
|
|
}
|
|
}
|