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.

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

Featured Replies

Posted

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 :)

 

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.