Skip to content

Commit 502a736

Browse files
committed
wip part 2
1 parent 08637c9 commit 502a736

File tree

89 files changed

+5756
-83
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+5756
-83
lines changed

networking/common/src/main/java/band/kessoku/lib/api/networking/KessokuNetworking.java renamed to networking/common/src/main/java/band/kessoku/lib/api/KessokuNetworking.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package band.kessoku.lib.api.networking;
1+
package band.kessoku.lib.api;
22

33
import org.slf4j.Marker;
44
import org.slf4j.MarkerFactory;
55

6-
public class KessokuNetworking {
6+
public final class KessokuNetworking {
77
public static final String MOD_ID = "kessoku_networking";
88
public static final String NAME = "Kessoku Networking API";
99
public static final Marker MARKER = MarkerFactory.getMarker("[" + NAME + "]");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package band.kessoku.lib.api.networking;
2+
3+
import java.util.Objects;
4+
5+
import org.jetbrains.annotations.ApiStatus;
6+
import org.jetbrains.annotations.Nullable;
7+
8+
import net.minecraft.network.PacketByteBuf;
9+
import net.minecraft.network.PacketCallbacks;
10+
import net.minecraft.network.packet.Packet;
11+
import net.minecraft.util.Identifier;
12+
13+
/**
14+
* Represents something that supports sending packets to login channels.
15+
* @see PacketSender
16+
*/
17+
@ApiStatus.NonExtendable
18+
public interface LoginPacketSender extends PacketSender {
19+
/**
20+
* Creates a packet for sending to a login channel.
21+
*
22+
* @param channelName the id of the channel
23+
* @param buf the content of the packet
24+
* @return the created packet
25+
*/
26+
Packet<?> createPacket(Identifier channelName, PacketByteBuf buf);
27+
28+
/**
29+
* Sends a packet to a channel.
30+
*
31+
* @param channel the id of the channel
32+
* @param buf the content of the packet
33+
*/
34+
default void sendPacket(Identifier channel, PacketByteBuf buf) {
35+
Objects.requireNonNull(channel, "Channel cannot be null");
36+
Objects.requireNonNull(buf, "Payload cannot be null");
37+
38+
this.sendPacket(this.createPacket(channel, buf));
39+
}
40+
41+
/**
42+
* Sends a packet to a channel.
43+
*
44+
* @param channel the id of the channel
45+
* @param buf the content of the packet
46+
* @param callback an optional callback to execute after the packet is sent, may be {@code null}
47+
*/
48+
default void sendPacket(Identifier channel, PacketByteBuf buf, @Nullable PacketCallbacks callback) {
49+
Objects.requireNonNull(channel, "Channel cannot be null");
50+
Objects.requireNonNull(buf, "Payload cannot be null");
51+
52+
this.sendPacket(this.createPacket(channel, buf), callback);
53+
}
54+
}

networking/common/src/main/java/band/kessoku/lib/api/networking/util/PacketByteBufHelper.java renamed to networking/common/src/main/java/band/kessoku/lib/api/networking/PacketByteBufHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package band.kessoku.lib.api.networking.util;
1+
package band.kessoku.lib.api.networking;
22

33
import io.netty.buffer.ByteBuf;
44
import io.netty.buffer.Unpooled;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package band.kessoku.lib.api.networking;
2+
3+
import org.jetbrains.annotations.ApiStatus;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import net.minecraft.network.PacketCallbacks;
7+
import net.minecraft.network.packet.CustomPayload;
8+
import net.minecraft.network.packet.Packet;
9+
import net.minecraft.text.Text;
10+
11+
/**
12+
* Represents something that supports sending packets to channels.
13+
* Any packets sent must be {@linkplain PayloadTypeRegistry registered} in the appropriate registry.
14+
*/
15+
@ApiStatus.NonExtendable
16+
public interface PacketSender {
17+
/**
18+
* Creates a packet from a packet payload.
19+
*
20+
* @param payload the packet payload
21+
*/
22+
Packet<?> createPacket(CustomPayload payload);
23+
24+
/**
25+
* Sends a packet.
26+
*
27+
* @param packet the packet
28+
*/
29+
default void sendPacket(Packet<?> packet) {
30+
sendPacket(packet, null);
31+
}
32+
33+
/**
34+
* Sends a packet.
35+
* @param payload the payload
36+
*/
37+
default void sendPacket(CustomPayload payload) {
38+
sendPacket(createPacket(payload));
39+
}
40+
41+
/**
42+
* Sends a packet.
43+
*
44+
* @param packet the packet
45+
* @param callback an optional callback to execute after the packet is sent, may be {@code null}.
46+
*/
47+
void sendPacket(Packet<?> packet, @Nullable PacketCallbacks callback);
48+
49+
/**
50+
* Sends a packet.
51+
*
52+
* @param payload the payload
53+
* @param callback an optional callback to execute after the packet is sent, may be {@code null}.
54+
*/
55+
default void sendPacket(CustomPayload payload, @Nullable PacketCallbacks callback) {
56+
sendPacket(createPacket(payload), callback);
57+
}
58+
59+
/**
60+
* Disconnects the player.
61+
* @param disconnectReason the reason for disconnection
62+
*/
63+
void disconnect(Text disconnectReason);
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package band.kessoku.lib.api.networking;
2+
3+
import band.kessoku.lib.impl.networking.PayloadTypeRegistryImpl;
4+
import net.minecraft.network.PacketByteBuf;
5+
import net.minecraft.network.RegistryByteBuf;
6+
import net.minecraft.network.codec.PacketCodec;
7+
import net.minecraft.network.packet.CustomPayload;
8+
import org.jetbrains.annotations.ApiStatus;
9+
10+
/**
11+
* A registry for payload types.
12+
*/
13+
@ApiStatus.NonExtendable
14+
public interface PayloadTypeRegistry<B extends PacketByteBuf> {
15+
16+
/**
17+
* Registers a custom payload type.
18+
*
19+
* <p>This must be done on both the sending and receiving side, usually during mod initialization
20+
* and <strong>before registering a packet handler</strong>.
21+
*
22+
* @param id the id of the payload type
23+
* @param codec the codec for the payload type
24+
* @param <T> the payload type
25+
* @return the registered payload type
26+
*/
27+
<T extends CustomPayload> CustomPayload.Type<? super B, T> register(CustomPayload.Id<T> id, PacketCodec<? super B, T> codec);
28+
29+
/**
30+
* @return the {@link PayloadTypeRegistry} instance for the client to server configuration channel.
31+
*/
32+
static PayloadTypeRegistry<PacketByteBuf> configC2S() {
33+
return PayloadTypeRegistryImpl.CONFIG_C2S;
34+
}
35+
36+
/**
37+
* @return the {@link PayloadTypeRegistry} instance for the server to client configuration channel.
38+
*/
39+
static PayloadTypeRegistry<PacketByteBuf> configS2C() {
40+
return PayloadTypeRegistryImpl.CONFIG_S2C;
41+
}
42+
43+
/**
44+
* @return the {@link PayloadTypeRegistry} instance for the client to server play channel.
45+
*/
46+
static PayloadTypeRegistry<RegistryByteBuf> playC2S() {
47+
return PayloadTypeRegistryImpl.PLAY_C2S;
48+
}
49+
50+
/**
51+
* @return the {@link PayloadTypeRegistry} instance for the server to client play channel.
52+
*/
53+
static PayloadTypeRegistry<RegistryByteBuf> playS2C() {
54+
return PayloadTypeRegistryImpl.PLAY_S2C;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package band.kessoku.lib.api.networking.util;
1+
package band.kessoku.lib.api.networking;
22

33
import net.minecraft.server.network.ServerPlayerConfigurationTask;
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package band.kessoku.lib.api.networking.client;
2+
3+
import java.util.List;
4+
5+
import net.minecraft.client.MinecraftClient;
6+
import net.minecraft.client.network.ClientConfigurationNetworkHandler;
7+
import net.minecraft.util.Identifier;
8+
9+
import band.kessoku.lib.event.api.Event;
10+
import band.kessoku.lib.api.networking.PacketSender;
11+
12+
/**
13+
* Offers access to events related to the indication of a connected server's ability to receive packets in certain channels.
14+
*/
15+
public final class C2SConfigurationChannelEvent {
16+
/**
17+
* An event for the client configuration network handler receiving an update indicating the connected server's ability to receive packets in certain channels.
18+
* This event may be invoked at any time after login and up to disconnection.
19+
*/
20+
public static final Event<Register> REGISTER = Event.of(registers -> (handler, sender, client, channels) -> {
21+
for (Register callback : registers) {
22+
callback.onChannelRegister(handler, sender, client, channels);
23+
}
24+
});
25+
26+
/**
27+
* 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.
28+
* This event may be invoked at any time after login and up to disconnection.
29+
*/
30+
public static final Event<Unregister> UNREGISTER = Event.of(unregisters -> (handler, sender, client, channels) -> {
31+
for (Unregister callback : unregisters) {
32+
callback.onChannelUnregister(handler, sender, client, channels);
33+
}
34+
});
35+
36+
/**
37+
* @see C2SConfigurationChannelEvent#REGISTER
38+
*/
39+
@FunctionalInterface
40+
public interface Register {
41+
void onChannelRegister(ClientConfigurationNetworkHandler handler, PacketSender sender, MinecraftClient client, List<Identifier> channels);
42+
}
43+
44+
/**
45+
* @see C2SConfigurationChannelEvent#UNREGISTER
46+
*/
47+
@FunctionalInterface
48+
public interface Unregister {
49+
void onChannelUnregister(ClientConfigurationNetworkHandler handler, PacketSender sender, MinecraftClient client, List<Identifier> channels);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package band.kessoku.lib.api.networking.client;
2+
3+
import java.util.List;
4+
5+
import net.minecraft.client.MinecraftClient;
6+
import net.minecraft.client.network.ClientPlayNetworkHandler;
7+
import net.minecraft.util.Identifier;
8+
9+
import band.kessoku.lib.event.api.Event;
10+
import band.kessoku.lib.api.networking.PacketSender;
11+
12+
/**
13+
* Offers access to events related to the indication of a connected server's ability to receive packets in certain channels.
14+
*/
15+
public final class C2SPlayChannelEvent {
16+
/**
17+
* An event for the client play network handler receiving an update indicating the connected server's ability to receive packets in certain channels.
18+
* This event may be invoked at any time after login and up to disconnection.
19+
*/
20+
public static final Event<Register> REGISTER = Event.of(registers -> (handler, sender, client, channels) -> {
21+
for (Register callback : registers) {
22+
callback.onChannelRegister(handler, sender, client, channels);
23+
}
24+
});
25+
26+
/**
27+
* 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.
28+
* This event may be invoked at any time after login and up to disconnection.
29+
*/
30+
public static final Event<Unregister> UNREGISTER = Event.of(unregisters -> (handler, sender, client, channels) -> {
31+
for (Unregister callback : unregisters) {
32+
callback.onChannelUnregister(handler, sender, client, channels);
33+
}
34+
});
35+
36+
/**
37+
* @see C2SPlayChannelEvent#REGISTER
38+
*/
39+
@FunctionalInterface
40+
public interface Register {
41+
void onChannelRegister(ClientPlayNetworkHandler handler, PacketSender sender, MinecraftClient client, List<Identifier> channels);
42+
}
43+
44+
/**
45+
* @see C2SPlayChannelEvent#UNREGISTER
46+
*/
47+
@FunctionalInterface
48+
public interface Unregister {
49+
void onChannelUnregister(ClientPlayNetworkHandler handler, PacketSender sender, MinecraftClient client, List<Identifier> channels);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package band.kessoku.lib.api.networking.client;
2+
3+
import net.minecraft.client.MinecraftClient;
4+
import net.minecraft.client.network.ClientConfigurationNetworkHandler;
5+
import net.minecraft.network.packet.CustomPayload;
6+
7+
import band.kessoku.lib.event.api.Event;
8+
9+
/**
10+
* Offers access to events related to the configuration connection to a server on a logical client.
11+
*/
12+
public final class ClientConfigurationConnectionEvent {
13+
/**
14+
* Event indicating a connection entering the CONFIGURATION state, ready for registering channel handlers.
15+
*
16+
* <p>No packets should be sent when this event is invoked.
17+
*
18+
* @see ClientConfigurationNetworking#registerReceiver(CustomPayload.Id, ClientConfigurationNetworking.ConfigurationPayloadHandler)
19+
*/
20+
public static final Event<Init> INIT = Event.of(inits -> (handler, client) -> {
21+
for (ClientConfigurationConnectionEvent.Init callback : inits) {
22+
callback.onConfigurationInit(handler, client);
23+
}
24+
});
25+
26+
/**
27+
* An event called after the connection has been initialized and is ready to start sending and receiving configuration packets.
28+
*
29+
* <p>Packets may be sent during this event.
30+
*/
31+
public static final Event<Start> START = Event.of(starts -> (handler, client) -> {
32+
for (ClientConfigurationConnectionEvent.Start callback : starts) {
33+
callback.onConfigurationStart(handler, client);
34+
}
35+
});
36+
37+
/**
38+
* An event called after the ReadyS2CPacket has been received, just before switching to the PLAY state.
39+
*
40+
* <p>No packets should be sent when this event is invoked.
41+
*/
42+
public static final Event<Complete> COMPLETE = Event.of(completes -> (handler, client) -> {
43+
for (ClientConfigurationConnectionEvent.Complete callback : completes) {
44+
callback.onConfigurationComplete(handler, client);
45+
}
46+
});
47+
48+
/**
49+
* An event for the disconnection of the client configuration network handler.
50+
*
51+
* <p>No packets should be sent when this event is invoked.
52+
*/
53+
public static final Event<Disconnect> DISCONNECT = Event.of(disconnects -> (handler, client) -> {
54+
for (ClientConfigurationConnectionEvent.Disconnect callback : disconnects) {
55+
callback.onConfigurationDisconnect(handler, client);
56+
}
57+
});
58+
59+
@FunctionalInterface
60+
public interface Init {
61+
void onConfigurationInit(ClientConfigurationNetworkHandler handler, MinecraftClient client);
62+
}
63+
64+
@FunctionalInterface
65+
public interface Start {
66+
void onConfigurationStart(ClientConfigurationNetworkHandler handler, MinecraftClient client);
67+
}
68+
69+
@FunctionalInterface
70+
public interface Complete {
71+
void onConfigurationComplete(ClientConfigurationNetworkHandler handler, MinecraftClient client);
72+
}
73+
74+
@FunctionalInterface
75+
public interface Disconnect {
76+
void onConfigurationDisconnect(ClientConfigurationNetworkHandler handler, MinecraftClient client);
77+
}
78+
}

0 commit comments

Comments
 (0)