I made a block like wire, when its neighbor block changed, its properties should be changed. But the function "blockstate.setValue()" doesn't work.
Block definition code:
package indi.mrzhang21626.moderntech.blocks;
import com.google.common.collect.Maps;
import indi.mrzhang21626.moderntech.blockentities.BaseWireBlockEntity;
import indi.mrzhang21626.moderntech.registries.BlockRegister;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.energy.CapabilityEnergy;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.Map;
public class BaseWireBlock extends BaseEntityBlock {
public static final Map<Direction, BooleanProperty> PROPERTY_MAP;
static {
Map<Direction, BooleanProperty> map = Maps.newEnumMap(Direction.class);
map.put(Direction.NORTH, BlockStateProperties.NORTH);
map.put(Direction.EAST, BlockStateProperties.EAST);
map.put(Direction.SOUTH, BlockStateProperties.SOUTH);
map.put(Direction.WEST, BlockStateProperties.WEST);
map.put(Direction.UP, BlockStateProperties.UP);
map.put(Direction.DOWN, BlockStateProperties.DOWN);
PROPERTY_MAP = Collections.unmodifiableMap(map);
}
public BaseWireBlock(Properties properties) {
super(properties);
}
public BaseWireBlock(Material material, float hardness) {
super(Properties.of(material).strength(hardness));
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(PROPERTY_MAP.values().toArray(new Property<?>[0]));
super.createBlockStateDefinition(builder);
}
@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
BlockState state = this.defaultBlockState();
for (var facing : Direction.values()) {
var level = context.getLevel();
var facingPos = context.getClickedPos().offset(facing.getNormal());
var facingState = level.getBlockState(facingPos);
state = state.setValue(PROPERTY_MAP.get(facing), this.canConnect(level, facing.getOpposite(), facingPos, facingState));
}
return state;
}
@Override
public void neighborChanged(BlockState state, Level level, BlockPos pos, Block neighborBlock, BlockPos neighborPos, boolean isMoving) {
Direction side = Direction.getNearest(neighborPos.getX() - pos.getX(), neighborPos.getY() - pos.getY(), neighborPos.getZ() - pos.getZ());
var neighborState = level.getBlockState(neighborPos);
state.setValue(PROPERTY_MAP.get(side), this.canConnect(level, side.getOpposite(), neighborPos, neighborState));
}
@Override
public void onNeighborChange(BlockState state, LevelReader level, BlockPos pos, BlockPos neighborPos) {
Direction side = Direction.getNearest(neighborPos.getX() - pos.getX(), neighborPos.getY() - pos.getY(), neighborPos.getZ() - pos.getZ());
var neighborState = level.getBlockState(neighborPos);
state.setValue(PROPERTY_MAP.get(side), this.canConnect(level, side.getOpposite(), neighborPos, neighborState));
}
private boolean canConnect(Level level, Direction facing, BlockPos pos, BlockState state) {
var blockEntity = level.getBlockEntity(pos);
if (state.getBlock().equals(BlockRegister.TEST_WIRE_BLOCK.get())) return true;
return blockEntity != null && blockEntity.getCapability(CapabilityEnergy.ENERGY, facing).isPresent();
}
private boolean canConnect(LevelReader level, Direction facing, BlockPos pos, BlockState state) {
var blockEntity = level.getBlockEntity(pos);
if (state.getBlock().equals(BlockRegister.TEST_WIRE_BLOCK.get())) return true;
return blockEntity != null && blockEntity.getCapability(CapabilityEnergy.ENERGY, facing).isPresent();
}
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos p_153215_, BlockState p_153216_) {
return new BaseWireBlockEntity(p_153215_, p_153216_);
}
}