Skip to content

Commit

Permalink
wip part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
TexBlock committed Oct 20, 2024
1 parent 08637c9 commit 502a736
Show file tree
Hide file tree
Showing 89 changed files with 5,756 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package band.kessoku.lib.api.networking;
package band.kessoku.lib.api;

import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

public class KessokuNetworking {
public final class KessokuNetworking {
public static final String MOD_ID = "kessoku_networking";
public static final String NAME = "Kessoku Networking API";
public static final Marker MARKER = MarkerFactory.getMarker("[" + NAME + "]");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package band.kessoku.lib.api.networking;

import java.util.Objects;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.PacketCallbacks;
import net.minecraft.network.packet.Packet;
import net.minecraft.util.Identifier;

/**
* Represents something that supports sending packets to login channels.
* @see PacketSender
*/
@ApiStatus.NonExtendable
public interface LoginPacketSender extends PacketSender {
/**
* Creates a packet for sending to a login channel.
*
* @param channelName the id of the channel
* @param buf the content of the packet
* @return the created packet
*/
Packet<?> createPacket(Identifier channelName, PacketByteBuf buf);

/**
* Sends a packet to a channel.
*
* @param channel the id of the channel
* @param buf the content of the packet
*/
default void sendPacket(Identifier channel, PacketByteBuf buf) {
Objects.requireNonNull(channel, "Channel cannot be null");
Objects.requireNonNull(buf, "Payload cannot be null");

this.sendPacket(this.createPacket(channel, buf));
}

/**
* Sends a packet to a channel.
*
* @param channel the id of the channel
* @param buf the content of the packet
* @param callback an optional callback to execute after the packet is sent, may be {@code null}
*/
default void sendPacket(Identifier channel, PacketByteBuf buf, @Nullable PacketCallbacks callback) {
Objects.requireNonNull(channel, "Channel cannot be null");
Objects.requireNonNull(buf, "Payload cannot be null");

this.sendPacket(this.createPacket(channel, buf), callback);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package band.kessoku.lib.api.networking.util;
package band.kessoku.lib.api.networking;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package band.kessoku.lib.api.networking;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import net.minecraft.network.PacketCallbacks;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.Packet;
import net.minecraft.text.Text;

/**
* Represents something that supports sending packets to channels.
* Any packets sent must be {@linkplain PayloadTypeRegistry registered} in the appropriate registry.
*/
@ApiStatus.NonExtendable
public interface PacketSender {
/**
* Creates a packet from a packet payload.
*
* @param payload the packet payload
*/
Packet<?> createPacket(CustomPayload payload);

/**
* Sends a packet.
*
* @param packet the packet
*/
default void sendPacket(Packet<?> packet) {
sendPacket(packet, null);
}

/**
* Sends a packet.
* @param payload the payload
*/
default void sendPacket(CustomPayload payload) {
sendPacket(createPacket(payload));
}

/**
* Sends a packet.
*
* @param packet the packet
* @param callback an optional callback to execute after the packet is sent, may be {@code null}.
*/
void sendPacket(Packet<?> packet, @Nullable PacketCallbacks callback);

/**
* Sends a packet.
*
* @param payload the payload
* @param callback an optional callback to execute after the packet is sent, may be {@code null}.
*/
default void sendPacket(CustomPayload payload, @Nullable PacketCallbacks callback) {
sendPacket(createPacket(payload), callback);
}

/**
* Disconnects the player.
* @param disconnectReason the reason for disconnection
*/
void disconnect(Text disconnectReason);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package band.kessoku.lib.api.networking;

import band.kessoku.lib.impl.networking.PayloadTypeRegistryImpl;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import org.jetbrains.annotations.ApiStatus;

/**
* A registry for payload types.
*/
@ApiStatus.NonExtendable
public interface PayloadTypeRegistry<B extends PacketByteBuf> {

/**
* Registers a custom payload type.
*
* <p>This must be done on both the sending and receiving side, usually during mod initialization
* and <strong>before registering a packet handler</strong>.
*
* @param id the id of the payload type
* @param codec the codec for the payload type
* @param <T> the payload type
* @return the registered payload type
*/
<T extends CustomPayload> CustomPayload.Type<? super B, T> register(CustomPayload.Id<T> id, PacketCodec<? super B, T> codec);

/**
* @return the {@link PayloadTypeRegistry} instance for the client to server configuration channel.
*/
static PayloadTypeRegistry<PacketByteBuf> configC2S() {
return PayloadTypeRegistryImpl.CONFIG_C2S;
}

/**
* @return the {@link PayloadTypeRegistry} instance for the server to client configuration channel.
*/
static PayloadTypeRegistry<PacketByteBuf> configS2C() {
return PayloadTypeRegistryImpl.CONFIG_S2C;
}

/**
* @return the {@link PayloadTypeRegistry} instance for the client to server play channel.
*/
static PayloadTypeRegistry<RegistryByteBuf> playC2S() {
return PayloadTypeRegistryImpl.PLAY_C2S;
}

/**
* @return the {@link PayloadTypeRegistry} instance for the server to client play channel.
*/
static PayloadTypeRegistry<RegistryByteBuf> playS2C() {
return PayloadTypeRegistryImpl.PLAY_S2C;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package band.kessoku.lib.api.networking.util;
package band.kessoku.lib.api.networking;

import net.minecraft.server.network.ServerPlayerConfigurationTask;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package band.kessoku.lib.api.networking.client;

import java.util.List;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientConfigurationNetworkHandler;
import net.minecraft.util.Identifier;

import band.kessoku.lib.event.api.Event;
import band.kessoku.lib.api.networking.PacketSender;

/**
* Offers access to events related to the indication of a connected server's ability to receive packets in certain channels.
*/
public final class C2SConfigurationChannelEvent {
/**
* An event for the client configuration network handler receiving an update indicating the connected server's ability to receive packets in certain channels.
* This event may be invoked at any time after login and up to disconnection.
*/
public static final Event<Register> REGISTER = Event.of(registers -> (handler, sender, client, channels) -> {
for (Register callback : registers) {
callback.onChannelRegister(handler, sender, client, channels);
}
});

/**
* An event for the client configuration network handler receiving an update indicating the connected server's lack of ability to receive packets in certain channels.
* This event may be invoked at any time after login and up to disconnection.
*/
public static final Event<Unregister> UNREGISTER = Event.of(unregisters -> (handler, sender, client, channels) -> {
for (Unregister callback : unregisters) {
callback.onChannelUnregister(handler, sender, client, channels);
}
});

/**
* @see C2SConfigurationChannelEvent#REGISTER
*/
@FunctionalInterface
public interface Register {
void onChannelRegister(ClientConfigurationNetworkHandler handler, PacketSender sender, MinecraftClient client, List<Identifier> channels);
}

/**
* @see C2SConfigurationChannelEvent#UNREGISTER
*/
@FunctionalInterface
public interface Unregister {
void onChannelUnregister(ClientConfigurationNetworkHandler handler, PacketSender sender, MinecraftClient client, List<Identifier> channels);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package band.kessoku.lib.api.networking.client;

import java.util.List;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.util.Identifier;

import band.kessoku.lib.event.api.Event;
import band.kessoku.lib.api.networking.PacketSender;

/**
* Offers access to events related to the indication of a connected server's ability to receive packets in certain channels.
*/
public final class C2SPlayChannelEvent {
/**
* An event for the client play network handler receiving an update indicating the connected server's ability to receive packets in certain channels.
* This event may be invoked at any time after login and up to disconnection.
*/
public static final Event<Register> REGISTER = Event.of(registers -> (handler, sender, client, channels) -> {
for (Register callback : registers) {
callback.onChannelRegister(handler, sender, client, channels);
}
});

/**
* An event for the client play network handler receiving an update indicating the connected server's lack of ability to receive packets in certain channels.
* This event may be invoked at any time after login and up to disconnection.
*/
public static final Event<Unregister> UNREGISTER = Event.of(unregisters -> (handler, sender, client, channels) -> {
for (Unregister callback : unregisters) {
callback.onChannelUnregister(handler, sender, client, channels);
}
});

/**
* @see C2SPlayChannelEvent#REGISTER
*/
@FunctionalInterface
public interface Register {
void onChannelRegister(ClientPlayNetworkHandler handler, PacketSender sender, MinecraftClient client, List<Identifier> channels);
}

/**
* @see C2SPlayChannelEvent#UNREGISTER
*/
@FunctionalInterface
public interface Unregister {
void onChannelUnregister(ClientPlayNetworkHandler handler, PacketSender sender, MinecraftClient client, List<Identifier> channels);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package band.kessoku.lib.api.networking.client;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientConfigurationNetworkHandler;
import net.minecraft.network.packet.CustomPayload;

import band.kessoku.lib.event.api.Event;

/**
* Offers access to events related to the configuration connection to a server on a logical client.
*/
public final class ClientConfigurationConnectionEvent {
/**
* Event indicating a connection entering the CONFIGURATION state, ready for registering channel handlers.
*
* <p>No packets should be sent when this event is invoked.
*
* @see ClientConfigurationNetworking#registerReceiver(CustomPayload.Id, ClientConfigurationNetworking.ConfigurationPayloadHandler)
*/
public static final Event<Init> INIT = Event.of(inits -> (handler, client) -> {
for (ClientConfigurationConnectionEvent.Init callback : inits) {
callback.onConfigurationInit(handler, client);
}
});

/**
* An event called after the connection has been initialized and is ready to start sending and receiving configuration packets.
*
* <p>Packets may be sent during this event.
*/
public static final Event<Start> START = Event.of(starts -> (handler, client) -> {
for (ClientConfigurationConnectionEvent.Start callback : starts) {
callback.onConfigurationStart(handler, client);
}
});

/**
* An event called after the ReadyS2CPacket has been received, just before switching to the PLAY state.
*
* <p>No packets should be sent when this event is invoked.
*/
public static final Event<Complete> COMPLETE = Event.of(completes -> (handler, client) -> {
for (ClientConfigurationConnectionEvent.Complete callback : completes) {
callback.onConfigurationComplete(handler, client);
}
});

/**
* An event for the disconnection of the client configuration network handler.
*
* <p>No packets should be sent when this event is invoked.
*/
public static final Event<Disconnect> DISCONNECT = Event.of(disconnects -> (handler, client) -> {
for (ClientConfigurationConnectionEvent.Disconnect callback : disconnects) {
callback.onConfigurationDisconnect(handler, client);
}
});

@FunctionalInterface
public interface Init {
void onConfigurationInit(ClientConfigurationNetworkHandler handler, MinecraftClient client);
}

@FunctionalInterface
public interface Start {
void onConfigurationStart(ClientConfigurationNetworkHandler handler, MinecraftClient client);
}

@FunctionalInterface
public interface Complete {
void onConfigurationComplete(ClientConfigurationNetworkHandler handler, MinecraftClient client);
}

@FunctionalInterface
public interface Disconnect {
void onConfigurationDisconnect(ClientConfigurationNetworkHandler handler, MinecraftClient client);
}
}
Loading

0 comments on commit 502a736

Please sign in to comment.