Buy and publish Exchange items from chat
This commit is contained in:
parent
171358f241
commit
43eeb17de6
1 changed files with 63 additions and 5 deletions
|
|
@ -41,7 +41,25 @@ final class ExchangeMenus implements Listener,AutoCloseable {
|
|||
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>].");
|
||||
}else if(args.length==3&&args[0].equalsIgnoreCase("sell")){
|
||||
try{
|
||||
int amount=Integer.parseInt(args[1]);long unitPrice=price(args[2]);
|
||||
var inventory=player.getInventory();var held=inventory.getItemInMainHand();
|
||||
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 <quantity> <price-per-item>.");}
|
||||
}else if(args.length==4&&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 <listing-id> <quantity> <price-per-item>.");}
|
||||
}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.");
|
||||
else submitPurchase(player,retry,false);
|
||||
}else player.sendMessage("Use /exchange collect, list, mine, kits, buy, sell, withdraw, or retry buy.");
|
||||
}
|
||||
private void browseText(Player player,String section,int page){
|
||||
boolean kits=section.equals("kits");
|
||||
|
|
@ -61,7 +79,7 @@ final class ExchangeMenus implements Listener,AutoCloseable {
|
|||
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 open(Player p){deliver(p,false);var retry=retries.get(p.getUniqueId());if(retry!=null){if(retry.row.has("title"))purchase(p,retry);else p.sendMessage("Use /exchange retry buy to resolve your pending purchase.");return;}list(p,false,0);}
|
||||
void openKits(Player p){deliver(p,false);kits(p,0);}
|
||||
private static String money(long value){return BigDecimal.valueOf(value,2).toPlainString()+" DM";}
|
||||
static long price(String text){return ExchangePrice.parse(text);}
|
||||
|
|
@ -80,7 +98,7 @@ final class ExchangeMenus implements Listener,AutoCloseable {
|
|||
if(page>0)button(s,45,Material.ARROW,"Previous","Page "+page,()->list(p,mine,page-1));if(data.get("hasNext").getAsBoolean())button(s,53,Material.ARROW,"Next","More listings",()->list(p,mine,page+1));button(s,48,Material.BOOK,"Imperial Codex","Return to your services",()->back.accept(p));button(s,49,Material.HOPPER,"Add held item","List an item from your main hand",()->draft(p));button(s,50,Material.CHEST_MINECART,"Collect purchases","Deliver paid items waiting for inventory space",()->{deliver(p,true);list(p,mine,page);});show(p,s);
|
||||
});}
|
||||
private void draft(Player p){var item=p.getInventory().getItemInMainHand();if(item.isEmpty()){p.sendMessage("Hold the item you want to sell first.");return;}sell(p,new Draft(item.clone(),p.getInventory().getHeldItemSlot(),Math.min(64,item.getAmount()),100));}
|
||||
private void sell(Player p,Draft draft){var s=panel(p,"Exchange · Add item");s.description=draft.item.getType().name()+" · "+draft.amount+" items\n"+money(draft.price)+" per item\nTotal: "+money(draft.price*draft.amount);s.inventory.setItem(13,draft.item.clone());quantityPresets(s,Math.min(64,draft.item.getAmount()),amount->sell(p,draft.quantity(amount)));button(s,30,Material.RED_DYE,"− Quantity","Sell "+draft.amount+" items",()->sell(p,draft.quantity(Math.max(1,draft.amount-1))));button(s,31,Material.PAPER,"Quantity: "+draft.amount,"Maximum "+Math.min(64,draft.item.getAmount()),()->sell(p,draft));button(s,32,Material.LIME_DYE,"+ Quantity","Add one item",()->sell(p,draft.quantity(Math.min(Math.min(64,draft.item.getAmount()),draft.amount+1))));button(s,40,Material.GOLD_INGOT,"Price: "+money(draft.price),"Price per item · total "+money(draft.price*draft.amount),()->editPrice(p,draft));button(s,45,Material.ARROW,"Back","Return to your listings",()->list(p,true,0));button(s,49,Material.EMERALD,"Publish listing","Move "+draft.amount+" items into the Exchange",()->{UUID id=UUID.randomUUID();var q=action("prepare_sale");q.addProperty("requestId",id.toString());q.addProperty("amount",draft.amount);q.addProperty("priceMinor",draft.price);request(p,q,permit->{try{bridge.exchangePublish(p,permit,id,draft.item,draft.slot,draft.amount,draft.price);p.sendMessage("Listing published. Items are held by the Exchange until sold or withdrawn.");list(p,true,0);}catch(Exception e){p.sendMessage(e.getMessage()==null?"The listing was not confirmed.":e.getMessage());}});});show(p,s);}
|
||||
private void sell(Player p,Draft draft){var s=panel(p,"Exchange · Add item");s.description=draft.item.getType().name()+" · "+draft.amount+" items\n"+money(draft.price)+" per item\nTotal: "+money(draft.price*draft.amount);s.inventory.setItem(13,draft.item.clone());quantityPresets(s,Math.min(64,draft.item.getAmount()),amount->sell(p,draft.quantity(amount)));button(s,30,Material.RED_DYE,"− Quantity","Sell "+draft.amount+" items",()->sell(p,draft.quantity(Math.max(1,draft.amount-1))));button(s,31,Material.PAPER,"Quantity: "+draft.amount,"Maximum "+Math.min(64,draft.item.getAmount()),()->sell(p,draft));button(s,32,Material.LIME_DYE,"+ Quantity","Add one item",()->sell(p,draft.quantity(Math.min(Math.min(64,draft.item.getAmount()),draft.amount+1))));button(s,40,Material.GOLD_INGOT,"Price: "+money(draft.price),"Price per item · total "+money(draft.price*draft.amount),()->editPrice(p,draft));button(s,45,Material.ARROW,"Back","Return to your listings",()->list(p,true,0));button(s,49,Material.EMERALD,"Publish listing","Move "+draft.amount+" items into the Exchange",()->publish(p,draft,true));show(p,s);}
|
||||
private void editPrice(Player p,Draft draft){
|
||||
var s=panel(p,"Exchange · Price");s.description=money(draft.price)+" per item · "+draft.amount+" items\nTotal: "+money(draft.price*draft.amount);s.inventory.setItem(13,icon(Material.GOLD_INGOT,"Price: "+money(draft.price),"Per item · total "+money(draft.price*draft.amount)));
|
||||
long[] steps={1,100,1000,10000};for(int i=0;i<steps.length;i++){long step=steps[i];button(s,27+i,Material.RED_DYE,"− "+money(step),"Decrease the price per item",()->editPrice(p,draft.price(Math.max(1,draft.price-step))));button(s,32+i,Material.LIME_DYE,"+ "+money(step),"Increase the price per item",()->editPrice(p,draft.price(Math.min(100000000,draft.price+step))));}
|
||||
|
|
@ -104,9 +122,49 @@ final class ExchangeMenus implements Listener,AutoCloseable {
|
|||
}).buildConversation(p);
|
||||
priceInputs.put(owner,conversation);conversation.begin();
|
||||
}
|
||||
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 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",()->submitPurchase(p,purchase,true));show(p,s);}
|
||||
private void publish(Player player,Draft draft,boolean reopen){
|
||||
UUID id=UUID.randomUUID();
|
||||
var query=action("prepare_sale");
|
||||
query.addProperty("requestId",id.toString());
|
||||
query.addProperty("amount",draft.amount);
|
||||
query.addProperty("priceMinor",draft.price);
|
||||
request(player,query,permit->{
|
||||
try{
|
||||
bridge.exchangePublish(player,permit,id,draft.item,draft.slot,draft.amount,draft.price);
|
||||
player.sendMessage("Listing published. Items are held by the Exchange until sold or withdrawn.");
|
||||
if(reopen)list(player,true,0);
|
||||
}catch(Exception failure){player.sendMessage(failure.getMessage()==null?"The listing could not be published.":failure.getMessage());}
|
||||
});
|
||||
}
|
||||
private void submitPurchase(Player player,Purchase purchase,boolean reopen){
|
||||
UUID playerId=player.getUniqueId();
|
||||
if(pending.containsKey(playerId)){player.sendMessage("J.A.R.V.I.S. is processing your Exchange action.");return;}
|
||||
Purchase previous=retries.get(playerId);
|
||||
if(previous!=null&&!previous.request.equals(purchase.request)){
|
||||
player.sendMessage("Resolve your pending Exchange purchase before starting another.");return;
|
||||
}
|
||||
retries.put(playerId,purchase);
|
||||
var query=action("buy");
|
||||
query.addProperty("requestId",purchase.request.toString());
|
||||
query.addProperty("listingId",purchase.row.get("id").getAsString());
|
||||
query.addProperty("amount",purchase.amount);
|
||||
query.add("priceMinor",purchase.row.get("priceMinor"));
|
||||
query.addProperty("confirmed",true);
|
||||
request(player,query,receipt->{
|
||||
retries.remove(playerId);
|
||||
player.sendMessage("Purchase paid: "+money(receipt.get("totalMinor").getAsLong())+". Your items are ready for delivery.");
|
||||
if(receipt.has("rebateMinor")&&receipt.get("rebateMinor").getAsLong()>0)player.sendMessage("Ordos trade incentive credited: "+money(receipt.get("rebateMinor").getAsLong())+".");
|
||||
deliver(player,true);
|
||||
if(reopen)list(player,false,0);
|
||||
},error->{
|
||||
player.sendMessage(error);
|
||||
if(retries.containsKey(playerId))player.sendMessage("Use /exchange retry buy to retry the same purchase reference.");
|
||||
if(reopen)purchase(player,purchase);
|
||||
});
|
||||
}
|
||||
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 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 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());
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff