diff --git a/DuneRealmsGUI/src/main/java/com/dunerealms/gui/ExchangeMenus.java b/DuneRealmsGUI/src/main/java/com/dunerealms/gui/ExchangeMenus.java index 9bd0eb22..38290e1e 100644 --- a/DuneRealmsGUI/src/main/java/com/dunerealms/gui/ExchangeMenus.java +++ b/DuneRealmsGUI/src/main/java/com/dunerealms/gui/ExchangeMenus.java @@ -48,18 +48,49 @@ final class ExchangeMenus implements Listener,AutoCloseable { if(held.isEmpty()||amount<1||amount>64||amount>held.getAmount())throw new IllegalArgumentException(); publish(player,new Draft(held.clone(),inventory.getHeldItemSlot(),amount,unitPrice),false); }catch(IllegalArgumentException invalid){player.sendMessage("Hold the item and use /exchange sell .");} - }else if(args.length==4&&args[0].equalsIgnoreCase("buy")){ + }else if((args.length==4||args.length==5)&&args[0].equalsIgnoreCase("buy")){ try{ long id=Long.parseLong(args[1]);int quantity=Integer.parseInt(args[2]);long unitPrice=price(args[3]); - if(id<1||quantity<1||quantity>64)throw new IllegalArgumentException(); - var row=new JsonObject();row.addProperty("id",Long.toString(id));row.addProperty("priceMinor",unitPrice); - submitPurchase(player,new Purchase(row,quantity,UUID.randomUUID()),false); - }catch(IllegalArgumentException invalid){player.sendMessage("Use /exchange buy .");} + int page=args.length==5?Integer.parseInt(args[4]):1; + if(id<1||quantity<1||quantity>64||page<1||page>10000)throw new IllegalArgumentException(); + buyFromPage(player,Long.toString(id),quantity,unitPrice,page-1,false); + }catch(IllegalArgumentException invalid){player.sendMessage("Use /exchange buy [page].");} + }else if((args.length==3||args.length==4)&&args[0].equalsIgnoreCase("buykit")){ + try{ + String id=UUID.fromString(args[1]).toString();long unitPrice=price(args[2]); + int page=args.length==4?Integer.parseInt(args[3]):1; + if(page<1||page>100)throw new IllegalArgumentException(); + buyFromPage(player,id,1,unitPrice,page-1,true); + }catch(IllegalArgumentException invalid){player.sendMessage("Use /exchange buykit [page].");} }else if(args.length==2&&args[0].equalsIgnoreCase("retry")&&args[1].equalsIgnoreCase("buy")){ var retry=retries.get(player.getUniqueId()); - if(retry==null||retry.row.has("kitsetId"))player.sendMessage("No item purchase is waiting for a retry."); + if(retry==null)player.sendMessage("No purchase is waiting for a retry."); + else if(retry.row.has("kitsetId"))submitKitPurchase(player,retry,false); else submitPurchase(player,retry,false); - }else player.sendMessage("Use /exchange collect, list, mine, kits, buy, sell, withdraw, or retry buy."); + }else player.sendMessage("Use /exchange collect, list, mine, kits, buy, buykit, sell, withdraw, or retry buy."); + } + private void buyFromPage(Player player,String id,int quantity,long expectedPrice,int page,boolean kit){ + if(retries.containsKey(player.getUniqueId())){player.sendMessage("Use /exchange retry buy before starting another purchase.");return;} + var query=action(kit?"read_kits":"read"); + query.addProperty("page",page); + if(!kit)query.addProperty("mine",false); + request(player,query,data->{ + for(var listing:data.getAsJsonArray("listings")){ + var row=listing.getAsJsonObject(); + if(!id.equals(row.get(kit?"kitsetId":"id").getAsString()))continue; + if(row.get("priceMinor").getAsLong()!=expectedPrice){player.sendMessage("The price changed. Check the current listing before buying.");return;} + if(kit){ + if(!row.get("purchaseAvailable").getAsBoolean()){player.sendMessage("This kit is currently unavailable.");return;} + submitKitPurchase(player,new Purchase(row,1,UUID.randomUUID()),false); + }else{ + if(row.get("quantity").getAsInt(){var s=panel(p,"Exchange · Kits");button(s,1,Material.EMERALD,"Buy items","Browse loose goods",()->list(p,false,0));button(s,4,Material.NETHERITE_CHESTPLATE,"Kits","Published organisation designs",()->kits(p,0));button(s,7,Material.CHEST,"Sell","Your listings",()->list(p,true,0));int index=0;for(var value:data.getAsJsonArray("listings")){var row=value.getAsJsonObject();int slot=10+(index/6)*9+index%6;index++;String title=row.get("title").getAsString(),description=row.get("seller").getAsString()+" · "+row.get("items").getAsInt()+" items · "+money(row.get("priceMinor").getAsLong());if(row.get("purchaseAvailable").getAsBoolean())button(s,slot,Material.NETHERITE_CHESTPLATE,title,description,()->kitPurchase(p,new Purchase(row.deepCopy(),1,UUID.randomUUID())));else{s.inventory.setItem(slot,icon(Material.GRAY_DYE,title,description,"Organisation banking is unavailable"));s.description+="\n"+title+" · unavailable";}}if(page>0)button(s,45,Material.ARROW,"Previous","Previous kits",()->kits(p,page-1));button(s,49,Material.ARROW,"Back","Imperial Codex",()->back.accept(p));if(data.get("hasNext").getAsBoolean())button(s,53,Material.ARROW,"Next","More kits",()->kits(p,page+1));show(p,s);});} - private void kitPurchase(Player p,Purchase purchase){var row=purchase.row;var s=panel(p,"Exchange · Purchase details");s.description=row.get("title").getAsString()+"\nOrganisation: "+row.get("seller").getAsString()+"\n"+row.get("items").getAsInt()+" items · "+money(row.get("priceMinor").getAsLong());s.inventory.setItem(13,icon(Material.NETHERITE_CHESTPLATE,row.get("title").getAsString(),row.get("seller").getAsString(),row.get("items").getAsInt()+" items"));button(s,45,Material.ARROW,"Back","Kit catalogue",()->{if(retries.containsKey(p.getUniqueId())){p.sendMessage("Resolve the previous purchase with the same reference first.");kitPurchase(p,purchase);}else kits(p,0);});button(s,49,Material.EMERALD,(retries.containsKey(p.getUniqueId())?"Retry kit · ":"Buy kit · ")+money(row.get("priceMinor").getAsLong()),"Pay personally; proceeds go to the organisation",()->{var previous=retries.get(p.getUniqueId());if(pending.containsKey(p.getUniqueId())||previous!=null&&!previous.request.equals(purchase.request)){p.sendMessage("Resolve your pending Exchange purchase first.");return;}retries.put(p.getUniqueId(),purchase);var q=action("buy_kit");q.addProperty("requestId",purchase.request.toString());q.addProperty("kitsetId",row.get("kitsetId").getAsString());q.add("listingVersion",row.get("listingVersion"));q.add("priceMinor",row.get("priceMinor"));q.addProperty("confirmed",true);request(p,q,receipt->{retries.remove(p.getUniqueId());p.sendMessage("Kit purchased: "+money(receipt.get("totalMinor").getAsLong())+". Delivery follows automatically; make room for the complete kit.");try{bridge.refreshKits(p);}catch(Exception unavailable){p.sendMessage("Your kit remains owed until the next inventory sync.");}kits(p,0);},error->{p.sendMessage(error+" Retry this confirmation with the same reference.");kitPurchase(p,purchase);});});show(p,s);} + private void kitPurchase(Player p,Purchase purchase){var row=purchase.row;var s=panel(p,"Exchange · Purchase details");s.description=row.get("title").getAsString()+"\nOrganisation: "+row.get("seller").getAsString()+"\n"+row.get("items").getAsInt()+" items · "+money(row.get("priceMinor").getAsLong());s.inventory.setItem(13,icon(Material.NETHERITE_CHESTPLATE,row.get("title").getAsString(),row.get("seller").getAsString(),row.get("items").getAsInt()+" items"));button(s,45,Material.ARROW,"Back","Kit catalogue",()->{if(retries.containsKey(p.getUniqueId())){p.sendMessage("Resolve the previous purchase with the same reference first.");kitPurchase(p,purchase);}else kits(p,0);});button(s,49,Material.EMERALD,(retries.containsKey(p.getUniqueId())?"Retry kit · ":"Buy kit · ")+money(row.get("priceMinor").getAsLong()),"Pay personally; proceeds go to the organisation",()->submitKitPurchase(p,purchase,true));show(p,s);} + private void submitKitPurchase(Player player,Purchase purchase,boolean reopen){ + UUID playerId=player.getUniqueId(); + var previous=retries.get(playerId); + if(pending.containsKey(playerId)||previous!=null&&!previous.request.equals(purchase.request)){ + player.sendMessage("Resolve your pending Exchange purchase first.");return; + } + retries.put(playerId,purchase); + var query=action("buy_kit"); + query.addProperty("requestId",purchase.request.toString()); + for(String key:List.of("kitsetId","listingVersion","priceMinor"))query.add(key,purchase.row.get(key)); + query.addProperty("confirmed",true); + request(player,query,receipt->{ + retries.remove(playerId); + player.sendMessage("Kit purchased: "+money(receipt.get("totalMinor").getAsLong())+". Make room for the complete kit."); + try{bridge.refreshKits(player);}catch(Exception unavailable){player.sendMessage("Your kit remains owed until the next inventory sync.");} + if(reopen)kits(player,0); + },error->{ + player.sendMessage(error); + if(retries.containsKey(playerId))player.sendMessage("Use /exchange retry buy to retry the same purchase reference."); + if(reopen)kitPurchase(player,purchase); + }); + } private void withdraw(Player p,JsonObject row){var s=panel(p,"Exchange · Withdraw listing");try{s.inventory.setItem(13,bridge.exchangeIcon(row));}catch(Exception bad){p.sendMessage("This stored item cannot be decoded.");return;}button(s,45,Material.ARROW,"Back","Your listings",()->list(p,true,0));button(s,49,Material.CHEST,"Withdraw "+row.get("quantity").getAsInt()+" items","Return all unsold stock to your inventory",()->withdrawListing(p,Long.parseLong(row.get("id").getAsString()),true));show(p,s);} private void withdrawListing(Player player,long listing,boolean reopen){ UUID id=UUID.randomUUID();var query=action("prepare_cancel");query.addProperty("requestId",id.toString());