Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (โ‹ฎ) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

ย 

ย 

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions โ†’ Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.