From c7795663bac447e01b7b0b08fced099a01e069e4 Mon Sep 17 00:00:00 2001 From: Ethan Stubbs <37120476+tgpethan@users.noreply.github.com> Date: Tue, 10 Aug 2021 08:28:28 +0100 Subject: [PATCH] ioopiij --- chunk.js | 6 +++--- index.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/chunk.js b/chunk.js index 01a7209..e2693f3 100644 --- a/chunk.js +++ b/chunk.js @@ -45,8 +45,8 @@ module.exports = class { } for(let i4 = l1; i4 <= j2; i4++) { - let j4 = k - i4 * 16; - let k4 = (k + j1) - i4 * 16; + let j4 = zPos - i4 * 16; + let k4 = (zPos + j1) - i4 * 16; if(j4 < 0) { j4 = 0; @@ -55,7 +55,7 @@ module.exports = class { { k4 = 16; } - k2 = getChunkFromChunkCoords(j3, i4).getChunkData(abyte0, k3, l2, j4, l3, i3, k4, k2); + k2 = 1;//getChunkFromChunkCoords(j3, i4).getChunkData(abyte0, k3, l2, j4, l3, i3, k4, k2); } } diff --git a/index.js b/index.js index 2830ad4..c5c4ebd 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,18 @@ const net = require('net'); const port = 25565; -const { createGzip, deflate } = require("zlib"); +const { createGzip, deflateSync } = require("zlib"); const chunk = require("./chunk.js"); const myman = require("./bufferStuff.js"); +const users = []; + +function addUser(socket, username) { + users.push([users.length + 1, socket, username]); +} + const server = new net.Server(); const protocolVersion = 14; @@ -41,6 +47,10 @@ server.on('connection', function(socket) { handshake(socket, reader); break; + case 3: + sendChat(socket, reader); + break; + case 0x0B: playerLook(socket, reader); break; @@ -95,6 +105,16 @@ function loginRequest(socket, reader = new myman.Reader) { socket.write(writer.buffer); preChunk(socket, 0, 0, true); + + //writeChunk(socket); + + writer.buffer = Buffer.alloc(0); + + writer.writeByte(0x0A); + + writer.writeBool(true); + + socket.write(writer.buffer); } function handshake(socket, reader = new myman.Reader) { @@ -109,6 +129,20 @@ function handshake(socket, reader = new myman.Reader) { socket.write(writer.buffer); } +function sendChat(socket, reader = new myman.Reader) { + const message = reader.readString(); + + if (message.length < 120) { + const writer = new myman.Writer(); + + writer.writeByte(0x03); + + writer.writeString(message); + + socket.write(writer.buffer); + } +} + function playerLook(socket, reader = new myman.Reader) { const x = reader.readDouble(); const y = reader.readDouble(); @@ -136,14 +170,29 @@ function preChunk(socket, x, y, load = false) { socket.write(writer.buffer); } -function writeChunk() { +function writeChunk(socket) { const writer = new myman.Writer(); writer.writeByte(0x33); // ID + + const chank = new chunk(0, 0); + + const buf = Buffer.alloc((15 * 127 * 15 * 5) / 2); + + const defl = deflateSync(chank.getChunkData(0, 0, 0, 15, 127, 15), {level:-1}); + writer.writeInt(0); // Chunk X writer.writeShort(0); // Chunk Y - writer.writeByte(15); // Chunk Z - writer.writeByte(127); // Chunk Size X - writer.writeByte(15); // Chunk Size Y - writer.writeInt(5); // Chunk Size Z + writer.writeInt(0); // Chunk Z + writer.writeByte(15); // Chunk Size X + writer.writeByte(127); // Chunk Size Y + writer.writeByte(15); // Chunk Size Z + writer.writeInt(defl.length); // Compressed size + console.log(defl.length); + console.log(defl.toString()); + for (let i = 0; i < defl.length; i++) { + writer.writeByte(defl[i] - 128); // Compressed data + } + + socket.write(writer.buffer); } \ No newline at end of file