diff --git a/core/ShopState.lua b/core/ShopState.lua index c530ffb..4655462 100644 --- a/core/ShopState.lua +++ b/core/ShopState.lua @@ -97,10 +97,15 @@ local function handlePurchase(transaction, meta, sentMetaname, transactionCurrency, transactionCurrency, state) local purchasedProduct = nil - for _, product in ipairs(state.products) do - if product.address:lower() == sentMetaname:lower() or product.name:gsub(" ", ""):lower() == sentMetaname:lower() then - purchasedProduct = product - break + if state.eventHooks and state.eventHooks.preProduct then + purchasedProduct = eventHook.execute(state.eventHooks.preProduct, transaction, transactionCurrency, meta, sentMetaname, state.products) + end + if purchasedProduct == nil then + for _, product in ipairs(state.products) do + if product.address:lower() == sentMetaname:lower() or product.name:gsub(" ", ""):lower() == sentMetaname:lower() then + purchasedProduct = product + break + end end end if purchasedProduct then @@ -295,7 +300,11 @@ end end, function() while state.running do - ScanInventory.updateProductInventory(state.products) + local onInventoryRefresh = nil + if state.eventHooks and state.eventHooks.onInventoryRefresh then + onInventoryRefresh = state.eventHooks.onInventoryRefresh + end + ScanInventory.updateProductInventory(state.products, onInventoryRefresh) if state.config.settings.hideUnavailableProducts then state.productsChanged = true end diff --git a/core/inventory/ScanInventory.lua b/core/inventory/ScanInventory.lua index 8ae47d0..b8ecca3 100644 --- a/core/inventory/ScanInventory.lua +++ b/core/inventory/ScanInventory.lua @@ -1,3 +1,5 @@ +local eventHook = require("util.eventHook") + local itemCache = {} local partialObjectMatches @@ -128,7 +130,7 @@ return items end -local function updateProductInventory(products) +local function updateProductInventory(products, onInventoryRefresh) for i = 1, #products do products[i].newQty = 0 end @@ -140,6 +142,9 @@ product.quantity = product.newQty product.newQty = nil end + if onInventoryRefresh then + eventHook.execute(onInventoryRefresh, products, items) + end end local function getItemCache() diff --git a/core/schemas.lua b/core/schemas.lua index 8a6c0bd..28c3da5 100644 --- a/core/schemas.lua +++ b/core/schemas.lua @@ -218,6 +218,7 @@ name = "string?", address = "string", category = "string?", + hidden = "boolean?", price = "number", priceOverrides = { __type = "array?", diff --git a/eventHooks.lua b/eventHooks.lua index 67f336b..56b6933 100644 --- a/eventHooks.lua +++ b/eventHooks.lua @@ -1,5 +1,8 @@ return { start = nil, -- function(version, config, products) + preProduct = nil, -- function(transaction, transactionCurrency, meta, productAddress, products) returns product + -- If product is nil, product will be selected by the shop, + -- If product is false, customer will be refunded for no product found. prePurchase = nil, -- function(product, amount, refundAmount, transaction, transactionCurrency) returns continueTransaction, error, errorMessage purchase = nil, -- function(product, amount, refundAmount, transaction, transactionCurrency) failedPurchase = nil, -- function(transaction, transactionCurrency, product, errorMessage) @@ -7,4 +10,5 @@ blink = nil, -- function(blinkState) called every 3 seconds while shop is running configSaved = nil, -- function(config) called when config is edited (replaced) productsSaved = nil, -- function(products) called when products object is edited (replaced) + onInventoryRefresh = nil, -- function(products, items) called when inventory is refreshed, product quantity can be set through products table } \ No newline at end of file diff --git a/products.lua b/products.lua index ce11724..dc5f839 100644 --- a/products.lua +++ b/products.lua @@ -31,5 +31,12 @@ } } } + }, + { + modid = "minecraft:barrier", + name = "Secret item", + address = "secret", + hidden = true, + price = 1.0 } } \ No newline at end of file diff --git a/radon.lua b/radon.lua index 6fdeee9..cd01fb8 100644 --- a/radon.lua +++ b/radon.lua @@ -1,4 +1,4 @@ -local version = "1.3.14" +local version = "1.3.15" local configHelpers = require "util.configHelpers" local schemas = require "core.schemas" local oldPullEvent = os.pullEvent diff --git a/util/renderHelpers.lua b/util/renderHelpers.lua index 303f1ea..54b74bc 100644 --- a/util/renderHelpers.lua +++ b/util/renderHelpers.lua @@ -37,27 +37,29 @@ local function getCategories(products) local categories = {} for _, product in ipairs(products) do - local category = product.category - if not category then - category = "*" - end - local found = nil - for i = 1, #categories do - if categories[i].name == category then - found = i - break + if not product.hidden then + local category = product.category + if not category then + category = "*" end - end - if not found then - if category == "*" then - table.insert(categories, 1, {name=category, products={}}) - found = 1 - else - table.insert(categories, {name=category, products={}}) - found = #categories + local found = nil + for i = 1, #categories do + if categories[i].name == category then + found = i + break + end end + if not found then + if category == "*" then + table.insert(categories, 1, {name=category, products={}}) + found = 1 + else + table.insert(categories, {name=category, products={}}) + found = #categories + end + end + table.insert(categories[found].products, product) end - table.insert(categories[found].products, product) end return categories end