Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[SOLVED] [1.10.2] getWeakPower() and updating block states properly


desht
 Share

Recommended Posts

The scenario:

 

I have a block (with a tile entity) which can output a redstone signal on each or any of its six sides; the redstone power level for each side depends in various ways on the contents of the tile entity's inventory:

 

@Override
public boolean canProvidePower(IBlockState state) {
    return true;
}

@Override
public int getWeakPower(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
    TileEntity te = blockAccess.getTileEntity(pos);
    if (te instanceof MyTileEntity) {
        return ((MyTileEntity) te).getRedstoneLevel(side);
    } else {
        return 0;
    }
}

 

(where getRedstoneLevel(EnumFacing side) is a method I added to return what the tile entity wants to emit on a given side in my MyTileEntity class).

 

Now, if I open the tile entity's GUI and manually insert or remove items, everything updates fine - different sides of the block emit a redstone signal as appropriate.  Some debug statements confirm that my getRedstoneLevel() method is always returning the right level for the given side, so I'm happy the logic is working here.

 

The problem arises if the TE's inventory is modified programatically  (via the CapabilityItemHandler & ItemStackHandler API, nothing unusual there) - in this case, the redstone signals do not update straight away, although I've confirmed again via debug statements that the TE's internal idea of the power levels is correctly updated.

 

Some more debug statements show that getWeakPower() isn't getting called in this case.  If I add (or break) some redstone around the block, that triggers the necessary redstone updates, and suddenly everything's OK again.  So clearly it's a matter of having the tile entity notify its neighbours of a change somehow when its desired redstone output levels change.  I had hoped calling worldObj.notifyNeighborsOfStateChange() would do the trick, but that doesn't seem to have any effect.

 

So the question is: what's getting called to cause redstone levels to update when I modify the TE's inventory via GUI, that isn't getting called when the same inventory is manipulated programatically?

 

Update: think I've figured it out.  worldObj.notifyNeighborsOfStateChange() is the right way to do it, but I was calling it at the wrong point (before my tile entity was providing the appropriate power levels).  A bit of code re-org and everything is now working nicely :)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.