ProtocolLib icon indicating copy to clipboard operation
ProtocolLib copied to clipboard

WIP - New API

Open Ingrim4 opened this issue 1 year ago • 1 comments

This PR initiates the development of a new API as outlined in #2996, aiming to improve the overall maintainability and flexibility of ProtocolLib. As this is the first draft, backward compatibility may be (temporarily) broken, and it will primarily serve to track progress and gather feedback on API design and concerns. The PR will remain a work-in-progress until all current features have been incorporated into the new API, with stability and structure refined for broader usage.

I encourage everyone to participate in shaping this new API. If you have suggestions, concerns, or alternative approaches, feel free to contribute your thoughts. Feedback on design decisions, modularity, or areas where backward compatibility might be preserved is especially welcome. This collaborative effort will help ensure the API meets the needs of both current and future developers. Please don’t hesitate to recommend changes or improvements as we refine the API together.

Register an async listener

protocolLib.createListener(plugin)
    .types(PacketTypes.Game.LEVEL_CHUNK)
    .priority(PacketListenerPriority.LOW)
    .ignoreCancelledPackets()
    .registerAsync((packet, context) -> {
        Chunk chunk = Chunk.from(packet);
        
        // do processing here ...

        context.addTransmissionListener(chunk::markSent);
        context.resumeProcessing();
    });

Sending/Receiving packets

protocolLib.connection(player)
    .packetOperation()
    .skipProcessing()
    .postTransmission(chunk::markSent)
    .send(chunk.packet());

protocolLib.connection(player)
    .sendPacket(chunk.packet());

protocolLib.sendPacket(player, chunk.packet());

Ingrim4 avatar Sep 22 '24 21:09 Ingrim4

At this stage everything looks easy to interact with. I'd like to see the rest of the interfaces.

Separation of API and implementation is a change that has been needed for a long time, and I'm glad there is someone willing to take it on now.

Dymeth avatar Oct 21 '24 01:10 Dymeth