Init Commit
This commit is contained in:
125
42/media/lua/server/RVReassignRoomServer.lua
Normal file
125
42/media/lua/server/RVReassignRoomServer.lua
Normal file
@@ -0,0 +1,125 @@
|
||||
if not isServer() then return end
|
||||
|
||||
local SERVER_MODULE = "PROJECTRVTools"
|
||||
local SERVER_COMMAND_REASSIGN = "ReassignVehicleToCurrentRoom"
|
||||
local CLIENT_COMMAND_RESULT = "ReassignVehicleResult"
|
||||
|
||||
local function getRoomTypeAtPosition(x, y, z)
|
||||
local RV = require("RVVehicleTypes")
|
||||
local vehicleTypes = RV.VehicleTypes
|
||||
|
||||
for roomType, typeDef in pairs(vehicleTypes) do
|
||||
for _, room in ipairs(typeDef.rooms) do
|
||||
local roomEndX = room.x + (typeDef.roomWidth or 2)
|
||||
local roomEndY = room.y + (typeDef.roomHeight or 3)
|
||||
|
||||
if x >= room.x and x < roomEndX and
|
||||
y >= room.y and y < roomEndY and
|
||||
z == (room.z or 0) then
|
||||
return roomType, room
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
local function findVehicleByPersistentId(vehicleId)
|
||||
local cell = getCell()
|
||||
if not cell or not cell.getVehicles then
|
||||
return nil
|
||||
end
|
||||
|
||||
local vehicles = cell:getVehicles()
|
||||
if not vehicles then
|
||||
return nil
|
||||
end
|
||||
|
||||
for i = 0, vehicles:size() - 1 do
|
||||
local vehicle = vehicles:get(i)
|
||||
if vehicle then
|
||||
local vmd = vehicle:getModData()
|
||||
if vmd and vmd.projectRV_uniqueId and tostring(vmd.projectRV_uniqueId) == vehicleId then
|
||||
return vehicle
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
local function reassignVehicleToCurrentRoom(player)
|
||||
local modData = ModData.getOrCreate("modPROJECTRVInterior")
|
||||
local pmd = player and player:getModData() or nil
|
||||
|
||||
if not pmd or not pmd.projectRV_playerId then
|
||||
return false, "No recent vehicle registered."
|
||||
end
|
||||
|
||||
local playerData = modData.Players and modData.Players[pmd.projectRV_playerId]
|
||||
if not playerData or not playerData.VehicleId then
|
||||
return false, "No recent vehicle found."
|
||||
end
|
||||
|
||||
local vehicleId = tostring(playerData.VehicleId)
|
||||
local originalVehicleType = playerData.RoomType or "normal"
|
||||
local x, y, z = player:getX(), player:getY(), player:getZ()
|
||||
local newRoomType, newRoom = getRoomTypeAtPosition(x, y, z)
|
||||
|
||||
if not newRoom then
|
||||
return false, "You are not in a valid RV room."
|
||||
end
|
||||
|
||||
local originalAssignedKey = (originalVehicleType == "normal") and "AssignedRooms" or ("AssignedRooms" .. originalVehicleType)
|
||||
if modData[originalAssignedKey] then
|
||||
modData[originalAssignedKey][vehicleId] = nil
|
||||
end
|
||||
|
||||
local newAssignedKey = (newRoomType == "normal") and "AssignedRooms" or ("AssignedRooms" .. newRoomType)
|
||||
modData[newAssignedKey] = modData[newAssignedKey] or {}
|
||||
modData[newAssignedKey][vehicleId] = newRoom
|
||||
|
||||
playerData.RoomType = newRoomType
|
||||
playerData.ActualRoom = newRoom
|
||||
|
||||
modData.VehicleTypeOverrides = modData.VehicleTypeOverrides or {}
|
||||
modData.VehicleTypeOverrides[vehicleId] = newRoomType
|
||||
|
||||
local vehicle = findVehicleByPersistentId(vehicleId)
|
||||
if vehicle then
|
||||
local vmd = vehicle:getModData()
|
||||
vmd.projectRV_type = newRoomType
|
||||
if vehicle.transmitModData then
|
||||
vehicle:transmitModData()
|
||||
end
|
||||
end
|
||||
|
||||
if ModData and ModData.transmit then
|
||||
ModData.transmit("modPROJECTRVInterior")
|
||||
end
|
||||
|
||||
print(string.format(
|
||||
"[RVReassignServer] Vehicle %s moved from type %s to %s at x=%d y=%d z=%d",
|
||||
vehicleId, tostring(originalVehicleType), tostring(newRoomType), newRoom.x, newRoom.y, newRoom.z
|
||||
))
|
||||
|
||||
return true, "Vehicle reassigned to room type: " .. tostring(newRoomType)
|
||||
end
|
||||
|
||||
local function onClientCommand(module, command, player, args)
|
||||
if module ~= SERVER_MODULE then
|
||||
return
|
||||
end
|
||||
|
||||
if command == SERVER_COMMAND_REASSIGN then
|
||||
local ok, message = reassignVehicleToCurrentRoom(player)
|
||||
if player then
|
||||
sendServerCommand(player, SERVER_MODULE, CLIENT_COMMAND_RESULT, {
|
||||
ok = ok,
|
||||
message = message
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Events.OnClientCommand.Add(onClientCommand)
|
||||
Reference in New Issue
Block a user