staminadevelopment / minecraftapi

A cross-version compatible API for interacting with the Minecraft client.
MIT License
11 stars 2 forks source link

Refine the packet API #3

Closed feature closed 6 years ago

feature commented 6 years ago

Motivation:

The current packet API is very low level, and tries to mimic packets very closely by their implementation details. While this makes the API very powerful, it also makes it impossible to reliably maintain across game versions with deviating implementations. Therefore we cannot provide any stability guarantees for the API, rendering it practically redundant.

Goal:

A new refined behavior-based packet API. This new API uses a much higher level abstraction, which makes it possible to define a maintainable, and stable API for working with packets with deviating implementations.

Requirements:

The new refined packet API must be able to

Example:

For these examples we will use the ChatPacket packet type.

Creating an instance of ChatPacket:

String message = ...;
ChatPacket chatPacket = factory.chat(message);

Checking if a packet is a ChatPacket and casting it:

Packet packet = ...;
if (factory.isChat(packet)) {
    ChatPacket chatPacket = factory.castChat(packet);
    String message = chatPacket.getMessage();
}
feature commented 6 years ago

Here is an example of an alternative API, with a similar syntax, but with more appropriate separation of conerns:

Getting the adapter:

ChatPacketAdapter chat = PacketAdapters.outgoing().chat();

Creating an instance of ChatPacket:

String message = ...;
ChatPacket chatPacket = chat.create(message);

Checking if a packet is a ChatPacket and casting it:

Packet packet = ...;
if (chat.is(packet)) {
    ChatPacket chatPacket = chat.cast(packet);
    String message = chatPacket.getMessage();
}