简体   繁体   中英

Java Timing A Trigger

I want to trigger an action after a certain time, I've been googling how I would do this but I have had no luck I guess it's just the way my game is coded. Anyways I need to make it to where 30 minutes after the code a1 is triggered, the code a2 is triggered.

a1:

if (itemId == 608) {
        c.sendMessage("The scroll has brought you to the Revenants.");
        c.sendMessage("They are very ghastly, powerful, undead creatures.");
        c.sendMessage("If you defeat them, you may receive astounding treasures.");
        c.sendMessage("If you donate you may visit the Revenants anytime without a scroll.");
        c.getPA().movePlayer(3668, 3497, 0);
        c.gfx0(398);
        c.getItems().deleteItem(608, 1);
}

a2:

c.getPA().movePlayer(x, y, 0);

There are plenty of ways to do timers in Java but to introduce yourself to a nice framework check out http://quartz-scheduler.org/ . Also Spring has quartz integration if you use it.

But more importantly if you are creating a game you will need a core technique of game programming called the event loop

This seems like a decent discussion of how to create a game architecture

You can use Thread.sleep(), but it will freeze your application if you call it in your main thread, so, create another thread and put your code in it. Doing this you will not stop the main application.

Here is a simple example.

public class MyThread implements Runnable {

    @Override
    public void run() {

        try {

            System.out.println( "executing first part..." );
            System.out.println( "Going to sleep ...zzzZZZ" );

            // will sleep for at least 5 seconds (5000 miliseconds)
            // 30 minutes are 1,800,000 miliseconds
            Thread.sleep( 5000L );

            System.out.println( "Waking up!" );
            System.out.println( "executing second part..." );

        } catch ( InterruptedException exc ) {
            exc.printStackTrace();
        }

    }

    public static void main( String[] args ) {
        new Thread( new MyThread() ).start();
    }

}

This will run just one time. To run several times, you will need an infinite loop that encloses the run method body (or a loop controled by a flag).

You have some other options like:

Since this code uses Project Insanity , you should use the built-in scheduled event facility provided by server.event.EventManager .

Below is example code:

if (itemId == 608) {
  c.sendMessage("The scroll has brought you to the Revenants.");
  c.sendMessage("They are very ghastly, powerful, undead creatures.");
  c.sendMessage("If you defeat them, you may receive astounding treasures.");
  c.sendMessage("If you donate you may visit the Revenants anytime without a scroll.");
  c.getPA().movePlayer(3668, 3497, 0);
  c.gfx0(398);
  c.getItems().deleteItem(608, 1);

  /* only if the parameter Client c isn't declared final */
  final Client client = c;
  /* set these to the location you'd like to teleport to */
  final int x = ...;
  final int y = ...;

  EventManager.getSingleton().addEvent(new Event() {

    public void execute(final EventContainer container) {
      client.getPA().movePlayer(x, y, 0);
    }
  }, 1800000); /* 30 min * 60 s/min * 1000 ms/s = 1800000 ms */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM