Offer native Exchange checkout with the original purchase actions

This commit is contained in:
unimatrixgit 2026-11-28 10:04:10 +01:00
commit a7448814de
2 changed files with 95 additions and 8 deletions

View file

@ -19,7 +19,7 @@ public interface DecisionsViewTransport {
}
}
enum Style { AUTO, ORGANISATION, EXCHANGE, HOUSE }
enum Style { AUTO, ORGANISATION, EXCHANGE, EXCHANGE_CHECKOUT, HOUSE }
record Identity(String logo, String mentat, int portrait) {}
record Document(String title, String description, List<Card> cards, Style style, Identity identity) {

View file

@ -117,7 +117,18 @@ final class ExchangeMenus implements Listener,AutoCloseable {
private static ItemStack icon(Material material,String title,String... lore){var item=new ItemStack(material);var meta=item.getItemMeta();meta.displayName(Component.text(title,NamedTextColor.GOLD));meta.lore(Arrays.stream(lore).map(s->Component.text(s,NamedTextColor.GRAY)).toList());item.setItemMeta(meta);return item;}
private Panel panel(Player p,String title){int screen=title.endsWith("Your listings")?1:title.endsWith("Add item")?2:title.endsWith("Purchase details")?3:title.endsWith("Withdraw listing")?4:0;var s=new Panel(p,title,artwork.test(p)&&!BedrockForms.isBedrock(p),screen);if(!s.artwork){var pane=icon(Material.GREEN_STAINED_GLASS_PANE," ");for(int i=0;i<54;i++)s.inventory.setItem(i,pane);}return s;}
private void button(Panel s,int slot,Material material,String title,String description,Runnable action){s.inventory.setItem(slot,icon(material,title,description));s.actions.put(slot,action);s.choices.add(new BedrockForms.Choice(title,description,action));}
private void show(Player p,Panel s){if(BedrockForms.isBedrock(p)){s.gate.expire();BedrockForms.show(plugin,p,s.title,s.description,s.choices);}else{if(Set.of("Exchange · Buy","Exchange · Your listings","Exchange · Kits").contains(s.title)&&NativeMenus.show(plugin,p,"development.fullscreen-exchange-viewers",s.title,s.description,s.inventory,s.actions,s.gate,DecisionsViewTransport.Style.EXCHANGE,null))return;if(MCGUIMenus.show(p,s.title,s.description,s.inventory,s.actions,s.gate))return;p.openInventory(s.inventory);Bukkit.getScheduler().runTaskLater(plugin,s.gate::expire,2400L);}}
private void show(Player p,Panel s){
if(BedrockForms.isBedrock(p)){s.gate.expire();BedrockForms.show(plugin,p,s.title,s.description,s.choices);return;}
var style=switch(s.title){
case "Exchange · Buy","Exchange · Your listings","Exchange · Kits" -> DecisionsViewTransport.Style.EXCHANGE;
case "Exchange · Purchase details" -> DecisionsViewTransport.Style.EXCHANGE_CHECKOUT;
default -> null;
};
if(style!=null&&NativeMenus.show(plugin,p,"development.fullscreen-exchange-viewers",s.title,s.description,s.inventory,s.actions,s.gate,style,null))return;
if(MCGUIMenus.show(p,s.title,s.description,s.inventory,s.actions,s.gate))return;
p.openInventory(s.inventory);
Bukkit.getScheduler().runTaskLater(plugin,s.gate::expire,2400L);
}
private void request(Player p,JsonObject input,Consumer<JsonObject> success,Consumer<String> failure){
UUID id=p.getUniqueId(),world=p.getWorld().getUID();if(pending.containsKey(id)){p.sendMessage("J.A.R.V.I.S. is processing your Exchange action.");return;}UUID token=UUID.randomUUID();pending.put(id,token);p.closeInventory();
Bukkit.getScheduler().runTaskAsynchronously(plugin,()->{JsonObject result=null;String error=null;try{result=bridge.exchange(id,input);}catch(Exception e){error=e.getMessage();}var response=result;String message=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);});});