Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -57,6 +58,7 @@ public static void load(CommandDispatcher<FabricClientCommandSource> dispatcher,
new ImageHologramCommand(),
new ImageParticleCommand(),
new SendTemplateCommand(),
new TemplateDataCommand(),
new PJoinCommand(),
//new NBSSearchCommand(),
//new CodeVaultCommand(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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...";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down