forked from unimatrix/web
Browse and withdraw Exchange listings from chat
This commit is contained in:
parent
618c611d82
commit
171358f241
1 changed files with 41 additions and 2 deletions
|
|
@ -30,7 +30,36 @@ final class ExchangeMenus implements Listener,AutoCloseable {
|
|||
void command(Player player,String[] args){
|
||||
if(args.length==0)open(player);
|
||||
else if(args.length==1&&args[0].equalsIgnoreCase("collect"))deliver(player,true);
|
||||
else player.sendMessage("Use /exchange [collect].");
|
||||
else if(args.length<=2&&Set.of("list","mine","kits").contains(args[0])){
|
||||
int page;
|
||||
try{page=args.length==1?1:Integer.parseInt(args[1]);}
|
||||
catch(NumberFormatException invalid){page=0;}
|
||||
if(page<1){player.sendMessage("Enter a positive page number.");return;}
|
||||
browseText(player,args[0],page-1);
|
||||
}else if(args.length==2&&args[0].equals("withdraw")){
|
||||
long listing;
|
||||
try{listing=Long.parseLong(args[1]);}catch(NumberFormatException invalid){listing=0;}
|
||||
if(listing<1){player.sendMessage("Use /exchange withdraw <listing-id>.");return;}
|
||||
withdrawListing(player,listing,false);
|
||||
}else player.sendMessage("Use /exchange [collect|list [page]|mine [page]|kits [page]|withdraw <listing-id>].");
|
||||
}
|
||||
private void browseText(Player player,String section,int page){
|
||||
boolean kits=section.equals("kits");
|
||||
var query=action(kits?"read_kits":"read");
|
||||
query.addProperty("page",page);
|
||||
if(!kits)query.addProperty("mine",section.equals("mine"));
|
||||
request(player,query,data->{
|
||||
var listings=data.getAsJsonArray("listings");
|
||||
if(listings.isEmpty()){player.sendMessage("No Exchange listings on this page.");return;}
|
||||
for(var value:listings){
|
||||
var row=value.getAsJsonObject();
|
||||
String id=row.get(kits?"kitsetId":"id").getAsString();
|
||||
String quantity=row.get(kits?"items":"quantity").getAsString();
|
||||
player.sendMessage("#"+id+" · "+row.get("title").getAsString()+" · "+money(row.get("priceMinor").getAsLong())+
|
||||
(kits?" per kit · "+quantity+" items":" each · "+quantity+" available"));
|
||||
}
|
||||
if(data.get("hasNext").getAsBoolean())player.sendMessage("Next page: /exchange "+section+" "+(page+2));
|
||||
});
|
||||
}
|
||||
void open(Player p){deliver(p,false);var retry=retries.get(p.getUniqueId());if(retry!=null){purchase(p,retry);return;}list(p,false,0);}
|
||||
void openKits(Player p){deliver(p,false);kits(p,0);}
|
||||
|
|
@ -78,7 +107,17 @@ final class ExchangeMenus implements Listener,AutoCloseable {
|
|||
private void purchase(Player p,Purchase purchase){if(purchase.row.has("kitsetId")){kitPurchase(p,purchase);return;}var row=purchase.row;var s=panel(p,"Exchange · Purchase details");s.description=row.get("title").getAsString()+"\nSeller: "+row.get("seller").getAsString()+"\nQuantity: "+purchase.amount+" · Total: "+money(row.get("priceMinor").getAsLong()*purchase.amount);try{s.inventory.setItem(13,bridge.exchangeIcon(row));}catch(Exception bad){p.sendMessage("This item cannot be delivered by your current version.");return;}quantityPresets(s,Math.min(64,row.get("quantity").getAsInt()),amount->purchase(p,retries.containsKey(p.getUniqueId())?purchase:purchase.quantity(amount)));button(s,30,Material.RED_DYE,"− Quantity","Buy "+purchase.amount,()->purchase(p,retries.containsKey(p.getUniqueId())?purchase:purchase.quantity(Math.max(1,purchase.amount-1))));button(s,31,Material.PAPER,"Quantity: "+purchase.amount,money(row.get("priceMinor").getAsLong())+" per item",()->purchase(p,purchase));button(s,32,Material.LIME_DYE,"+ Quantity","Maximum "+Math.min(64,row.get("quantity").getAsInt()),()->purchase(p,retries.containsKey(p.getUniqueId())?purchase:purchase.quantity(Math.min(Math.min(64,row.get("quantity").getAsInt()),purchase.amount+1))));button(s,45,Material.ARROW,"Back","Return to the Exchange",()->{if(retries.containsKey(p.getUniqueId())){p.sendMessage("Retry the previous purchase first.");purchase(p,purchase);}else list(p,false,0);});button(s,49,Material.EMERALD,(retries.containsKey(p.getUniqueId())?"Retry purchase · ":"Buy · ")+money(row.get("priceMinor").getAsLong()*purchase.amount),"Pay from your central personal bank account",()->{retries.put(p.getUniqueId(),purchase);var q=action("buy");q.addProperty("requestId",purchase.request.toString());q.addProperty("listingId",row.get("id").getAsString());q.addProperty("amount",purchase.amount);q.addProperty("priceMinor",row.get("priceMinor").getAsLong());q.addProperty("confirmed",true);request(p,q,receipt->{retries.remove(p.getUniqueId());p.sendMessage("Purchase confirmed: "+money(receipt.get("totalMinor").getAsLong())+". Your items are ready for delivery.");if(receipt.has("rebateMinor")&&receipt.get("rebateMinor").getAsLong()>0)p.sendMessage("Ordos trade incentive credited: "+money(receipt.get("rebateMinor").getAsLong())+".");deliver(p,true);list(p,false,0);},error->{p.sendMessage(error+" Retry with the same purchase reference.");purchase(p,purchase);});});show(p,s);}
|
||||
private void kits(Player p,int page){var q=action("read_kits");q.addProperty("page",page);request(p,q,data->{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",()->{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 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",()->{UUID id=UUID.randomUUID();var q=action("prepare_cancel");q.addProperty("requestId",id.toString());request(p,q,permit->{try{int quantity=bridge.exchangeCancel(p,permit,id,Long.parseLong(row.get("id").getAsString()));p.sendMessage("Listing withdrawn. Returned "+quantity+" items.");list(p,true,0);}catch(Exception e){p.sendMessage(e.getMessage()==null?"The listing was not withdrawn.":e.getMessage());}});});show(p,s);}
|
||||
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());
|
||||
request(player,query,permit->{
|
||||
try{
|
||||
int quantity=bridge.exchangeCancel(player,permit,id,listing);
|
||||
player.sendMessage("Listing withdrawn. Returned "+quantity+" items.");
|
||||
if(reopen)list(player,true,0);
|
||||
}catch(Exception failure){player.sendMessage(failure.getMessage()==null?"The listing could not be withdrawn.":failure.getMessage());}
|
||||
});
|
||||
}
|
||||
private void deliver(Player p,boolean explain){if(!p.isOnline()||!delivering.add(p.getUniqueId()))return;UUID id=p.getUniqueId();var q=action("pending");Bukkit.getScheduler().runTaskAsynchronously(plugin,()->{JsonObject response=null;try{response=bridge.exchange(id,q);}catch(Exception ignored){}var data=response;if(!plugin.isEnabled())return;Bukkit.getScheduler().runTask(plugin,()->{delivering.remove(id);if(!p.isOnline())return;if(data==null||!data.has("pending")){if(explain)p.sendMessage("Paid deliveries remain queued. Reopen the Exchange to try again.");return;}if(data.getAsJsonArray("pending").isEmpty()){if(explain)p.sendMessage("No paid items are waiting for delivery.");return;}try{int count=bridge.exchangeDeliver(p);if(explain||count>0)p.sendMessage(count>0?"J.A.R.V.I.S. delivered "+count+" purchased items.":"Make inventory space. Your paid items remain queued.");}catch(Exception unavailable){if(explain)p.sendMessage("Paid items remain queued until your shared inventory is ready.");}});});}
|
||||
@EventHandler public void join(PlayerJoinEvent e){Bukkit.getScheduler().runTaskLater(plugin,()->deliver(e.getPlayer(),false),60L);}
|
||||
@EventHandler(priority=EventPriority.HIGHEST) public void click(InventoryClickEvent e){var holder=e.getView().getTopInventory().getHolder();if(!(holder instanceof Panel s))return;e.setCancelled(true);if(!(e.getWhoClicked() instanceof Player p)||!Set.of(ClickType.LEFT,ClickType.RIGHT).contains(e.getClick())||!s.owner.equals(p.getUniqueId())||!s.world.equals(p.getWorld().getUID())||!s.actions.containsKey(e.getRawSlot())||!s.gate.claim(p.getUniqueId()))return;Bukkit.getScheduler().runTask(plugin,s.actions.get(e.getRawSlot()));}
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff