// Generated by delombok at Tue Dec 16 19:36:13 UTC 2025
package net.md_5.bungee.forge;

import java.util.ArrayDeque;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.forge.ForgeLogger.LogDirection;
import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.protocol.packet.PluginMessage;

/**
 * Contains data about the Forge server, and handles the handshake.
 */
public class ForgeServerHandler {
    private final UserConnection con;
    private final ChannelWrapper ch;
    private final ServerInfo serverInfo;
    private ForgeServerHandshakeState state = ForgeServerHandshakeState.START;
    private boolean serverForge = false;
    private final ArrayDeque<PluginMessage> packetQueue = new ArrayDeque<PluginMessage>();

    /**
     * Handles any {@link PluginMessage} that contains a FML Handshake or Forge
     * Register.
     *
     * @param message The message to handle.
     * @throws IllegalArgumentException If the wrong packet is sent down.
     */
    public void handle(PluginMessage message) throws IllegalArgumentException {
        if (!message.getTag().equalsIgnoreCase(ForgeConstants.FML_HANDSHAKE_TAG) && !message.getTag().equalsIgnoreCase(ForgeConstants.FORGE_REGISTER)) {
            throw new IllegalArgumentException("Expecting a Forge REGISTER or FML Handshake packet.");
        }
        message.setAllowExtendedPacket(true); // FML allows extended packets so this must be enabled
        ForgeServerHandshakeState prevState = state;
        packetQueue.add(message);
        state = state.send(message, con);
        if (state != prevState) // send packets
         {
            synchronized (packetQueue) {
                while (!packetQueue.isEmpty()) {
                    ForgeLogger.logServer(LogDirection.SENDING, prevState.name(), packetQueue.getFirst());
                    con.getForgeClientHandler().receive(packetQueue.removeFirst());
                }
            }
        }
    }

    /**
     * Receives a {@link PluginMessage} from ForgeClientData to pass to Server.
     *
     * @param message The message to being received.
     * @throws IllegalArgumentException if invalid packet received
     */
    public void receive(PluginMessage message) throws IllegalArgumentException {
        state = state.handle(message, ch);
    }

    /**
     * Flags the server as a Forge server. Cannot be used to set a server back
     * to vanilla (there would be no need)
     */
    public void setServerAsForgeServer() {
        serverForge = true;
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public ForgeServerHandler(final UserConnection con, final ChannelWrapper ch, final ServerInfo serverInfo) {
        this.con = con;
        this.ch = ch;
        this.serverInfo = serverInfo;
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public ChannelWrapper getCh() {
        return this.ch;
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    ServerInfo getServerInfo() {
        return this.serverInfo;
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public ForgeServerHandshakeState getState() {
        return this.state;
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public boolean isServerForge() {
        return this.serverForge;
    }
}
