Skip to content

Commit

Permalink
Add client side command /mclogsc
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Jun 18, 2024
1 parent db8f8c6 commit 0d7dd25
Show file tree
Hide file tree
Showing 13 changed files with 347 additions and 174 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- Makes use of API version 4.0.0, allowing for marginal performance increases as API calls are now Async
- Add client side command /mclogsc
15 changes: 0 additions & 15 deletions src/main/java/gs/mclo/fabric/CommandMclogs.java

This file was deleted.

73 changes: 0 additions & 73 deletions src/main/java/gs/mclo/fabric/CommandMclogsList.java

This file was deleted.

55 changes: 0 additions & 55 deletions src/main/java/gs/mclo/fabric/CommandMclogsShare.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package gs.mclo.fabric;

import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import gs.mclo.api.Log;
import gs.mclo.api.MclogsClient;
import gs.mclo.api.response.UploadLogResponse;
import gs.mclo.fabric.commands.Command;
import gs.mclo.fabric.commands.MclogsCommand;
import gs.mclo.fabric.commands.MclogsListCommand;
import gs.mclo.fabric.commands.MclogsShareCommand;
import gs.mclo.fabric.commands.source.Source;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.LiteralText;
Expand All @@ -24,34 +34,39 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class MclogsFabricLoader implements DedicatedServerModInitializer {
public class MclogsFabric implements DedicatedServerModInitializer, ClientModInitializer, ModInitializer {
public static final Logger logger = LogManager.getLogger();
private static final MclogsClient client = new MclogsClient("Mclogs-fabric");
private static final Command[] COMMANDS = new Command[]{
new MclogsCommand(),
new MclogsListCommand(),
new MclogsShareCommand()
};

/**
* @param context command context
* @param source command source
* @return log files
* @throws IOException io exception
*/
public static String[] getLogs(CommandContext<ServerCommandSource> context) throws IOException {
return client.listLogsInDirectory(context.getSource().getMinecraftServer().getRunDirectory().getCanonicalPath());
public static String[] getLogs(Source source) throws IOException {
return client.listLogsInDirectory(source.getRunDirectory().toString());
}

/**
* @param context command context
* @param source command source
* @return crash reports
* @throws IOException io exception
*/
public static String[] getCrashReports(CommandContext<ServerCommandSource> context) throws IOException {
return client.listCrashReportsInDirectory(context.getSource().getMinecraftServer().getRunDirectory().getCanonicalPath());
public static String[] getCrashReports(Source source) throws IOException {
return client.listCrashReportsInDirectory(source.getRunDirectory().toString());
}

public static int share(ServerCommandSource source, String filename) {
client.setMinecraftVersion(source.getMinecraftServer().getVersion());
logger.log(Level.INFO,"Sharing "+filename);
public static int share(Source source, String filename) {
client.setMinecraftVersion(source.getMinecraftVersion());
logger.log(Level.INFO, "Sharing {}", filename);
source.sendFeedback(new LiteralText("Sharing " + filename), false);

Path directory = source.getMinecraftServer().getRunDirectory().toPath();
Path directory = source.getRunDirectory();
Path logs = directory.resolve("logs");
Path crashReports = directory.resolve("crash-reports");
Path log = directory.resolve("logs").resolve(filename);
Expand All @@ -65,8 +80,8 @@ public static int share(ServerCommandSource source, String filename) {
Path logPath = log.toRealPath();
isInAllowedDirectory = (logs.toFile().exists() && logPath.startsWith(logs.toRealPath()))
|| (crashReports.toFile().exists() && logPath.startsWith(crashReports.toRealPath()));
} catch (IOException ignored) {
}
catch (IOException ignored) {}

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

LiteralText link = new LiteralText(res.getUrl());
Style linkStyle = Style.EMPTY.withColor(Formatting.BLUE);
linkStyle = linkStyle.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL,res.getUrl()));
Style linkStyle = Style.EMPTY
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, res.getUrl()))
.withFormatting(Formatting.UNDERLINE);
link.setStyle(linkStyle);

source.sendFeedback(feedback.append(link),true);
source.sendFeedback(feedback.append(link), true);
return 1;
}
else {
} else {
logger.error("An error occurred when uploading your log: ");
logger.error(res.getError());
LiteralText error = new LiteralText("An error occurred. Check your log for more details");
source.sendError(error);
return 0;
}
}
catch (FileNotFoundException|IllegalArgumentException e) {
LiteralText error = new LiteralText("The log file "+filename+" doesn't exist. Use '/mclogs list' to list all logs.");
} catch (FileNotFoundException | IllegalArgumentException e) {
LiteralText error = new LiteralText("The log file " + filename + " doesn't exist. Use '/mclogs list' to list all logs.");
source.sendError(error);
return -1;
}
catch (IOException | InterruptedException | ExecutionException e) {
} catch (IOException | InterruptedException | ExecutionException e) {
source.sendError(new LiteralText("An error occurred. Check your log for more details"));
logger.error("Could not get log file!");
logger.error(e);
Expand All @@ -113,13 +125,33 @@ public static int share(ServerCommandSource source, String filename) {
}

@Override
public void onInitializeServer() {
public void onInitialize() {
Optional<ModContainer> mclogs = FabricLoader.getInstance().getModContainer("mclogs");
client.setProjectVersion(mclogs.isPresent() ? mclogs.get().getMetadata().getVersion().getFriendlyString() : "unknown");
}

@Override
public void onInitializeServer() {
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
CommandMclogs.register(dispatcher);
CommandMclogsList.register(dispatcher);
CommandMclogsShare.register(dispatcher);
if (!dedicated) {
return;
}

logger.info("Registering server commands");
LiteralArgumentBuilder<ServerCommandSource> mclogs = CommandManager.literal("mclogs");
for (Command command : COMMANDS) {
dispatcher.register(command.buildServer(mclogs));
}
});
}

@Override
public void onInitializeClient() {
logger.info("Registering client commands");
LiteralArgumentBuilder<FabricClientCommandSource> mclogsc = ClientCommandManager.literal("mclogsc");
for (Command command : COMMANDS) {
ClientCommandManager.DISPATCHER.register(command.buildClient(mclogsc));
}
}

}
10 changes: 10 additions & 0 deletions src/main/java/gs/mclo/fabric/commands/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gs.mclo.fabric.commands;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
import net.minecraft.server.command.ServerCommandSource;

public interface Command {
LiteralArgumentBuilder<FabricClientCommandSource> buildClient(LiteralArgumentBuilder<FabricClientCommandSource> builder);
LiteralArgumentBuilder<ServerCommandSource> buildServer(LiteralArgumentBuilder<ServerCommandSource> builder);
}
23 changes: 23 additions & 0 deletions src/main/java/gs/mclo/fabric/commands/MclogsCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gs.mclo.fabric.commands;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import gs.mclo.fabric.MclogsFabric;
import gs.mclo.fabric.commands.source.ClientSource;
import gs.mclo.fabric.commands.source.ServerSource;
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
import net.minecraft.server.command.ServerCommandSource;


public class MclogsCommand implements Command {
@Override
public LiteralArgumentBuilder<FabricClientCommandSource> buildClient(LiteralArgumentBuilder<FabricClientCommandSource> builder) {
return builder.executes(context -> MclogsFabric.share(new ClientSource(context.getSource()), "latest.log"));
}

@Override
public LiteralArgumentBuilder<ServerCommandSource> buildServer(LiteralArgumentBuilder<ServerCommandSource> builder) {
return builder
.requires(source -> source.hasPermissionLevel(2))
.executes(context -> MclogsFabric.share(new ServerSource(context.getSource()), "latest.log"));
}
}
Loading

0 comments on commit 0d7dd25

Please sign in to comment.