Jump to content

(SOLVED) [1.11.2] Create cooldown for onBlockActivated


MSpace-Dev

Recommended Posts

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
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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