MCProtocolLib Documentation

repository·master·Indexed 21 days ago

https://github.com/geysermc/mcprotocollib

A library for communicating with Minecraft clients and servers, enabling the creation of custom bots, clients, or servers. It provides a Server interface for lifecycle and session management, a system for global flags, and comprehensive tools for representing Minecraft command trees, including CommandNode, CommandParser, and various property constraints like IntegerProperties, FloatProperties, and ResourceProperties.

Tokens
4.4K
Snippets
21
Records
25
Agent score
75%

What's inside MCProtocolLib

  1. Add MCProtocolLib as a Gradle (Groovy DSL) dependency

    master

    To use MCProtocolLib in a Gradle project using Groovy, add the Open Collaboration repository to your repositories block and include the protocol dependency in your dependencies block. Replace (version here) with the desired version number.

    repositories {
        maven {
            name 'opencollab'
            url 'https://repo.opencollab.dev/main/'
        }
    }
    
    dependencies {
        implementation 'org.geysermc.mcprotocollib:protocol:(version here)'
    }
  2. Add MCProtocolLib as a Maven dependency

    master

    To use MCProtocolLib in a Maven project, first add the Open Collaboration repository to your pom.xml, then add the protocol artifact as a dependency. Replace (version here) with the desired version number.

    <repositories>
        <repository>
            <id>opencollab</id>
            <url>https://repo.opencollab.dev/main/</url>
        </repository>
    </repositories>
    
    <dependency>
        <groupId>org.geysermc.mcprotocollib</groupId>
        <artifactId>protocol</artifactId>
        <version>(version here)</version>
    </dependency>
  3. Add MCProtocolLib as a Gradle (Kotlin DSL) dependency

    master

    To use MCProtocolLib in a Gradle project using Kotlin DSL, add the Open Collaboration repository to your repositories block and include the protocol dependency in your dependencies block. Replace (version here) with the desired version number.

    repositories {
        maven("https://repo.opencollab.dev/main/") {
            name = "opencollab"
        }
    }
    
    dependencies {
        implementation("org.geysermc.mcprotocollib:protocol:(version here)")
    }
  4. Manage a Minecraft server instance with the Server interface

    master

    The Server interface is the primary entrypoint for managing a Minecraft server instance in MCProtocolLib. It allows you to bind to a network address, manage active sessions, handle server-wide flags, and attach listeners to respond to server events.

    Core Capabilities

    • Lifecycle Management: Use bind() to start listening on the configured host and port, and close() to shut down the server.
    • Session Management: Access all currently connected clients via getSessions().
    • Event Handling: Attach ServerListener instances using addListener(ServerListener listener) to react to connection events.
    • Global Flags: Store and retrieve server-wide configuration or state using the Flag<T> system. Flags set on the server act as defaults for individual sessions if the session does not override them.
    • Protocol Configuration: Retrieve the server's packet protocol via getPacketProtocol().
  5. Bind and close the Server

    master

    To start or stop the server, use the bind and close methods. These methods support optional waiting and callbacks to handle asynchronous lifecycle transitions.

    Binding

    • bind(): Binds the listener to its host and port.
    • bind(boolean wait): Binds and optionally waits for the binding process to complete.
    • bind(boolean wait, Runnable callback): Binds and executes a callback once the binding is finished.

    Closing

    • close(): Closes the listener.
    • close(boolean wait): Closes the listener and optionally waits for it to finish.
    • close(boolean wait, Runnable callback): Closes the listener and executes a callback once the closing is finished.
  6. Use global flags on a Server

    master

    The Server interface provides a type-safe way to manage global flags. These flags can be used to pass configuration or state down to individual sessions.

    Methods

    • setGlobalFlag(Flag<T> flag, T value): Sets the value for a specific flag.
    • hasGlobalFlag(Flag<?> flag): Returns true if the flag is currently set.
    • getGlobalFlag(Flag<T> flag): Retrieves the value. Throws IllegalStateException if the value is not of the expected type.
    • getGlobalFlag(Flag<T> flag, T def): Retrieves the value, returning the provided def (default) if the flag is not set.
    • getGlobalFlagSupplied(Flag<T> flag, Supplier<T> defSupplier): Retrieves the value, using the Supplier to provide a default if the flag is not set.
  7. Identify command suggestion types using SuggestionType

    master

    The SuggestionType enum defines the specific categories of command completions (suggestions) available in Minecraft. Each type is associated with a Key (resource location) that identifies the category. When receiving a suggestion request, you can use SuggestionType.from(Key) to determine the type of suggestion being requested. If the provided key does not match a known type, it defaults to ASK_SERVER.

    // Example: Determining the suggestion type from a resource location key
    Key key = Key.key("available_sounds");
    SuggestionType type = SuggestionType.from(key);
    
    if (type == SuggestionType.AVAILABLE_SOUNDS) {
        // Handle sound suggestions
    }
  8. Available CommandParser types

    master

    The CommandParser enum defines the supported data types for parsing Minecraft command arguments. These types are used to identify how a specific part of a command string should be interpreted and converted into structured data.

    BOOL,
    FLOAT,
    DOUBLE,
    INTEGER,
    LONG,
    STRING,
    ENTITY,
    GAME_PROFILE,
    BLOCK_POS,
    COLUMN_POS,
    VEC3,
    VEC2,
    BLOCK_STATE,
    BLOCK_PREDICATE,
    ITEM_STACK,
    ITEM_PREDICATE,
    COLOR,
    HEX_COLOR,
    COMPONENT,
    STYLE,
    MESSAGE,
    NBT_COMPOUND_TAG,
    NBT_TAG,
    NBT_PATH,
    OBJECTIVE,
    OBJECTIVE_CRITERIA,
    OPERATION,
    PARTICLE,
    ANGLE,
    ROTATION,
    SCOREBOARD_SLOT,
    SCORE_HOLDER,
    SWIZZLE,
    TEAM,
    ITEM_SLOT,
    ITEM_SLOTS,
    RESOURCE_LOCATION,
    FUNCTION,
    ENTITY_ANCHOR,
    INT_RANGE,
    FLOAT_RANGE,
    DIMENSION,
    GAMEMODE,
    TIME,
    RESOURCE_OR_TAG,
    RESOURCE_OR_TAG_KEY,
    RESOURCE,
    RESOURCE_KEY,
    RESOURCE_SELECTOR,
    TEMPLATE_MIRROR,
    TEMPLATE_ROTATION,
    HEIGHTMAP,
    LOOT_TABLE,
    LOOT_PREDICATE,
    LOOT_MODIFIER,
    DIALOG,
    UUID
  9. Available SuggestionType constants

    master

    The following SuggestionType constants are available. Each constant has an associated Key (resource location) which can be retrieved via getResourceLocation().

    // Available SuggestionType constants and their resource locations:
    // ASK_SERVER -> key("ask_server")
    // AVAILABLE_SOUNDS -> key("available_sounds")
    // SUMMONABLE_ENTITIES -> key("summonable_entities")