Skip to content

Instantly share code, notes, and snippets.

@Densamisten
Created March 3, 2024 08:16
Show Gist options
  • Select an option

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

Select an option

Save Densamisten/c004dff140818c86730ad6f747c1b7a0 to your computer and use it in GitHub Desktop.
Copies the clipboard contents onto the clipboard in MinecraftForge! Usage: /clipboard
package net.kaupenjoe.mccourse.command;
import com.mojang.blaze3d.platform.ClipboardManager;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.client.Minecraft;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
public class ClipboardCommand {
public ClipboardCommand(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("clipboard")
.then(Commands.argument("text", StringArgumentType.string())
.executes(context -> copyToClipboard(context.getSource(), StringArgumentType.getString(context, "text")))
)
);
}
private static int copyToClipboard(CommandSourceStack source, String text) {
// Create an instance of ClipboardManager
ClipboardManager clipboard = new ClipboardManager();
// Get the GLFW window handle (assuming it's available)
long windowHandle = Minecraft.getInstance().getWindow().getWindow();
// Set the clipboard content
clipboard.setClipboard(windowHandle, text);
source.sendSuccess(() -> Component.literal("Text copied to clipboard: " + text), true);
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment