Skip to content

Instantly share code, notes, and snippets.

@Densamisten
Last active February 21, 2024 09:42
Show Gist options
  • Select an option

  • Save Densamisten/4ae3a7acb76fef18e9abf2de7a4b6cdd to your computer and use it in GitHub Desktop.

Select an option

Save Densamisten/4ae3a7acb76fef18e9abf2de7a4b6cdd to your computer and use it in GitHub Desktop.

License: CC0-1.0

Usage

/info [:file_folder:] :file_folder:

Where is a file location of an absolute path.

package io.github.densamisten.command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class CopyRawDataCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager.literal("copyraw")
.then(CommandManager.argument("file", StringArgumentType.string())
.executes(CopyRawDataCommand::copyRawData)
)
);
}
private static int copyRawData(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
String filePath = context.getArgument("file", String.class);
try {
byte[] rawData = Files.readAllBytes(Path.of(filePath));
context.getSource().sendFeedback(() -> Text.of("Raw data copied to clipboard."), false);
GLFW.glfwSetClipboardString(GLFW.glfwGetCurrentContext(), bytesToHexString(rawData));
} catch (IOException e) {
context.getSource().sendError(Text.of("Failed to read file: " + e.getMessage()));
}
return 1;
}
private static String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}
}
package io.github.densamisten.command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ContentTypeCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager.literal("info")
.then(CommandManager.argument("file", StringArgumentType.string())
.executes(RawFileCopyCommand::copyRawData)
)
);
}
private static int copyRawData(CommandContext<ServerCommandSource> context) {
String filePath = context.getArgument("file", String.class);
try {
String rawData = Files.probeContentType(Paths.get(filePath));
context.getSource().sendFeedback(() -> Text.of("Raw data copied to clipboard."), false);
GLFW.glfwSetClipboardString(GLFW.glfwGetCurrentContext(), rawData);
} catch (IOException e) {
context.getSource().sendError(Text.of("Failed to read file: " + e.getMessage()));
}
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment