Skip to content

Commit

Permalink
chages :D
Browse files Browse the repository at this point in the history
  • Loading branch information
AnyBananaGAME committed Aug 18, 2024
1 parent 52340d7 commit 0516138
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 176 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ tokens/
.vscode/
dev/
.idx/
U2NyZWV0/
U2NyZWV0/
plugins/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const client = new Client({
client.connect();

// 📥 Handle incoming TextPacket events
client.on(TextPacket.name, (packet: TextPacket): void => {
client.on("TextPacket", (packet: TextPacket): void => {
if (packet.parameters) {
// 🗨️ Handle standard chat messages
if (packet.message.includes("chat.type.text")) {
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sanctumterra/client",
"version": "2.0.4",
"version": "2.0.5",
"description": "Get started by customizing your environment (defined in the .idx/dev.nix file) with the tools and IDE extensions you'll need for your project!",
"main": "./dist/index.js",
"type": "commonjs",
Expand All @@ -20,26 +20,26 @@
"chalk": "^5.3.0",
"chokidar": "^3.6.0",
"lodash.debounce": "^4.0.8",
"ora": "^8.0.1",
"typescript": "^5.5.4"
},
"maintainers": [
{
"name": "SanctumTerra",
"url": "https://github.com/TerraOfficial"
"name": "SanctumTerra",
"url": "https://github.com/TerraOfficial"
}
],
"dependencies": {
"@sanctumterra/raknet": "^1.0.16",
"@serenityjs/binarystream": "^2.6.6",
"@serenityjs/network": "^0.4.2",
"@serenityjs/protocol": "^0.4.2",
"@serenityjs/raknet": "^0.4.2",
"@swc/helpers": "^0.5.12",
"@types/node": "^22.3.0",
"jose": "^5.6.3",
"jsonwebtoken": "^9.0.2",
"prismarine-auth": "github:AnyBananaGAME/prismarine-auth",
"semver": "^7.6.3",
"prismarine-realms": "^1.3.2",
"uuid-1345": "^1.0.2"
}
}
8 changes: 5 additions & 3 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Listener } from "./client/Listener";
import { ProtocolValidator } from "./vendor/ProtocolValidator";
import { PacketSorter } from "./vendor/PacketSorter";
import { PacketEncryptor } from "./vendor/PacketEncryptor";
import { DataPacket, RequestNetworkSettingsPacket, TextPacket, TextPacketType } from "@serenityjs/protocol";
import { DataPacket, RequestNetworkSettingsPacket, TextPacket, TextPacketType, Vector3f } from "@serenityjs/protocol";
import { Priority } from "@serenityjs/raknet";
import { PluginLoader } from "./vendor/PluginLoader";

Expand All @@ -20,6 +20,8 @@ class Client extends Listener {

public runtimeEntityId!: bigint;
public username!: string;
public position!: Vector3f;

public playStatus!: number;
public _encryption: boolean = false;
public _encryptor!: PacketEncryptor;
Expand All @@ -37,9 +39,9 @@ class Client extends Listener {

public async connect(): Promise<void> {
const protocolValidator = new ProtocolValidator(this);
console.time('Protocol Validation');
//console.time('Protocol Validation');
await protocolValidator.validateAndInstall();
console.timeEnd('Protocol Validation');
//console.timeEnd('Protocol Validation');
await this.pluginLoader.init();
this.initializeSession();
}
Expand Down
19 changes: 18 additions & 1 deletion src/client/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Authflow, Titles } from "prismarine-auth";
import { Client } from "../Client";
import { v3 } from "uuid-1345";
import { Logger } from "../vendor/Logger";
import { ClientboundCloseFormPacket } from "@serenityjs/protocol";
const { RealmAPI } = require('prismarine-realms')

interface Profile {
name: string;
Expand Down Expand Up @@ -50,6 +52,21 @@ async function authenticate(client: Client): Promise<void> {
}
}

async function realmAuthenticate (client: Client) {
console.log(".")
if(!client.options.realmOptions || client.options.realmOptions == null) {

Check failure on line 57 in src/client/Auth.ts

View workflow job for this annotation

GitHub Actions / build

Property 'realmOptions' does not exist on type 'ClientOptions'.

Check failure on line 57 in src/client/Auth.ts

View workflow job for this annotation

GitHub Actions / build

Property 'realmOptions' does not exist on type 'ClientOptions'.
new Error("An error has slipped by, please create an Issue on Github.");
return;
}
const api = RealmAPI.from(createAuthflow(client), 'bedrock')
const realm = await api.getRealmFromInvite(client.options.realmOptions.realmInvite)

Check failure on line 62 in src/client/Auth.ts

View workflow job for this annotation

GitHub Actions / build

Property 'realmOptions' does not exist on type 'ClientOptions'.
if (!realm) throw Error("No realm found with that invite.\nPlease join one first.")
Logger.info("Successfully joined realm")
const { host, port } = await realm.getAddress()
client.options.host = host
client.options.port = port
}

function createAuthflow(client: Client): Authflow {
return new Authflow(
client.options.username,
Expand Down Expand Up @@ -97,4 +114,4 @@ function setupClientChains(client: Client): void {
client.data.loginData.clientUserChain = client.data.createClientUserChain(client.data.loginData.ecdhKeyPair.privateKey);
}

export { authenticate, createOfflineSession };
export { authenticate, createOfflineSession, realmAuthenticate };
12 changes: 10 additions & 2 deletions src/client/ClientOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ enum ProtocolList {
"1.21.20" = 712
}

type RealmOptions = {
realmInvite: string;
}

type ClientOptions = {
version: (typeof Versions)[number];
Expand All @@ -17,9 +20,11 @@ type ClientOptions = {
debug: boolean;
tokensFolder: string;
viewDistance: number;
//realm: boolean;
//realmOptions: RealmOptions | null;
}

const defaultOptions: Required<ClientOptions> = {
const defaultOptions: ClientOptions = {
version: "1.21.20",
offline: false,
username: "defaultUser",
Expand All @@ -28,11 +33,14 @@ const defaultOptions: Required<ClientOptions> = {
skinData: null,
debug: false,
tokensFolder: process.cwd() + "/tokens",
viewDistance: 10
viewDistance: 10,
//realm: false,
//realmOptions: null
};

export {
ClientOptions,
RealmOptions,
defaultOptions,
ProtocolList
}
168 changes: 17 additions & 151 deletions src/client/Listener.ts
Original file line number Diff line number Diff line change
@@ -1,161 +1,27 @@
import {
ActorEventPacket, AddEntityPacket, AddItemActorPacket,
AddPlayerPacket, AnimateEntityPacket, AnimatePacket,
AvailableActorIdentifiersPacket, AvailableCommandsPacket, AwardAchievementPacket,
BiomeDefinitionListPacket, BlockActorDataPacket, BlockPickRequestPacket,
BookEditPacket, BossEventPacket, CameraInstructionsPacket,
CameraPresetsPacket, CameraShakePacket, ChangeDimensionPacket,
ChunkRadiusUpdatePacket, ClientboundCloseFormPacket, CommandOutputPacket,
CommandRequestPacket, CompletedUsingItemPacket, ContainerClosePacket,
ContainerOpenPacket, ContainerSetDataPacket, CraftingDataPacket,
CreativeContentPacket, DeathInfoPacket, DimensionDataPacket,
DisconnectPacket, EmoteListPacket, EmotePacket,
InteractPacket, InventoryContentPacket, InventorySlotPacket,
InventoryTransactionPacket, ItemComponentPacket, ItemStackRequestPacket,
ItemStackResponsePacket, LevelChunkPacket, LevelEventPacket,
LevelSoundEventPacket, LoginPacket, MobEffectPacket,
MobEquipmentPacket, ModalFormRequestPacket, ModalFormResponsePacket,
MoveActorAbsolutePacket, MovePlayerPacket, NetworkChunkPublisherUpdatePacket,
NetworkSettingsPacket, NetworkStackLatencyPacket, NpcDialoguePacket,
NpcRequestPacket, OpenSignPacket, PlayStatusPacket,
PlayerActionPacket, PlayerAuthInputPacket, PlayerHotbarPacket,
PlayerListPacket, PlayerSkinPacket, PlayerStartItemCooldownPacket,
RemoveEntityPacket, RemoveObjectivePacket, RequestChunkRadiusPacket,
RequestNetworkSettingsPacket, ResourcePackChunkDataPacket, ResourcePackChunkRequestPacket,
ResourcePackClientResponsePacket, ResourcePackDataInfoPacket, ResourcePackStackPacket,
ResourcePacksInfoPacket, RespawnPacket, ScriptMessagePacket,
ServerToClientHandshakePacket, ServerboundLoadingScreenPacketPacket, SetActorDataPacket,
SetActorMotionPacket, SetCommandsEnabledPacket, SetDisplayObjectivePacket,
SetHudPacket, SetLocalPlayerAsInitializedPacket, SetPlayerGameTypePacket,
SetPlayerInventoryOptionsPacket, SetScorePacket, SetScoreboardIdentityPacket,
SetTimePacket, SetTitlePacket, SpawnParticleEffectPacket,
StartGamePacket, StructureBlockUpdatePacket, TakeItemActorPacket,
TextPacket, ToastRequestPacket, TransferPacket,
UpdateAbilitiesPacket, UpdateAdventureSettingsPacket, UpdateAttributesPacket,
UpdateBlockPacket
} from "@serenityjs/protocol";
import { EventEmitter } from "events";
import * as Protocol from "@serenityjs/protocol";
import type { NetworkEvents } from "@serenityjs/network";

class Listener extends EventEmitter {
emit<K extends keyof ListenerEvents>(eventName: K, ...args: Parameters<ListenerEvents[K]>): boolean;
emit(eventName: string | symbol, ...args: any[]): boolean;
emit(eventName: any, ...args: any[]): boolean {
return super.emit(eventName, ...args);
emit<K extends keyof ListenerEvents>(eventName: K, ...args: Parameters<ListenerEvents[K]>): boolean {
return super.emit(eventName, ...args);
}

on(eventName: string | symbol, listener: (...args: any[]) => void): this;
on<K extends keyof ListenerEvents>(eventName: K, listener: ListenerEvents[K]): this;
on(eventName: string | symbol, listener: (...args: any[]) => void): this {
return super.on(eventName, listener);
on<K extends keyof ListenerEvents>(eventName: K, listener: ListenerEvents[K]): this {
return super.on(eventName, listener);
}
}

interface ListenerEvents {
'session': () => void;
'TextPacket': (packet: TextPacket) => void;
"DisconnectPacket": (packet: DisconnectPacket) => void;
'SetTimePacket': (packet: SetTimePacket) => void;
'RespawnPacket': (packet: RespawnPacket) => void;
'SetLocalPlayerAsInitializedPacket': (packet: SetLocalPlayerAsInitializedPacket) => void;
'NetworkChunkPublisherUpdatePacket': (packet: NetworkChunkPublisherUpdatePacket) => void;
'ResourcePackClientResponsePacket': (packet: ResourcePackClientResponsePacket) => void;
'SetPlayerInventoryOptionsPacket': (packet: SetPlayerInventoryOptionsPacket) => void;
'AvailableActorIdentifiersPacket': (packet: AvailableActorIdentifiersPacket) => void;
'ServerboundLoadingScreenPacketPacket': (packet: ServerboundLoadingScreenPacketPacket) => void;
'ResourcePackChunkRequestPacket': (packet: ResourcePackChunkRequestPacket) => void;
'UpdateAdventureSettingsPacket': (packet: UpdateAdventureSettingsPacket) => void;
'ServerToClientHandshakePacket': (packet: ServerToClientHandshakePacket) => void;
'PlayerStartItemCooldownPacket': (packet: PlayerStartItemCooldownPacket) => void;
'RequestNetworkSettingsPacket': (packet: RequestNetworkSettingsPacket) => void;
'SetScoreboardIdentityPacket': (packet: SetScoreboardIdentityPacket) => void;
'ResourcePackChunkDataPacket': (packet: ResourcePackChunkDataPacket) => void;
'StructureBlockUpdatePacket': (packet: StructureBlockUpdatePacket) => void;
'InventoryTransactionPacket': (packet: InventoryTransactionPacket) => void;
'ClientboundCloseFormPacket': (packet: ClientboundCloseFormPacket) => void;
'ResourcePackDataInfoPacket': (packet: ResourcePackDataInfoPacket) => void;
'SetDisplayObjectivePacket': (packet: SetDisplayObjectivePacket) => void;
'NetworkStackLatencyPacket': (packet: NetworkStackLatencyPacket) => void;
'BiomeDefinitionListPacket': (packet: BiomeDefinitionListPacket) => void;
'SpawnParticleEffectPacket': (packet: SpawnParticleEffectPacket) => void;
'SetCommandsEnabledPacket': (packet: SetCommandsEnabledPacket) => void;
'RequestChunkRadiusPacket': (packet: RequestChunkRadiusPacket) => void;
'CompletedUsingItemPacket': (packet: CompletedUsingItemPacket) => void;
'CameraInstructionsPacket': (packet: CameraInstructionsPacket) => void;
'SetPlayerGameTypePacket': (packet: SetPlayerGameTypePacket) => void;
'MoveActorAbsolutePacket': (packet: MoveActorAbsolutePacket) => void;
'ModalFormResponsePacket': (packet: ModalFormResponsePacket) => void;
'ItemStackResponsePacket': (packet: ItemStackResponsePacket) => void;
'ChunkRadiusUpdatePacket': (packet: ChunkRadiusUpdatePacket) => void;
'AvailableCommandsPacket': (packet: AvailableCommandsPacket) => void;
'ResourcePacksInfoPacket': (packet: ResourcePacksInfoPacket) => void;
'ResourcePackStackPacket': (packet: ResourcePackStackPacket) => void;
'UpdateAttributesPacket': (packet: UpdateAttributesPacket) => void;
'ModalFormRequestPacket': (packet: ModalFormRequestPacket) => void;
'ItemStackRequestPacket': (packet: ItemStackRequestPacket) => void;
'InventoryContentPacket': (packet: InventoryContentPacket) => void;
'ContainerSetDataPacket': (packet: ContainerSetDataPacket) => void;
'BlockPickRequestPacket': (packet: BlockPickRequestPacket) => void;
'AwardAchievementPacket': (packet: AwardAchievementPacket) => void;
'UpdateAbilitiesPacket': (packet: UpdateAbilitiesPacket) => void;
'RemoveObjectivePacket': (packet: RemoveObjectivePacket) => void;
'PlayerAuthInputPacket': (packet: PlayerAuthInputPacket) => void;
'NetworkSettingsPacket': (packet: NetworkSettingsPacket) => void;
'LevelSoundEventPacket': (packet: LevelSoundEventPacket) => void;
'CreativeContentPacket': (packet: CreativeContentPacket) => void;
'ChangeDimensionPacket': (packet: ChangeDimensionPacket) => void;
'SetActorMotionPacket': (packet: SetActorMotionPacket) => void;
'ContainerClosePacket': (packet: ContainerClosePacket) => void;
'CommandRequestPacket': (packet: CommandRequestPacket) => void;
'BlockActorDataPacket': (packet: BlockActorDataPacket) => void;
'TakeItemActorPacket': (packet: TakeItemActorPacket) => void;
'ScriptMessagePacket': (packet: ScriptMessagePacket) => void;
'ItemComponentPacket': (packet: ItemComponentPacket) => void;
'InventorySlotPacket': (packet: InventorySlotPacket) => void;
'DimensionDataPacket': (packet: DimensionDataPacket) => void;
'ContainerOpenPacket': (packet: ContainerOpenPacket) => void;
'CommandOutputPacket': (packet: CommandOutputPacket) => void;
'CameraPresetsPacket': (packet: CameraPresetsPacket) => void;
'AnimateEntityPacket': (packet: AnimateEntityPacket) => void;
'ToastRequestPacket': (packet: ToastRequestPacket) => void;
'SetActorDataPacket': (packet: SetActorDataPacket) => void;
'RemoveEntityPacket': (packet: RemoveEntityPacket) => void;
'PlayerHotbarPacket': (packet: PlayerHotbarPacket) => void;
'PlayerActionPacket': (packet: PlayerActionPacket) => void;
'MobEquipmentPacket': (packet: MobEquipmentPacket) => void;
'CraftingDataPacket': (packet: CraftingDataPacket) => void;
'AddItemActorPacket': (packet: AddItemActorPacket) => void;
'UpdateBlockPacket': (packet: UpdateBlockPacket) => void;
'NpcDialoguePacket': (packet: NpcDialoguePacket) => void;
'CameraShakePacket': (packet: CameraShakePacket) => void;
'PlayerSkinPacket': (packet: PlayerSkinPacket) => void;
'PlayerListPacket': (packet: PlayerListPacket) => void;
'PlayStatusPacket': (packet: PlayStatusPacket) => void;
'NpcRequestPacket': (packet: NpcRequestPacket) => void;
'LevelEventPacket': (packet: LevelEventPacket) => void;
'LevelChunkPacket': (packet: LevelChunkPacket) => void;
'MovePlayerPacket': (packet: MovePlayerPacket) => void;
'ActorEventPacket': (packet: ActorEventPacket) => void;
'StartGamePacket': (packet: StartGamePacket) => void;
'MobEffectPacket': (packet: MobEffectPacket) => void;
'EmoteListPacket': (packet: EmoteListPacket) => void;
'DeathInfoPacket': (packet: DeathInfoPacket) => void;
'BossEventPacket': (packet: BossEventPacket) => void;
'AddPlayerPacket': (packet: AddPlayerPacket) => void;
'AddEntityPacket': (packet: AddEntityPacket) => void;
'TransferPacket': (packet: TransferPacket) => void;
'SetScorePacket': (packet: SetScorePacket) => void;
'SetTitlePacket': (packet: SetTitlePacket) => void;
'OpenSignPacket': (packet: OpenSignPacket) => void;
'InteractPacket': (packet: InteractPacket) => void;
'BookEditPacket': (packet: BookEditPacket) => void;
'AnimatePacket': (packet: AnimatePacket) => void;
'SetHudPacket': (packet: SetHudPacket) => void;
'LoginPacket': (packet: LoginPacket) => void;
'EmotePacket': (packet: EmotePacket) => void;
}

type PacketNames = {
[K in keyof typeof Protocol]: K extends `${string}Packet` ? K : never
}[keyof typeof Protocol];


export {
Listener
type ListenerEvents = {
// @ts-ignore rararara
[K in PacketNames]: (packet: InstanceType<typeof Protocol[K]>) => void;
} & {
'session': () => void;
'spawn': () => void;
};

export { Listener };
19 changes: 14 additions & 5 deletions src/tools/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SetTimePacket, TextPacket } from "@serenityjs/protocol";
import { PlayerAuthInputData, PlayerAuthInputPacket, SetTimePacket, TextPacket } from "@serenityjs/protocol";
import { Client } from "../Client";
import { Logger } from "../vendor/Logger";

Expand All @@ -9,12 +9,20 @@ const client = new Client({
version: "1.21.20"
})
client.connect();
console.time("SpawnTime");

client.on(SetTimePacket.name, () => {
client.on("spawn", () => {
console.timeEnd("SpawnTime");
setInterval(async () => {
Logger.info(`§l§a`, client.position.x, client.position.y, client.position.z)
}, 2500)
});

client.on("SetTimePacket", () => {
client.sendMessage("hey")
})
});

client.on(TextPacket.name, (packet: TextPacket): void => {
client.on("TextPacket", (packet: TextPacket): void => {
if(packet.parameters){
if(packet.message.includes("chat.type.text")) return Logger.chat(`§f<${packet.parameters[0]}> ${packet.parameters[1]}`)
if(packet.message.includes("multiplayer.player.joined")) return Logger.chat(`§e${packet.parameters[0]} §ejoined the game`)
Expand All @@ -25,4 +33,5 @@ client.on(TextPacket.name, (packet: TextPacket): void => {
})

Logger.info("§l§bINFO?§6!");
Logger.error("§l§cERROR§6!")
Logger.error("§l§cERROR§6!")

Loading

0 comments on commit 0516138

Please sign in to comment.