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

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!

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.

  • Author
3 hours ago, Draco18s said:

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

Ah okay. Thank you!

  • 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

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.