Skip to content

Commit

Permalink
implement config to set custom time
Browse files Browse the repository at this point in the history
  • Loading branch information
Chew committed Oct 29, 2019
1 parent 09bb889 commit 461b056
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# The time (in milliseconds) to shutdown the server when no one is on.
# Default is 20 seconds (or 20000 ms)
shutdownAfter: 20000
10 changes: 4 additions & 6 deletions src/pw/chew/noplayersnoserver/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ public class HelpCommand implements CommandExecutor {

// This method is called, when somebody uses our command
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Server server = sender.getServer();
int players = server.getOnlinePlayers().size();
Server server = sender.getServer();
int players = server.getOnlinePlayers().size();

Player p = (Player) sender;
p.sendMessage("Welcome to NPNS!\nThis plugin works automatically. Just leave and 10 seconds later the server will stop!\nCurrently there are " + players + " player(s) online");
}
Player p = (Player) sender;
p.sendMessage("Welcome to NPNS!\nThis plugin works automatically. Just leave and a set amount of seconds later the server will stop!\nCurrently there are " + players + " player(s) online!\nThe server is set to shutdown after " + NoPlayersNoServer.time + " milliseconds.");

// If the player (or console) uses our command correct, we can return true
return true;
Expand Down
12 changes: 12 additions & 0 deletions src/pw/chew/noplayersnoserver/NoPlayersNoServer.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package pw.chew.noplayersnoserver;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

public class NoPlayersNoServer extends JavaPlugin {
static int time = 0;
// Fired when plugin is first enabled
TimerUntilBye timer = new TimerUntilBye();
public void onEnable() {
FileConfiguration config = this.getConfig();
config.addDefault("shutdownAfter", 20000);
config.options().copyDefaults(true);
saveConfig();
time = config.getInt("shutdownAfter");

this.getCommand("npns").setExecutor(new HelpCommand());
getServer().getPluginManager().registerEvents(new ByeByePlayer(), this);
System.out.println("Timer won't activate until someone leaves.");
Expand All @@ -13,4 +21,8 @@ public void onEnable() {
public void onDisable() {

}

public int getTime() {
return time;
}
}
2 changes: 1 addition & 1 deletion src/pw/chew/noplayersnoserver/TimerUntilBye.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public TimerUntilBye() {
}

public void startTimer() {
timer.schedule(task, 20000, 5000);
timer.schedule(task, NoPlayersNoServer.time, 5000);
}

public void restartTimer() {
Expand Down

0 comments on commit 461b056

Please sign in to comment.