Skip to content

Commit 7aedea6

Browse files
committed
make links clickable and add restart command
1 parent f0115e5 commit 7aedea6

1 file changed

Lines changed: 38 additions & 18 deletions

File tree

src/main/java/fn10/smm/ModManagerCommands.java

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@
55
import com.mojang.brigadier.arguments.IntegerArgumentType;
66
import fn10.smm.server.ModManagerWebServer;
77
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
8-
import net.minecraft.client.Screenshot;
98
import net.minecraft.commands.CommandBuildContext;
109
import net.minecraft.commands.CommandSourceStack;
1110
import net.minecraft.commands.Commands;
11+
import net.minecraft.network.chat.ClickEvent;
1212
import net.minecraft.network.chat.Component;
13-
import net.minecraft.server.level.ServerPlayer;
13+
import net.minecraft.network.chat.Style;
1414
import net.minecraft.server.permissions.Permissions;
1515

16+
import java.net.URI;
17+
1618
public class ModManagerCommands implements CommandRegistrationCallback {
1719

1820
private static ModManagerWebServer currentServer;
19-
21+
public static final Command<CommandSourceStack> stopCommand = css -> {
22+
final CommandSourceStack src = css.getSource();
23+
if (ModManagerWebServer.isServerRunning()) {
24+
currentServer.stop();
25+
src.sendSuccess(() -> Component.literal("Stopped web server"), true);
26+
return 1;
27+
} else {
28+
src.sendFailure(Component.literal("No web server is running."));
29+
return 0;
30+
}
31+
};
2032
public static final Command<CommandSourceStack> startCommand = css -> {
2133
final CommandSourceStack src = css.getSource();
2234
Integer port = 8880;
@@ -25,18 +37,28 @@ public class ModManagerCommands implements CommandRegistrationCallback {
2537
} catch (Exception ignored) {
2638
}
2739
final Integer finalPort = port;
28-
src.sendSystemMessage(Component.literal("Starting web server at port: " + finalPort));
40+
final URI uri = URI.create("http://localhost:" + finalPort);
41+
src.sendSystemMessage(Component.literal("Starting web server at ").append(
42+
Component.literal(uri.toString())
43+
.withStyle(Style.EMPTY
44+
.withUnderlined(true)
45+
.withClickEvent(new ClickEvent.OpenUrl(uri)))));
2946
if (ModManagerWebServer.isServerRunning()) {
3047
src.sendFailure(Component.literal("Failed to start web server. Server is already running."));
3148
return 0;
3249
} else {
33-
try {
50+
try {
3451
currentServer = new ModManagerWebServer(port, src.getServer());
3552
currentServer.start();
36-
src.sendSuccess(() -> Component.literal("Server started at localhost:" + finalPort), true);
53+
src.sendSuccess(() -> Component.literal("Server started at ")
54+
.append(
55+
Component.literal(uri.toString())
56+
.withStyle(Style.EMPTY
57+
.withUnderlined(true)
58+
.withClickEvent(new ClickEvent.OpenUrl(uri)))), true);
3759
} catch (Exception e) {
38-
src.sendFailure(Component.literal("Failed to start web server. See log for details."));
39-
}
60+
src.sendFailure(Component.literal("Failed to start web server. See log for details."));
61+
}
4062
}
4163
return 1;
4264
};
@@ -53,22 +75,20 @@ public void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBu
5375
Commands.literal("start")
5476
.requires(src -> src.permissions().hasPermission(Permissions.COMMANDS_ADMIN))
5577
.executes(startCommand)
56-
.then(Commands.argument("port", IntegerArgumentType.integer(1,65535))
78+
.then(Commands.argument("port", IntegerArgumentType.integer(1, 65535))
5779
.executes(startCommand))
5880

5981
).then(
6082
Commands.literal("stop")
83+
.requires(src -> src.permissions().hasPermission(Permissions.COMMANDS_ADMIN))
84+
.executes(stopCommand)
85+
).then(
86+
Commands.literal("restart")
6187
.requires(src -> src.permissions().hasPermission(Permissions.COMMANDS_ADMIN))
6288
.executes(css -> {
63-
final CommandSourceStack src = css.getSource();
64-
if (ModManagerWebServer.isServerRunning()) {
65-
currentServer.stop();
66-
src.sendSuccess(() -> Component.literal("Stopped web server"), true);
67-
return 1;
68-
} else {
69-
src.sendFailure(Component.literal("No web server is running."));
70-
return 0;
71-
}
89+
stopCommand.run(css);
90+
startCommand.run(css);
91+
return 0;
7292
})
7393
)
7494
);

0 commit comments

Comments
 (0)