Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
TexBlock committed Oct 13, 2024
1 parent 1710e9f commit 08637c9
Show file tree
Hide file tree
Showing 15 changed files with 410 additions and 1 deletion.
1 change: 0 additions & 1 deletion gradle/example/common/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
apply from: rootProject.file("gradle/scripts/klib-common.gradle")

group = "band.kessoku.lib.package"
base.archivesName = rootProject.name + "-package"

// You can delete this section if you need to
Expand Down
9 changes: 9 additions & 0 deletions networking/common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apply from: rootProject.file("gradle/scripts/klib-common.gradle")

base.archivesName = rootProject.name + "-package"

kessoku {
modules(["base", "event-base"], "common")

testModule("event-base", "common")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package band.kessoku.lib.api.networking;

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

public 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 + "]");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package band.kessoku.lib.api.networking.util;

import band.kessoku.lib.impl.networking.ChannelRegisterImpl;
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 ChannelRegister<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 ChannelRegister} instance for the client to server configuration channel.
*/
static ChannelRegister<PacketByteBuf> configC2S() {
return ChannelRegisterImpl.CONFIG_C2S;
}

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

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

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

import band.kessoku.lib.impl.networking.AbstractNetworkAddon;

public interface NetworkHandlerExtension {
AbstractNetworkAddon<?> getAddon();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package band.kessoku.lib.api.networking.util;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minecraft.network.PacketByteBuf;

import java.util.Objects;

/**
* Helper methods for working with and creating {@link PacketByteBuf}s.
*/
public final class PacketByteBufHelper {
private static final PacketByteBuf EMPTY_PACKET_BYTE_BUF = new PacketByteBuf(Unpooled.EMPTY_BUFFER);

/**
* Returns an empty instance of packet byte buf.
*
* @return an empty buf
*/
public static PacketByteBuf empty() {
return EMPTY_PACKET_BYTE_BUF;
}

/**
* Returns a new heap memory-backed instance of packet byte buf.
*
* @return a new buf
*/
public static PacketByteBuf create() {
return new PacketByteBuf(Unpooled.buffer());
}

// Convenience methods for byte buf methods that return a new byte buf

/**
* Wraps the newly created buf from {@code buf.readBytes} in a packet byte buf.
*
* @param buf the original buf
* @param length the number of bytes to transfer
* @return the transferred bytes
* @see ByteBuf#readBytes(int)
*/
public static PacketByteBuf readBytes(ByteBuf buf, int length) {
Objects.requireNonNull(buf, "ByteBuf cannot be null");

return new PacketByteBuf(buf.readBytes(length));
}

/**
* Wraps the newly created buf from {@code buf.readSlice} in a packet byte buf.
*
* @param buf the original buf
* @param length the size of the new slice
* @return the newly created slice
* @see ByteBuf#readSlice(int)
*/
public static PacketByteBuf readSlice(ByteBuf buf, int length) {
Objects.requireNonNull(buf, "ByteBuf cannot be null");

return new PacketByteBuf(buf.readSlice(length));
}

/**
* Wraps the newly created buf from {@code buf.readRetainedSlice} in a packet byte buf.
*
* @param buf the original buf
* @param length the size of the new slice
* @return the newly created slice
* @see ByteBuf#readRetainedSlice(int)
*/
public static PacketByteBuf readRetainedSlice(ByteBuf buf, int length) {
Objects.requireNonNull(buf, "ByteBuf cannot be null");

return new PacketByteBuf(buf.readRetainedSlice(length));
}

/**
* Wraps the newly created buf from {@code buf.copy} in a packet byte buf.
*
* @param buf the original buf
* @return a copy of the buf
* @see ByteBuf#copy()
*/
public static PacketByteBuf copy(ByteBuf buf) {
Objects.requireNonNull(buf, "ByteBuf cannot be null");

return new PacketByteBuf(buf.copy());
}

/**
* Wraps the newly created buf from {@code buf.copy} in a packet byte buf.
*
* @param buf the original buf
* @param index the starting index
* @param length the size of the copy
* @return a copy of the buf
* @see ByteBuf#copy(int, int)
*/
public static PacketByteBuf copy(ByteBuf buf, int index, int length) {
Objects.requireNonNull(buf, "ByteBuf cannot be null");

return new PacketByteBuf(buf.copy(index, length));
}

/**
* Wraps the newly created buf from {@code buf.slice} in a packet byte buf.
*
* @param buf the original buf
* @return a slice of the buf
* @see ByteBuf#slice()
*/
public static PacketByteBuf slice(ByteBuf buf) {
Objects.requireNonNull(buf, "ByteBuf cannot be null");

return new PacketByteBuf(buf.slice());
}

/**
* Wraps the newly created buf from {@code buf.retainedSlice} in a packet byte buf.
*
* @param buf the original buf
* @return a slice of the buf
* @see ByteBuf#retainedSlice()
*/
public static PacketByteBuf retainedSlice(ByteBuf buf) {
Objects.requireNonNull(buf, "ByteBuf cannot be null");

return new PacketByteBuf(buf.retainedSlice());
}

/**
* Wraps the newly created buf from {@code buf.slice} in a packet byte buf.
*
* @param buf the original buf
* @param index the starting index
* @param length the size of the copy
* @return a slice of the buf
* @see ByteBuf#slice(int, int)
*/
public static PacketByteBuf slice(ByteBuf buf, int index, int length) {
Objects.requireNonNull(buf, "ByteBuf cannot be null");

return new PacketByteBuf(buf.slice(index, length));
}

/**
* Wraps the newly created buf from {@code buf.retainedSlice} in a packet byte buf.
*
* @param buf the original buf
* @param index the starting index
* @param length the size of the copy
* @return a slice of the buf
* @see ByteBuf#retainedSlice(int, int)
*/
public static PacketByteBuf retainedSlice(ByteBuf buf, int index, int length) {
Objects.requireNonNull(buf, "ByteBuf cannot be null");

return new PacketByteBuf(buf.retainedSlice(index, length));
}

/**
* Wraps the newly created buf from {@code buf.duplicate} in a packet byte buf.
*
* @param buf the original buf
* @return a duplicate of the buf
* @see ByteBuf#duplicate()
*/
public static PacketByteBuf duplicate(ByteBuf buf) {
Objects.requireNonNull(buf, "ByteBuf cannot be null");

return new PacketByteBuf(buf.duplicate());
}

/**
* Wraps the newly created buf from {@code buf.retainedDuplicate} in a packet byte buf.
*
* @param buf the original buf
* @return a duplicate of the buf
* @see ByteBuf#retainedDuplicate()
*/
public static PacketByteBuf retainedDuplicate(ByteBuf buf) {
Objects.requireNonNull(buf, "ByteBuf cannot be null");

return new PacketByteBuf(buf.retainedDuplicate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package band.kessoku.lib.api.networking.util;

import net.minecraft.server.network.ServerPlayerConfigurationTask;

public interface ServerConfigurationNetworkHandlerExtension {
/**
* Enqueues a {@link ServerPlayerConfigurationTask} task to be processed.
*
* <p>Before adding a task use {@link ServerConfigurationNetworking#canSend(ServerConfigurationNetworkHandler, Identifier)}
* to ensure that the client can process this task.
*
* <p>Once the client has handled the task a packet should be sent to the server.
* Upon receiving this packet the server should call {@link ServerConfigurationNetworkHandlerExtension#kessokulib$completeTask(ServerPlayerConfigurationTask.Key)},
* otherwise the client cannot join the world.
*
* @param task the task
*/
default void kessokulib$addTask(ServerPlayerConfigurationTask task) {
throw new UnsupportedOperationException("Implemented via mixin");
}

/**
* Completes the task identified by {@code key}.
*
* @param key the task key
* @throws IllegalStateException if the current task is not {@code key}
*/
default void kessokulib$completeTask(ServerPlayerConfigurationTask.Key key) {
throw new UnsupportedOperationException("Implemented via mixin");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package band.kessoku.lib.impl.networking;

/**
* A network addon is a simple abstraction to hold information about a player's registered channels.
*
* @param <H> the channel handler type
*/
public abstract class AbstractNetworkAddon<H> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package band.kessoku.lib.impl.networking;

import band.kessoku.lib.api.networking.util.ChannelRegister;
import net.minecraft.network.NetworkPhase;
import net.minecraft.network.NetworkSide;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class ChannelRegisterImpl<B extends PacketByteBuf> implements ChannelRegister<B> {
public static final ChannelRegisterImpl<PacketByteBuf> CONFIG_C2S = new ChannelRegisterImpl<>(NetworkPhase.CONFIGURATION, NetworkSide.SERVERBOUND);
public static final ChannelRegisterImpl<PacketByteBuf> CONFIG_S2C = new ChannelRegisterImpl<>(NetworkPhase.CONFIGURATION, NetworkSide.CLIENTBOUND);
public static final ChannelRegisterImpl<RegistryByteBuf> PLAY_C2S = new ChannelRegisterImpl<>(NetworkPhase.PLAY, NetworkSide.SERVERBOUND);
public static final ChannelRegisterImpl<RegistryByteBuf> PLAY_S2C = new ChannelRegisterImpl<>(NetworkPhase.PLAY, NetworkSide.CLIENTBOUND);

private final Map<Identifier, CustomPayload.Type<B, ? extends CustomPayload>> packetTypes = new HashMap<>();
private final NetworkPhase state;
private final NetworkSide side;

private ChannelRegisterImpl(NetworkPhase state, NetworkSide side) {
this.state = state;
this.side = side;
}
@Override
public <T extends CustomPayload> CustomPayload.Type<? super B, T> register(CustomPayload.Id<T> id, PacketCodec<? super B, T> codec) {
Objects.requireNonNull(id, "id");
Objects.requireNonNull(codec, "codec");

final CustomPayload.Type<B, T> payloadType = new CustomPayload.Type<>(id, codec.cast());

if (packetTypes.containsKey(id.id())) {
throw new IllegalArgumentException("Packet type " + id + " is already registered!");
}

packetTypes.put(id.id(), payloadType);
return payloadType;
}

@Nullable
public CustomPayload.Type<B, ? extends CustomPayload> get(Identifier id) {
return packetTypes.get(id);
}

@Nullable
public <T extends CustomPayload> CustomPayload.Type<B, T> get(CustomPayload.Id<T> id) {
//noinspection unchecked
return (CustomPayload.Type<B, T>) packetTypes.get(id.id());
}

public NetworkPhase getPhase() {
return state;
}

public NetworkSide getSide() {
return side;
}
}
12 changes: 12 additions & 0 deletions networking/fabric/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import net.fabricmc.loom.util.ModPlatform

apply from: rootProject.file("gradle/scripts/klib-fabric.gradle")

base.archivesName = rootProject.name + "-networking"

kessoku {
module("base", "common")

common("networking", ModPlatform.FABRIC)
shadowBundle("networking", ModPlatform.FABRIC)
}
Loading

0 comments on commit 08637c9

Please sign in to comment.