Jump to content

Recommended Posts

Posted

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_);
    }
}

 

 

Posted

The function works fine, you are just using it wrong.

Look at RedstoneWireBlock.neighborChanged/updatePowerStrength()

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.