cc-mc-server/mc/server.lua

211 lines
5.8 KiB
Lua
Raw Normal View History

2024-07-14 14:23:30 +01:00
local chunk = require("chunk")
2024-07-12 16:58:47 +01:00
local packet = require("packet")
2024-07-14 14:23:30 +01:00
local packet_names = require("packet_names")
2024-07-12 16:58:47 +01:00
local bufferStuff = require("bufferstuff")
2024-07-14 14:23:30 +01:00
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
2024-07-12 19:49:56 +01:00
local ws = nil
local playerX = 0
local playerY = 0
local playerZ = 0
local playerYaw = 0
local playerPitch = 0
local onGround = false
2024-07-12 16:58:47 +01:00
2024-07-14 14:23:30 +01:00
term.clear()
if monitor ~= nil then
monitor.setTextScale(2)
end
term.setCursorPos(2, 2)
term.write("Minecraft Server lmao this is dumb")
2024-07-12 16:58:47 +01:00
local function doWebsocket()
2024-07-14 14:23:30 +01:00
if monitor ~= nil then
ws = http.websocket("wss://ws.eusv.net/mcinmc")
else
ws = http.websocket("ws://localhost:25567")
end
2024-07-12 16:58:47 +01:00
while true do
local b, bv, data, bx = os.pullEvent("websocket_message")
local reader = bufferStuff.Reader:new(data)
2024-07-14 14:23:30 +01:00
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
2024-07-12 19:49:56 +01:00
end
2024-07-14 14:23:30 +01:00
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()
2024-07-12 19:49:56 +01:00
end
2024-07-12 16:58:47 +01:00
end
end
end
2024-07-14 14:23:30 +01:00
local keepalivePacket = bufferStuff.Writer:new()
keepalivePacket:writeByte(packet.KeepAlive)
keepalivePacket = keepalivePacket:toBuffer()
local d = false
2024-07-12 19:49:56 +01:00
2024-07-14 14:23:30 +01:00
local timeLastFrame = os.clock()
2024-07-12 16:58:47 +01:00
local tickCount = 0
local function serverTickLoop()
while true do
2024-07-14 14:23:30 +01:00
if tickCount % 20 == 0 then
if ws ~= nil then
ws.send(keepalivePacket)
end
end
2024-07-12 19:49:56 +01:00
2024-07-14 14:23:30 +01:00
os.sleep(0.05)
2024-07-12 16:58:47 +01:00
tickCount = tickCount + 1
2024-07-14 14:23:30 +01:00
-- 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()
2024-07-12 16:58:47 +01:00
end
end
2024-07-14 14:23:30 +01:00
parallel.waitForAll(doWebsocket, serverTickLoop)
if monitor ~= nil then
term.redirect(oldTerm)
end