cc-mc-server/mc/server.lua
2024-07-12 19:49:56 +01:00

154 lines
No EOL
4.4 KiB
Lua

--local chunk = require("chunk")
local packet = require("packet")
local bufferStuff = require("bufferstuff")
--local libDeflate = require("LibDeflate")
-- 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
local function doWebsocket()
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.KeepAlive then
ws.send(data, true)
elseif 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.PlayerPositionLook)
-- teleportWriter:writeDouble(8)
-- teleportWriter:writeDouble(75)
-- teleportWriter:writeDouble(75.62)
-- teleportWriter:writeDouble(8)
-- teleportWriter:writeFloat(0)
-- teleportWriter:writeFloat(0)
-- teleportWriter:writeBool(true)
-- 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(2)
blockWriter:writeByte(0)
end
end
ws.send(blockWriter:toBuffer(), true)
-- chunk
-- 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:CompressDeflate(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.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()
elseif packet == packet.PlayerLook then
playerYaw = reader:readFloat()
playerPitch = reader:readFloat()
onGround = reader:readBool()
end
end
end
local keepAliveWriter = bufferStuff.Writer:new()
keepAliveWriter:writeByte(packet.KeepAlive)
local keepAlivePacket = keepAliveWriter:toBuffer()
local tickCount = 0
local function serverTickLoop()
while true do
--print(playerX, playerY, playerZ, playerYaw, playerPitch, onGround)
os.sleep(0.1)
tickCount = tickCount + 1
end
end
parallel.waitForAll(doWebsocket, serverTickLoop)