Developer

5 min readUpdated Yesterday

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

Stations

  • CanUseStation(src, propId, family?): true while the player may use that station right now. Call it before committing each craft, repair or recycle action. A past opening does not authorize later actions.

Tree Platforms

  • RegisterTreeAnchors(catalogue): adds a tree catalogue for the calling resource. Returns true, count or false, reason. See Custom Tree Maps on the Structures page
  • RemoveTreeAnchors(): removes that resource's catalogue

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)

Shared Hooks

Located in open/shared.lua.

  • OnSharedReady(): runs on both sides once the shared hooks are loaded

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

Stations

MLBB.StationBindings in open/shared.lua links imported stations to a crafting resource. Keys are workbench, chemistry, repair, recycler and stove, or a model name to override one family. false leaves a station unconfigured.

open/shared.lua
1MLBB.StationBindings.workbench = {
2    adapter = 'server_export',
3    resource = 'my_crafting',
4    name = 'OpenBaseStation',
5    args = { recipeSet = 'workbench' },
6}

The export receives (src, context, args) and returns true once its interface is open, or false to refuse. context holds propId, kind, model, stationId, ownerCid, flagId, bucket, coords and rotation.

Other adapters: ox_inventory for an existing bench (stationId, index), client_export, client_event, and server_hook, which calls MLBB.Hooks.OpenStation(src, context, args) in open/server.lua. A client event only confirms dispatch, so use an export or hook when the result matters.

Recycling stations need power. Set requireFire = true on a cooking binding to require a lit stove.

More Server Events

  • ml_base_building:server:ready: bases are loaded
  • ml_base_building:server:crewMemberAdded: crewId, cid
  • ml_base_building:server:crewMemberRemoved: crewId, cid, reason, where reason is kicked, left or admin_kicked
  • ml_base_building:server:crewPermsChanged: crewId, cid, perms
  • ml_base_building:server:crewDisbanded: crewId
  • ml_base_building:server:flagRemoved: flagId
  • ml_base_building:server:stationRemoved: propId
  • ml_base_building:server:machinePlaced, machineRemoved, machineToggle, machinePower, machineReadopt: for resources using RegisterMachine, each with a table payload

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
Outpost Developer, FiveM Docs | Micio Mods