Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added backend commands: help, serverlist, and stop
- Loading branch information
Showing
8 changed files
with
389 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/net/doubledoordev/backend/commands/Bindings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
105
src/main/java/net/doubledoordev/backend/commands/CommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/net/doubledoordev/backend/commands/CommandNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
Oops, something went wrong.