Solved Transaction Menu System

Discussion in 'Spigot Plugin Development' started by Accessory, Jul 21, 2021.

  1. I'm working on creating a menu that will ask a player for confirmation before making any purchases on the network.

    This is how the inventory will look:
    [​IMG]

    The Redstone block cancels the purchase, and the Emerald block confirms the purchase.

    I have no problems with cancelling a transaction through the use of the Redstone block, but the issues lie when a player confirms their purchase.

    When I initiate a transaction, I call my "Transaction" class which takes basic information such as the player buying, the item, price, and the creation of a callback.

    When the Emerald block is clicked, I make sure that the player has the required amount of funds for the payment. If not, the transaction will fail for "Insufficient funds".

    If they do however have enough funds, the transaction is accepted, the money is deducted, and the callback is called (to allow me to make any changes after completion such as giving the player the item, etc).

    Now, you're probably wondering... What is the problem then?

    The problem lies when a player tries to purchase an item that has been already purchased by another player. For example: If Player1 tries to buy a Diamond, but takes too long within the transaction menu, Player2 can go and buy the Diamond before Player1 had the chance to click the emerald blocks within their transaction menu.

    How could I add a check within the callback to cancel a transaction if the item is no longer available once they choose to accept the purchase?

    I do not want to add the check directly into the Transaction class as this class will be used for many other purchases that do not have this problem (aka purchases that do not have a finite amount in stock, i.e infinite).
     
    #1 Accessory, Jul 21, 2021
    Last edited: Jul 23, 2021
  2. So you want to call the Transaction classes' "cancel" method from within your callback?
     
  3. My suggestion: make the transaction class receive an extra parameter during initialization of a Function which sole purpose will be to check for specific errors from where it was called.
    This will fix your problem and let you use it in other places as well.
     
  4. Essentially yeah, but I also want to be able to stop the "Purchase successful" message if the callback cancels the transaction. Currently, I am receiving both the success message and the message saying that the item is out of stock.
     
  5. I'm confused. Do you mind elaborating?
     
  6. There's a class called Function that can accept values. Make that a parameter (can receive null for nothing) that's responsible for checking the transaction before continuing.