Created
September 26, 2024 00:46
-
-
Save Densamisten/27fa1d87be1ad3533d684920eea12eec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exonihility.client.event; | |
import net.minecraft.network.packet.Packet; | |
import net.minecraft.network.NetworkSide; | |
import net.minecraft.util.Identifier; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import net.minecraft.network.packet.s2c.login.LoginDisconnectS2CPacket; | |
import net.minecraft.text.Text; | |
public class PacketLogger { | |
private static final Logger LOGGER = LogManager.getLogger("Packet Logger"); | |
/** | |
* Logs when a packet is sent. | |
* | |
* @param packet The packet being sent. | |
* @param side The network side (client or server). | |
*/ | |
public static void logSentPacket(Packet<?> packet, NetworkSide side) { | |
String sideName = getSideName(side); | |
String packetName = packet.getClass().getSimpleName(); | |
LOGGER.info("Sending packet: '{}' ({})", packetName, sideName); | |
// Example: Log additional information for specific packets | |
if (packet instanceof LoginDisconnectS2CPacket) { | |
LoginDisconnectS2CPacket disconnectPacket = (LoginDisconnectS2CPacket) packet; | |
Text reason = disconnectPacket.getReason(); | |
LOGGER.info("Disconnect reason: {}", reason.getString()); | |
} | |
} | |
/** | |
* Logs when a packet is received. | |
* | |
* @param packet The packet being received. | |
* @param context Additional context to log. | |
*/ | |
public static void logReceivedPacket(Packet<?> packet, String context) { | |
String packetName = packet.getClass().getSimpleName(); | |
LOGGER.info("Received packet: '{}' ({})", packetName, context); | |
} | |
/** | |
* Gets the readable name for the network side. | |
* | |
* @param side The NetworkSide (CLIENTBOUND or SERVERBOUND). | |
* @return A string representing the side. | |
*/ | |
private static String getSideName(NetworkSide side) { | |
if (side == NetworkSide.CLIENTBOUND) { | |
return "client"; | |
} else if (side == NetworkSide.SERVERBOUND) { | |
return "server"; | |
} | |
return "unknown"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment