Jump to content

How to Schedule a Task in Forge


Jimmeh

Recommended Posts

Hi there, I'm getting started in Forge after years in Spigot. In Spigot, there's a few simple methods like:

plugin.getServer().getScheduler().runTaskLater(plugin, action, ticksDelay);

plugin.getServer().getScheduler().runTaskTimer(plugin, action, ticksDelay, ticksRepeat);

Each of these also have an asynchronous version as well. What would the Forge equivalent of these be?

 

Thank you!

Link to comment
Share on other sites

There isn't one, just create a tick event handler and count ticks yourself.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

  • 1 year later...

I just came across the same topic myself and wanted to leave my solution for anyone in need in the future. Apologies if I shouldn't revive.
Note: Using Forge 1.19 as of writing

The quickest "two-liner" seems to be possible with a ScheduledExecutorService:

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.schedule(() -> doSomething(), time, TimeUnit.MILLISECONDS);

Disclaimer: While I've confirmed that this works, I don't know if there exist strong conventions against using custom thread code, although this is a simple use.

My specific use case was to throw a burst of projectiles in sequence after right-clicking only once. If that's relevant, here's a more comprehensive example:

// Implementing the Item.use method to throw multiple projectiles over time
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand)
{
    ItemStack itemStack = player.getItemInHand(hand);
    int itemsToThrow = Math.min(itemStack.getCount(), BURST_COUNT);

    if (!world.isClientSide)
    {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
        for(int i = 0; i < itemsToThrow; ++i)
        {
            executor.schedule(() -> throwItem(world, player, itemStack, randomsource), 200 * i, TimeUnit.MILLISECONDS);
        }
    }

    player.awardStat(Stats.ITEM_USED.get(this), itemsToThrow);
    if (!player.getAbilities().instabuild)
    {
        itemStack.shrink(itemsToThrow);
    }
    return InteractionResultHolder.sidedSuccess(itemStack, world.isClientSide());
}

 

Edited by ChexxorDev
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.



×
×
  • Create New...

Important Information

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