Hi guys, I know this seems like a stupid question (and probably is), but how can I make my script pause in Java? I wasn't able to find a working solution via googling on the internet, oddly. So... Yeah. Help please
One way to do it is to use a TickHandler or any of the update methods (such as entity onUpdate) and a variable to store time, then increment or decrement the time each tick/update until you want to continue.
Okay, so let's say I wanted to wait 60 seconds, for example. What would the code be?
Lol, ok well, it depends what you're waiting for Give us some context.
Generically, it would be just like I said above. There are tutorials on both TickHandlers and EventHandlers, and the onUpdate methods are already there to use as well if that's easier.
Ok... so maybe Java wait() method is what you want... if you want the whole thread to wait... but typically you wouldn't want to do that. You're not waiting for anything, so why wait at all? What exactly is it you are trying to do?
public ItemStack onEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) {
--par1ItemStack.stackSize;
par3EntityPlayer.getFoodStats().addStats(this);
par2World.playSoundAtEntity(par3EntityPlayer, "random.burp", 0.5F, par2World.rand.nextFloat() * 0.1F + 0.9F);
this.onFoodEaten(par1ItemStack, par2World, par3EntityPlayer);
par3EntityPlayer.capabilities.allowFlying = true;
par3EntityPlayer.addChatMessage("You can now soar to the Milky Way!");
// Wait (50 Seconds)
par3EntityPlayer.addChatMessage("You better fly down to the ground, your flight ends in 10 seconds.");
// Wait (10 Seconds)
par3EntityPlayer.addChatMessage("Flight to the Milky Way is over. Sorry :(");
par3EntityPlayer.capabilities.allowFlying = false;
return par1ItemStack;
}}
You'll need to move the chat messages out of that method to either a TickHandler or onLivingUpdate method like I mentioned. Check my tutorial on using EventHandler for setting one up and getting the LivingUpdate event.
Once you've got that, try this code:
// a variable stored in your Event Handler class
private int timer;
// within the LivingUpdateEvent method:
public void onLivingUpdate(LivingUpdateEvent event)
{
// check if your entity is a player
// if so, check if your player can fly and that timer == 0
// if so, set timer to 60 and print chat message #1
// otherwise, decrement timer by 1
// if timer == 10, print chat message #2
// if timer == 0, print chat message #3, set allowFlying to false (and also isFlying to make the player fall)
}
I'd create a Timer to execute a command after x amount of time has passed. To do this, create a class called "MyTimer" and "MyTimerEvent" or something like that and fill "MyTimer" with:
public class MyTimer extends Thread {
private long time;
private MyTimerEvent e;
public MyTimer(long time, MyTimerEvent e) {
this.time = time;
this.e = e;
this.start();
this.run();
}
@Override
public void run() {
try {
Thread.sleep(time);
e.run();
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
and "MyTimerEvent" with:
public abstract class MyTimerEvent {
public abstract void run();
}
Then plug the timer into your code somewhere and make it look similar to this:
new MyTimer(timeInMilliseconds, new MyTimerEvent() {
@Override
public void run() {
//Insert code to run after the time has passed here.
}
});
All of this will allow you to execute any code you want after a certain amount of time, measured in milliseconds, has passed.
Rollback Post to RevisionRollBack
java.lang.NullPointerException - Signature cannot be null!
Interesting solution you've got there, JanitorialMilk! I wonder which is more resource-heavy, though, creating a new thread every time you want a timer or using Minecrafts built-in 'tick'? I dunno. Anyways, good to see an alternative
Using the built in tick method is probably less resource-heavy, but the ticks in the game vary on the system. I think they try to stay at 60 ticks per second though. The method I gave is just a way to make something happen exactly after x amount of time has passed, whereas in Minecraft ticks, it may be a bit before or a bit after the x amount of time you want.
Also, Java has its own Timer class, I just find making your own Timer class is more satisfying
Rollback Post to RevisionRollBack
java.lang.NullPointerException - Signature cannot be null!
Also, Java has its own Timer class, I just find making your own Timer class is more satisfying
I know what you mean - I remember back in the day when I took a couple C++ classes, we had to make our own Stack, Queue, Linked List and Dynamic Linked List classes. That was pretty fun Never did get into multi-threaded processes or any of the more advanced stuff, unfortunately. Lots more to learn!
I know what you mean - I remember back in the day when I took a couple C++ classes, we had to make our own Stack, Queue, Linked List and Dynamic Linked List classes. That was pretty fun Never did get into multi-threaded processes or any of the more advanced stuff, unfortunately. Lots more to learn!
Oh yeah building array lists and hash maps on your own, that was very educational and fun
I'd create a Timer to execute a command after x amount of time has passed. To do this, create a class called "MyTimer" and "MyTimerEvent" or something like that and fill "MyTimer" with:
public class MyTimer extends Thread {
private long time;
private MyTimerEvent e;
public MyTimer(long time, MyTimerEvent e) {
this.time = time;
this.e = e;
this.start();
this.run();
}
@Override
public void run() {
try {
Thread.sleep(time);
e.run();
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
and "MyTimerEvent" with:
public abstract class MyTimerEvent {
public abstract void run();
}
Then plug the timer into your code somewhere and make it look similar to this:
new MyTimer(timeInMilliseconds, new MyTimerEvent() {
@Override
public void run() {
//Insert code to run after the time has passed here.
}
});
All of this will allow you to execute any code you want after a certain amount of time, measured in milliseconds, has passed.
I already have a Thread.wait() and such in my code... I came here because I wanted to replace that with something else. Thread.wait() appears to make all threads wait...
You'll need to move the chat messages out of that method to either a TickHandler or onLivingUpdate method like I mentioned. Check my tutorial on using EventHandler for setting one up and getting the LivingUpdate event.
Once you've got that, try this code:
// a variable stored in your Event Handler class
private int timer;
// within the LivingUpdateEvent method:
public void onLivingUpdate(LivingUpdateEvent event)
{
// check if your entity is a player
// if so, check if your player can fly and that timer == 0
// if so, set timer to 60 and print chat message #1
// otherwise, decrement timer by 1
// if timer == 10, print chat message #2
// if timer == 0, print chat message #3, set allowFlying to false (and also isFlying to make the player fall)
}
The player has to eat the item. It's the code at the very top of what I showed you.... onEaten()
I already have a Thread.wait() and such in my code... I came here because I wanted to replace that with something else. Thread.wait() appears to make all threads wait...
That depends on how you call it I guess.
I do believe the suggested solution above could work although I'm not sure about how threadsafe the rest of it would be
coolAlias's solution will work without a doubt though you will need to adjust it to fit your exact needs
The player has to eat the item. It's the code at the very top of what I showed you.... onEaten()
So set allowFlying to true when the food is eaten, then the living update event should take care of the rest. If for some reason that doesn't work (which it won't in multiplayer anyway), try storing the timer in IExtendedEntityProperties of the player. See the tutorial directly under the EventHandler tutorial I linked earlier.
I already have a Thread.wait() and such in my code... I came here because I wanted to replace that with something else. Thread.wait() appears to make all threads wait...
The code I posted is how you can replace the "Thread.wait();" method. The code I posted does not stop all threads.
Rollback Post to RevisionRollBack
java.lang.NullPointerException - Signature cannot be null!
In Java multithreading you have methods wait(), notify(), and sleep(). The first two correspond with each other. You call wait() on an object which you want a lock on; not a Thread. The thread in which you called wait() on object will "wait" or just sit there until notify() is called on same object. Sleep() will "wait" for a specific amount of time. Take a look at this example.
public class Executor {
static Executor executor = new Executor();
final Object object = new Object();
boolean stop = false;
public void doOperation() {
System.out.println("Called doOperation()");
while (stop) {
// wait until stop becomes true
try {
synchronized (object) {
object.wait();
}
} catch (InterruptedException e) {
// Handle when a thread calls interrupt();
// This can happen when the object doesn't relieve notify()
// and you want to stop this thread lock.
// Most of the time you can do nothing or send a message to logger.
}
}
// Stop is now true
System.out.println("Operation has continued!");
}
public void toggleStop() {
stop = !stop;
}
public void wakeup() {
synchronized (object) {
// call notify on same object. Now the thread will continue
object.notify();
}
}
public static void main(String[] args) throws InterruptedException {
// create new thread to sleep
new Thread() {
@Override
public void run() {
try {
System.out.println("Sleeping for 5 seconds");
Thread.sleep(5000);
System.out.println("Done sleeping");
} catch (InterruptedException e) {
// same as I said above
}
// change boolean to false
executor.toggleStop();
// wakeup executor
executor.wakeup();
}
}.start();
// change boolean to true
executor.toggleStop();
// execute operation
executor.doOperation();
}
}
So don't use wait() because that will stop the current thread as you mentioned. Is this set time you want to wait part of the game? Waiting for a set amount of ticks in a TickHandler can work and it will be concurrent with the rest of the game. When you pause the game and you have a separate Thread sleeping, that thread will keep on going even though the rest of the game is in a paused state. But if you want this to be a separate task, then a TimerTask is a good idea.
Rollback Post to RevisionRollBack
Same ****, different day - Modification Development Section
The Java wait() method is not what you want here.
Lol, ok well, it depends what you're waiting for Give us some context.
Generically, it would be just like I said above. There are tutorials on both TickHandlers and EventHandlers, and the onUpdate methods are already there to use as well if that's easier.
You'll need to move the chat messages out of that method to either a TickHandler or onLivingUpdate method like I mentioned. Check my tutorial on using EventHandler for setting one up and getting the LivingUpdate event.
Once you've got that, try this code:
and "MyTimerEvent" with:
Then plug the timer into your code somewhere and make it look similar to this:
All of this will allow you to execute any code you want after a certain amount of time, measured in milliseconds, has passed.
java.lang.NullPointerException - Signature cannot be null!
Also, Java has its own Timer class, I just find making your own Timer class is more satisfying
java.lang.NullPointerException - Signature cannot be null!
Also ticks should tick at a certian interval in mc, 1sec = 20 ticks
I know what you mean - I remember back in the day when I took a couple C++ classes, we had to make our own Stack, Queue, Linked List and Dynamic Linked List classes. That was pretty fun Never did get into multi-threaded processes or any of the more advanced stuff, unfortunately. Lots more to learn!
Oh yeah building array lists and hash maps on your own, that was very educational and fun
I already have a Thread.wait() and such in my code... I came here because I wanted to replace that with something else. Thread.wait() appears to make all threads wait...
The player has to eat the item. It's the code at the very top of what I showed you.... onEaten()
That depends on how you call it I guess.
I do believe the suggested solution above could work although I'm not sure about how threadsafe the rest of it would be
coolAlias's solution will work without a doubt though you will need to adjust it to fit your exact needs
So set allowFlying to true when the food is eaten, then the living update event should take care of the rest. If for some reason that doesn't work (which it won't in multiplayer anyway), try storing the timer in IExtendedEntityProperties of the player. See the tutorial directly under the EventHandler tutorial I linked earlier.
The code I posted is how you can replace the "Thread.wait();" method. The code I posted does not stop all threads.
java.lang.NullPointerException - Signature cannot be null!
In Java multithreading you have methods wait(), notify(), and sleep(). The first two correspond with each other. You call wait() on an object which you want a lock on; not a Thread. The thread in which you called wait() on object will "wait" or just sit there until notify() is called on same object. Sleep() will "wait" for a specific amount of time. Take a look at this example.