package com.dunerealms.menus; import com.dunerealms.jarvis.DunemarkDenomination; import java.util.ArrayList; import java.util.List; import org.bukkit.Material; import org.bukkit.entity.Player; /** * Focused cash operations and a bank-reported receipt; no extra confirmation screen. * Deposit : tap bills and coins, one click -> a new RIA ID is created and shown in the ID box. * Redeem : type an RIA ID, then one click pays it out as Cash or To bank. * Withdraw : tap bills and coins, one click takes the cash out of your own account. */ final class AtmMenu extends Menu { enum Tab { HOME, DEPOSIT, REDEEM, WITHDRAW, RECEIPT } private static final int[] BILLS = {10, 11, 12, 19, 20}; private static final int[] COINS = {14, 15, 16}; private static final List NOTES = List.of(DunemarkDenomination.NOTE_10, DunemarkDenomination.NOTE_20, DunemarkDenomination.NOTE_50, DunemarkDenomination.NOTE_100, DunemarkDenomination.NOTE_500); private static final List COIN_VALUES = List.of(DunemarkDenomination.COIN_050, DunemarkDenomination.COIN_1DM, DunemarkDenomination.COIN_2DM); private Tab tab; private final List selected = new ArrayList<>(); private String entered; // RIA ID typed on the Redeem tab private String receiptOperation,receiptReference,receiptMessage; private Long receiptAmount; AtmMenu(DuneMenusPlugin plugin, Player p, Menu parent, Tab tab) { super(plugin, p, parent); this.tab = tab; } String id() { return switch(tab){case WITHDRAW->"withdraw";case DEPOSIT->"deposit";case REDEEM->"redeem";case RECEIPT->"receipt";default->"atm";}; } Skin skin() { return tab == Tab.REDEEM ? Skin.GOLD : Skin.TRAVEL; } String title() { return switch(tab){case HOME->"Rihla ATM";case DEPOSIT->"Rihla · Deposit cash";case WITHDRAW->"Rihla · Withdraw cash";case REDEEM->"Rihla · Redeem RIA ID";case RECEIPT->"Rihla · Transaction receipt";}; } String subtitle() { return switch (tab) { case HOME -> "Choose a cash service. Bills and coins are selected inside the operation."; case DEPOSIT -> "Put in cash. You get an RIA ID to keep or give away."; case REDEEM -> "Got an RIA ID? Type it in and take the money as cash or to your bank."; case WITHDRAW -> "Select bills and coins, then withdraw the displayed amount."; case RECEIPT -> "Your bank-reported transaction result. Keep your RIA ID."; }; } int rows() { return 5; } @Override boolean renderWhileLoading(){return true;} @Override boolean navigationWhileLoading(int slot){return slot==0||slot==1||slot==2||slot==36;} private void tabButton(int slot, Material m, String label, Tab t) { var item = Ui.item(m, (tab == t ? "" : "") + label); put(slot, tab == t ? Ui.glow(item) : item, c -> { tab = t; selected.clear(); redraw(); }); } private void add(int slot, DunemarkDenomination denomination) { var selector=denomination.displayItem();var meta=selector.getItemMeta();meta.displayName(net.kyori.adventure.text.Component.text(Ui.dm(denomination.faceMinor())));meta.lore(List.of(net.kyori.adventure.text.Component.text("Select one for this "+(tab==Tab.DEPOSIT?"deposit":"withdrawal"))));selector.setItemMeta(meta); put(slot, selector, c -> { if (selected.size() < 36) selected.add(denomination); redraw(); }); } void draw() { backdrop(Material.BLACK_STAINED_GLASS_PANE); for (int s : new int[]{3, 5, 6, 7, 8}) put(s, Ui.pane(Material.CYAN_STAINED_GLASS_PANE)); if(tab==Tab.HOME){ tabButton(0, Material.EMERALD, "Deposit cash", Tab.DEPOSIT); tabButton(1, Material.NAME_TAG, "Redeem RIA ID", Tab.REDEEM); tabButton(2, Material.GOLD_INGOT, "Withdraw cash", Tab.WITHDRAW); put(36,Ui.item(Material.ARROW,"Back"),c->{end();plugin.openNativeService(player,"hub");}); return; } put(36,Ui.item(Material.ARROW,"Back"),c->{tab=Tab.HOME;selected.clear();redraw();}); if(tab==Tab.RECEIPT){ put(13,Ui.item(Material.PAPER,""+receiptOperation, receiptAmount==null?""+receiptMessage:"Amount: "+Ui.dm(receiptAmount),""+receiptMessage)); if(receiptReference!=null&&!receiptReference.isBlank())put(29,Ui.item(Material.NAME_TAG,"RIA ID",""+receiptReference)); return; } if (tab == Tab.REDEEM) drawRedeem(); else drawCash(); } private void drawCash() { long minor = selected.stream().mapToLong(DunemarkDenomination::faceMinor).sum(); boolean dep = tab == Tab.DEPOSIT; String verb = dep ? "Deposit" : "Withdraw"; put(9, Ui.item(Material.GREEN_STAINED_GLASS_PANE, "Bills")); put(13, Ui.item(Material.YELLOW_STAINED_GLASS_PANE, "Coins")); for (int i = 0; i < BILLS.length; i++) add(BILLS[i], NOTES.get(i)); for (int i = 0; i < COINS.length; i++) add(COINS[i], COIN_VALUES.get(i)); put(24, minor == 0 ? Ui.item(Material.GRAY_DYE, "" + verb, "Choose bills and coins first") : Ui.glow(Ui.item(dep ? Material.EMERALD : Material.GOLD_INGOT, "" + verb + " " + Ui.dm(minor), dep ? "Creates a new RIA ID" : "From your bank account")), c -> { if (minor <= 0) return; var pieces = List.copyOf(selected); if (dep) load(backend().atmCreateRia(player, pieces), r -> { say(new Backend.Result(r.ok(), r.message())); if (r.ok()) { receipt("Cash deposit",r.receipt(),r.message()); } else redraw(); }); else load(backend().bankWithdraw(player, pieces), r -> { say(r); if (r.ok()) receipt("Cash withdrawal",r.receipt(),r.message()); else redraw(); }); }); } private void receipt(String operation,Backend.CashReceipt facts,String message){ receiptOperation=facts!=null&&facts.recovered()?"Recovered Rihla transaction":operation; receiptAmount=facts==null?null:facts.amountMinor();receiptReference=facts==null?null:facts.riaId();receiptMessage=message; selected.clear();tab=Tab.RECEIPT;redraw(); } private void drawRedeem() { put(13, Ui.item(Material.WRITABLE_BOOK, entered == null ? "Enter RIA ID" : "Change RIA ID", "Type the reference in chat", "Type cancel to return"), c -> { end(); plugin.closeNativePresentation(player); plugin.enterRia(player,text->{if(text!=null)entered=text;open();}); }); for (int s : new int[]{28, 30}) put(s, Ui.pane(Material.YELLOW_STAINED_GLASS_PANE)); if (entered == null) return; put(29,Ui.item(Material.NAME_TAG,"RIA ID",""+entered)); final String id = entered; load(backend().atmLookup(player, id), v -> { if (v == null) { put(22, Ui.item(Material.BARRIER, "RIA ID not found")); return; } put(22, Ui.glow(Ui.item(Material.GOLD_BLOCK, "" + Ui.dm(v.amountMinor()), "From: " + v.fromName()))); put(24, Ui.glow(Ui.item(Material.GOLD_INGOT, "Take as cash", "Bills and coins in your inventory")), c -> load(backend().atmRedeem(player, v, false), r -> { say(r); if (r.ok()) {entered = null;receipt("RIA payout · Cash",r.receipt(),r.message());} else redraw(); })); put(26, Ui.glow(Ui.item(Material.ENDER_CHEST, "Send to my bank", "Credited to your account")), c -> load(backend().atmRedeem(player, v, true), r -> { say(r); if (r.ok()) {entered = null;receipt("RIA payout · Bank",r.receipt(),r.message());} else redraw(); })); }); } }