cc-mc-server/mc/server.lua

65 lines
1.7 KiB
Lua
Raw Normal View History

2024-07-12 16:58:47 +01:00
-- local bufferStuff = require("bufferstuff")
-- local writer = bufferStuff.Writer:new()
-- writer:writeUByte(10)
-- writer:writeUByte(67)
-- writer:writeString16("Hello World!")
-- local reader = bufferStuff.Reader:new(writer:toBuffer())
-- print(reader:readUByte())
-- print(reader:readUByte())
-- print(reader:readString16())
local chunk = require("chunk")
local packet = require("packet")
local bufferStuff = require("bufferstuff")
local tempChunk = chunk:new()
local function doWebsocket()
local ws = http.websocket("ws://localhost:25567")
while true do
local b, bv, data, bx = os.pullEvent("websocket_message")
local reader = bufferStuff.Reader:new(data)
local packetId = reader:readByte()
if packetId == packet.LoginRequest then
local protocolVersion = reader:readInt()
local username = reader:readString16()
local mapSeed = reader:readLong()
local dimension = reader:readByte()
print(username .. " is requesting login")
local writer = bufferStuff.Writer:new()
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())
print("Login granted")
elseif packetId == packet.Handshake then
local username = reader:readString16()
print(username .. " is probing the server")
local writer = bufferStuff.Writer:new()
writer:writeByte(packet.Handshake)
writer:writeString16("-")
ws.send(writer:toBuffer())
print("Handshake ACK sent")
end
end
end
local tickCount = 0
local function serverTickLoop()
while true do
os.sleep(0.05)
tickCount = tickCount + 1
end
end
parallel.waitForAll(doWebsocket, serverTickLoop)