Skip to content

Commit

Permalink
new raknet system handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AnyBananaGAME committed Sep 6, 2024
1 parent bc49693 commit a81f531
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/client/Listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Listener extends EventEmitter {
on<K extends keyof ListenerEvents>(eventName: K, listener: ListenerEvents[K]): this {
return super.on(eventName, listener);
}

once<K extends keyof ListenerEvents>(eventName: K, listener: ListenerEvents[K]): this {
return super.once(eventName, listener);
}
}

type PacketNames = {
Expand All @@ -26,4 +30,4 @@ type ListenerEvents = {
'PrePlayerAuthInputPacket': (packet: Protocol.PlayerAuthInputPacket, cancel: boolean) => void;
};

export { Listener };
export { Listener, ListenerEvents };
50 changes: 46 additions & 4 deletions src/tools/test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { RespawnPacket, RespawnState, TextPacket, Vector3f } from "@serenityjs/protocol";
import { MoveActorAbsolutePacket, MovePlayerPacket, RespawnPacket, RespawnState, TextPacket, UpdateBlockPacket, Vector3f } from "@serenityjs/protocol";
import { Client } from "../Client";
import { Logger } from "../vendor/Logger";
import { Priority } from "@serenityjs/raknet";
import { BlockIdentifier, BlockPermutation } from "@serenityjs/block";
import { inspect } from "util";
import { Block, Dimension } from "@serenityjs/world";

const client = new Client({
host: "127.0.0.1",
offline: false,
username: "SanctumTerra",
version: "1.21.20",
port: 19133,
loadPlugins: false,
validateProtocol: false
});

try {
Expand All @@ -20,10 +25,10 @@ try {

client.on("spawn", () => {
console.timeEnd("SpawnTime");

client.sneak();
setInterval(() => {
const [x, y, z] = Math.random() < 0.5 ? [258, 74, 257] : [251, 72, 257];
client.lookAt(x, y, z);
// client.lookAt(x, y, z);
}, 1000);

setInterval(async () => {
Expand All @@ -40,14 +45,37 @@ client.on("SetTimePacket", () => {
client.sendMessage(`Hello! My position is ${Math.floor(x)} ${Math.floor(y - 1.62)} ${Math.floor(z)}`);
});

client.on("TextPacket", (packet: TextPacket): void => {
client.on("TextPacket", async (packet: TextPacket): Promise<void> => {
if (!packet.parameters) return Logger.chat(packet.message);

const [param1, param2] = packet.parameters;
if (packet.message.includes("chat.type.text")) Logger.chat(`§f<${param1}> ${param2}`);
else if (packet.message.includes("multiplayer.player.joined")) Logger.chat(`§e${param1} §ejoined the game`);
else if (packet.message.includes("multiplayer.player.left")) Logger.chat(`§f${param1} §7left the game`);
else console.log(packet.message);

if (packet.message.includes("chat.type.text") && param2.includes("rat")) {
const breakBlock = async (x: number, y: number, z: number) => {
console.log(`Breaking block at ${x}, ${y}, ${z}`);
await client.break(new Vector3f(x, y, z));
console.log(`Finished breaking block at ${x}, ${y}, ${z}`);
// Add a small delay between breaking blocks
await new Promise(resolve => setTimeout(resolve, 200));
};

for (let i = 0; i < 3; i++) {
for (let ix = 0; ix < 3; ix++) {
for (let iz = 0; iz < 3; iz++) {
const x = Math.floor(client.position.x) + ix;
const y = Math.floor(client.position.y-1.62) - i;
const z = Math.floor(client.position.z) + iz;
await breakBlock(x, y, z);
}
}
}

console.log("All blocks broken sequentially");
}
});

client.on("RespawnPacket", (packet: RespawnPacket) => {
Expand All @@ -65,4 +93,18 @@ client.on("RespawnPacket", (packet: RespawnPacket) => {

client.position = packet.position;
client.sendPacket(response, Priority.Immediate);
});


client.on("MoveActorAbsolutePacket", (packet: MoveActorAbsolutePacket) => {
if(packet.runtimeId == client.runtimeEntityId) return;
// client.lookAt(packet.position.x, packet.position.y, packet.position.z);
});

client.on("UpdateBlockPacket", (packet: UpdateBlockPacket) => {
// const block = BlockPermutation.resolve(BlockIdentifier.Air);
//console.log(inspect(block, true, 999, true))
if(packet.networkBlockId == 11844) return;
console.log(packet.networkBlockId);
client.break(new Vector3f(packet.position.x, packet.position.y, packet.position.z));
});
8 changes: 7 additions & 1 deletion src/vendor/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ class Logger {
static chat(...data: any[]): void {
this.log('§7<§l§aCHAT§7>§r', ...data);
}

static debug(...data: any[]) {
this.log('§7<§l§dDEBUG§7>§r', ...data);
}
static warn(...data: any[]) {
this.log('§7<§l§eWARN§7>§r', ...data);
}

static date(): string {
const date = new Date();
return this.colorize(`§7<§f${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}§7>§r:`)[0];
Expand Down
11 changes: 3 additions & 8 deletions src/vendor/PacketSorter.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { Frame, Priority, Reliability } from "@serenityjs/raknet";
import { Client } from "../Client";
import { Logger } from "@sanctumterra/raknet";
import { Logger } from "../vendor/Logger";
import {
CompressionMethod,
DataPacket,
Framer,
NetworkSettingsPacket,
getPacketId,
Packets,
PlayStatusPacket,
ResourcePacksInfoPacket,
ServerToClientHandshakePacket,
SetScorePacket,
StartGamePacket,
getPacketId
} from "@serenityjs/protocol";
import { deflateRawSync, inflateRawSync } from "zlib";
import { PacketHandler } from "./PacketHandler";
Expand Down Expand Up @@ -48,7 +43,7 @@ export class PacketSorter {
frame.reliability = Reliability.ReliableOrdered;
frame.orderChannel = 0;
frame.payload = payload;
this.client.raknet.queue.sendFrame(frame, priority);
this.client.raknet.sender.sendFrame(frame, priority);
}

private preparePayload(framed: Buffer): Buffer {
Expand Down

0 comments on commit a81f531

Please sign in to comment.