Skip to content

Commit

Permalink
Merge branch 'SerenityJS:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
AnyBananaGAME committed Aug 27, 2024
2 parents edb087e + aca7440 commit 9af7002
Show file tree
Hide file tree
Showing 30 changed files with 1,156 additions and 360 deletions.
17 changes: 6 additions & 11 deletions packages/nbt/src/tags/compound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class CompoundTag<T = unknown> extends NBTTag<T> {
*/
public static write<T = unknown>(
stream: BinaryStream,
tag: CompoundTag<T>,
root: CompoundTag<T>,
varint = false,
type = true
): void {
Expand All @@ -358,29 +358,24 @@ class CompoundTag<T = unknown> extends NBTTag<T> {
stream.writeByte(this.type);

// Write the name.
this.writeString(tag.name, stream, varint);
this.writeString(root.name, stream, varint);
}

// Write the tags.
for (const key in tag.value) {
// Get the type.
const type = tag.value[key] as NBTTag<unknown>;

for (const tag of root.getTags()) {
// Find the tag.
const writter = NBT_TAGS.find(
(tag) => type instanceof tag
) as typeof NBTTag;
const writter = NBT_TAGS.find((x) => tag instanceof x) as typeof NBTTag;

// Check if the tag was found.
if (!writter) {
throw new Error(`Unknown tag type: ${type}`);
}

// Write the tag.
writter.write(stream, type, varint);
writter.write(stream, tag, varint);
}

// Write the end.
// Write the end tag.
stream.writeByte(Tag.End);
}
}
Expand Down
11 changes: 4 additions & 7 deletions packages/nbt/src/tags/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,22 +258,19 @@ class ListTag<T = unknown> extends NBTTag<Array<T>> {
const compound = value as CompoundTag<Record<string, NBTTag>>;

// Write the tags.
for (const key in compound.value) {
// Get the type.
const type = compound.value[key]!;

for (const tag of compound.getTags()) {
// Find the tag.
const writter = NBT_TAGS.find(
(tag) => type instanceof tag
(x) => tag instanceof x
) as typeof NBTTag;

// Check if the tag was found.
if (!writter) {
throw new Error(`Unknown tag type: ${type}`);
throw new Error(`Unknown tag type for: ${tag.name}`);
}

// Write the tag.
writter.write(stream, type, varint, false);
writter.write(stream, tag, varint);
}

// Write the end tag.
Expand Down
10 changes: 8 additions & 2 deletions packages/protocol/src/proto/types/behavior-pack-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BehaviorPackInfo extends DataType {
public subpackName: string;
public uuid: string;
public version: string;
public addonPack: boolean;

public constructor(
contentIdentity: string,
Expand All @@ -19,7 +20,8 @@ class BehaviorPackInfo extends DataType {
size: number,
subpackName: string,
uuid: string,
version: string
version: string,
addonPack: boolean
) {
super();
this.contentIdentity = contentIdentity;
Expand All @@ -29,6 +31,7 @@ class BehaviorPackInfo extends DataType {
this.subpackName = subpackName;
this.uuid = uuid;
this.version = version;
this.addonPack = addonPack;
}

public static override read(stream: BinaryStream): Array<BehaviorPackInfo> {
Expand All @@ -49,6 +52,7 @@ class BehaviorPackInfo extends DataType {
const subpackName = stream.readVarString();
const contentIdentity = stream.readVarString();
const hasScripts = stream.readBool();
const addonPack = stream.readBool();

// Push the pack to the array.
packs.push(
Expand All @@ -59,7 +63,8 @@ class BehaviorPackInfo extends DataType {
size,
subpackName,
uuid,
version
version,
addonPack
)
);
}
Expand All @@ -85,6 +90,7 @@ class BehaviorPackInfo extends DataType {
stream.writeVarString(pack.subpackName);
stream.writeVarString(pack.contentIdentity);
stream.writeBool(pack.hasScripts);
stream.writeBool(pack.addonPack);
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions packages/protocol/src/proto/types/texture-pack-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TexturePackInfo extends DataType {
public subpackName: string;
public uuid: string;
public version: string;
public addonPack: boolean;

public constructor(
contentIdentity: string,
Expand All @@ -21,7 +22,8 @@ class TexturePackInfo extends DataType {
size: bigint,
subpackName: string,
uuid: string,
version: string
version: string,
addonPack: boolean
) {
super();
this.contentIdentity = contentIdentity;
Expand All @@ -32,6 +34,7 @@ class TexturePackInfo extends DataType {
this.subpackName = subpackName;
this.uuid = uuid;
this.version = version;
this.addonPack = addonPack;
}

public static override read(stream: BinaryStream): Array<TexturePackInfo> {
Expand All @@ -52,6 +55,7 @@ class TexturePackInfo extends DataType {
const subpackName = stream.readVarString();
const contentIdentity = stream.readVarString();
const hasScripts = stream.readBool();
const addonPack = stream.readBool();
const rtxEnabled = stream.readBool();

// Push the pack to the array.
Expand All @@ -64,7 +68,8 @@ class TexturePackInfo extends DataType {
size,
subpackName,
uuid,
version
version,
addonPack
)
);
}
Expand All @@ -91,6 +96,7 @@ class TexturePackInfo extends DataType {
stream.writeVarString(pack.contentIdentity);
stream.writeBool(pack.hasScripts);
stream.writeBool(pack.rtxEnabled);
stream.writeBool(pack.addonPack);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/serenity/src/handlers/disconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Disconnect extends SerenityHandler {
const player = this.serenity.getPlayer(session);
if (!player) return;

// Save the player data
player.dimension.world.provider.writePlayer(player);

// Create a new player leave signal
// This event cannot be cancelled.
new PlayerLeaveSignal(
Expand Down
23 changes: 18 additions & 5 deletions packages/serenity/src/handlers/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ class Login extends SerenityHandler {
// Decode the tokens given by the client.
// This contains the client data, identity data, and public key.
// Along with the players XUID, display name, and uuid.
const data = this.decode(packet.tokens);
const tokens = this.decode(packet.tokens);

// Get the clients xuid and username.
const xuid = data.identityData.XUID;
const username = data.identityData.displayName;
const xuid = tokens.identityData.XUID;
const uuid = tokens.identityData.identity;
const username = tokens.identityData.displayName;

// TODO: This is a temporary solution to the reliability and channel issue.
session.reliablity = Reliability.Reliable;
Expand Down Expand Up @@ -88,10 +89,21 @@ class Login extends SerenityHandler {
// Get the permission level of the player.
const permission = this.serenity.permissions.get(xuid, username);

// Create the options for the player
const options = { session, permission, tokens };

// Create a new player instance.
// Since we have gotten the players login data, we can create a new player instance.
// We will also add the player to the players map.
const player = new Player(session, data, dimension, permission);
const player = world.provider.hasPlayer(uuid)
? (Player.deserialize(
world.provider.readPlayer(uuid),
dimension,
options
) as Player)
: new Player(dimension, options);

// Set the players xuid and username.
this.serenity.players.set(xuid, player);

// Create the player join signal and emit it.
Expand Down Expand Up @@ -129,7 +141,8 @@ class Login extends SerenityHandler {
pack.originalSize,
pack.selectedSubpack,
pack.uuid,
pack.version
pack.version,
false
);

packs.texturePacks.push(packInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class ResourcePackClientResponse extends SerenityHandler {
packet.entityId = player.unique;
packet.runtimeEntityId = player.runtime;
packet.playerGamemode = player.gamemode;
packet.playerPosition = player.dimension.spawn;
packet.playerPosition = player.position;
packet.pitch = player.rotation.pitch;
packet.yaw = player.rotation.yaw;
packet.seed = BigInt(player.dimension.generator.seed);
Expand Down
Loading

0 comments on commit 9af7002

Please sign in to comment.