All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jacobtread.blaze.PacketBuilder.kt Maven / Gradle / Ivy

The newest version!
package com.jacobtread.blaze

import com.jacobtread.blaze.packet.*
import com.jacobtread.blaze.tdf.Tdf

/**
 * Alias for a function that initializes the contents of a TdfBuilder
 * @see TdfBuilder
 */
typealias ContentInitializer = TdfBuilder.() -> Unit

/**
 * Uses the provided [ContentInitializer] to initialize
 * a [TdfBuilder] and returns the resulting values.
 *
 * @param init The content initializer
 * @return The content values
 */
inline fun getTdfList(init: ContentInitializer): ArrayList> {
    val builder = TdfBuilder()
    builder.init()
    return builder.values
}

/**
 * Creates a new client packet using the provided values and
 * the [ContentInitializer]. Client packets are listed here
 * for creation so that they can be used to emulate clients
 * when accessing the official servers (e.g. for origin fetching)
 *
 * @param component The component for this packet
 * @param command The command for this packet
 * @param id The id for this packet
 * @param init The content initializer
 * @return The created packet
 */
inline fun clientPacket(component: Int, command: Int, id: Int, init: ContentInitializer): Packet {
    return RequestPacket(component, command, id, getTdfList(init))
}

/**
 * Creates a new client packet using the provided values and
 * the [ContentInitializer]. Client packets are listed here
 * for creation so that they can be used to emulate clients
 * when accessing the official servers (e.g. for origin fetching)
 *
 * This implementation has no content.
 *
 * @param component The component for this packet
 * @param command The command for this packet
 * @param id The id for this packet
 * @return The created packet
 */
fun clientPacket(component: Int, command: Int, id: Int): Packet {
    return RequestPacket(component, command, id, emptyList())
}


/**
 * Creates a new response packet which is used to respond to an existing
 * packet. This is an extension of [Packet] so that it can be directly sent
 * for an existing packet.
 *
 * @param init The initializer for the content of this packet
 * @return The created packet
 */
inline fun Packet.respond(init: ContentInitializer): Packet {
    return ResponsePacket(component, command, id, getTdfList(init))
}

/**
 * Creates a new response packet which is used to respond to an existing
 * packet. This is an extension of [Packet] so that it can be directly sent
 * for an existing packet.
 *
 * This implementation has no content
 *
 * @return The created packet
 */
fun Packet.respond(): Packet {
    return ResponsePacket(component, command, id, emptyList())
}

/**
 * Creates a new notify packet which is used to notify clients of
 * changes or new data without it being requested first.
 *
 * @param component The component for this packet
 * @param command The command for this packet
 * @param init The content initializer
 * @return The created packet
 */
inline fun notify(
    component: Int,
    command: Int,
    init: ContentInitializer,
): NotifyPacket {
    return NotifyPacket(component, command, getTdfList(init))
}

/**
 * Creates a new notify packet which is used to notify clients of
 * changes or new data without it being requested first.
 *
 * This implementation has no content
 *
 * @param component The component for this packet
 * @param command The command for this packet
 * @return The created packet
 */
fun notify(component: Int, command: Int): NotifyPacket {
    return NotifyPacket(component, command, emptyList())
}

/**
 * Creates a new error packet which is responding to a previous request
 * packet with the provided [error] and content generated by [init]
 *
 * @param error The error for this packet
 * @param init The content initializer
 * @return The created packet
 */
inline fun Packet.error(error: Int, init: ContentInitializer): Packet {
    return ErrorPacket(component, command, id, error, getTdfList(init))
}

/**
 * Creates a new error packet which is responding to a previous request
 * packet with the provided [error]
 *
 * This implementation has no content
 *
 * @param error The error for this packet
 * @return The created packet
 */
fun Packet.error(error: Int): Packet {
    return ErrorPacket(component, command, id, error, emptyList())
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy