diff --git a/core/ShopState.lua b/core/ShopState.lua index e5d890e..938dfcb 100644 --- a/core/ShopState.lua +++ b/core/ShopState.lua @@ -317,6 +317,7 @@ self.kryptonListeners = {} self:setupKrypton() ScanInventory.clearNbtCache() + local transactions = {} parallel.waitForAny(function() while true do local event, transactionEvent = os.pullEvent("transaction") @@ -342,7 +343,14 @@ sentMetaname = meta[1] end if sentMetaname then - os.queueEvent("radon_purchase", transaction, meta, sentMetaname, transactionCurrency) + local purchaseData = { + transaction = transaction, + meta = meta, + sentMetaname = sentMetaname, + transactionCurrency = transactionCurrency + } + transactions[#transactions + 1] = purchaseData + os.queueEvent("radon_purchase", purchaseData) -- for hooks that might be able to catch it (parallel). elseif self.config.settings.refundMissingMetaname then if self.config.settings.refundInvalidMetaname then refund(transactionCurrency, transaction.from, meta, transaction.value, self.config.lang.refundNoProduct, true) @@ -357,15 +365,25 @@ 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) + -- Run event hook for the parallel constant running task + -- This can do things like listen to events or host applications + if self.eventHooks and self.eventHooks.parallel then + eventHook.execute(self.eventHooks.parallel) + end + sleep(blinkFrequency) + end + end, function() + while self.running do + os.pullEvent("radon_purchase") + while #transactions > 0 do + local purchaseData = table.remove(transactions, 1) + local success, err = pcall(ShopState.handlePurchase, self, purchaseData.transaction, purchaseData.meta, purchaseData.sentMetaname, purchaseData.transactionCurrency) if success then -- Success :D else - refund(transactionCurrency, transaction.from, meta, transaction.value, self.config.lang.refundError, true) + refund(purchaseData.transactionCurrency, purchaseData.transaction.from, purchaseData.meta, purchaseData.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) + eventHook.execute(self.eventHooks.failedPurchase, purchaseData.transaction, purchaseData.transactionCurrency, nil, self.config.lang.refundError) end error(err) end @@ -408,15 +426,6 @@ end end, function() while self.running do - -- Run event hook for the parallel constant running task - -- This can do things like listen to events or host applications - if self.eventHooks and self.eventHooks.parallel then - eventHook.execute(self.eventHooks.parallel) - end - sleep(blinkFrequency) - end - end, function() - while self.running do sleep(shopSyncFrequency) if self.config.shopSync and self.config.shopSync.enabled and self.peripherals.shopSyncModem then local items = {}