From c83c164006765ac4d4487663068d8ef6e0d00c83 Mon Sep 17 00:00:00 2001 From: kitUIN Date: Wed, 27 Nov 2024 17:13:24 +0800 Subject: [PATCH] :sparkles: try neoforge-1.21.0 --- neoforge/origin/gradle.properties | 12 +- .../io/github/kituin/chatimage/ChatImage.java | 50 ++++-- .../chatimage/network/ChatImagePacket.java | 67 ++++++- .../network/DownloadFileChannelHandler.java | 5 +- .../network/DownloadFileChannelPacket.java | 43 +++-- .../chatimage/network/FileChannelHandler.java | 10 +- .../chatimage/network/FileChannelPacket.java | 42 +++-- .../network/FileInfoChannelHandler.java | 13 +- .../network/FileInfoChannelPacket.java | 164 +++++++++--------- 9 files changed, 265 insertions(+), 141 deletions(-) diff --git a/neoforge/origin/gradle.properties b/neoforge/origin/gradle.properties index 29912390..6483b9e3 100644 --- a/neoforge/origin/gradle.properties +++ b/neoforge/origin/gradle.properties @@ -2,25 +2,35 @@ org.gradle.daemon=false org.gradle.debug=false -targetJavaVersion=17 + # IF neoforge-1.20.2 +#targetJavaVersion=17 #minecraft_version=1.20.2 #minecraft_version_range=[1.20.2,1.21) #neo_version=20.2.88 #neo_version_range=[20.2,) #loader_version_range=[1,) # ELSE IF neoforge-1.20.3 +#targetJavaVersion=17 #minecraft_version=1.20.3 #minecraft_version_range=[1.20.3,1.21) #neo_version=20.3.8-beta #neo_version_range=[20,) #loader_version_range=[1,) # ELSE IF neoforge-1.20.4 +#targetJavaVersion=17 #minecraft_version=1.20.4 #minecraft_version_range=[1.20.4,1.21) #neo_version=20.4.237 #neo_version_range=[20.4,) #loader_version_range=[2,) +# ELSE IF neoforge-1.21.0 +#targetJavaVersion=21 +#minecraft_version=1.21.0 +#minecraft_version_range=[1.21.0,1.22) +#neo_version=21.0.167 +#neo_version_range=[21,) +#loader_version_range=[4,) # END IF # Uncomment this to activate parchment #neogradle.subsystems.parchment.minecraftVersion=1.20.2 diff --git a/neoforge/origin/src/main/java/io/github/kituin/chatimage/ChatImage.java b/neoforge/origin/src/main/java/io/github/kituin/chatimage/ChatImage.java index 00137c26..7acdf541 100644 --- a/neoforge/origin/src/main/java/io/github/kituin/chatimage/ChatImage.java +++ b/neoforge/origin/src/main/java/io/github/kituin/chatimage/ChatImage.java @@ -10,6 +10,7 @@ import io.github.kituin.chatimage.integration.ChatImageClientAdapter; import io.github.kituin.chatimage.integration.ChatImageLogger; import io.github.kituin.chatimage.network.*; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.neoforged.neoforge.client.event.RegisterClientCommandsEvent; import io.github.kituin.ChatImageCode.ChatImageCodeInstance; import io.github.kituin.ChatImageCode.ChatImageConfig; @@ -30,14 +31,14 @@ import net.neoforged.neoforge.event.RegisterCommandsEvent; import net.neoforged.neoforge.event.server.ServerStartingEvent; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; // IF <= neoforge-1.20.3 //import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; -// ELSE -//import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent; -//import net.neoforged.neoforge.network.registration.IPayloadRegistrar; +// ELSE IF >= neoforge-1.21.0 +// import net.neoforged.neoforge.network.handling.DirectionalPayloadHandler; // END IF import java.io.File; @@ -75,15 +76,39 @@ public ChatImage(IEventBus modEventBus) { // LOGGER.info("[ChatImage]Channel Register"); // } // ELSE -// public static void register(final RegisterPayloadHandlerEvent event) { -// final IPayloadRegistrar registrar = event.registrar(MOD_ID).optional(); -// registrar.play(DownloadFileChannelPacket.DOWNLOAD_FILE_CHANNEL, DownloadFileChannelPacket::new, handler -> handler -// .client(DownloadFileChannelHandler.getInstance()::clientHandle)); -// registrar.play(FileChannelPacket.FILE_CHANNEL, FileChannelPacket::new, handler -> handler -// .server(FileChannelHandler.getInstance()::serverHandle)); -// registrar.play(FileInfoChannelPacket.FILE_INFO, FileInfoChannelPacket::new, handler -> handler -// .client(FileInfoChannelHandler.getInstance()::clientHandle) -// .server(FileInfoChannelHandler.getInstance()::serverHandle)); +// public static void register(final #RegisterPayloadHandlerEvent# event) { +// final #IPayloadRegistrar# registrar = event.registrar(MOD_ID).optional(); + // IF >= neoforge-1.21.0 +// registrar.playToClient(DownloadFileChannelPacket.TYPE, +// DownloadFileChannelPacket.CODEC, +// new DirectionalPayloadHandler<>( +// DownloadFileChannelHandler.getInstance()::clientHandle, +// null +// ) +// ); +// registrar.playToServer(FileChannelPacket.TYPE, +// FileChannelPacket.CODEC, +// new DirectionalPayloadHandler<>( +// null, +// FileChannelHandler.getInstance()::serverHandle +// ) +// ); +// registrar.playBidirectional(FileInfoChannelPacket.TYPE, +// FileInfoChannelPacket.CODEC, +// new DirectionalPayloadHandler<>( +// FileInfoChannelHandler.getInstance()::clientHandle, +// FileInfoChannelHandler.getInstance()::serverHandle +// ) +// ); +// ELSE +// registrar.play(DownloadFileChannelPacket.DOWNLOAD_FILE_CHANNEL, DownloadFileChannelPacket::new, handler -> handler +// .client(DownloadFileChannelHandler.getInstance()::clientHandle)); +// registrar.play(FileChannelPacket.FILE_CHANNEL, FileChannelPacket::new, handler -> handler +// .server(FileChannelHandler.getInstance()::serverHandle)); +// registrar.play(FileInfoChannelPacket.FILE_INFO, FileInfoChannelPacket::new, handler -> handler +// .client(FileInfoChannelHandler.getInstance()::clientHandle) +// .server(FileInfoChannelHandler.getInstance()::serverHandle)); +// END IF // } // END IF @@ -112,6 +137,7 @@ public static void onClientSetup(FMLClientSetupEvent event) { LOGGER.info("[ChatImage]Client start"); ModLoadingContext.get().registerExtensionPoint(ConfigScreenHandler.ConfigScreenFactory.class, () -> new ConfigScreenHandler.ConfigScreenFactory((minecraft, screen) -> new ConfigScreen(screen))); + NeoForge.EVENT_BUS.addListener(ClientModEvents::onKeyInput); NeoForge.EVENT_BUS.addListener(ClientModEvents::onClientStaring); NeoForge.EVENT_BUS.addListener(ClientModEvents::onClientCommand); diff --git a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/ChatImagePacket.java b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/ChatImagePacket.java index 9cdf3875..563cd8f2 100644 --- a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/ChatImagePacket.java +++ b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/ChatImagePacket.java @@ -6,11 +6,13 @@ import io.github.kituin.ChatImageCode.ChatImageIndex; import net.minecraft.client.Minecraft; import net.minecraft.server.level.ServerPlayer; +import net.neoforged.neoforge.network.PacketDistributor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.UUID; import static io.github.kituin.ChatImageCode.ClientStorage.AddImageError; @@ -32,7 +34,13 @@ public class ChatImagePacket { */ public static void sendFilePackets(List bufs) { for (String buf : bufs) { - FileChannel.sendToServer(new FileChannelPacket(buf)); +// IF <= neoforge-1.20.3 +// FileChannel.sendToServer(new FileChannelPacket(buf)); +// ELSE IF < neoforge-1.21.0 +// PacketDistributor.SERVER.noArg().send(new FileChannelPacket(buf)); +// ELSE +// PacketDistributor.sendToServer(new FileChannelPacket(buf)); +// END IF } } @@ -43,7 +51,13 @@ public static void sendFilePackets(List bufs) { */ public static void loadFromServer(String url) { if (Minecraft.getInstance().player != null) { - FileInfoChannel.sendToServer(new FileInfoChannelPacket(url)); +// IF <= neoforge-1.20.3 +// FileInfoChannel.sendToServer(new FileInfoChannelPacket(url)); +// ELSE IF < neoforge-1.21.0 +// PacketDistributor.SERVER.noArg().send(new FileInfoChannelPacket(url)); +// ELSE +// PacketDistributor.sendToServer(new FileInfoChannelPacket(url)); +// END IF LOGGER.info("[try get from server]" + url); } else { @@ -71,7 +85,14 @@ public static void serverFileChannelReceived(ServerPlayer player, String res) { List names = USER_CACHE_MAP.get(title.url); for (String uuid : names) { ServerPlayer serverPlayer = player.server.getPlayerList().getPlayer(UUID.fromString(uuid)); - FileBackChannel.sendToPlayer(new FileInfoChannelPacket("true->" + title.url), serverPlayer); +// IF <= neoforge-1.20.3 +// FileBackChannel.sendToPlayer(new FileInfoChannelPacket("true->" + title.url), serverPlayer); +// ELSE IF < neoforge-1.21.0 +// serverPlayer.connection.send(new FileInfoChannelPacket("true->" + title.url)); +// ELSE +// PacketDistributor.sendToPlayer(serverPlayer, new FileInfoChannelPacket("true->" + title.url)); +// END IF + LOGGER.info("[echo to client(" + uuid + ")]" + title.url); } USER_CACHE_MAP.put(title.url, Lists.newArrayList()); @@ -95,4 +116,42 @@ public static void clientDownloadFileChannelReceived(String res) { LOGGER.info("[DownloadFileChannel-Merge]" + title.url); } } -} + public static void serverFileInfoChannelReceived(net.minecraft.server.level.ServerPlayer player, String url) { + if (SERVER_BLOCK_CACHE.containsKey(url) && FILE_COUNT_MAP.containsKey(url)) { + HashMap list = SERVER_BLOCK_CACHE.get(url); + Integer total = FILE_COUNT_MAP.get(url); + if (total == list.size()) { + // 服务器存在缓存图片,直接发送给客户端 + for (Map.Entry entry : list.entrySet()) { + LOGGER.debug("[GetFileChannel->Client:{}/{}]{}", entry.getKey(), list.size() - 1, url); +// IF <= neoforge-1.20.3 +// DownloadFileChannel.sendToPlayer(new DownloadFileChannelPacket(entry.getValue()), player); +// ELSE IF < neoforge-1.21.0 +// player.connection.send(new DownloadFileChannelPacket(entry.getValue())); +// ELSE +// PacketDistributor.sendToPlayer(player, new DownloadFileChannelPacket(entry.getValue())); +// END IF + } + LOGGER.info("[GetFileChannel->Client]{}", url); + return; + } + } + //通知客户端无文件 +// IF <= neoforge-1.20.3 +// FileBackChannel.sendToPlayer(new FileInfoChannelPacket("null->" + url), player); +// ELSE IF < neoforge-1.21.0 +// player.connection.send(new FileInfoChannelPacket("null->" + url)); +// ELSE +// PacketDistributor.sendToPlayer(player, new FileInfoChannelPacket("null->" + url)); +// END IF + LOGGER.error("[GetFileChannel]not found in server:{}", url); + // 记录uuid,后续有文件了推送 + List names = USER_CACHE_MAP.containsKey(url) ? USER_CACHE_MAP.get(url) : Lists.newArrayList(); + if (player != null) { + names.add(player.getStringUUID()); + } + USER_CACHE_MAP.put(url, names); + LOGGER.info("[GetFileChannel]记录uuid:{}", player.getStringUUID()); + LOGGER.info("[not found in server]{}", url); + } +} \ No newline at end of file diff --git a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/DownloadFileChannelHandler.java b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/DownloadFileChannelHandler.java index cc4be467..50cd8948 100644 --- a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/DownloadFileChannelHandler.java +++ b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/DownloadFileChannelHandler.java @@ -1,7 +1,6 @@ // ONLY > neoforge-1.20.3 package io.github.kituin.chatimage.network; -import net.neoforged.neoforge.network.handling.PlayPayloadContext; import static io.github.kituin.chatimage.network.ChatImagePacket.clientDownloadFileChannelReceived; @@ -15,7 +14,7 @@ public static DownloadFileChannelHandler getInstance() { return INSTANCE; } - public void clientHandle(final DownloadFileChannelPacket data, final PlayPayloadContext context) { + public void clientHandle(final DownloadFileChannelPacket data, final #PlayPayloadContext# context) { clientDownloadFileChannelReceived(data.message()); } -} +} \ No newline at end of file diff --git a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/DownloadFileChannelPacket.java b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/DownloadFileChannelPacket.java index 23052755..1511416b 100644 --- a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/DownloadFileChannelPacket.java +++ b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/DownloadFileChannelPacket.java @@ -1,5 +1,6 @@ package io.github.kituin.chatimage.network; + import net.minecraft.network.FriendlyByteBuf; @@ -28,31 +29,49 @@ // } // } // ELSE +// +//import net.minecraft.network.codec.ByteBufCodecs; //import net.minecraft.network.protocol.common.custom.CustomPacketPayload; //import net.minecraft.resources.ResourceLocation; //import org.jetbrains.annotations.NotNull; +//import io.netty.buffer.ByteBuf; // //import static io.github.kituin.chatimage.ChatImage.MOD_ID; -// +///** +// * 发送文件分块到客户端通道(Map) +// */ //public record DownloadFileChannelPacket(String message) implements CustomPacketPayload { -// /** -// * 发送文件分块到客户端通道(Map) -// */ -// public static ResourceLocation DOWNLOAD_FILE_CHANNEL = new ResourceLocation(MOD_ID, "download_file_channel"); // // public DownloadFileChannelPacket(FriendlyByteBuf buffer) { // this(buffer.readUtf()); // } // -// @Override -// public void write(final FriendlyByteBuf buffer) { -// buffer.writeUtf(message()); +// IF >= neoforge-1.21.0 +// public static ResourceLocation DOWNLOAD_FILE_CHANNEL = ResourceLocation.fromNamespaceAndPath(MOD_ID, "download_file_channel"); +// public static final CustomPacketPayload.Type TYPE = +// new CustomPacketPayload.Type<>(DOWNLOAD_FILE_CHANNEL); +// public Type type() { +// return TYPE; // } +// public static final net.minecraft.network.codec.StreamCodec CODEC = +// net.minecraft.network.codec.StreamCodec.composite( +// ByteBufCodecs.STRING_UTF8, +// DownloadFileChannelPacket::message, +// DownloadFileChannelPacket::new +// ); +// ELSE +//public static ResourceLocation DOWNLOAD_FILE_CHANNEL = new ResourceLocation(MOD_ID, "download_file_channel"); +// @Override +// public void write(final FriendlyByteBuf buffer) { +// buffer.writeUtf(message()); +// } +// +// @Override +// public @NotNull ResourceLocation id() { +// return DOWNLOAD_FILE_CHANNEL; +// } +// END IF // -// @Override -// public @NotNull ResourceLocation id() { -// return DOWNLOAD_FILE_CHANNEL; -// } // //} // END IF \ No newline at end of file diff --git a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileChannelHandler.java b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileChannelHandler.java index 501e7ff6..371ac008 100644 --- a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileChannelHandler.java +++ b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileChannelHandler.java @@ -2,7 +2,6 @@ package io.github.kituin.chatimage.network; import net.minecraft.server.level.ServerPlayer; -import net.neoforged.neoforge.network.handling.PlayPayloadContext; import static io.github.kituin.chatimage.network.ChatImagePacket.serverFileChannelReceived; @@ -20,7 +19,10 @@ public static FileChannelHandler getInstance() { /** * 服务端接收 图片文件分块 的处理 */ - public void serverHandle(final FileChannelPacket packet, final PlayPayloadContext ctx) { - ctx.player().ifPresent(player -> serverFileChannelReceived((ServerPlayer) player, packet.message())); + public void serverHandle(final FileChannelPacket packet, final #PlayPayloadContext# ctx) { + ServerPlayer player = (ServerPlayer) ctx.player(); + if (player == null) { + serverFileChannelReceived(player, packet.message()); + } } -} +} \ No newline at end of file diff --git a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileChannelPacket.java b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileChannelPacket.java index a02c530e..a99da870 100644 --- a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileChannelPacket.java +++ b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileChannelPacket.java @@ -37,31 +37,45 @@ // // } // ELSE +//import io.netty.buffer.ByteBuf; +//import net.minecraft.network.codec.ByteBufCodecs; //import net.minecraft.network.protocol.common.custom.CustomPacketPayload; //import net.minecraft.resources.ResourceLocation; //import org.jetbrains.annotations.NotNull; // //import static io.github.kituin.chatimage.ChatImage.MOD_ID; -// +///** +// * 客户端发送文件分块到服务器通道(Map) +// */ //public record FileChannelPacket(String message) implements CustomPacketPayload { -// /** -// * 客户端发送文件分块到服务器通道(Map) -// */ -// public static ResourceLocation FILE_CHANNEL = new ResourceLocation(MOD_ID, "file_channel"); // // public FileChannelPacket(FriendlyByteBuf buffer) { // this(buffer.readUtf()); // } +// IF >= neoforge-1.21.0 +// public static ResourceLocation FILE_CHANNEL = ResourceLocation.fromNamespaceAndPath(MOD_ID, "file_channel"); +// public static final CustomPacketPayload.Type TYPE = +// new CustomPacketPayload.Type<>(FILE_CHANNEL); +// public Type type() { +// return TYPE; +// } +// public static final net.minecraft.network.codec.StreamCodec CODEC = +// net.minecraft.network.codec.StreamCodec.composite( +// ByteBufCodecs.STRING_UTF8, +// FileChannelPacket::message, +// FileChannelPacket::new +// ); +// ELSE +// public static ResourceLocation FILE_CHANNEL = new ResourceLocation(MOD_ID, "file_channel"); +// public void write(final FriendlyByteBuf buffer) { +// buffer.writeUtf(message()); +// } // -// public void write(final FriendlyByteBuf buffer) { -// buffer.writeUtf(message()); -// } -// -// @Override -// public @NotNull ResourceLocation id() { -// return FILE_CHANNEL; -// } -// +// @Override +// public @NotNull ResourceLocation id() { +// return FILE_CHANNEL; +// } +// END IF // //} // END IF \ No newline at end of file diff --git a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileInfoChannelHandler.java b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileInfoChannelHandler.java index e7c98f15..2445f42a 100644 --- a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileInfoChannelHandler.java +++ b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileInfoChannelHandler.java @@ -4,7 +4,6 @@ import com.google.common.collect.Lists; import io.github.kituin.ChatImageCode.ChatImageFrame; import net.minecraft.server.level.ServerPlayer; -import net.neoforged.neoforge.network.handling.PlayPayloadContext; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -25,10 +24,13 @@ public static FileInfoChannelHandler getInstance() { return INSTANCE; } - public void serverHandle(final FileInfoChannelPacket packet, final PlayPayloadContext ctx) { - ctx.player().ifPresent(player -> serverFileInfoChannelReceived((ServerPlayer) player, packet.message())); + public void serverHandle(final FileInfoChannelPacket packet, final #PlayPayloadContext# ctx) { + ServerPlayer player = (ServerPlayer) ctx.player(); + if (player == null) { + serverFileInfoChannelReceived(player, packet.message()); + } } - public void clientHandle(final FileInfoChannelPacket packet, final PlayPayloadContext ctx) { + public void clientHandle(final FileInfoChannelPacket packet, final #PlayPayloadContext# ctx) { String data = packet.message(); String url = data.substring(6); LOGGER.info(url); @@ -40,5 +42,4 @@ public void clientHandle(final FileInfoChannelPacket packet, final PlayPayloadCo loadFromServer(url); } } -} - +} \ No newline at end of file diff --git a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileInfoChannelPacket.java b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileInfoChannelPacket.java index 3a775dcd..e14b23c5 100644 --- a/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileInfoChannelPacket.java +++ b/neoforge/origin/src/main/java/io/github/kituin/chatimage/network/FileInfoChannelPacket.java @@ -1,110 +1,104 @@ package io.github.kituin.chatimage.network; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.protocol.common.custom.CustomPacketPayload; -import net.minecraft.resources.ResourceLocation; -import org.jetbrains.annotations.NotNull; - -import static io.github.kituin.chatimage.ChatImage.MOD_ID; // IF <= neoforge-1.20.3 // -// import com.google.common.collect.Lists; -// import io.github.kituin.ChatImageCode.ChatImageFrame; -// import net.minecraft.server.level.ServerPlayer; -// import net.neoforged.neoforge.network.NetworkEvent; -// import org.apache.logging.log4j.LogManager; -// import org.apache.logging.log4j.Logger; +//import io.github.kituin.ChatImageCode.ChatImageFrame; +//import net.minecraft.server.level.ServerPlayer; +//import net.neoforged.neoforge.network.NetworkEvent; +//import org.apache.logging.log4j.LogManager; +//import org.apache.logging.log4j.Logger; // -// import java.util.HashMap; -// import java.util.List; -// import java.util.Map; +//import static io.github.kituin.ChatImageCode.ClientStorage.AddImageError; // -// import static io.github.kituin.ChatImageCode.ClientStorage.AddImageError; +//import static io.github.kituin.chatimage.network.ChatImagePacket.loadFromServer; +//import static io.github.kituin.chatimage.network.ChatImagePacket.serverFileInfoChannelReceived; // -// import static io.github.kituin.ChatImageCode.ServerStorage.*; -// import static io.github.kituin.chatimage.network.ChatImagePacket.loadFromServer; -// public class FileInfoChannelPacket { -// public final String message; -// private static final Logger LOGGER = LogManager.getLogger(); +//public class FileInfoChannelPacket { +// public final String message; +// private static final Logger LOGGER = LogManager.getLogger(); // -// public FileInfoChannelPacket(FriendlyByteBuf buffer) { -// message = buffer.readUtf(); -// } +// public FileInfoChannelPacket(FriendlyByteBuf buffer) { +// message = buffer.readUtf(); +// } // -// public FileInfoChannelPacket(String message) { -// this.message = message; -// } +// public FileInfoChannelPacket(String message) { +// this.message = message; +// } // -// public void toBytes(FriendlyByteBuf buf) { -// buf.writeUtf(this.message); -// } +// public void toBytes(FriendlyByteBuf buf) { +// buf.writeUtf(this.message); +// } // -// public static void handler(FileInfoChannelPacket packet,NetworkEvent.Context ctx) { -// ctx.enqueueWork(() -> { -// String url = packet.message; -// ServerPlayer player = ctx.getSender(); -// if (SERVER_BLOCK_CACHE.containsKey(url) && FILE_COUNT_MAP.containsKey(url)) { -// HashMap list = SERVER_BLOCK_CACHE.get(url); -// Integer total = FILE_COUNT_MAP.get(url); -// if (total == list.size()) { -// // 服务器存在缓存图片,直接发送给客户端 -// for (Map.Entry entry : list.entrySet()) { -// LOGGER.debug("[GetFileChannel->Client:" + entry.getKey() + "/" + (list.size() - 1) + "]" + url); -// DownloadFileChannel.sendToPlayer(new DownloadFileChannelPacket(entry.getValue()), player); -// } -// LOGGER.info("[GetFileChannel->Client]" + url); -// return; -// } -// } -// //通知客户端无文件 -// FileBackChannel.sendToPlayer(new FileInfoChannelPacket("null->" + url), player); -// LOGGER.error("[GetFileChannel]not found in server:" + url); -// // 记录uuid,后续有文件了推送 -// List names = USER_CACHE_MAP.containsKey(url) ? USER_CACHE_MAP.get(url) : Lists.newArrayList(); -// if (player != null) { -// names.add(player.getStringUUID()); -// } -// USER_CACHE_MAP.put(url, names); -// LOGGER.info("[GetFileChannel]记录uuid:" + player.getStringUUID()); -// LOGGER.info("[not found in server]" + url); -// }); -// ctx.setPacketHandled(true); -// } -// public static void clientHandle(FileInfoChannelPacket packet, NetworkEvent.Context ctx) { -// ctx.enqueueWork(() -> { -// String data = packet.message; -// String url = data.substring(6); -// LOGGER.info(url); -// if (data.startsWith("null")) { -// LOGGER.info("[GetFileChannel-NULL]" + url); -// AddImageError(url, ChatImageFrame.FrameError.FILE_NOT_FOUND); -// } else if (data.startsWith("true")) { -// LOGGER.info("[GetFileChannel-Retry]" + url); -// loadFromServer(url); -// } -// }); -// ctx.setPacketHandled(true); -// } -// } +// public static void handler(FileInfoChannelPacket packet, NetworkEvent.Context ctx) { +// ctx.enqueueWork(() -> { +// ServerPlayer player = ctx.getSender(); +// serverFileInfoChannelReceived(player, packet.message); +// }); +// ctx.setPacketHandled(true); +// } +// +// public static void clientHandle(FileInfoChannelPacket packet, NetworkEvent.Context ctx) { +// ctx.enqueueWork(() -> { +// String data = packet.message; +// String url = data.substring(6); +// LOGGER.info(url); +// if (data.startsWith("null")) { +// LOGGER.info("[GetFileChannel-NULL]" + url); +// AddImageError(url, ChatImageFrame.FrameError.FILE_NOT_FOUND); +// } else if (data.startsWith("true")) { +// LOGGER.info("[GetFileChannel-Retry]" + url); +// loadFromServer(url); +// } +// }); +// ctx.setPacketHandled(true); +// } +//} // ELSE -//public record FileInfoChannelPacket(String message) implements CustomPacketPayload { -// public static ResourceLocation FILE_INFO = new ResourceLocation(MOD_ID, "file_info"); // +//import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +//import net.minecraft.resources.ResourceLocation; +//import org.jetbrains.annotations.NotNull; +// IF >= neoforge-1.21.0 +//import io.netty.buffer.ByteBuf; +//import net.minecraft.network.codec.ByteBufCodecs; +// END IF +// +//import static io.github.kituin.chatimage.ChatImage.MOD_ID; +// +// +//public record FileInfoChannelPacket(String message) implements CustomPacketPayload { // public FileInfoChannelPacket(FriendlyByteBuf buffer) { // this(buffer.readUtf()); // } -// -// public void write(final FriendlyByteBuf buffer) { -// buffer.writeUtf(message()); +// IF >= neoforge-1.21.0 +// public static ResourceLocation FILE_INFO = ResourceLocation.fromNamespaceAndPath(MOD_ID, "file_info"); +// public static final CustomPacketPayload.Type TYPE = +// new CustomPacketPayload.Type<>(FILE_INFO); +// public Type type() { +// return TYPE; // } +// public static final net.minecraft.network.codec.StreamCodec CODEC = +// net.minecraft.network.codec.StreamCodec.composite( +// ByteBufCodecs.STRING_UTF8, +// FileInfoChannelPacket::message, +// FileInfoChannelPacket::new +// ); +// ELSE +// public static ResourceLocation FILE_INFO = new ResourceLocation(MOD_ID, "file_info"); // -// @Override -// public @NotNull ResourceLocation id() { -// return FILE_INFO; -// } // +// public void write(final FriendlyByteBuf buffer) { +// buffer.writeUtf(message()); +// } +// +// @Override +// public @NotNull ResourceLocation id() { +// return FILE_INFO; +// } +// END IF //} // // END IF \ No newline at end of file