I know I know, you want to jump in there and throw ingredients left right and centre and make some fantastic code stew. But let’s hold the horses for a moment and think. Do we want vegetable soup or do we want lamb(da) soup

What does our server shutdown plugin need to do? Shutdown the server right? Well yes, but we also want to do it in a controlled manner. Calling;


System.exit(0);

Would work. But it’s a bit like hitting a brick wall to stop your car (sorry I mean horse and cart) – we need to gentle apply the brakes and bring old Nelly down to a controlled stop. Something like;


server.saveAll();
server.shutdown();

That seems a little more sensible. So we’ll start with that in mind as our `end behaviour`. So now that we’re not unexpectedly shutting down our server – for the servers own sanity, we also need to take into consideration the sanity of our players. They’ll need a warning!

We can achieve this with the use of Timers. Timers are very important. So important in-fact you’ll see (if you haven’t already) that Timer is one of the parent classes. next to Plugin.

Creating a new timer in your plugin (utilising the amazin’ lambda) looks a little like this;


	Timer timer = new Timer(60f, 0f, -1, () -> { });

Well what timers will we actually need?

  • Well, we need to know a prescheduled shutdown time and the current time – so that we can check to see if this condition is ever true (it should be at least once a day).
  • We’ll also need to know for how long should we be warning our players of the imminent server restart.

So how will we set (and remember) what our prescheduled time should be? Well there are many ways to skin this cat, but what we’re going to do here is use an XML file; which will contain our restart time and maybe some other fun stuff too (later). But first it’ll be super simple like this;

</pre>
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<event>
<hour>18</hour>
<minute>0</minute>
</event>
</settings>
<pre>

 

So it looks like we’re going to schedule a reboot event for 18:00.Great, that’s about super time!

Now that we know what we want, let’s start bashing out some of that code.