Skip to content
This repository was archived by the owner on Apr 28, 2026. It is now read-only.

Create and trigger new openShop hook on shop open#51

Merged
antond15 merged 4 commits into
CommunityOx:mainfrom
JHansen2000:main
Oct 3, 2025
Merged

Create and trigger new openShop hook on shop open#51
antond15 merged 4 commits into
CommunityOx:mainfrom
JHansen2000:main

Conversation

@JHansen2000

Copy link
Copy Markdown

Trigger the openInventory hook when a shop is opened by the player.

Additional Notes:

  • Currently, the openedInventory and closedInventory events fire when a shop is opened/closed with only data pertaining to the player who is performing the action. It may be worth modifying these events to trigger with the data of the shop instead. Please let me know if you all agree. If so, I will extend this PR to also include those changes.

@Maximus7474

Maximus7474 commented Sep 7, 2025

Copy link
Copy Markdown
Member

So going over the conversation that took place in #🧩│scripting

It seems like the decision was intentional, shops weren't planned to be included in the open inventory not being an inventory.
I think the best solution would be to create a new hook event for openShop as it should contain more information then just inventory type and id.

The events are however handled on a lower level, it's triggered straight from within the meta object when the open method is called, same for closed inventory.

---Open a player's inventory, optionally with a secondary inventory.
---@param inv? inventory
function OxInventory:openInventory(inv)
if not self?.player then return end
inv = Inventory(inv)
if not inv then return end
inv:set('open', true)
inv.openedBy[self.id] = true
self.open = inv.id
TriggerEvent('ox_inventory:openedInventory', self.id, inv.id)
end

@JHansen2000

Copy link
Copy Markdown
Author

Just to make sure I've got the finer details clear, can you confirm this path forward?

New openShop hook:

  • source
  • shopType
  • shopId
  • label
  • groups
  • coords
  • Is there any other valuable data that should be included in the hook payload?

For the events, do we want to change them so that openInventory sends the id of the shop, or should it trigger a different new event?

  • Having it send the current will be easier, but probably not as valuable because both the shop type and shop id are needed to accurately key the shop. This also seems anti-pattern per our conversation in #🧩│scripting
  • Creating a new event is fine, if we want to go that route, but I think we'll still have to trigger the existing openedInventory event so that the player's state is updated as having an inventory open.

Let me know what you think. New commit coming in soon with the new hook as described above.

@JHansen2000 JHansen2000 changed the title Trigger openInventory hook on shop open Create and trigger new openShop hook on shop open Sep 8, 2025
@SpellLivedBackwards

Copy link
Copy Markdown

It would be a significant improvement if duty status were supported, similar to what's implemented in #41, though I realize that may not be feasible. Even if we use a hook to block shop access while off duty, the text labels, targets and blips still appear, which isn't ideal.

@JHansen2000

Copy link
Copy Markdown
Author

It would be a significant improvement if duty status were supported, similar to what's implemented in #41, though I realize that may not be feasible. Even if we use a hook to block shop access while off duty, the text labels, targets and blips still appear, which isn't ideal.

I think that's a good idea, but a little out of scope for this PR. I want this PR to stay focused on the addition of a new hook, rather than changing the core functionality of the shops.

Comment thread modules/shops/server.lua Outdated
shopType = shop.type,
shopId = shop.id,
label = shop.name,
groups = shop.groups or shop.jobs,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shop.jobs will always be nil it's the internal parameter used by the inventory not the parameter made public that developers will interact with.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local hookPayload = {
    source = source,
    id = shop.id, -- string <name index>
    label = shop.label, -- string
    slots = shop.slots, -- integer
    items = shop.items, -- table
    groups = shop.groups, -- table | nil
    coords = shop.coords, -- vec3 | nil
    distance = shop.distance -- float | nil
}

New payload, tested and verified

@Maximus7474 Maximus7474 Sep 8, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shop.distance isn't it number or false ? Iirc it's false when not using targeting.
Why did you annotate the other values being possibly nil ?

@JHansen2000 JHansen2000 Sep 8, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

distance just came back as nil in my tests. groups, coords, and distance might be nil if the hook is triggered by a shop created at runtime or if the fields aren't created. Here's some sample data I got from my test server.

[
  {
    "label": "Runtime Burger Shot",
    "items": [
      {
        "weight": 100,
        "name": "bs_bleeder",
        "count": 0,
        "price": 53,
        "slot": 1
      },
      { "weight": 100, "name": "bs_fries", "count": 0, "price": 25, "slot": 2 },
      {
        "weight": 100,
        "name": "bs_heartstopper",
        "count": 0,
        "price": 50,
        "slot": 3
      },
      {
        "weight": 100,
        "name": "bs_heartstopper_meal",
        "count": 0,
        "price": 80,
        "slot": 4
      },
      {
        "weight": 100,
        "name": "bs_moneyshot",
        "count": 0,
        "price": 50,
        "slot": 5
      },
      {
        "weight": 100,
        "name": "bs_moneyshot_meal",
        "count": 0,
        "price": 80,
        "slot": 6
      },
      {
        "weight": 100,
        "name": "bs_nugget_meal",
        "count": 0,
        "price": 80,
        "slot": 7
      },
      {
        "weight": 100,
        "name": "bs_nuggets",
        "count": 0,
        "price": 25,
        "slot": 8
      },
      {
        "weight": 100,
        "name": "bs_torpedo",
        "count": 0,
        "price": 50,
        "slot": 9
      },
      {
        "weight": 100,
        "name": "bs_torpedo_meal",
        "count": 0,
        "price": 80,
        "slot": 10
      },
      { "weight": 100, "name": "coffee", "count": 0, "price": 8, "slot": 11 },
      { "weight": 100, "name": "ecola", "count": 0, "price": 5, "slot": 12 },
      { "weight": 100, "name": "sprunk", "count": 0, "price": 5, "slot": 13 }
    ],
    "slots": 13,
    "id": "RuntimeBurgerShot 1",
    "type": "shop"
  },
  {
    "label": "Shop",
    "items": [
      { "weight": 220, "name": "burger", "price": 10, "slot": 1 },
      { "weight": 500, "name": "water", "price": 10, "slot": 2 },
      { "price": 10, "name": "cola" }
    ],
    "type": "shop",
    "slots": 3,
    "id": "General 1",
    "coords": {
      "x": 25.05999946594238,
      "y": -1347.3199462890626,
      "z": 29.70000076293945
    },
    "distance": 1.5
  },
  {
    "groups": { "sasp": 0, "police": 0, "bcso": 0 },
    "label": "Police Armoury",
    "items": [
      { "weight": 7, "name": "ammo-9", "price": 5, "slot": 1 },
      { "weight": 4, "name": "ammo-rifle", "price": 5, "slot": 2 },
      { "weight": 125, "name": "WEAPON_FLASHLIGHT", "price": 200, "slot": 3 },
      { "weight": 1000, "name": "WEAPON_NIGHTSTICK", "price": 100, "slot": 4 },
      {
        "weight": 1130,
        "name": "WEAPON_PISTOL",
        "price": 500,
        "metadata": { "registered": true, "serial": "POL" },
        "license": "weapon",
        "slot": 5
      },
      {
        "weight": 3100,
        "name": "WEAPON_CARBINERIFLE",
        "price": 1000,
        "metadata": { "registered": true, "serial": "POL" },
        "grade": 3,
        "license": "weapon",
        "slot": 6
      },
      {
        "weight": 227,
        "name": "WEAPON_STUNGUN",
        "price": 500,
        "metadata": { "registered": true, "serial": "POL" },
        "slot": 7
      }
    ],
    "coords": {
      "x": 453.2099914550781,
      "y": -980.030029296875,
      "z": 31.43000030517578
    },
    "slots": 7,
    "id": "PoliceArmoury 1",
    "type": "shop",
    "distance": 6
  }
]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah those prints are just showing general shop data after adding them no ? It isn't printed from within the openShop callback ? What exactly are you printing ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These came from printing the shop object directly prior to triggering the new openShop hook.

Comment thread modules/shops/server.lua Outdated

@Maximus7474 Maximus7474 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires some minor changes

@JHansen2000

Copy link
Copy Markdown
Author

Any status on this? Are there any changes that need to be made?

@JHansen2000

Copy link
Copy Markdown
Author

Any status on this? Are there any changes that need to be made? It's been over 2 weeks without any movement, not sure if there's anything I need to do to get things moving again. Let me know.

@ESK0

ESK0 commented Sep 29, 2025

Copy link
Copy Markdown
Member

Please update docs with new hook type.

@JHansen2000

Copy link
Copy Markdown
Author

Please update docs with new hook type.

Please reference this linked PR:
CommunityOx/docs#35

@JHansen2000

Copy link
Copy Markdown
Author

Ready for review

@antond15 antond15 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@antond15 antond15 merged commit beac03d into CommunityOx:main Oct 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants