Developer

3 min readUpdated Today

Overview

Other resources integrate through server exports and the hooks in open/server.lua, open/client.lua and open/shared.lua. Hooks receive live data: treat propData and prop as read-only and return quickly.

Exports answer ml_ resources

Most server exports answer only resources whose name starts with ml_ and return nil, false or {} to any other caller. IsReady, GetTotalCount, CountPropsByModel, GetGeneratorData and the machine registration exports answer every resource.

lua
1local apiVersion = exports.ml_base_building:GetOpenApiVersion()

Server Exports

Pieces

  • IsReady(): true once bases are loaded
  • GetActiveProps(), GetTotalCount(): all placed pieces and their count
  • CountPropsByModel(model): pieces of one model
  • GetPropsByOwner(cid), GetOwnerCount(cid): pieces of one character and their count
  • IsPropListComplete(): true when every piece has loaded

Flags and placement

  • GetFlagAtCoords(coords, bucket?), GetFlagsAtCoordsBatch(list): the flag that covers a position
  • GetFlagInfo(flagId), FlagExists(flagId), IsFlagUnderRaid(flagId)
  • CanBuildAt(cid, coords, bucket?): whether a character can build there
  • GetPlacementInfo(cid, coords, bucket?, permission?): flag, owner and permission result at a position
  • CanPlaceAt(cid, coords, bucket?, policy?), CanPlaceAtSrc(src, coords, policy?): placement check for your own placeable objects

Crews

  • GetCrewByCid(cid): the crew of a character, { id, name }
  • IsCrewMember(crewId, cid), GetCrewMemberCids(crewId)
  • CrewPermHas(crewId, cid, permName): checks a permission from shared/data/permissions.lua, such as 'canBuild'
  • GetCrewRaidLockUntil(crewId): end of the raid lock of a crew

Generators and machines

  • GetGeneratorData(propId), AddGeneratorFuel(propId, amount, fuelType)
  • RegisterMachine(spec), RegisterMachineVeto(fn), SetMachineOffered(kind, offered), PublishMachineCatalogue(list), ReadoptMachines(): let another resource add its own machines as base pieces
  • CanUseMachine(src, propId), IsMachinePowered(propId), GetMachineLife(propId), RepairMachine(src, propId, amount), GetMachineProps(), MachineCapabilities()

Beds

  • GetPlayerBeds(cid), ResolveBedSpawn(cid, bedId), ClaimRespawn(claim): base bed respawns for your death or spawn script

Client Exports

  • IsBuildEntity(entity): true when the entity is a base piece
  • isSheltered(): true while the player stands under a base roof
  • MLAdminOpen(), MLAdminPanel(): open the admin panel, and its label and icon for a staff menu

Server Hooks

Located in open/server.lua.

open/server.lua
1function MLBB.Hooks.CanPlace(src, model, coords, flagId)
2end
3
4function MLBB.Hooks.CanDamage(attackerSrc, propData, damage, damageContext)
5end
  • CanPlace: return false, localeKey to refuse a placement. nil keeps the normal checks. An error or a first value that is not a boolean also refuses
  • CanDamage: return false to refuse damage, or true, newDamage to replace it

Notification hooks, called after the action:

  • OnServerReady()
  • OnPropPlaced(src, propData), OnPropRemoved(src, propData, reason)
  • OnPropDamaged(attackerSrc, propData, damage, lifeAfter, damageContext)
  • OnDoorToggled(src, propData, isOpen), OnStorageOpened(src, propData)
  • OnGeneratorRefueled(src, propId, litres, fuelType, fuelAfter), OnPowerChanged(propId, powered)
  • OnExplosivePlanted(src, propData, itemName)
  • OnFlagCreated(flagId, propId, ownerCid), OnFlagRemoved(flagId)

src can be nil for decay, server removals and offline attackers.

Providers, return nil to keep the default:

  • GetGameHour(): game hour from 0 to 23, for solar output and raidSchedule
  • GetWeather(): GTA weather name, for solar output
  • GetWindSpeed(propId, coords): wind speed for a turbine in m/s, 0 to 1000, from a trusted server weather resource. Output is speed divided by ratedSpeed

Client Hooks

Located in open/client.lua.

  • OnClientReady()
  • OnPropEnterRange(prop, entity), OnPropExitRange(prop)
  • OnPowerStateChanged(propId, powered), OnDoorStateChanged(propId, isOpen)
  • OnPlacementStarted(model), OnPlacementConfirmed(model, propId), OnPlacementEnded(model)

Server Events

  • ml_base_building:server:generatorNoise: fired on the server for each running generator while Config.Generator.noiseEnabled is on, with { propId, coords, radius, bucket }. A zombie or AI resource can listen to it

Examples

open/server.lua
1function MLBB.Hooks.CanPlace(src, model, coords, flagId)
2    if #(coords - vec3(298.0, -584.0, 43.0)) < 150.0 then
3        return false, 'placement_disabled_zone'
4    end
5end
open/server.lua
1function MLBB.Hooks.CanDamage(attackerSrc, propData, damage, damageContext)
2    local hour = Bridge.GetGameTime() or 12
3    if hour >= 22 or hour < 6 then
4        return true, damage * 0.5
5    end
6end

The calling resource name must start with ml_.

ml_yourscript/server.lua
1local crew = exports.ml_base_building:GetCrewByCid(cid)
2if crew and exports.ml_base_building:CrewPermHas(crew.id, cid, 'canOpenStorages') then
3end