Jump to content

Recommended Posts

Posted

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?

 

Posted

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?

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.

Posted

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.

 

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.

Posted

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.

Posted

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.

Posted

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.

Posted

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.

}

Posted

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

  • Like 1

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

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