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

Hey all,

 

I'm trying to make the code in onBlockActivated to only run when a cooldown reaches 0. I've seen there is randomTick, updateTick, etc. However, I cannot get them to work.

 

public class BlockMod extends ModBlock implements ITileEntityProvider{
    public BlockMod(String name, Material materialIn) {
        super(name, materialIn);
        this.setTickRandomly(false);
    }

    @Nullable
    @Override
    public TileEntity createNewTileEntity(World worldIn, int meta) {
        return new TileEntityBlockMod();
    }

    @Override
    public int tickRate(World worldIn)
    {
        return 1;
    }

    @Override
    public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random random)
    {
        Utils.getLogger().info("tick");
        super.randomTick(worldIn, pos, state, random);
    }
}

 

Edited by MSpace-Dev
Solved

  • Author

Solved it. Had to scheduleUpdate(). Here is the revised code:

 

public class BlockExample extends BlockMod implements ITileEntityProvider{
    // For testing purposes
    private int i;

    public BlockExample(String name, Material materialIn) {
        super(name, materialIn);
    }

    @Nullable
    @Override
    public TileEntity createNewTileEntity(World worldIn, int meta) {
        return new TileEntityBlockExample();
    }

    @Override
    public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
    {
        this.updateTick(worldIn, pos, state, worldIn.rand);
        super.onBlockAdded(worldIn, pos, state);
    }

    @Override
    public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
    {
        Utils.getLogger().info(++i);
        worldIn.scheduleUpdate(pos, this, 1); // <<-- This is important!
        super.updateTick(worldIn, pos, state, rand);
    }
}

 

  • Author

I already stated that int i was for testing purposes in the code. I am not storing the cooldown in the block class. I understand they are singleton-like. The int was just to confirm it was working.

 

As for the tile entity creation, I will apply that.

  • Author

Yeah, I will use the tile entity for that too, thanks.
 

Btw, trying the new TileEntity init method. This isn't working. How would I do this exactly?

 

    @Nullable
    @Override
    public TileEntity createTileEntity(World world, IBlockState state)
    {
        return new TileEntityBlock();
    }

    @Override
    public boolean hasTileEntity()
    {
        return true;
    }

 

Also tried to call createTileEnity() in the onBlockAdded() method.

Edited by MSpace-Dev
Other methods

  • Author

Ah, that worked. Thanks for the improvements. TileEntity init works and cooldown is now running using getTotalWorldTime() on the TileEntity.

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.