From 047848ea2d4e333a3c4ea91ea384dc5f055fdf18 Mon Sep 17 00:00:00 2001 From: RyanLand Date: Thu, 18 Jan 2024 15:17:51 +0100 Subject: [PATCH] Add /templatedata command + some minor improvements to TemplateUtil --- .../recode/mod/commands/CommandHandler.java | 2 + .../item/template/TemplateDataCommand.java | 61 +++++++++++++++++++ .../sys/hypercube/templates/TemplateUtil.java | 26 ++++++-- 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 src/main/java/io/github/homchom/recode/mod/commands/impl/item/template/TemplateDataCommand.java diff --git a/src/main/java/io/github/homchom/recode/mod/commands/CommandHandler.java b/src/main/java/io/github/homchom/recode/mod/commands/CommandHandler.java index fddbf4a70..55194e2c2 100644 --- a/src/main/java/io/github/homchom/recode/mod/commands/CommandHandler.java +++ b/src/main/java/io/github/homchom/recode/mod/commands/CommandHandler.java @@ -5,6 +5,7 @@ import io.github.homchom.recode.mod.commands.impl.image.ImageParticleCommand; import io.github.homchom.recode.mod.commands.impl.item.*; import io.github.homchom.recode.mod.commands.impl.item.template.SendTemplateCommand; +import io.github.homchom.recode.mod.commands.impl.item.template.TemplateDataCommand; import io.github.homchom.recode.mod.commands.impl.item.template.WebviewCommand; import io.github.homchom.recode.mod.commands.impl.other.*; import io.github.homchom.recode.mod.commands.impl.text.*; @@ -57,6 +58,7 @@ public static void load(CommandDispatcher dispatcher, new ImageHologramCommand(), new ImageParticleCommand(), new SendTemplateCommand(), + new TemplateDataCommand(), new PJoinCommand(), //new NBSSearchCommand(), //new CodeVaultCommand(), diff --git a/src/main/java/io/github/homchom/recode/mod/commands/impl/item/template/TemplateDataCommand.java b/src/main/java/io/github/homchom/recode/mod/commands/impl/item/template/TemplateDataCommand.java new file mode 100644 index 000000000..064275299 --- /dev/null +++ b/src/main/java/io/github/homchom/recode/mod/commands/impl/item/template/TemplateDataCommand.java @@ -0,0 +1,61 @@ +package io.github.homchom.recode.mod.commands.impl.item.template; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import io.github.homchom.recode.sys.hypercube.templates.TemplateUtil; +import io.github.homchom.recode.sys.networking.websocket.SocketHandler; +import io.github.homchom.recode.sys.player.chat.ChatType; +import io.github.homchom.recode.sys.player.chat.ChatUtil; +import io.github.homchom.recode.sys.sidedchat.ChatPattern; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.components.ChatComponent; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.ClickEvent; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.HoverEvent; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.world.item.ItemStack; + +public class TemplateDataCommand extends AbstractTemplateCommand { + @Override + public String getDescription() { + return "[blue]/templatedata[reset]\n" + + "\n" + + "Prints the raw data of the code template in your main hand," + + "allowing you to copy it"; + } + + @Override + public String getName() { + return "/templatedata"; + } + + @Override + protected String getCmdName() { + return "templatedata"; + } + + @Override + protected void withTemplate(ItemStack stack) { + JsonObject nbt = TemplateUtil.read(stack); + String compressed = nbt.get("code").getAsString(); + String json = TemplateUtil.dataToJson(compressed).toString(); + + MutableComponent component1 = Component.literal("Click to copy compressed code data: §8" + truncateString(compressed)) + .withStyle(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal("§7Click to copy!"))) + .withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/copytxt " + compressed))); + MutableComponent component2 = Component.literal("Click to copy raw JSON code data: §8" + truncateString(json)) + .withStyle(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal("§7Click to copy!"))) + .withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/copytxt " + json))); + + ChatUtil.sendMessage(component1, ChatType.INFO_BLUE); + ChatUtil.sendMessage(component2, ChatType.INFO_BLUE); + Minecraft.getInstance().player.playSound(SoundEvents.ARMOR_EQUIP_ELYTRA); + } + + private String truncateString(String input) { + if (input.length() < 400) return input; + return input.substring(0, 400) + "§7..."; + } +} diff --git a/src/main/java/io/github/homchom/recode/sys/hypercube/templates/TemplateUtil.java b/src/main/java/io/github/homchom/recode/sys/hypercube/templates/TemplateUtil.java index 0ea281659..22471bf4c 100644 --- a/src/main/java/io/github/homchom/recode/sys/hypercube/templates/TemplateUtil.java +++ b/src/main/java/io/github/homchom/recode/sys/hypercube/templates/TemplateUtil.java @@ -36,16 +36,34 @@ public static void applyRawTemplateNBT(ItemStack stack, Component name, String a stack.setHoverName(name); } + /** + * Takes raw JSON code data and turns it into raw compressed (gzip+base64) code data. + */ + public static String jsonToData(String json) { + try { + byte[] b64 = CompressionUtil.toBase64(CompressionUtil.toGZIP(json.getBytes(StandardCharsets.UTF_8))); + return new String(b64); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } - public static void compressTemplateNBT(ItemStack stack, String name, String author, String template) { + /** + * Takes raw compressed (gzip+base64) code data and turns it into raw JSON code data. + */ + public static JsonObject dataToJson(String code) { try { - byte[] b64 = CompressionUtil.toBase64(CompressionUtil.toGZIP(template.getBytes(StandardCharsets.UTF_8))); - String exported = new String(b64); - applyRawTemplateNBT(stack, name, author, exported); + byte[] bytes = CompressionUtil.fromGZIP(CompressionUtil.fromBase64(code.getBytes())); + return JsonParser.parseString(new String(bytes)).getAsJsonObject(); } catch (IOException e) { e.printStackTrace(); } + return null; + } + public static void compressTemplateNBT(ItemStack stack, String name, String author, String template) { + applyRawTemplateNBT(stack, name, author, jsonToData(template)); } public static JsonObject read(ItemStack stack) {