Skip to content

Commit

Permalink
Refactor base packet class
Browse files Browse the repository at this point in the history
  • Loading branch information
Melledy committed Nov 30, 2023
1 parent 3981cae commit f801b44
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions src/main/java/emu/lunarcore/server/packet/BasePacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,28 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import lombok.Getter;
import us.hebi.quickbuf.ProtoMessage;

@Getter
public class BasePacket {
public static final int HEADER_CONST = 0x9d74c714;
public static final int TAIL_CONST = 0xd7a152c8;

private int opcode;
private int cmdId;
private byte[] data;

// Encryption
private boolean useDispatchKey;
public boolean shouldEncrypt = true;

public BasePacket(int opcode) {
this.opcode = opcode;
public BasePacket(int cmdId) {
this.cmdId = cmdId;
}

public int getOpcode() {
return opcode;

public BasePacket(int cmdId, byte[] data) {
this.cmdId = cmdId;
this.data = data;
}

public void setOpcode(int opcode) {
this.opcode = opcode;
}

public boolean useDispatchKey() {
return useDispatchKey;
}

public void setUseDispatchKey(boolean useDispatchKey) {
this.useDispatchKey = useDispatchKey;
}

public byte[] getData() {
return data;
this.cmdId = opcode;
}

public void setData(byte[] data) {
Expand All @@ -56,7 +43,7 @@ public byte[] build() {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4 + 2 + 4 + getData().length + 4);

this.writeUint32(baos, HEADER_CONST);
this.writeUint16(baos, opcode);
this.writeUint16(baos, cmdId);
this.writeUint16(baos, 0); // Empty header
this.writeUint32(baos, data.length);
this.writeBytes(baos, data);
Expand All @@ -67,21 +54,21 @@ public byte[] build() {
return packet;
}

public void writeUint16(ByteArrayOutputStream baos, int i) {
private void writeUint16(ByteArrayOutputStream baos, int i) {
// Unsigned short
baos.write((byte) ((i >>> 8) & 0xFF));
baos.write((byte) (i & 0xFF));
}

public void writeUint32(ByteArrayOutputStream baos, int i) {
private void writeUint32(ByteArrayOutputStream baos, int i) {
// Unsigned int (long)
baos.write((byte) ((i >>> 24) & 0xFF));
baos.write((byte) ((i >>> 16) & 0xFF));
baos.write((byte) ((i >>> 8) & 0xFF));
baos.write((byte) (i & 0xFF));
}

public void writeBytes(ByteArrayOutputStream baos, byte[] bytes) {
private void writeBytes(ByteArrayOutputStream baos, byte[] bytes) {
try {
baos.write(bytes);
} catch (IOException e) {
Expand Down

0 comments on commit f801b44

Please sign in to comment.