Skip to content

Commit

Permalink
Added sk89q's cmd library.
Browse files Browse the repository at this point in the history
Added backend commands: help, serverlist, and stop
  • Loading branch information
dries007 committed Oct 18, 2014
1 parent 0d97cc6 commit 1ecb37f
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 14 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Expand Up @@ -50,6 +50,9 @@ idea {

repositories {
mavenCentral()
maven {
url = "http://maven.sk89q.com/repo/"
}
}

//noinspection GroovyAssignabilityCheck
Expand All @@ -61,6 +64,7 @@ dependencies {
compile group: "org.freemarker", name: "freemarker", version: "2.3.20"
compile group: "org.apache.logging.log4j", name: "log4j-core", version: "2.0.2"
compile group: "net.lingala.zip4j", name:"zip4j", version: "1.3.2"
compile group: "com.sk89q", name:"intake", version: "3.1.1-SNAPSHOT"
}

jar {
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/net/doubledoordev/backend/Main.java
Expand Up @@ -40,6 +40,9 @@

package net.doubledoordev.backend;

import com.sk89q.intake.dispatcher.Dispatcher;
import com.sk89q.intake.fluent.CommandGraph;
import net.doubledoordev.backend.commands.CommandHandler;
import net.doubledoordev.backend.server.Server;
import net.doubledoordev.backend.server.rcon.RCon;
import net.doubledoordev.backend.util.Cache;
Expand Down Expand Up @@ -67,6 +70,7 @@ public class Main
{
public static final Logger LOGGER = LogManager.getLogger(Main.class.getSimpleName());
public static String adminKey;
public static boolean running = true;

private Main()
{
Expand Down Expand Up @@ -120,19 +124,9 @@ public static void main(String[] args) throws Exception
LOGGER.warn("Admin token: " + adminKey);
}

LOGGER.info("Loading done. Press any key to terminate the program.");
// Wait for user input.
try
{
//noinspection ResultOfMethodCallIgnored
System.in.read();
}
catch (Throwable ignored)
{
// Noop
}
LOGGER.info("Use the help command for help.");

shutdown();
CommandHandler.init();
}

@SuppressWarnings("ResultOfMethodCallIgnored")
Expand All @@ -143,6 +137,7 @@ private static void mkdirs()

public static synchronized void shutdown()
{
running = false;
Settings.save();
LOGGER.info("Attempting graceful shutdown of all servers...");
for (final Server server : Settings.SETTINGS.servers.values())
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/net/doubledoordev/backend/commands/Bindings.java
@@ -0,0 +1,91 @@
/*
* Unless otherwise specified through the '@author' tag or comments at
* the top of the file or on a specific portion of the code the following license applies:
*
* Copyright (c) 2014, DoubleDoorDevelopment
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* The header specified or the above copyright notice, this list of conditions
* and the following disclaimer below must be displayed at the top of the source code
* of any web page received while using any part of the service this software provides.
*
* The header to be displayed:
* This page was generated by DoubleDoorDevelopment's D3Backend or a derivative thereof.
*
* Neither the name of the project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

package net.doubledoordev.backend.commands;

import com.sk89q.intake.parametric.ParameterException;
import com.sk89q.intake.parametric.argument.ArgumentStack;
import com.sk89q.intake.parametric.binding.BindingBehavior;
import com.sk89q.intake.parametric.binding.BindingHelper;
import com.sk89q.intake.parametric.binding.BindingMatch;
import net.doubledoordev.backend.permissions.User;
import net.doubledoordev.backend.server.Server;
import net.doubledoordev.backend.util.Settings;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
* @author Dries007
*/
public class Bindings extends BindingHelper
{
@BindingMatch(type = Server.class, behavior = BindingBehavior.CONSUMES, consumedCount = 1)
public Server getServer(ArgumentStack context) throws ParameterException
{
return Settings.getServerByName(context.next());
}

@BindingMatch(type = Server[].class, behavior = BindingBehavior.CONSUMES)
public Server[] getServers(ArgumentStack context) throws ParameterException
{
Pattern pattern = Pattern.compile(context.next());
List<Server> servers = new ArrayList<>();
for (Server server : Settings.SETTINGS.servers.values()) if (pattern.matcher(server.getName()).matches()) servers.add(server);
return servers.toArray(new Server[servers.size()]);
}

@BindingMatch(type = User.class, behavior = BindingBehavior.CONSUMES, consumedCount = 1)
public User getUser(ArgumentStack context) throws ParameterException
{
return Settings.getUserByName(context.next());
}

@BindingMatch(type = User[].class, behavior = BindingBehavior.CONSUMES, consumedCount = 1)
public User[] getUsers(ArgumentStack context) throws ParameterException
{
Pattern pattern = Pattern.compile(context.next());
List<User> users = new ArrayList<>();
for (User server : Settings.SETTINGS.users.values()) if (pattern.matcher(server.getUsername()).matches()) users.add(server);
return users.toArray(new User[users.size()]);
}
}
105 changes: 105 additions & 0 deletions src/main/java/net/doubledoordev/backend/commands/CommandHandler.java
@@ -0,0 +1,105 @@
/*
* Unless otherwise specified through the '@author' tag or comments at
* the top of the file or on a specific portion of the code the following license applies:
*
* Copyright (c) 2014, DoubleDoorDevelopment
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* The header specified or the above copyright notice, this list of conditions
* and the following disclaimer below must be displayed at the top of the source code
* of any web page received while using any part of the service this software provides.
*
* The header to be displayed:
* This page was generated by DoubleDoorDevelopment's D3Backend or a derivative thereof.
*
* Neither the name of the project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

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.util.auth.AuthorizationException;
import net.doubledoordev.backend.Main;

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

import static net.doubledoordev.backend.Main.LOGGER;
import static net.doubledoordev.backend.util.Constants.JOINER_COMMA_SPACE;

/**
* Using sk89q's Intake lib
*
* @author Dries007
*/
public class CommandHandler implements Runnable
{
public final Dispatcher dispatcher;
private CommandHandler()
{
ParametricBuilder parametricBuilder = new ParametricBuilder();
parametricBuilder.addBinding(new Bindings());

dispatcher = new CommandGraph()
.builder(parametricBuilder)
.commands()
.registerMethods(new Commands(this))
.registerMethods(this)
.graph()
.getDispatcher();
}

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

@Override
public void run()
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (Main.running)
{
try
{
dispatcher.call(reader.readLine(), new CommandLocals(), new String[0]);
}
catch (CommandException | IOException | AuthorizationException e)
{
LOGGER.warn(e);
}
}
}
}
@@ -0,0 +1,55 @@
/*
* Unless otherwise specified through the '@author' tag or comments at
* the top of the file or on a specific portion of the code the following license applies:
*
* Copyright (c) 2014, DoubleDoorDevelopment
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* The header specified or the above copyright notice, this list of conditions
* and the following disclaimer below must be displayed at the top of the source code
* of any web page received while using any part of the service this software provides.
*
* The header to be displayed:
* This page was generated by DoubleDoorDevelopment's D3Backend or a derivative thereof.
*
* Neither the name of the project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

package net.doubledoordev.backend.commands;

import com.sk89q.intake.CommandException;

/**
* @author Dries007
*/
public class CommandNotFoundException extends CommandException
{
public CommandNotFoundException(String command)
{
super(String.format("%s is not a valid command.", command));
}
}

0 comments on commit 1ecb37f

Please sign in to comment.