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

  • Author
  On 8/26/2016 at 6:56 PM, Draco18s said:

  Quote
Having the above in a while loop*

 

*Defeats the whole purpose.

 

DO NOT USE A LOOP ANYWHERE IN YOUR CODE.  The tick event handler IS the loop.

 

Ok..

 

So basically what I had was WHILE harvest was enabled (enabled by the key press), do some stuff including press down a key, after the key is pressed down I need to count ticks and when x ticks have gone by, release the key.

 

How would I go about doing this if I cant use a while loop?

 

  On 8/26/2016 at 7:04 PM, OnePoundPP said:

  Quote

  Quote
Having the above in a while loop*

 

*Defeats the whole purpose.

 

DO NOT USE A LOOP ANYWHERE IN YOUR CODE.  The tick event handler IS the loop.

 

Ok..

 

So basically what I had was WHILE harvest was enabled (enabled by the key press), do some stuff including press down a key, after the key is pressed down I need to count ticks and when x ticks have gone by, release the key.

 

How would I go about doing this if I cant use a while loop?

  Quote

  Quote
Having the above in a while loop*

 

*Defeats the whole purpose.

 

DO NOT USE A LOOP ANYWHERE IN YOUR CODE.  The tick event handler IS the loop.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

if(harvest_enabled) { do_stuff(); }

not

while(harvest_enabled) { do_stuff(); }

 

Yes, I realized that you want to "do stuff while harvest is enabled" but "while" has a different meaning in code.  It means "do this thing over and over again until the condition is false, even if that means hogging the CPU and doing nothing else until the universe experiences heat death."

 

What you need to do is press the key and stop doing anything else the rest of this tick.  Then you count ticks.  Then when the counter is big enough, release the key.

 

  On 8/26/2016 at 6:56 PM, Draco18s said:

The tick event handler IS the while loop.

 

If it helps, write out what you want to do as a Finite State Machine.

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.

Guess how Minecraft ticks? It's already in a loop, when the server gets launched, it gets into a snippet

while (this.serverRunning) { // this is the server
      // This is where Minecraft server code happens, including running tick handlers' code every relevant tick depending on what they subscribed for, then it sleeps for at most 50 milliseconds before the loop goes on
}

 

This means that wherever you are in Minecraft, you are on a loop (one of 2 actually, one for server and one for client, but that's not the point). If a lot happens on the same tick, the game lags because the game loop doesn't go on.

Forge added tick handling events so mods can run their code on each relevant tick, if modders want to tick without pausing the game they start counting their ticks (on a global variable you still didn't make) and each time the tick counter reaches certain amount the mod runs its code.

Eeehh, close enough.

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.

You also don't have pressTime ++ anywhere.

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

SOLVED: Thanks to everyone who helped, especially Draco18s!

 

For anyone who has the same issue in the future:

 

To simulate a delay between two lines of code you cannot use Thread.Sleep() or anything as such because it causes the entire thread, including minecraft to freeze. Instead you must force a line of code to be executed on one tick and wait for another tick to come before executing the next.

 

Example:

 

int pressTime;

public void onPlayerTick(ClientTickEvent event){

    if(pressTime == 20){  *20 TICKS IS 1 SECOND*

        //do one line of code

    }else if(pressTime == 40){  *SIMULATES A 1 SECOND DELAY*

        //do another line of code

    }

    pressTime++;  *increments presstime by 1 every tick.

}

And remember to both initialize and reset pressTime to zero somewhere so you can do your magic first time and every time  :)

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.