Developer

2 min readUpdated 2 weeks ago

Overview

Other resources read plant data through server and client exports. The plant flow is changed in server/handlers.lua and client/handlers.lua, which stay unencrypted.

Server Exports

lua
1local plantType = exports.ml_planting:GetPlantTypeConfig(typeId)
2local allTypes = exports.ml_planting:GetAllPlantTypes()
3local categories = exports.ml_planting:GetCategories()
4local category = exports.ml_planting:GetCategoryConfig(categoryId)
  • GetPlantTypeConfig(typeId): one plant type
  • GetAllPlantTypes(): every plant type
  • GetCategories(): every category
  • GetCategoryConfig(categoryId): one category

Client Exports

lua
1local zoneId = exports.ml_planting:IsInGrowZone(coords)
2local plants = exports.ml_planting:GetNearbyPlants(distance)
3local entity = exports.ml_planting:GetPlantEntity(plantId)
  • IsInGrowZone(coords): ID of the growth zone at those coordinates, or nil
  • GetNearbyPlants(distance?): loaded plants within that distance of the player. Without a distance it uses Config.StreamRadius
  • GetPlantEntity(plantId): prop entity of a loaded plant

Server Handlers

Located in server/handlers.lua.

server/handlers.lua
1function Handlers.OnPlantCreated(source, plantData)
2end
3
4function Handlers.OnPlantWatered(source, plant, item, amount)
5end
6
7function Handlers.OnPlantFertilized(source, plant, item, amount)
8end
9
10function Handlers.OnPlantHarvested(source, plant, rewards)
11end
12
13function Handlers.OnPlantDestroyed(source, plant)
14end
15
16function Handlers.CanPlant(source, seed, coords)
17    return true
18end
19
20function Handlers.CanHarvest(source, plant)
21    return true
22end
23
24function Handlers.ModifyHarvestRewards(source, plant, rewards)
25    return rewards
26end
  • OnPlantCreated, OnPlantWatered, OnPlantFertilized, OnPlantHarvested: plant events
  • OnPlantDestroyed: a player or an admin destroys a plant. Plants that die from neglect do not fire it
  • CanPlant: return false to refuse a seed at that spot
  • CanHarvest: return false to refuse a harvest
  • ModifyHarvestRewards: edit the rewards before they are given and return them, as a list of { name, count }. An invalid list cancels the harvest

Client Handlers

Located in client/handlers.lua.

client/handlers.lua
1function Handlers.CanInteractWithPlant(plant)
2    return true
3end
4
5function Handlers.OnPlantSpawned(plant, entity)
6end
7
8function Handlers.OnPlantDespawned(plantId)
9end
10
11function Handlers.FilterInteractionOptions(plant, options)
12    return options
13end
  • CanInteractWithPlant: return false to hide the menu of a plant. The in-world plant panel does not use it
  • OnPlantSpawned: the prop of a plant spawns near the player. Add effects or props here
  • OnPlantDespawned: the prop is removed
  • FilterInteractionOptions: change or remove the options shown in the plant menu. Each option has an id such as 'harvest'

Examples

server/handlers.lua
1function Handlers.OnPlantHarvested(source, plant, rewards)
2    exports['my_logging']:LogAction(source, 'harvested', plant.seed)
3end
server/handlers.lua
1function Handlers.CanHarvest(source, plant)
2    local job = Bridge.GetJob(source)
3    if job and job.name == 'police' then
4        return false
5    end
6    return true
7end
client/handlers.lua
1function Handlers.FilterInteractionOptions(plant, options)
2    local job = Bridge.GetJob()
3    if job and job.name == 'police' then
4        for i = #options, 1, -1 do
5            if options[i].id == 'harvest' then table.remove(options, i) end
6        end
7    end
8    return options
9end
Planting Developer, FiveM Docs | Micio Mods