Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 0d7dd25

Browse files
committed
Add client side command /mclogsc
1 parent db8f8c6 commit 0d7dd25

13 files changed

+347
-174
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
- Makes use of API version 4.0.0, allowing for marginal performance increases as API calls are now Async
1+
- Add client side command /mclogsc

src/main/java/gs/mclo/fabric/CommandMclogs.java

-15
This file was deleted.

src/main/java/gs/mclo/fabric/CommandMclogsList.java

-73
This file was deleted.

src/main/java/gs/mclo/fabric/CommandMclogsShare.java

-55
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package gs.mclo.fabric;
22

3-
import com.mojang.brigadier.context.CommandContext;
3+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
44
import gs.mclo.api.Log;
55
import gs.mclo.api.MclogsClient;
66
import gs.mclo.api.response.UploadLogResponse;
7+
import gs.mclo.fabric.commands.Command;
8+
import gs.mclo.fabric.commands.MclogsCommand;
9+
import gs.mclo.fabric.commands.MclogsListCommand;
10+
import gs.mclo.fabric.commands.MclogsShareCommand;
11+
import gs.mclo.fabric.commands.source.Source;
12+
import net.fabricmc.api.ClientModInitializer;
713
import net.fabricmc.api.DedicatedServerModInitializer;
14+
import net.fabricmc.api.ModInitializer;
15+
import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager;
16+
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
817
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
918
import net.fabricmc.loader.api.FabricLoader;
1019
import net.fabricmc.loader.api.ModContainer;
20+
import net.minecraft.server.command.CommandManager;
1121
import net.minecraft.server.command.ServerCommandSource;
1222
import net.minecraft.text.ClickEvent;
1323
import net.minecraft.text.LiteralText;
@@ -24,34 +34,39 @@
2434
import java.util.concurrent.CompletableFuture;
2535
import java.util.concurrent.ExecutionException;
2636

27-
public class MclogsFabricLoader implements DedicatedServerModInitializer {
37+
public class MclogsFabric implements DedicatedServerModInitializer, ClientModInitializer, ModInitializer {
2838
public static final Logger logger = LogManager.getLogger();
2939
private static final MclogsClient client = new MclogsClient("Mclogs-fabric");
40+
private static final Command[] COMMANDS = new Command[]{
41+
new MclogsCommand(),
42+
new MclogsListCommand(),
43+
new MclogsShareCommand()
44+
};
3045

3146
/**
32-
* @param context command context
47+
* @param source command source
3348
* @return log files
3449
* @throws IOException io exception
3550
*/
36-
public static String[] getLogs(CommandContext<ServerCommandSource> context) throws IOException {
37-
return client.listLogsInDirectory(context.getSource().getMinecraftServer().getRunDirectory().getCanonicalPath());
51+
public static String[] getLogs(Source source) throws IOException {
52+
return client.listLogsInDirectory(source.getRunDirectory().toString());
3853
}
3954

4055
/**
41-
* @param context command context
56+
* @param source command source
4257
* @return crash reports
4358
* @throws IOException io exception
4459
*/
45-
public static String[] getCrashReports(CommandContext<ServerCommandSource> context) throws IOException {
46-
return client.listCrashReportsInDirectory(context.getSource().getMinecraftServer().getRunDirectory().getCanonicalPath());
60+
public static String[] getCrashReports(Source source) throws IOException {
61+
return client.listCrashReportsInDirectory(source.getRunDirectory().toString());
4762
}
4863

49-
public static int share(ServerCommandSource source, String filename) {
50-
client.setMinecraftVersion(source.getMinecraftServer().getVersion());
51-
logger.log(Level.INFO,"Sharing "+filename);
64+
public static int share(Source source, String filename) {
65+
client.setMinecraftVersion(source.getMinecraftVersion());
66+
logger.log(Level.INFO, "Sharing {}", filename);
5267
source.sendFeedback(new LiteralText("Sharing " + filename), false);
5368

54-
Path directory = source.getMinecraftServer().getRunDirectory().toPath();
69+
Path directory = source.getRunDirectory();
5570
Path logs = directory.resolve("logs");
5671
Path crashReports = directory.resolve("crash-reports");
5772
Path log = directory.resolve("logs").resolve(filename);
@@ -65,8 +80,8 @@ public static int share(ServerCommandSource source, String filename) {
6580
Path logPath = log.toRealPath();
6681
isInAllowedDirectory = (logs.toFile().exists() && logPath.startsWith(logs.toRealPath()))
6782
|| (crashReports.toFile().exists() && logPath.startsWith(crashReports.toRealPath()));
83+
} catch (IOException ignored) {
6884
}
69-
catch (IOException ignored) {}
7085

7186
if (!log.toFile().exists() || !isInAllowedDirectory
7287
|| !log.getFileName().toString().matches(Log.ALLOWED_FILE_NAME_PATTERN.pattern())) {
@@ -81,30 +96,27 @@ public static int share(ServerCommandSource source, String filename) {
8196
res.setClient(client);
8297
if (res.isSuccess()) {
8398
LiteralText feedback = new LiteralText("Your log has been uploaded: ");
84-
feedback.setStyle(Style.EMPTY.withColor(Formatting.GREEN));
8599

86100
LiteralText link = new LiteralText(res.getUrl());
87-
Style linkStyle = Style.EMPTY.withColor(Formatting.BLUE);
88-
linkStyle = linkStyle.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL,res.getUrl()));
101+
Style linkStyle = Style.EMPTY
102+
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, res.getUrl()))
103+
.withFormatting(Formatting.UNDERLINE);
89104
link.setStyle(linkStyle);
90105

91-
source.sendFeedback(feedback.append(link),true);
106+
source.sendFeedback(feedback.append(link), true);
92107
return 1;
93-
}
94-
else {
108+
} else {
95109
logger.error("An error occurred when uploading your log: ");
96110
logger.error(res.getError());
97111
LiteralText error = new LiteralText("An error occurred. Check your log for more details");
98112
source.sendError(error);
99113
return 0;
100114
}
101-
}
102-
catch (FileNotFoundException|IllegalArgumentException e) {
103-
LiteralText error = new LiteralText("The log file "+filename+" doesn't exist. Use '/mclogs list' to list all logs.");
115+
} catch (FileNotFoundException | IllegalArgumentException e) {
116+
LiteralText error = new LiteralText("The log file " + filename + " doesn't exist. Use '/mclogs list' to list all logs.");
104117
source.sendError(error);
105118
return -1;
106-
}
107-
catch (IOException | InterruptedException | ExecutionException e) {
119+
} catch (IOException | InterruptedException | ExecutionException e) {
108120
source.sendError(new LiteralText("An error occurred. Check your log for more details"));
109121
logger.error("Could not get log file!");
110122
logger.error(e);
@@ -113,13 +125,33 @@ public static int share(ServerCommandSource source, String filename) {
113125
}
114126

115127
@Override
116-
public void onInitializeServer() {
128+
public void onInitialize() {
117129
Optional<ModContainer> mclogs = FabricLoader.getInstance().getModContainer("mclogs");
118130
client.setProjectVersion(mclogs.isPresent() ? mclogs.get().getMetadata().getVersion().getFriendlyString() : "unknown");
131+
}
132+
133+
@Override
134+
public void onInitializeServer() {
119135
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
120-
CommandMclogs.register(dispatcher);
121-
CommandMclogsList.register(dispatcher);
122-
CommandMclogsShare.register(dispatcher);
136+
if (!dedicated) {
137+
return;
138+
}
139+
140+
logger.info("Registering server commands");
141+
LiteralArgumentBuilder<ServerCommandSource> mclogs = CommandManager.literal("mclogs");
142+
for (Command command : COMMANDS) {
143+
dispatcher.register(command.buildServer(mclogs));
144+
}
123145
});
124146
}
147+
148+
@Override
149+
public void onInitializeClient() {
150+
logger.info("Registering client commands");
151+
LiteralArgumentBuilder<FabricClientCommandSource> mclogsc = ClientCommandManager.literal("mclogsc");
152+
for (Command command : COMMANDS) {
153+
ClientCommandManager.DISPATCHER.register(command.buildClient(mclogsc));
154+
}
155+
}
156+
125157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package gs.mclo.fabric.commands;
2+
3+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
4+
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
5+
import net.minecraft.server.command.ServerCommandSource;
6+
7+
public interface Command {
8+
LiteralArgumentBuilder<FabricClientCommandSource> buildClient(LiteralArgumentBuilder<FabricClientCommandSource> builder);
9+
LiteralArgumentBuilder<ServerCommandSource> buildServer(LiteralArgumentBuilder<ServerCommandSource> builder);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package gs.mclo.fabric.commands;
2+
3+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
4+
import gs.mclo.fabric.MclogsFabric;
5+
import gs.mclo.fabric.commands.source.ClientSource;
6+
import gs.mclo.fabric.commands.source.ServerSource;
7+
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
8+
import net.minecraft.server.command.ServerCommandSource;
9+
10+
11+
public class MclogsCommand implements Command {
12+
@Override
13+
public LiteralArgumentBuilder<FabricClientCommandSource> buildClient(LiteralArgumentBuilder<FabricClientCommandSource> builder) {
14+
return builder.executes(context -> MclogsFabric.share(new ClientSource(context.getSource()), "latest.log"));
15+
}
16+
17+
@Override
18+
public LiteralArgumentBuilder<ServerCommandSource> buildServer(LiteralArgumentBuilder<ServerCommandSource> builder) {
19+
return builder
20+
.requires(source -> source.hasPermissionLevel(2))
21+
.executes(context -> MclogsFabric.share(new ServerSource(context.getSource()), "latest.log"));
22+
}
23+
}

0 commit comments

Comments
 (0)