Skip to content

Commit

Permalink
✨ try neoforge-1.21.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kitUIN committed Nov 27, 2024
1 parent 736325b commit c83c164
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 141 deletions.
12 changes: 11 additions & 1 deletion neoforge/origin/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +34,13 @@ public class ChatImagePacket {
*/
public static void sendFilePackets(List<String> 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
}
}

Expand All @@ -43,7 +51,13 @@ public static void sendFilePackets(List<String> 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 {
Expand Down Expand Up @@ -71,7 +85,14 @@ public static void serverFileChannelReceived(ServerPlayer player, String res) {
List<String> 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());
Expand All @@ -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<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()) {
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<String> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.kituin.chatimage.network;


import net.minecraft.network.FriendlyByteBuf;


Expand Down Expand Up @@ -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<DownloadFileChannelPacket> TYPE =
// new CustomPacketPayload.Type<>(DOWNLOAD_FILE_CHANNEL);
// public Type<? extends CustomPacketPayload> type() {
// return TYPE;
// }
// public static final net.minecraft.network.codec.StreamCodec<ByteBuf, DownloadFileChannelPacket> 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileChannelPacket> TYPE =
// new CustomPacketPayload.Type<>(FILE_CHANNEL);
// public Type<? extends CustomPacketPayload> type() {
// return TYPE;
// }
// public static final net.minecraft.network.codec.StreamCodec<ByteBuf, FileChannelPacket> 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
Loading

0 comments on commit c83c164

Please sign in to comment.