Let’s get the core of our plugin written. We’re going to add loads of comments so it’s super simple to read. When the plugin starting (the calling of onEnable() override) we’re going to start a heartbeat timer which will ask the system what the current time is. We’re not really concerned what the time is exactly, so a rough time will surface – we’ll ask once every minute. Once this timer knows that the current time is the same as the scheduled time we’ll start a second timer which will begin our shutdown event, and kill our first timer. Brutal.


package risingworldshutdown;

import net.risingworld.api.Plugin;
import net.risingworld.api.Server;
import net.risingworld.api.Timer;

public class RisingWorldShutdown extends Plugin {

    private Timer heartbeat_timer;
    private Timer shutdown_timer;
    private final int HOUR = 18;
    private final int MINUTE = 0;
    private Server server;
    private int remaining_time = 60;
  
    
    @Override
    public void onEnable() {
        
        //lets grab a reference to our server
        server = getServer();
        //check every minute, no delay in checking, keep it up
        heartbeat_timer = new Timer(60.0f, 0.0f, -1, () -> { 
            
        //if the heatbeat time is the same as our schudeled time
        if (         java.time.LocalTime.now().getHour() == HOUR 
                &&   java.time.LocalTime.now().getMinute() == MINUTE ) {
           //kill the heart
            heartbeat_timer.kill();
            //begin the shutdown
            shutdown_timer.start();
        }
        
	});
        //start our heart
        heartbeat_timer.start();
        
        //every 10 seconds, no delay in checking, keep it up
        shutdown_timer = new Timer(10.0f, 0.0f, -1, () ->; { 
            //let's grab all the players on the server and go through them 
            getServer().getAllPlayers().forEach((player) ->; {
                //send them a warning message
                player.sendYellMessage("SERVER RESTARTING IN " 
                                      + remaining_time + " SECONDS.");
                //let's also beep at them just to be sure!
                player.playGameSound("message");
                
                
            });
            //if our remaining time is up. . . 
            if ( remaining_time <= 0 ) { 
            //shutdown the server gracefully. 
             server.saveAll(); 
             server.shutdown(); }
            // reduce our counter by the delay
            remaining_time-= 10;
	});

    }

    @Override
    public void onDisable() {
        
        
    }
    
 
    
}


 

That’s the core done. With that code compiled your server will begin a shutdown at 6pm every day – and for one minute prior to the shutdown it will warn all online players of the impending inevitable. But what we really need now is some way to change the time to suit our needs, without having to edit the code and recompile each time! That’ll been explained in our next article – reading from XML.