Skip to content

Instantly share code, notes, and snippets.

@Densamisten
Forked from Ensamisten/WriteBookCommand.java
Last active March 23, 2024 11:18
Show Gist options
  • Select an option

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

Select an option

Save Densamisten/d2332b93d6777c629fcc856cf541e6a2 to your computer and use it in GitHub Desktop.
Usage: Get a Book and Quill and send the command to the chat: /book write, or /book read. Works in Fabric and Forge 1.20.4
package io.github.densamisten.command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.client.util.Clipboard;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
public class FabrientBookCommand {
private static final Clipboard clipboardManager = new Clipboard();
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(
CommandManager.literal("book")
.then(CommandManager.literal("write")
.then(CommandManager.literal("nbt")
.executes(BookCommand::writeNbt))
.then(CommandManager.literal("text")
.executes(BookCommand::writeText)))
.then(CommandManager.literal("read")
.then(CommandManager.literal("nbt")
.executes(BookCommand::readNbt))
.then(CommandManager.literal("text")
.executes(BookCommand::readText))));
}
private static int writeText(CommandContext<ServerCommandSource> context) {
PlayerEntity player = context.getSource().getPlayer();
ItemStack heldItem = player.getMainHandStack();
// Check if the held item is a writable book
if (heldItem.getItem() == Items.WRITABLE_BOOK) {
String clipboardText = clipboardManager.getClipboard(GLFW.glfwGetCurrentContext(), GLFWErrorCallback.createPrint(System.err));
// Check if clipboard has text
if (clipboardText.isEmpty()) {
context.getSource().sendError(Text.literal("Clipboard is empty!"));
return 0;
}
NbtList pages = new NbtList();
StringBuilder currentPage = new StringBuilder();
// Iterate over clipboard contents and write them to book pages
for (int i = 0; i < clipboardText.length(); i++) {
// Append character to current page
currentPage.append(clipboardText.charAt(i));
// Check if the current page exceeds the character limit or if all clipboard content has been processed
if (currentPage.length() >= 256 || i == clipboardText.length() - 1) {
// Add current page to the list of pages
pages.add(NbtString.of(currentPage.toString()));
// Reset current page
currentPage = new StringBuilder();
}
}
// Add pages to the book
writeTextToBook(heldItem, pages);
context.getSource().sendMessage(Text.literal("Clipboard contents written to the book!"));
return 1;
} else {
context.getSource().sendError(Text.literal("You need to hold a writable book in your hand!"));
return 0;
}
}
private static void writeTextToBook(ItemStack book, NbtList pages) {
NbtCompound bookTag = book.getOrCreateNbt();
bookTag.put("pages", pages);
}
private static int writeNbt(CommandContext<ServerCommandSource> context) {
PlayerEntity player = context.getSource().getPlayer();
ItemStack heldItem = player.getMainHandStack();
// Check if the held item is a writable book
if (heldItem.getItem() == Items.WRITABLE_BOOK) {
String clipboardText = clipboardManager.getClipboard(GLFW.glfwGetCurrentContext(), GLFWErrorCallback.createPrint(System.err));
// Check if clipboard has text
if (clipboardText.isEmpty()) {
context.getSource().sendError(Text.literal("Clipboard is empty!"));
return 0;
}
// Add clipboard text as raw data to the book
NbtCompound bookTag = heldItem.getOrCreateNbt();
bookTag.putString("raw_data", clipboardText);
context.getSource().sendMessage(Text.literal("Clipboard contents written to the book!"));
return 1;
} else {
context.getSource().sendError(Text.literal("You need to hold a writable book in your hand!"));
return 0;
}
}
private static int readNbt(CommandContext<ServerCommandSource> context) {
PlayerEntity player = context.getSource().getPlayer();
ItemStack heldItem = player.getMainHandStack();
if (heldItem.getItem() == Items.WRITABLE_BOOK || heldItem.getItem() == Items.WRITTEN_BOOK) {
NbtCompound bookTag = heldItem.getNbt();
if (bookTag != null && bookTag.contains("raw_data")) {
String rawData = bookTag.getString("raw_data");
clipboardManager.setClipboard(GLFW.glfwGetCurrentContext(), rawData);
context.getSource().sendMessage(Text.literal("Clipboard contents read from the book!"));
return 1;
} else {
context.getSource().sendError(Text.literal("The book does not contain raw data!"));
return 0;
}
} else {
context.getSource().sendError(Text.literal("You need to hold a written book in your hand!"));
return 0;
}
}
private static int readText(CommandContext<ServerCommandSource> context) {
PlayerEntity player = context.getSource().getPlayer();
ItemStack heldItem = player.getMainHandStack();
if (heldItem.getItem() == Items.WRITABLE_BOOK || heldItem.getItem() == Items.WRITTEN_BOOK) {
NbtCompound bookTag = heldItem.getNbt();
if (bookTag != null && bookTag.contains("pages")) {
NbtList pagesTag = bookTag.getList("pages", 8); // Assuming it's a ListTag of strings
StringBuilder textBuilder = new StringBuilder();
for (int i = 0; i < pagesTag.size(); i++) {
textBuilder.append(pagesTag.getString(i)).append("\n");
}
String text = textBuilder.toString().trim();
clipboardManager.setClipboard(GLFW.glfwGetCurrentContext(), text);
return 1;
} else {
context.getSource().sendError(Text.literal("The book is empty!"));
return 0;
}
} else {
context.getSource().sendError(Text.literal("You need to hold a written book in your hand!"));
return 0;
}
}
}
package io.github.densamisten.command;
import com.mojang.blaze3d.platform.ClipboardManager;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
public class BookCommand {
private static final ClipboardManager clipboardManager = new ClipboardManager();
public BookCommand(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal("book")
.then(Commands.literal("write")
.then(Commands.literal("nbt")
.executes(BookCommand::writeNbt))
.then(Commands.literal("text")
.executes(BookCommand::writeText)))
.then(Commands.literal("read")
.then(Commands.literal("nbt")
.executes(BookCommand::readNbt))
.then(Commands.literal("text")
.executes(BookCommand::readText)))
.then(Commands.literal("copy")
.then(Commands.argument("target", EntityArgument.player())
.executes(context -> copyBook(context, EntityArgument.getPlayer(context, "target"))))));
}
private static int writeText(CommandContext<CommandSourceStack> context) {
Player player = context.getSource().getPlayer();
ItemStack heldItem = player != null ? player.getMainHandItem() : null;
// Check if the held item is a writable book
if (heldItem.getItem() == Items.WRITABLE_BOOK) {
String clipboardText = clipboardManager.getClipboard(GLFW.glfwGetCurrentContext(), GLFWErrorCallback.createPrint(System.err));
// Check if clipboard has text
if (clipboardText.isEmpty()) {
context.getSource().sendFailure(Component.literal("Clipboard is empty!"));
return 0;
}
ListTag pages = new ListTag();
StringBuilder currentPage = new StringBuilder();
// Iterate over clipboard contents and write them to book pages
for (int i = 0; i < clipboardText.length(); i++) {
// Append character to current page
currentPage.append(clipboardText.charAt(i));
// Check if the current page exceeds the character limit or if all clipboard content has been processed
if (currentPage.length() >= 256 || i == clipboardText.length() - 1) {
// Add current page to the list of pages
pages.add(StringTag.valueOf(currentPage.toString()));
// Reset current page
currentPage = new StringBuilder();
}
}
// Add pages to the book
writeTextToBook(heldItem, pages);
context.getSource().sendSuccess(() -> Component.literal("Clipboard contents written to the book!"), false);
return 1;
} else {
context.getSource().sendFailure(Component.literal("You need to hold a writable book in your hand!"));
return 0;
}
}
private static void writeTextToBook(ItemStack book, ListTag pages) {
CompoundTag bookTag = book.getOrCreateTag();
bookTag.put("pages", pages);
}
private static int writeNbt(CommandContext<CommandSourceStack> context) {
Player player = context.getSource().getPlayer();
ItemStack heldItem = player.getMainHandItem();
// Check if the held item is a writable book
if (heldItem.getItem() == Items.WRITABLE_BOOK) {
String clipboardText = clipboardManager.getClipboard(GLFW.glfwGetCurrentContext(), GLFWErrorCallback.createPrint(System.err));
// Check if clipboard has text
if (clipboardText.isEmpty()) {
context.getSource().sendFailure(Component.literal("Clipboard is empty!"));
return 0;
}
// Add clipboard text as raw data to the book
CompoundTag bookTag = heldItem.getOrCreateTag();
bookTag.putString("raw_data", clipboardText);
context.getSource().sendSuccess(() -> Component.literal("Clipboard contents written to the book!"), false);
return 1;
} else {
context.getSource().sendFailure(Component.literal("You need to hold a writable book in your hand!"));
return 0;
}
}
private static int readNbt(CommandContext<CommandSourceStack> context) {
Player player = context.getSource().getPlayer();
ItemStack heldItem = player.getMainHandItem();
if (heldItem.getItem() == Items.WRITABLE_BOOK || heldItem.getItem() == Items.WRITTEN_BOOK) {
CompoundTag bookTag = heldItem.getTag();
if (bookTag != null && bookTag.contains("raw_data")) {
String rawData = bookTag.getString("raw_data");
clipboardManager.setClipboard(GLFW.glfwGetCurrentContext(), rawData);
context.getSource().sendSuccess(() -> Component.literal("Clipboard contents read from the book!"), false);
return 1;
} else {
context.getSource().sendFailure(Component.literal("The book does not contain raw data!"));
return 0;
}
} else {
context.getSource().sendFailure(Component.literal("You need to hold a written book in your hand!"));
return 0;
}
}
private static int readText(CommandContext<CommandSourceStack> context) {
Player player = context.getSource().getPlayer();
ItemStack heldItem = player.getMainHandItem();
if (heldItem.getItem() == Items.WRITABLE_BOOK || heldItem.getItem() == Items.WRITTEN_BOOK) {
CompoundTag bookTag = heldItem.getTag();
if (bookTag != null && bookTag.contains("pages")) {
ListTag pagesTag = bookTag.getList("pages", 8); // Assuming it's a ListTag of strings
StringBuilder textBuilder = new StringBuilder();
for (int i = 0; i < pagesTag.size(); i++) {
textBuilder.append(pagesTag.getString(i)).append("\n");
}
String text = textBuilder.toString().trim();
clipboardManager.setClipboard(GLFW.glfwGetCurrentContext(), text);
return 1;
} else {
context.getSource().sendFailure(Component.literal("The book is empty!"));
return 0;
}
} else {
context.getSource().sendFailure(Component.literal("You need to hold a written book in your hand!"));
return 0;
}
}
private static int copyBook(CommandContext<CommandSourceStack> context, ServerPlayer targetPlayer) throws CommandSyntaxException {
CommandSourceStack sourceStack = context.getSource();
ServerPlayer sourcePlayer = sourceStack.getPlayerOrException();
// Check if the source player is holding a book
ItemStack heldItem = sourcePlayer.getMainHandItem();
if (!(heldItem.getItem() instanceof WritableBookItem || heldItem.getItem() instanceof WrittenBookItem)) {
sourceStack.sendFailure(Component.literal("You must be holding a written or writable book to copy."));
return 0;
}
// Copy the book to the target player's inventory
ItemStack copiedBook = heldItem.copy();
copiedBook.setCount(1); // Ensure only one copy is added
// If the book is written, get its title and print it
if (heldItem.getItem() instanceof WrittenBookItem) {
CompoundTag bookTag = heldItem.getTag();
if (bookTag != null && bookTag.contains("title")) {
String title = bookTag.getString("title");
sourceStack.sendSuccess(() -> Component.literal("Copied book \"" + title + "\" to " + targetPlayer.getName().getString() + "'s inventory."), true);
targetPlayer.displayClientMessage(Component.literal("You received a copied book \"" + title + "\" from " + sourcePlayer.getName().getString() + "."), false);
}
}
Inventory targetInventory = targetPlayer.getInventory();
targetInventory.add(copiedBook);
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment