Skip to content

Commit

Permalink
fixed commands and autostart
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Nov 4, 2014
1 parent 47e8352 commit 03cf3e1
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 178 deletions.
18 changes: 18 additions & 0 deletions src/main/java/net/doubledoordev/backend/Main.java
Expand Up @@ -61,7 +61,9 @@
import java.util.UUID;

import static net.doubledoordev.backend.util.Constants.NAME;
import static net.doubledoordev.backend.util.Constants.SERVERS;
import static net.doubledoordev.backend.util.Settings.SETTINGS;
import static net.doubledoordev.backend.util.Settings.getServerByName;

/**
* @author Dries007
Expand Down Expand Up @@ -146,6 +148,21 @@ public static void main(String[] args) throws Exception
LOGGER.info("Use the help command for help.");

CommandHandler.init();

for (Server server : SETTINGS.servers.values())
{
if (server.getAutoStart())
{
try
{
server.startServer();
}
catch (Exception ignored)
{
ignored.printStackTrace();
}
}
}
}

@SuppressWarnings("ResultOfMethodCallIgnored")
Expand All @@ -158,6 +175,7 @@ public static synchronized void shutdown()
{
running = false;
Settings.save();
Cache.init();
LOGGER.info("Attempting graceful shutdown of all servers...");
for (final Server server : Settings.SETTINGS.servers.values())
{
Expand Down
126 changes: 123 additions & 3 deletions src/main/java/net/doubledoordev/backend/commands/CommandHandler.java
Expand Up @@ -41,27 +41,39 @@

package net.doubledoordev.backend.commands;

import com.sk89q.intake.Command;
import com.sk89q.intake.CommandException;
import com.sk89q.intake.CommandMapping;
import com.sk89q.intake.context.CommandLocals;
import com.sk89q.intake.dispatcher.Dispatcher;
import com.sk89q.intake.fluent.CommandGraph;
import com.sk89q.intake.parametric.ParametricBuilder;
import com.sk89q.intake.parametric.annotation.Optional;
import com.sk89q.intake.parametric.annotation.Switch;
import com.sk89q.intake.parametric.annotation.Text;
import com.sk89q.intake.util.auth.AuthorizationException;
import net.doubledoordev.backend.Main;
import net.doubledoordev.backend.server.Server;
import net.doubledoordev.backend.server.WorldManager;
import net.doubledoordev.backend.util.Cache;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static net.doubledoordev.backend.util.Constants.JOINER_COMMA_SPACE;
import static net.doubledoordev.backend.util.Settings.SETTINGS;

/**
* Using sk89q's Intake lib
*
* @author Dries007
*/
public class CommandHandler implements Runnable
{
public static final CommandHandler INSTANCE = new CommandHandler();
public static final Logger CMDLOGGER = LogManager.getLogger("cmd");
public final Dispatcher dispatcher;

Expand All @@ -73,15 +85,14 @@ private CommandHandler()
dispatcher = new CommandGraph()
.builder(parametricBuilder)
.commands()
.registerMethods(new Commands(this))
.registerMethods(this)
.graph()
.getDispatcher();
}

public static void init()
{
new Thread(new CommandHandler(), "CommandHandler").start();
new Thread(INSTANCE, "CommandHandler").start();
}

@Override
Expand All @@ -92,12 +103,121 @@ public void run()
{
try
{
dispatcher.call(reader.readLine(), new CommandLocals(), new String[0]);
String command = reader.readLine();
if (dispatcher.get(command.split(" ")[0]) != null) dispatcher.call(command, new CommandLocals(), new String[0]);
else throw new CommandNotFoundException(command);
}
catch (CommandException | IOException | AuthorizationException e)
{
CMDLOGGER.warn(e);
e.printStackTrace();
}
}
}

@Command(aliases = {"help", "?"}, desc = "Get a list of commands", help = "Use this to get help", usage = "[Command]", max = 1)
public void cmdHelp(@Optional String command) throws CommandException
{
// Command list
if (command == null)
{
CMDLOGGER.info("--==## Command list ##==--");
for (CommandMapping cmd : dispatcher.getCommands())
{
CMDLOGGER.info(cmd.getPrimaryAlias() + ' ' + cmd.getDescription().getUsage() + " => " + cmd.getDescription().getShortDescription()); // Looks like this: Name ListOfParameters => Description
}
}
else
{
CommandMapping cmd = dispatcher.get(command);

if (cmd == null) throw new CommandNotFoundException(command);

CMDLOGGER.info(String.format("--==## Help for %s ##==--", command));
CMDLOGGER.info(String.format("Name: %s \t Aliases: %s", cmd.getPrimaryAlias(), JOINER_COMMA_SPACE.join(cmd.getAllAliases())));
CMDLOGGER.info(String.format("Usage: %s %s", cmd.getPrimaryAlias(), cmd.getDescription().getUsage()));
CMDLOGGER.info(String.format("Short description: %s", cmd.getDescription().getShortDescription()));
CMDLOGGER.info(String.format("Help text: %s", cmd.getDescription().getHelp()));
}
}

@Command(aliases = {"serverlist", "servers"}, desc = "List all servers", max = 0)
public void cmdServerList()
{
CMDLOGGER.info("All servers:");
CMDLOGGER.info(JOINER_COMMA_SPACE.join(SETTINGS.getServers()));
CMDLOGGER.info("Online servers:");
CMDLOGGER.info(JOINER_COMMA_SPACE.join(SETTINGS.getOnlineServers()));
}

@Command(aliases = "message", desc = "Send message to servers (with /say)", usage = "<server name (regex)> <message ...>", min = 2)
public void cmdMessage(Server[] servers, @Text String msg) throws CommandException
{
for (Server server : servers)
{
if (!server.getOnline()) continue;
server.send(String.format("/say %s", msg));
}
}

@Command(aliases = "backup", desc = "Make full backup of one or more servers", usage = "<server name (regex)>", min = 1, max = 1)
public void cmdBackup(Server[] servers) throws CommandException
{
for (Server server : servers)
{
try
{
server.getWorldManager().bypassLimits = true;
server.getWorldManager().makeAllOfTheBackup();
}
catch (WorldManager.BackupException e)
{
CMDLOGGER.warn("Error when making a backup of " + server.getName());
CMDLOGGER.warn(e);
}
}
}

@Command(aliases = "stop", desc = "Stop one or more servers", usage = "<server name (regex)> [-f (force the stop)] [message ...]", min = 1)
public void cmdStop(Server[] servers, @Optional @Switch('f') boolean force, @Optional("Stopping the server.") @Text String msg) throws CommandException
{
for (Server server : servers)
{
if (!server.getOnline()) continue;
if (server.stopServer(msg)) CMDLOGGER.info(String.format("Shutdown command send to %s", server.getName()));
else CMDLOGGER.warn(String.format("Server %s did not shutdown with a message.", server.getName()));
}
}

@Command(aliases = "start", desc = "Start one or more servers", usage = "<server name (regex)>", min = 1)
public void cmdStart(Server[] servers, @Optional @Switch('f') boolean force, @Optional("Stopping the server.") @Text String msg) throws CommandException
{
for (Server server : servers)
{
if (server.getOnline()) continue;
try
{
server.startServer();
}
catch (Exception e)
{
CMDLOGGER.warn("Not able to start server " + server.getName());
CMDLOGGER.warn(e);
}
}
}

@Command(aliases = "version", desc = "Get current version and latest version available.", usage = "", min = 0, max = 0)
public void cmdVersion() throws CommandException
{
CMDLOGGER.info("Current version: {}. Build: {}", Main.version, Main.build);
CMDLOGGER.info("Latest version available: {}", Cache.getUpdateVersion());
}

@Command(aliases = {"shutdown", "exit"}, usage = "", desc = "Stop the backend", max = 0, flags = "f")
public void cmdShutdown(@Optional @Switch('f') boolean force) throws CommandException
{
if (force) System.exit(0);
Main.shutdown();
}
}
161 changes: 0 additions & 161 deletions src/main/java/net/doubledoordev/backend/commands/Commands.java

This file was deleted.

0 comments on commit 03cf3e1

Please sign in to comment.