9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/patches/server/0033-Chat-Image-protocol.patch
2024-12-13 21:19:57 -05:00

199 lines
8.8 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: HaHaWTH <fsjk947@gmail.com>
Date: Wed, 13 Mar 2024 18:11:10 +0800
Subject: [PATCH] Chat Image protocol
This patch is Powered by ChatImage (https://github.com/kitUIN/ChatImage)
diff --git a/src/main/java/org/dreeam/leaf/config/modules/network/ProtocolSupport.java b/src/main/java/org/dreeam/leaf/config/modules/network/ProtocolSupport.java
index db48291d191e1eefb7e497dda7430fc1644bb5ea..4c38648af0a6f258c1618da7c162c44795ae36d6 100644
--- a/src/main/java/org/dreeam/leaf/config/modules/network/ProtocolSupport.java
+++ b/src/main/java/org/dreeam/leaf/config/modules/network/ProtocolSupport.java
@@ -14,6 +14,7 @@ public class ProtocolSupport extends ConfigModules {
public static boolean jadeProtocol = false;
public static boolean appleskinProtocol = false;
public static int appleskinSyncTickInterval = 20;
+ public static boolean chatImageProtocol = false;
public static boolean xaeroMapProtocol = false;
public static int xaeroMapServerID = new Random().nextInt();
public static boolean syncmaticaProtocol = false;
@@ -25,6 +26,7 @@ public class ProtocolSupport extends ConfigModules {
jadeProtocol = config.getBoolean(getBasePath() + ".jade-protocol", jadeProtocol);
appleskinProtocol = config.getBoolean(getBasePath() + ".appleskin-protocol", appleskinProtocol);
appleskinSyncTickInterval = config.getInt(getBasePath() + ".appleskin-protocol-sync-tick-interval", appleskinSyncTickInterval);
+ chatImageProtocol = config.getBoolean(getBasePath() + ".chatimage-protocol", chatImageProtocol);
xaeroMapProtocol = config.getBoolean(getBasePath() + ".xaero-map-protocol", xaeroMapProtocol);
xaeroMapServerID = config.getInt(getBasePath() + ".xaero-map-server-id", xaeroMapServerID);
syncmaticaProtocol = config.getBoolean(getBasePath() + ".syncmatica-protocol", syncmaticaProtocol);
diff --git a/src/main/java/org/leavesmc/leaves/protocol/ChatImageProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/ChatImageProtocol.java
new file mode 100644
index 0000000000000000000000000000000000000000..dd652437c0e999f0b523b69bca8f5803611ead6c
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/protocol/ChatImageProtocol.java
@@ -0,0 +1,143 @@
+package org.leavesmc.leaves.protocol;
+
+import com.google.common.collect.Lists;
+import com.google.gson.Gson;
+import net.minecraft.network.FriendlyByteBuf;
+import net.minecraft.network.protocol.Packet;
+import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
+import net.minecraft.resources.ResourceLocation;
+import net.minecraft.server.level.ServerPlayer;
+import org.dreeam.leaf.config.modules.network.ProtocolSupport;
+import org.jetbrains.annotations.Contract;
+import org.jetbrains.annotations.NotNull;
+import org.leavesmc.leaves.protocol.chatimage.ChatImageIndex;
+import org.leavesmc.leaves.protocol.core.LeavesCustomPayload;
+import org.leavesmc.leaves.protocol.core.LeavesProtocol;
+import org.leavesmc.leaves.protocol.core.LeavesProtocolManager;
+import org.leavesmc.leaves.protocol.core.ProtocolHandler;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+@LeavesProtocol(namespace = "chatimage")
+public class ChatImageProtocol {
+
+ public static final String PROTOCOL_ID = "chatimage";
+ private static final Map<String, HashMap<Integer, String>> SERVER_BLOCK_CACHE = new HashMap<>();
+ private static final Map<String, Integer> FILE_COUNT_MAP = new HashMap<>();
+ private static final Map<String, List<String>> USER_CACHE_MAP = new HashMap<>();
+ public static int MAX_STRING = 532767;
+ private static final Gson gson = new Gson();
+
+ public record FileInfoChannelPacket(
+ String message) implements LeavesCustomPayload<LeavesProtocolManager.LeavesPayload> {
+ private static final ResourceLocation FILE_INFO = ChatImageProtocol.id("file_info");
+
+ @New
+ public FileInfoChannelPacket(ResourceLocation id, FriendlyByteBuf buffer) {
+ this(buffer.readUtf(MAX_STRING));
+ }
+
+ @Override
+ public void write(final FriendlyByteBuf buffer) {
+ buffer.writeUtf(message(), MAX_STRING);
+ }
+
+ @Override
+ public @NotNull ResourceLocation id() {
+ return FILE_INFO;
+ }
+ }
+
+ public record DownloadFileChannelPacket(
+ String message) implements LeavesCustomPayload<LeavesProtocolManager.LeavesPayload> {
+ private static final ResourceLocation DOWNLOAD_FILE_CHANNEL = ChatImageProtocol.id("download_file_channel");
+
+ @New
+ public DownloadFileChannelPacket(ResourceLocation id, FriendlyByteBuf buffer) {
+ this(buffer.readUtf(MAX_STRING));
+ }
+
+ @Override
+ public void write(final FriendlyByteBuf buffer) {
+ buffer.writeUtf(message(), MAX_STRING);
+ }
+
+ @Override
+ public @NotNull ResourceLocation id() {
+ return DOWNLOAD_FILE_CHANNEL;
+ }
+
+ }
+
+ public record FileChannelPacket(
+ String message) implements LeavesCustomPayload<LeavesProtocolManager.LeavesPayload> {
+ private static final ResourceLocation FILE_CHANNEL = ChatImageProtocol.id("file_channel");
+
+ @New
+ public FileChannelPacket(ResourceLocation id, FriendlyByteBuf buffer) {
+ this(buffer.readUtf(MAX_STRING));
+ }
+
+ @Override
+ public void write(final FriendlyByteBuf buffer) {
+ buffer.writeUtf(message(), MAX_STRING);
+ }
+
+ @Override
+ public @NotNull ResourceLocation id() {
+ return FILE_CHANNEL;
+ }
+
+ }
+
+ @ProtocolHandler.PayloadReceiver(payload = FileChannelPacket.class, payloadId = "file_channel")
+ public static void serverFileChannelReceived(ServerPlayer player, String res) {
+ ChatImageIndex title = gson.fromJson(res, ChatImageIndex.class);
+ HashMap<Integer, String> blocks = SERVER_BLOCK_CACHE.containsKey(title.url) ? SERVER_BLOCK_CACHE.get(title.url) : new HashMap<>();
+ blocks.put(title.index, res);
+ SERVER_BLOCK_CACHE.put(title.url, blocks);
+ FILE_COUNT_MAP.put(title.url, title.total);
+ if (title.total == blocks.size()) {
+ if (USER_CACHE_MAP.containsKey(title.url)) {
+ List<String> names = USER_CACHE_MAP.get(title.url);
+ for (String uuid : names) {
+ ServerPlayer serverPlayer = player.server.getPlayerList().getPlayer(UUID.fromString(uuid));
+ if (serverPlayer != null) {
+ sendToPlayer(new FileInfoChannelPacket("true->" + title.url), serverPlayer);
+ }
+ }
+ USER_CACHE_MAP.put(title.url, Lists.newArrayList());
+ }
+ }
+ }
+
+ @ProtocolHandler.PayloadReceiver(payload = FileInfoChannelPacket.class, payloadId = "file_info")
+ public static void serverFileInfoChannelReceived(ServerPlayer player, String url) {
+ if (SERVER_BLOCK_CACHE.containsKey(url) && FILE_COUNT_MAP.containsKey(url)) {
+ HashMap<Integer, String> list = SERVER_BLOCK_CACHE.get(url);
+ Integer total = FILE_COUNT_MAP.get(url);
+ if (total == list.size()) {
+ for (Map.Entry<Integer, String> entry : list.entrySet()) {
+ sendToPlayer(new DownloadFileChannelPacket(entry.getValue()), player);
+ }
+ return;
+ }
+ }
+ sendToPlayer(new FileInfoChannelPacket("null->" + url), player);
+ List<String> names = USER_CACHE_MAP.containsKey(url) ? USER_CACHE_MAP.get(url) : Lists.newArrayList();
+ names.add(player.getStringUUID());
+ USER_CACHE_MAP.put(url, names);
+ }
+
+ @Contract("_ -> new")
+ public static @NotNull ResourceLocation id(String path) {
+ return ResourceLocation.fromNamespaceAndPath(PROTOCOL_ID, path);
+ }
+
+ public static void sendToPlayer(CustomPacketPayload payload, ServerPlayer player) {
+ player.connection.send((Packet<?>) payload);
+ }
+}
diff --git a/src/main/java/org/leavesmc/leaves/protocol/chatimage/ChatImageIndex.java b/src/main/java/org/leavesmc/leaves/protocol/chatimage/ChatImageIndex.java
new file mode 100644
index 0000000000000000000000000000000000000000..e4b55c5b606fcb92bf035910e82d9f32606d1c2b
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/protocol/chatimage/ChatImageIndex.java
@@ -0,0 +1,16 @@
+package org.leavesmc.leaves.protocol.chatimage;
+
+public class ChatImageIndex {
+
+ public int index;
+ public int total;
+ public String url;
+ public String bytes;
+
+ public ChatImageIndex(int index, int total, String url, String bytes) {
+ this.index = index;
+ this.total = total;
+ this.url = url;
+ this.bytes = bytes;
+ }
+}