Skip to content

Commit

Permalink
chore: move packets to interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Codestech1 committed May 28, 2024
1 parent 7a60dd4 commit 60e0a18
Show file tree
Hide file tree
Showing 19 changed files with 258 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.hypejet.jet.protocol.ProtocolState;
import net.hypejet.jet.protocol.packet.clientbound.ClientBoundPacket;
import net.hypejet.jet.protocol.packet.clientbound.login.DisconnectPacket;
import net.hypejet.jet.protocol.packet.clientbound.login.disconnect.DisconnectPacket;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,28 @@
* @since 1.0
* @author Codestech
*/
public abstract class ClientBoundPacket {

private final int packetId;
private final ProtocolState protocolState;

/**
* Constructs a {@link ClientBoundPacket client-bound packet}.
*
* @param packetId an id of the packet
* @param protocolState a protocol state of the packet
* @since 1.0
*/
public ClientBoundPacket(int packetId, @NonNull ProtocolState protocolState) {
this.packetId = packetId;
this.protocolState = protocolState;
}

public interface ClientBoundPacket {
/**
* Gets an identifier of the packet.
*
* @return the identifier.
* @since 1.0
*/
public final int getPacketId() {
return this.packetId;
}
int getPacketId();

/**
* Gets a {@link ProtocolState protocol state} of the packet.
*
* @return the protocol state.
* @since 1.0
*/
public final @NonNull ProtocolState getProtocolState() {
return this.protocolState;
}
@NonNull ProtocolState getProtocolState();

/**
* Writes the packet to a {@link NetworkBuffer network buffer}.
*
* @param buffer the byte buffer
* @since 1.0
*/
public abstract void write(@NonNull NetworkBuffer buffer);
void write(@NonNull NetworkBuffer buffer);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.hypejet.jet.protocol.packet.clientbound.login.disconnect;

import net.hypejet.jet.protocol.packet.clientbound.ClientBoundPacket;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;

/**
* Represents a {@link ClientBoundPacket client-bound packet} sent by a server to disconnect a player.
*
* @since 1.0
* @author Codestech
*/
public sealed interface DisconnectPacket extends ClientBoundPacket permits DisconnectPacketImpl {
/**
* Gets a reason of the disconnection.
*
* @return the reason
* @since 1.0
*/
@NonNull Component reason();

/**
* Creates a new {@link Builder disconnect packet builder}.
*
* @return the builder
* @since 1.0
*/
static @NonNull Builder builder() {
return new DisconnectPacketImpl.Builder();
}

/**
* Represents a builder that creates a {@link DisconnectPacket disconnect packet}.
*
* @since 1.0
* @author Codestech
*/
interface Builder {
/**
* Sets a reason of the disconnection.
*
* @param reason the reason
* @return the builder
* @since 1.0
*/
@NonNull Builder reason(@NonNull Component reason);

/**
* Builds the {@link DisconnectPacket disconnect packet}.
*
* @return the disconnect packet
* @since 1.0
*/
@NonNull DisconnectPacket build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.hypejet.jet.protocol.packet.clientbound.login.disconnect;

import net.hypejet.jet.buffer.NetworkBuffer;
import net.hypejet.jet.protocol.ProtocolState;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;

/**
* Represents an implementation of {@link DisconnectPacket disconnect packet}.
*
* @param reason a reason of the disconnection
* @since 1.0
* @author Codestech
* @see DisconnectPacket
*/
record DisconnectPacketImpl(@NonNull Component reason) implements DisconnectPacket {

@Override
public void write(@NonNull NetworkBuffer buffer) {
buffer.writeJsonTextComponent(this.reason);
}

@Override
public int getPacketId() {
return 0;
}

@Override
public @NonNull ProtocolState getProtocolState() {
return ProtocolState.LOGIN;
}

/**
* Represents an implementation of {@link DisconnectPacket.Builder disconnect packet builder}.
*
* @since 1.0
* @author Codestech
* @see DisconnectPacket.Builder
*/
static final class Builder implements DisconnectPacket.Builder {

private Component reason = Component.empty();

@Override
public DisconnectPacket.@NonNull Builder reason(@NonNull Component reason) {
this.reason = reason;
return this;
}

@Override
public @NonNull DisconnectPacket build() {
return new DisconnectPacketImpl(this.reason);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
package net.hypejet.jet.protocol.packet.serverbound.handshake;

import net.hypejet.jet.player.PlayerConnection;
import net.hypejet.jet.protocol.ProtocolState;
import net.hypejet.jet.protocol.packet.serverbound.ServerBoundPacket;
import org.checkerframework.checker.nullness.qual.NonNull;

/**
* Represents a {@link ServerBoundPacket server-bound packet} that initializes connection of a player.
*
* @param protocolVersion a version of the Minecraft protocol
* @param serverAddress an address of the server
* @param serverPort a port of the server
* @param nextState a next protocol state, which the connection will switch to
* @since 1.0
* @author Codestech
*/
public record HandshakePacket(int protocolVersion, @NonNull String serverAddress,
int serverPort, @NonNull ProtocolState nextState) implements ServerBoundPacket {}
public interface HandshakePacket extends ServerBoundPacket {
/**
* Gets a version of the Minecraft protocol.
*
* @return the version
* @since 1.0
*/
int protocolVersion();

/**
* Gets an address of a server that the client tries to connect to.
*
* @return the address
* @since 1.0
*/
@NonNull String serverAddress();

/**
* Gets a port of a server that the client tries to connect to.
*
* @return the port
* @since 1.0
*/
int serverPort();

/**
* Gets a next {@link ProtocolState protocol state}, which a {@link PlayerConnection player connection} will switch
* to.
*
* @return the protocol state
* @since 1.0
*/
@NonNull ProtocolState nextState();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,23 @@
/**
* Represents a {@link ServerBoundPacket server-bound packet} that requests a server to send a login data.
*
* @param username a username of a player that sends the packet
* @param uniqueId a unique id of a player that sends the packet
* @since 1.0
* @author Codestech
*/
public record LoginRequestPacket(@NonNull String username, @NonNull UUID uniqueId)
implements ServerBoundPacket {}
public interface LoginRequestPacket extends ServerBoundPacket {
/**
* Gets a username of a player that sends the packet.
*
* @return the username
* @since 1.0
*/
@NonNull String username();

/**
* Gets a {@link UUID unique identifier} of a player that sends the packet.
*
* @return the unique identifier
* @since 1.0
*/
@NonNull UUID uniqueId();
}
4 changes: 2 additions & 2 deletions server/src/main/java/net/hypejet/jet/server/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import net.hypejet.jet.server.network.PlayerChannelInitializer;
import net.hypejet.jet.server.protocol.JetServerBoundPacketRegistry;
import net.hypejet.jet.server.protocol.ServerBoundPacketRegistry;

public class Main {
public static void main(String[] args) {
Expand All @@ -17,7 +17,7 @@ public static void main(String[] args) {
ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new PlayerChannelInitializer(new JetServerBoundPacketRegistry()));
.childHandler(new PlayerChannelInitializer(new ServerBoundPacketRegistry()));

ChannelFuture future = bootstrap.bind(25565).sync();
future.channel().closeFuture().sync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import net.hypejet.jet.protocol.packet.serverbound.ServerBoundPacketRegistry;
import net.hypejet.jet.server.network.serialization.PacketDecoder;
import net.hypejet.jet.server.network.serialization.PacketEncoder;
import net.hypejet.jet.server.network.serialization.PacketLengthEncoder;
import net.hypejet.jet.server.player.JetPlayerConnection;
import net.hypejet.jet.server.protocol.ServerBoundPacketRegistry;
import org.checkerframework.checker.nullness.qual.NonNull;

public final class PlayerChannelInitializer extends ChannelInitializer<SocketChannel> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import net.hypejet.jet.buffer.ReadOnlyNetworkBuffer;
import net.hypejet.jet.protocol.ProtocolState;
import net.hypejet.jet.protocol.packet.serverbound.ServerBoundPacket;
import net.hypejet.jet.protocol.packet.serverbound.ServerBoundPacketRegistry;
import net.hypejet.jet.server.buffer.ReadOnlyNetworkBufferImpl;
import net.hypejet.jet.server.player.JetPlayerConnection;
import net.hypejet.jet.server.protocol.ServerBoundPacketRegistry;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.List;
Expand Down
Loading

0 comments on commit 60e0a18

Please sign in to comment.