local chunk = require("chunk") local packet = require("packet") local packet_names = require("packet_names") local bufferStuff = require("bufferstuff") local libDeflate = require("LibDeflate") local monitor = peripheral.wrap("back") local oldTerm = nil if monitor ~= nil then oldTerm = term.redirect(monitor) end local tempChunk = chunk:new() for x = 0, 15 do for z = 0, 15 do tempChunk:setBlock(2, x, 62, z) end end local ws = nil local playerX = 0 local playerY = 0 local playerZ = 0 local playerYaw = 0 local playerPitch = 0 local onGround = false term.clear() if monitor ~= nil then monitor.setTextScale(2) end term.setCursorPos(2, 2) term.write("Minecraft Server lmao this is dumb") local function doWebsocket() if monitor ~= nil then ws = http.websocket("wss://ws.eusv.net/mcinmc") else ws = http.websocket("ws://localhost:25567") end while true do local b, bv, data, bx = os.pullEvent("websocket_message") local reader = bufferStuff.Reader:new(data) while reader.offset <= #reader.bufferData do local packetId = reader:readByte() print(packet_names[packetId], packetId, reader.offset, #reader.bufferData) if packetId == packet.LoginRequest then local protocolVersion = reader:readInt() local username = reader:readString16() local mapSeed = reader:readLong() local dimension = reader:readByte() local writer = bufferStuff.Writer:new() -- Login packet writer:writeByte(packet.LoginRequest) -- Packet ID writer:writeInt(1) -- Entity ID writer:writeString16("") -- Login Token writer:writeLong(0) -- Seed writer:writeByte(0) -- Dimension ws.send(writer:toBuffer(), true) -- teleport local teleportWriter = bufferStuff.Writer:new() teleportWriter:writeByte(packet.PlayerPosition) teleportWriter:writeDouble(8) teleportWriter:writeDouble(75) teleportWriter:writeDouble(75.62) teleportWriter:writeDouble(8) teleportWriter:writeBool(false) ws.send(teleportWriter:toBuffer(), true) -- pre chunk local preChunkWriter = bufferStuff.Writer:new() preChunkWriter:writeByte(packet.PreChunk) preChunkWriter:writeInt(0) preChunkWriter:writeInt(0) preChunkWriter:writeBool(true) ws.send(preChunkWriter:toBuffer(), true) local blockWriter = bufferStuff.Writer:new() for x = 0, 15 do for z = 0, 15 do blockWriter:writeByte(packet.BlockChange) blockWriter:writeInt(x) blockWriter:writeByte(62) blockWriter:writeInt(z) blockWriter:writeByte(20) blockWriter:writeByte(0) end end ws.send(blockWriter:toBuffer(), true) -- chunk -- TODO: Speed this up, it's SLOW as balls local chunkWriter = bufferStuff.Writer:new() local chunkData = bufferStuff.Writer:new() for _, id in ipairs(tempChunk.blocks) do chunkData:writeUByte(id) end for _, id in ipairs(tempChunk.metadata) do chunkData:writeUByte(id) end for _, id in ipairs(tempChunk.blockLight) do chunkData:writeUByte(id) end for _, id in ipairs(tempChunk.skyLight) do chunkData:writeUByte(id) end chunkWriter:writeByte(packet.MapChunk) chunkWriter:writeInt(0) chunkWriter:writeShort(0) chunkWriter:writeInt(0) chunkWriter:writeByte(15) chunkWriter:writeByte(127) chunkWriter:writeByte(15) local compressedData = libDeflate:CompressZlib(chunkData:toBuffer()) chunkWriter:writeInt(#compressedData) local chunkPacket = chunkWriter:toBuffer() ws.send(chunkPacket .. compressedData, true) elseif packetId == packet.Handshake then local username = reader:readString16() local writer = bufferStuff.Writer:new() writer:writeByte(packet.Handshake) writer:writeString16("-") ws.send(writer:toBuffer(), true) elseif packetId == packet.Player then onGround = reader:readBool() elseif packetId == packet.PlayerLook then playerYaw = reader:readFloat() playerPitch = reader:readFloat() onGround = reader:readBool() elseif packetId == packet.PlayerPosition then playerX = reader:readDouble() playerY = reader:readDouble() reader:readDouble() playerZ = reader:readDouble() onGround = reader:readBool() elseif packetId == packet.PlayerPositionLook then playerX = reader:readDouble() playerY = reader:readDouble() reader:readDouble() playerZ = reader:readDouble() playerYaw = reader:readFloat() playerPitch = reader:readFloat() onGround = reader:readBool() end end end end local keepalivePacket = bufferStuff.Writer:new() keepalivePacket:writeByte(packet.KeepAlive) keepalivePacket = keepalivePacket:toBuffer() local d = false local timeLastFrame = os.clock() local tickCount = 0 local function serverTickLoop() while true do if tickCount % 20 == 0 then if ws ~= nil then ws.send(keepalivePacket) end end os.sleep(0.05) tickCount = tickCount + 1 -- Term output -- term.setCursorPos(2, 4) -- term.write("X: " .. playerX .. " ") -- term.setCursorPos(2, 5) -- term.write("Y: " .. playerY .. " ") -- term.setCursorPos(2, 6) -- term.write("Z: " .. playerZ .. " ") -- term.setCursorPos(2, 8) -- term.write("Yaw: " .. playerYaw .. " ") -- term.setCursorPos(2, 9) -- term.write("Pitch: " .. playerPitch .. " ") -- term.setCursorPos(2, 11) -- term.write("On Ground: " .. tostring(onGround) .. " ") -- term.setCursorPos(2, 13) -- term.write("Tick Counter: " .. tostring(tickCount) .. " TPS: " .. tostring(math.floor((1 / (os.clock() - timeLastFrame)) + 0.5))) -- if tickCount % 20 == 0 then -- d = not d -- if not d then -- term.write(" - ") -- else -- term.write(" | ") -- end -- end timeLastFrame = os.clock() end end parallel.waitForAll(doWebsocket, serverTickLoop) if monitor ~= nil then term.redirect(oldTerm) end