Skip to content

Commit

Permalink
Initial project structure (#3)
Browse files Browse the repository at this point in the history
* Initial gradle project structure

* An initial structure of packet reading

* chore: rename NetworkManager to JetPlayerConnection, feat: add a PlayerConnection interface to API and create a PlayerChannelInitializer

* feat: client-bound packets

* chore: move packets to interfaces

* chore: more effective packet encoding

* chore: client-bound packet implementations to their packet readers

* chore: remove useless protocol version from PlayerConnection

* chore: safer handshake packet reading

* chore: remove useless ReadOnlyNetworkBuffer from API and merger it with NetworkBuffer

* chore: add more informative javadocs

* feat: add tests for NetworkBuffer and NetworkUtil

* fix: NetworkUtil#readString does not check remaining amount of readable bytes

* feat: add tests for reading packets

* chore: add comments with reason why NetworkBufferImpl are not implemented *yet*

* feat: add a maven publication

* feat: add logging to exception handling

* fix: packet encoder encodes a packet length with var-long
  • Loading branch information
Codestech1 authored May 28, 2024
1 parent b353663 commit 81ac3c5
Show file tree
Hide file tree
Showing 34 changed files with 2,238 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### IntelliJ IDEA ###
.idea

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
15 changes: 15 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
`maven-publish`
alias(libs.plugins.checker.framework)
}

dependencies {
api(libs.adventure)
api(libs.slf4j)
}

publishing {
publications.create<MavenPublication>("maven") {
from(components["java"])
}
}
285 changes: 285 additions & 0 deletions api/src/main/java/net/hypejet/jet/buffer/NetworkBuffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
package net.hypejet.jet.buffer;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.common.value.qual.IntRange;

import java.util.UUID;

/**
* Represents a container of data allowed by Minecraft protocol.
*
* @since 1.0
* @author Codestech
*/
public interface NetworkBuffer {
/**
* Reads a boolean from the buffer.
*
* @return the boolean
* @since 1.0
*/
boolean readBoolean();

/**
* Writes a boolean to the buffer.
*
* @param value the boolean
* @since 1.0
*/
void writeBoolean(boolean value);

/**
* Reads a byte from the buffer.
*
* @return the byte
* @since 1.0
*/
byte readByte();

/**
* Writes a byte to the buffer.
*
* @param value the byte
* @since 1.0
*/
void writeByte(byte value);

/**
* Reads an unsigned byte from the buffer.
*
* @return the unsigned byte
* @since 1.0
*/
@IntRange(from = 0, to = 255)
short readUnsignedByte();

/**
* Writes an unsigned byte to the buffer.
*
* @param value the unsigned byte
* @since 1.0
*/
void writeUnsignedByte(@IntRange(from = 0, to = 255) short value);

/**
* Reads a short from the buffer.
*
* @return the short
* @since 1.0
*/
short readShort();

/**
* Writes a short to the buffer.
*
* @param value the short
* @since 1.0
*/
void writeShort(short value);

/**
* Reads an unsigned short from the buffer.
*
* @return the unsigned short
* @since 1.0
*/
@IntRange(from = 0, to = 65535) int readUnsignedShort();

/**
* Writes an unsigned short to the buffer.
*
* @param value the unsigned short
* @since 1.0
*/
void writeUnsignedShort(@IntRange(from = 0, to = 65535) int value);

/**
* Reads an integer from the buffer.
*
* @return the integer
* @since 1.0
*/
int readInt();

/**
* Writes an integer to the buffer.
*
* @param value the integer
* @since 1.0
*/
void writeInt(int value);

/**
* Reads a long from the buffer.
*
* @return the long
* @since 1.0
*/
long readLong();

/**
* Writes a long to the buffer.
*
* @param value the long
* @since 1.0
*/
void writeLong(long value);

/**
* Reads a float from the buffer.
*
* @return the float
* @since 1.0
*/
float readFloat();

/**
* Writes a float to the buffer.
*
* @param value the float
* @since 1.0
*/
void writeFloat(float value);

/**
* Reads a double from the buffer.
*
* @return the double
* @since 1.0
*/
double readDouble();

/**
* Writes a double to the buffer.
*
* @param value the double
* @since 1.0
*/
void writeDouble(double value);

/**
* Reads a string from the buffer.
*
* @return the string
* @since 1.0
*/
@NonNull
String readString();

/**
* Writes a string to the buffer.
*
* @param value the string
* @since 1.0
*/
void writeString(@NonNull String value);

/**
* Reads a text component, which is serialized as an NBT tag.
*
* @return the text component
*/
@NonNull Component readTextComponent();

/**
* Writes a text component, which is serialized as an NBT tag.
*
* @param value the text component
* @since 1.0
*/
void writeTextComponent(@NonNull Component value);

/**
* Reads a text component, which is serialized as a json string.
*
* @return the text component
* @since 1.0
*/
@NonNull Component readJsonTextComponent();

/**
* Reads a text component, which is serialized as a json string.
*
* @param value the text component
* @since 1.0
*/
void writeJsonTextComponent(@NonNull Component value);


/**
* Reads a variable-length integer from the buffer.
*
* @return the variable-length integer
* @since 1.0
*/
int readVarInt();

/**
* Writes a variable-length integer to the buffer.
*
* @param value the variable-length integer
* @since 1.0
*/
void writeVarInt(int value);

/**
* Reads a variable-length long from the buffer.
*
* @return the variable-length long
* @since 1.0
*/
long readVarLong();

/**
* Writes a variable-length long to the buffer.
*
* @param value the variable-length long
* @since 1.0
*/
void writeVarLong(long value);

/**
* Reads an unique id from the buffer.
*
* @return the unique id
* @since 1.0
*/
@NonNull UUID readUniqueId();

/**
* Writes a unique id to the buffer.
*
* @param value the unique id
* @since 1.0
*/
void writeUniqueId(@NonNull UUID value);

/**
* Reads an identifier from the buffer.
*
* @return the identifier
* @since 1.0
*/
default @NonNull Key readIdentifier() {
String identifier = this.readString();
if (!Key.parseable(identifier)) throw identifierNotParseable(identifier);
// Use another method to create a key to avoid warning in IDE, which are already solved
return Key.key(identifier, Key.DEFAULT_SEPARATOR);
}

/**
* Writes an identifier to the buffer.
*
* @param identifier the identifier
* @since 1.0
*/
default void writeIdentifier(@NonNull Key identifier) {
this.writeString(identifier.asString());
}

private static @NonNull RuntimeException identifierNotParseable(@NonNull String identifier) {
return new IllegalArgumentException("An identifier of \"" + identifier + "\" contains illegal characters");
}
}
39 changes: 39 additions & 0 deletions api/src/main/java/net/hypejet/jet/player/PlayerConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.hypejet.jet.player;

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

/**
* Represents a Minecraft protocol connection.
*
* @since 1.0
* @author Codestech
*/
public interface PlayerConnection {
/**
* Gets a protocol state of the connection.
*
* @return the protocol state
* @since 1.0
*/
@NonNull ProtocolState getProtocolState();

/**
* Sends a {@link ClientBoundPacket client-bound packet} to a client.
*
* @param packet the paket
* @since 1.0
*/
void sendPacket(@NonNull ClientBoundPacket packet);

/**
* Sends a {@link DisconnectPacket disconnect packet} and closes the connection.
*
* @param reason a reason of the disconnection
* @since 1.0
*/
void kick(@NonNull Component reason);
}
Loading

0 comments on commit 81ac3c5

Please sign in to comment.