-- 动态区块保护系统 local BlockProtection = { protectedAreas = {}, -- 存储所有受保护的区域 ownerRights = {}, -- 玩家权限数据 log = {} -- 操作日志 } -- 添加一个新的受保护区域 function BlockProtection.addProtectedArea(name, x1, y1, z1, x2, y2, z2, owner) local newArea = { name = name, x1 = math.min(x1, x2), y1 = math.min(y1, y2), z1 = math.min(z1, z2), x2 = math.max(x1, x2), y2 = math.max(y1, y2), z2 = math.max(z1, z2), owner = owner, allowedPlayers = {[owner] = true}, -- 默认所有者有权限 creationTime = os.time() } BlockProtection.protectedAreas[name] = newArea BlockProtection.logAction(owner, "创建保护区 "..name) return true end -- 检查坐标是否在受保护区域内 function BlockProtection.isPositionProtected(x, y, z) for name, area in pairs(BlockProtection.protectedAreas) do if x >= area.x1 and x <= area.x2 and y >= area.y1 and y <= area.y2 and z >= area.z1 and z <= area.z2 then return name, area end end return nil end -- 检查玩家是否有权限修改区块 function BlockProtection.canModify(player, x, y, z) local areaName, area = BlockProtection.isPositionProtected(x, y, z) if not area then return true end -- 不在保护区内 -- 检查是否是所有者或授权玩家 if area.allowedPlayers[player] then return true end -- 检查玩家是否有全局权限 if BlockProtection.ownerRights[player] and BlockProtection.ownerRights[player].global then return true end return false, areaName, area.owner end -- 添加玩家到保护区的授权列表 function BlockProtection.addPlayerToArea(areaName, owner, playerToAdd) local area = BlockProtection.protectedAreas[areaName] if not area or area.owner ~= owner then return false, "你不是该区域的所有者或区域不存在" end area.allowedPlayers[playerToAdd] = true BlockProtection.logAction(owner, "添加玩家 "..playerToAdd.." 到保护区 "..areaName) return true end -- 从保护区移除玩家权限 function BlockProtection.removePlayerFromArea(areaName, owner, playerToRemove) local area = BlockProtection.protectedAreas[areaName] if not area or area.owner ~= owner then return false, "你不是该区域的所有者或区域不存在" end area.allowedPlayers[playerToRemove] = nil BlockProtection.logAction(owner, "从保护区 "..areaName.." 移除玩家 "..playerToRemove) return true end -- 删除保护区 function BlockProtection.removeProtectedArea(areaName, owner) local area = BlockProtection.protectedAreas[areaName] if not area or area.owner ~= owner then return false, "你不是该区域的所有者或区域不存在" end BlockProtection.protectedAreas[areaName] = nil BlockProtection.logAction(owner, "删除保护区 "..areaName) return true end -- 记录操作日志 function BlockProtection.logAction(player, action) local entry = { player = player, action = action, timestamp = os.time() } table.insert(BlockProtection.log, entry) -- 保持日志不超过100条 if #BlockProtection.log > 100 then table.remove(BlockProtection.log, 1) end end -- 获取玩家的所有保护区 function BlockProtection.getPlayerAreas(player) local areas = {} for name, area in pairs(BlockProtection.protectedAreas) do if area.owner == player then table.insert(areas, area) end end return areas end -- 保存数据到文件 function BlockProtection.saveData(filename) local data = { protectedAreas = BlockProtection.protectedAreas, ownerRights = BlockProtection.ownerRights } local serialized = minetest.serialize(data) -- 假设使用Minetest的序列化函数 local file = io.open(filename, "w") if file then file:write(serialized) file:close() return true end return false end -- 从文件加载数据 function BlockProtection.loadData(filename) local file = io.open(filename, "r") if file then local content = file:read("*a") file:close() local data = minetest.deserialize(content) -- 假设使用Minetest的反序列化函数 if data then BlockProtection.protectedAreas = data.protectedAreas or {} BlockProtection.ownerRights = data.ownerRights or {} return true end end return false end print("此脚本由动态区块保护") return BlockProtection