This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
/
WebCommand.java
107 lines (90 loc) · 3.51 KB
/
WebCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package space.morphanone.webizen.commands;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import space.morphanone.webizen.server.WebServer;
import java.io.IOException;
import java.net.BindException;
public class WebCommand extends AbstractCommand {
public WebCommand() {
setName("web");
setSyntax("web [start/stop] (port:<#>)");
setRequiredArguments(1, 2);
}
// <--[command]
// @Name Web
// @Syntax web [start/stop] (port:<#>)
// @Required 1
// @Maximum 2
// @Short Manages the web server ran on your minecraft server.
// @group addons
//
// @Description
// Lets you start or stop a web server.
// Only one web server can be run at a time.
// The default port when the port argument is not specified, is 80.
// TODO: Document command details!
//
// @Usage
// Use to start the web server on port 10123.
// - web start port:10123
//
// @Usage
// Use to stop the web server.
// - web stop
//
// @Plugin Webizen
// -->
private enum Action { START, STOP }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (arg.matchesEnum(Action.class)
&& !scriptEntry.hasObject("action")) {
scriptEntry.addObject("action", arg.asElement());
}
else if (arg.matchesPrefix("port")
&& arg.matchesInteger()
&& !scriptEntry.hasObject("port")) {
scriptEntry.addObject("port", arg.asElement());
}
else arg.reportUnhandled();
}
if (!scriptEntry.hasObject("action"))
throw new InvalidArgumentsException("Must specify an action!");
scriptEntry.defaultObject("port", new ElementTag(80));
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag actionElementTag = scriptEntry.getElement("action");
ElementTag portElementTag = scriptEntry.getElement("port");
Debug.report(scriptEntry, getName(), actionElementTag, portElementTag);
switch (Action.valueOf(actionElementTag.asString().toUpperCase())) {
case START:
if (WebServer.isRunning()) {
Debug.log("A Webizen server is already running!");
return;
}
try {
WebServer.start(portElementTag.asInt());
} catch (BindException e) {
Debug.echoError("Could not bind a Webizen server to port " + portElementTag.asInt() + ". Perhaps there" +
" is already something using that port?");
} catch (IOException e) {
Debug.echoError("There was a problem while starting a Webizen server...");
Debug.echoError(e);
}
break;
case STOP:
if (!WebServer.isRunning()) {
Debug.log("There is no Webizen server currently running!");
return;
}
WebServer.stop();
break;
}
}
}