diff --git a/core/ShopState.lua b/core/ShopState.lua index 2351d23..e5d890e 100644 --- a/core/ShopState.lua +++ b/core/ShopState.lua @@ -166,7 +166,7 @@ end end if self.eventHooks and self.eventHooks.preStockCheck then - local allowPurchase, err, errMessage, invisible = eventHook.execute(self.eventHooks.preStockCheck, transaction, productsPurchased, self.products) + local allowPurchase, err, errMessage, invisible = eventHook.execute(self.eventHooks.preStockCheck, transaction, productsPurchased, self.products, amountPurchased) if allowPurchase == false then if not invisible then refund(transactionCurrency, transaction.from, meta, transaction.value, errMessage or self.config.lang.refundDenied, err) @@ -342,16 +342,7 @@ sentMetaname = meta[1] end if sentMetaname then - local success, err = pcall(ShopState.handlePurchase, self, transaction, meta, sentMetaname, transactionCurrency) - if success then - -- Success :D - else - refund(transactionCurrency, transaction.from, meta, transaction.value, self.config.lang.refundError, true) - if self.eventHooks and self.eventHooks.failedPurchase then - eventHook.execute(self.eventHooks.failedPurchase, transaction, transactionCurrency, nil, self.config.lang.refundError) - end - error(err) - end + os.queueEvent("radon_purchase", transaction, meta, sentMetaname, transactionCurrency) elseif self.config.settings.refundMissingMetaname then if self.config.settings.refundInvalidMetaname then refund(transactionCurrency, transaction.from, meta, transaction.value, self.config.lang.refundNoProduct, true) @@ -366,6 +357,22 @@ end end, function() while self.running do + local event, transaction, meta, sentMetaname, transactionCurrency = os.pullEvent("radon_purchase") + if event == "radon_purchase" then + local success, err = pcall(ShopState.handlePurchase, self, transaction, meta, sentMetaname, transactionCurrency) + if success then + -- Success :D + else + refund(transactionCurrency, transaction.from, meta, transaction.value, self.config.lang.refundError, true) + if self.eventHooks and self.eventHooks.failedPurchase then + eventHook.execute(self.eventHooks.failedPurchase, transaction, transactionCurrency, nil, self.config.lang.refundError) + end + error(err) + end + end + end + end, function() + while self.running do local onInventoryRefresh = nil if self.eventHooks and self.eventHooks.onInventoryRefresh then onInventoryRefresh = self.eventHooks.onInventoryRefresh diff --git a/core/inventory/ScanInventory.lua b/core/inventory/ScanInventory.lua index a5a12c0..55574d0 100644 --- a/core/inventory/ScanInventory.lua +++ b/core/inventory/ScanInventory.lua @@ -159,6 +159,10 @@ products = score.copyDeep(originalProducts) local items = getAllInventoryItems(inventories, products) itemCache = items + if onInventoryRefresh then + eventHook.execute(onInventoryRefresh, products, items) + end + for i = 1, #products do local product = products[i] product.predicatesString = nil @@ -186,9 +190,6 @@ product.__opaque = true originalProducts[i] = product end - if onInventoryRefresh then - eventHook.execute(onInventoryRefresh, products, items) - end end local function getItemCache() diff --git a/eventHooks.lua b/eventHooks.lua index 49c6c51..aa9c3b8 100644 --- a/eventHooks.lua +++ b/eventHooks.lua @@ -4,7 +4,7 @@ -- If product is nil, product will be selected by the shop, -- If product is false, customer will be refunded for no product found. -- If product is false and errored is true, customer will be refunded with error message. - preStockCheck = nil, -- function(transaction, productsPurchased, products) returns continueTransaction, errored, errorMessage, invisible + preStockCheck = nil, -- function(transaction, productsPurchased, products, amountPurchased) returns continueTransaction, errored, errorMessage, invisible prePurchase = nil, -- function(product, amount, refundAmount, transaction, transactionCurrency) returns continueTransaction, errored, errorMessage, invisible purchase = nil, -- function(product, amount, refundAmount, transaction, transactionCurrency) failedPurchase = nil, -- function(transaction, transactionCurrency, product, errorMessage) diff --git a/radon.lua b/radon.lua index 3419744..e72f687 100644 --- a/radon.lua +++ b/radon.lua @@ -1,4 +1,4 @@ -local version = "1.3.26" +local version = "1.3.28" local configHelpers = require "util.configHelpers" local schemas = require "core.schemas" local ScanInventory = require("core.inventory.ScanInventory")