Remove ICustomPacket and add PayloadChannel. (#9972)

New PayloadChannel that uses the vanilla payload Type for packet distinction
Implement the minecraft:register/unregister channels using the new PayloadChannel
New generic channel builder function allowing people to implement channels however they want.
This commit is contained in:
LexManos authored and GitHub committed 2024-05-14 08:36:42 -07:00
1 parent 640c206117
commit 2b3a4137e1
34 files changed
+1035 -338

No files matched your search

@@ -10,11 +10,17 @@ import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import io.netty.buffer.Unpooled;
import net.minecraft.network.Connection;
import net.minecraft.network.PacketListener;
import net.minecraft.network.ProtocolInfo;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
import net.minecraft.network.protocol.configuration.ClientboundFinishConfigurationPacket;
import net.minecraftforge.network.ICustomPacket;
import net.minecraft.network.protocol.login.ClientboundCustomQueryPacket;
import net.minecraft.network.protocol.login.ServerboundCustomQueryAnswerPacket;
/*
* This is a utility class that just dumps packets to the logger.
@@ -33,20 +39,44 @@ public class PacketLogger {
}
public void send(Packet<?> packet) {
common(connection.getReceiving(), connection.getSending(), packet);
common(connection.getReceiving(), connection.getSending(), connection.getOutputboundProtocolInfo(), packet);
}
public void recv(Packet<?> packet) {
common(connection.getReceiving(), connection.getReceiving(), packet);
common(connection.getReceiving(), connection.getReceiving(), connection.getInboundProtocolInfo(), packet);
}
private void common(PacketFlow side, PacketFlow flow, Packet<?> packet) {
private void common(PacketFlow side, PacketFlow flow, ProtocolInfo<?> protocol, Packet<?> packet) {
if (!enabled) return;
if (packet instanceof ClientboundFinishConfigurationPacket)
enabled = false;
if (packet instanceof ICustomPacket custom && custom.getInternalData() != null) {
LOGGER.info(MARKER, "{} {} {} {}\n{}", side(side), dir(flow), packet.getClass().getName(), custom.getName(), HexDumper.dump(custom.getInternalData()));
String channel = null;
String hex = null;
switch (packet) {
case ClientboundCustomPayloadPacket custom -> {
channel = custom.payload().type().id().toString();
hex = hex(protocol, packet);
}
case ServerboundCustomPayloadPacket custom -> {
channel = custom.payload().type().id().toString();
hex = hex(protocol, packet);
}
case ClientboundCustomQueryPacket custom -> {
channel = custom.payload().id().toString() + " id " + custom.transactionId();
hex = hex(protocol, packet);
}
case ServerboundCustomQueryAnswerPacket custom -> {
channel = packet.getClass().getName() + " id " + custom.transactionId();
hex = hex(protocol, packet);
}
default -> {}
}
if (channel != null && hex != null) {
LOGGER.info(MARKER, "{} {} {} {}\n{}", side(side), dir(flow), packet.getClass().getName(), channel, hex);
} else if (channel != null) {
LOGGER.info(MARKER, "{} {} {} {}", side(side), dir(flow), packet.getClass().getName(), channel);
} else {
LOGGER.info(MARKER, "{} {} {}", side(side), dir(flow), packet.getClass().getName());
}
@@ -58,4 +88,11 @@ public class PacketLogger {
private static String dir(PacketFlow flow) {
return flow == PacketFlow.CLIENTBOUND ? "S->C" : "C->S";
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private static <T extends PacketListener> String hex(ProtocolInfo protocol, Packet packet) {
var buf = Unpooled.buffer();
protocol.codec().encode(buf, packet);
return HexDumper.dump(buf);
}
}