for(varelement:listingPage.document.select(".market-tool,[data-slot='1013']")){varbounds=listingFrame.boundsOf(element);varat=listingFrame.elementAt(bounds.getCenterX(),bounds.getCenterY());if(at==null||!at.closest("[data-slot]").attr("data-slot").equals(element.attr("data-slot")))thrownewAssertionError("Detail control hitbox");}
System.out.println("PASS: "+checked+" direct action hitboxes across15 menu families.");
finalUUIDowner,world;finalGuiSessiongate;finalMap<Integer,Runnable>actions=newHashMap<>();finalList<BedrockForms.Choice>choices=newArrayList<>();finalInventoryinventory;finalStringtitle;finalbooleanartwork;Stringdescription="Central bank · J.A.R.V.I.S. Exchange";
UUIDid=p.getUniqueId(),world=p.getWorld().getUID();if(pending.containsKey(id)){p.sendMessage("J.A.R.V.I.S. is processing your Exchange action.");return;}UUIDtoken=UUID.randomUUID();pending.put(id,token);p.closeInventory();
Bukkit.getScheduler().runTaskAsynchronously(plugin,()->{JsonObjectresult=null;Stringerror=null;try{result=bridge.exchange(id,input);}catch(Exceptione){error=e.getMessage();}varresponse=result;Stringmessage=error;if(!plugin.isEnabled())return;Bukkit.getScheduler().runTask(plugin,()->{if(!token.equals(pending.get(id)))return;pending.remove(id);if(!p.isOnline()||!world.equals(p.getWorld().getUID()))return;if(response==null){failure.accept(message==null?"The Exchange is temporarily unavailable.":message);return;}if(response.has("rejectedStatus")){if(Set.of("buy","buy_kit").contains(input.get("action").getAsString()))retries.remove(id);failure.accept(response.has("message")?response.get("message").getAsString():"The Exchange rejected this action.");return;}success.accept(response);});});
Bukkit.getScheduler().runTaskAsynchronously(plugin,()->{JsonObjectresult=null;Stringerror=null;try{result=bridge.exchange(id,input);}catch(Exceptione){error=e.getMessage();}varresponse=result;Stringmessage=error;if(!plugin.isEnabled())return;Bukkit.getScheduler().runTask(plugin,()->{if(!token.equals(pending.get(id)))return;pending.remove(id);if(!p.isOnline()||!world.equals(p.getWorld().getUID()))return;if(response==null){failure.accept(message==null?"The Exchange is temporarily unavailable.":message);return;}if(response.has("rejectedStatus")){Stringoperation=input.get("action").getAsString();intstatus=response.get("rejectedStatus").getAsInt();if(Set.of("buy","buy_kit").contains(operation)||"buy_resell".equals(operation)&&Set.of(400,403,409).contains(status))retries.remove(id);failure.accept(response.has("message")?response.get("message").getAsString():"The Exchange rejected this action.");return;}success.accept(response);});});
privatevoidlist(Playerp,booleanmine,intpage){varq=action("read");q.addProperty("mine",mine);q.addProperty("page",page);request(p,q,data->{vars=panel(p,mine?"Exchange · Your listings":"Exchange · Buy");button(s,1,Material.EMERALD,"Buy","Browse all available items",()->list(p,false,0));button(s,4,Material.NETHERITE_CHESTPLATE,"Kits","Published organisation stillsuit kits",()->kits(p,0));button(s,7,Material.CHEST,"My listings","Manage your existing listings",()->list(p,true,0));intindex=0;for(varvalue:data.getAsJsonArray("listings")){varrow=value.getAsJsonObject();intslot=10+(index/6)*9+index%6;index++;try{varitem=bridge.exchangeIcon(row);varmeta=item.getItemMeta();Stringtitle=row.get("title").getAsString();booleanown=row.get("sellerUuid").getAsString().equalsIgnoreCase(p.getUniqueId().toString());meta.displayName(listingName(meta.displayName(),title,own));List<Component>lore=newArrayList<>(meta.hasLore()?meta.lore():List.of());lore.add(Component.text(""));lore.add(Component.text(own?"Your listing · Select to withdraw":"Seller: "+row.get("seller").getAsString(),NamedTextColor.GRAY));lore.add(Component.text(row.get("quantity").getAsInt()+" available · "+money(row.get("priceMinor").getAsLong())+" each",NamedTextColor.GOLD));meta.lore(lore);item.setItemMeta(meta);s.inventory.setItem(slot,item);Runnablepick=()->{if(row.get("sellerUuid").getAsString().equals(p.getUniqueId().toString()))withdraw(p,row);elsepurchase(p,newPurchase(row,1,UUID.randomUUID()));};s.actions.put(slot,pick);s.choices.add(newBedrockForms.Choice(own?"Yours · "+title:title,money(row.get("priceMinor").getAsLong())+" · "+row.get("quantity").getAsInt()+" available",pick));}catch(Exceptionunsupported){Stringtitle=row.get("title").getAsString();Stringdetails=money(row.get("priceMinor").getAsLong())+" each · "+row.get("quantity").getAsInt()+" available";s.inventory.setItem(slot,icon(Material.BARRIER,title,details,"Item preview unavailable."));s.description+="\n"+title+" · "+details+" · Item preview unavailable.";}}
if(index==0)s.inventory.setItem(22,icon(Material.PAPER,"No listings",mine?"No own listings. Hold an item and choose Sell item.":"Check again after players publish goods."));
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,"Sell item","List the item in 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);});resumeButton(p,s);show(p,s);
order==current?"Current order":"Apply to all listings",()->{
listingSorts.put(player.getUniqueId(),order);
list(player,mine,0);
});
}
button(panel,45,Material.ARROW,"Back","Keep the current order",()->list(player,mine,page));
show(player,panel);
}
privatevoidlist(Playerp,booleanmine,intpage){varorder=listingSorts.getOrDefault(p.getUniqueId(),ListingSort.NEWEST);varcategory=listingCategories.getOrDefault(p.getUniqueId(),ListingCategory.ALL);varq=action("read");q.addProperty("mine",mine);q.addProperty("page",page);q.addProperty("sort",order.token);q.addProperty("category",category.token);request(p,q,data->{vars=panel(p,mine?"Exchange · Your listings":"Exchange · Buy");button(s,1,Material.EMERALD,"Buy","Browse all available items",()->list(p,false,0));button(s,4,Material.NETHERITE_CHESTPLATE,"Kits","Published organisation stillsuit kits",()->kits(p,0));button(s,7,Material.CHEST,"My listings","Manage your existing listings",()->list(p,true,0));intindex=0;for(varvalue:data.getAsJsonArray("listings")){varrow=value.getAsJsonObject();intslot=10+(index/6)*9+index%6;index++;try{varitem=bridge.exchangeIcon(row);varmeta=item.getItemMeta();Stringtitle=row.get("title").getAsString();booleanown=row.get("sellerUuid").getAsString().equalsIgnoreCase(p.getUniqueId().toString());meta.displayName(listingName(meta.displayName(),title,own));List<Component>lore=newArrayList<>(meta.hasLore()?meta.lore():List.of());lore.add(Component.text(""));lore.add(Component.text(own?"Your listing · Select to withdraw":"Seller: "+row.get("seller").getAsString(),NamedTextColor.GRAY));lore.add(Component.text(row.get("quantity").getAsInt()+" available · "+money(row.get("priceMinor").getAsLong())+" each",NamedTextColor.GOLD));meta.lore(lore);item.setItemMeta(meta);s.inventory.setItem(slot,item);Runnablepick=()->{if(row.get("sellerUuid").getAsString().equals(p.getUniqueId().toString()))withdraw(p,row);elsepurchase(p,newPurchase(row,1,UUID.randomUUID()));};s.actions.put(slot,pick);s.choices.add(newBedrockForms.Choice(own?"Yours · "+title:title,money(row.get("priceMinor").getAsLong())+" · "+row.get("quantity").getAsInt()+" available",pick));}catch(Exceptionunsupported){Stringtitle=row.get("title").getAsString();Stringdetails=money(row.get("priceMinor").getAsLong())+" each · "+row.get("quantity").getAsInt()+" available";s.inventory.setItem(slot,icon(Material.BARRIER,title,details,"Item preview unavailable."));s.description+="\n"+title+" · "+details+" · Item preview unavailable.";}}
if(index==0)s.inventory.setItem(22,icon(Material.PAPER,"No listings",category!=ListingCategory.ALL?"No listings in "+category.label+".":mine?"No own listings. Hold an item and choose Sell item.":"Check again after players publish goods."));
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,52,Material.BOOKSHELF,"Categories",category.label,()->filterListings(p,mine,page));button(s,47,Material.COMPASS,"Sort listings",order.label,()->sortListings(p,mine,page));button(s,48,Material.BOOK,"Imperial Codex","Return to your services",()->back.accept(p));button(s,49,Material.HOPPER,"Sell item","List the item in 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);});resumeButton(p,s);show(p,s);
});}
privatevoiddraft(Playerp){varitem=p.getInventory().getItemInMainHand();if(item.isEmpty()){p.sendMessage("Hold the item you want to sell first.");return;}sell(p,newDraft(item.clone(),p.getInventory().getHeldItemSlot(),Math.min(64,item.getAmount()),100));}
privatevoidsell(Playerp,Draftdraft){vars=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,"Confirm listing","Move "+draft.amount+" items into the Exchange",()->{UUIDid=UUID.randomUUID();varq=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(Exceptione){p.sendMessage(e.getMessage()==null?"The listing was not confirmed.":e.getMessage());}});});show(p,s);}
@ -58,7 +109,19 @@ final class ExchangeMenus implements Listener,AutoCloseable {
}
privatevoidquantityPresets(Panels,intavailable,IntConsumerchoose){intslot=21;for(intamount:ExchangeQuantity.presets(available))button(s,slot++,Material.CHEST,String.valueOf(amount),"Set quantity · maximum "+available,()->choose.accept(amount));}
privatevoidtypePrice(Playerp,Draftdraft){
if(BedrockForms.isBedrock(p)){BedrockForms.input(plugin,p,"Listing price","Dunemarks per item",BigDecimal.valueOf(draft.price,2).toPlainString(),text->{try{sell(p,draft.price(price(text.trim())));}catch(IllegalArgumentExceptione){p.sendMessage(e.getMessage());sell(p,draft);}});return;}
Bukkit.getScheduler().runTask(plugin,()->{if(!p.isOnline()||!world.equals(p.getWorld().getUID()))return;if(event.gracefulExit()&&valueinstanceofLongamount){sell(p,draft.price(amount));}else{p.sendMessage("Price entry cancelled. Your listing has not been published.");sell(p,draft);}});
privatevoidresumeButton(Playerp,Panelpanel){varretry=retries.get(p.getUniqueId());if(retry!=null)button(panel,46,Material.CLOCK,"Resume pending purchase","Retry the original purchase reference; no new order",()->purchase(p,retry));}
privatevoidpurchase(Playerp,Purchasepurchase){varexisting=resume(purchase,retries.get(p.getUniqueId()));if(existing!=purchase){purchase(p,existing);return;}if(purchase.row.has("kitsetId")){kitPurchase(p,purchase);return;}varrow=purchase.row;vars=panel(p,"Exchange · Confirm purchase");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(Exceptionbad){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",()->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);varq=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 this confirmation with the same purchase reference.");purchase(p,purchase);});});show(p,s);}
privatevoidpurchase(Playerp,Purchasepurchase){varexisting=resume(purchase,retries.get(p.getUniqueId()));if(existing!=purchase){purchase(p,existing);return;}if(purchase.resalePrice>0){confirmResale(p,purchase);return;}if(purchase.row.has("kitsetId")){kitPurchase(p,purchase);return;}varrow=purchase.row;vars=panel(p,"Exchange · Confirm purchase");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(Exceptionbad){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",()->list(p,false,0));if(!retries.containsKey(p.getUniqueId())&&row.get("priceMinor").getAsLong()<100000000)button(s,53,Material.GOLD_INGOT,"Buy and relist","Enter a higher unit price before confirming",()->enterResalePrice(p,purchase));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);varq=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 this confirmation with the same purchase reference.");purchase(p,purchase);});});show(p,s);}
privatevoidkitPurchase(Playerp,Purchasepurchase){varexisting=resume(purchase,retries.get(p.getUniqueId()));if(existing!=purchase){purchase(p,existing);return;}varrow=purchase.row;vars=panel(p,"Exchange · Confirm purchase");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",()->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);varq=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(Exceptionunavailable){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);}
privatevoidwithdraw(Playerp,JsonObjectrow){vars=panel(p,"Exchange · Withdraw listing");try{s.inventory.setItem(13,bridge.exchangeIcon(row));}catch(Exceptionbad){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",()->{UUIDid=UUID.randomUUID();varq=action("prepare_cancel");q.addProperty("requestId",id.toString());request(p,q,permit->{try{intquantity=bridge.exchangeCancel(p,permit,id,Long.parseLong(row.get("id").getAsString()));p.sendMessage("Listing withdrawn. Returned "+quantity+" items.");list(p,true,0);}catch(Exceptione){p.sendMessage(e.getMessage()==null?"The listing was not withdrawn.":e.getMessage());}});});show(p,s);}
@ -85,6 +216,6 @@ final class ExchangeMenus implements Listener,AutoCloseable {