From da299712000fb6989ec2ed6409017aeac7da8a16 Mon Sep 17 00:00:00 2001 From: Dev Date: Thu, 26 Nov 2026 00:22:09 +0100 Subject: [PATCH] Use authoritative sell-all transaction for Spice GUI without redundant read gates --- .../java/com/dunerealms/gui/SpiceMenus.java | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/DuneRealmsGUI/src/main/java/com/dunerealms/gui/SpiceMenus.java b/DuneRealmsGUI/src/main/java/com/dunerealms/gui/SpiceMenus.java index b8fc35e3..f4818344 100644 --- a/DuneRealmsGUI/src/main/java/com/dunerealms/gui/SpiceMenus.java +++ b/DuneRealmsGUI/src/main/java/com/dunerealms/gui/SpiceMenus.java @@ -29,21 +29,32 @@ final class SpiceMenus implements Listener { private static JsonObject action(String name){var q=new JsonObject();q.addProperty("action",name);return q;} private void call(Player p,JsonObject q,Consumer done){ if(!arrakis(p)){p.sendMessage("Spice buyers are available on Arrakis only.");return;}if(!busy.add(p.getUniqueId())){p.sendMessage("Your spice sale is being processed.");return;} - Bukkit.getScheduler().runTaskAsynchronously(plugin,()->{JsonObject data=null;String error=null;try{data=bridge.spice(p.getUniqueId(),q);}catch(Exception e){error=e.getMessage();}var result=data;var message=error;if(!plugin.isEnabled())return;Bukkit.getScheduler().runTask(plugin,()->{busy.remove(p.getUniqueId());if(!p.isOnline())return;if(result==null||result.has("rejectedStatus")){p.sendMessage(result!=null&&result.has("message")?result.get("message").getAsString():message==null?"Spice banking unavailable; committed sales remain payable.":message);return;}done.accept(result);});}); + Bukkit.getScheduler().runTaskAsynchronously(plugin,()->{JsonObject data=null;String error=null;try{data=bridge.spice(p.getUniqueId(),q);}catch(Exception e){error=e.getMessage();}var result=data;var message=error;if(!plugin.isEnabled())return;Bukkit.getScheduler().runTask(plugin,()->{busy.remove(p.getUniqueId());if(!p.isOnline())return;if(result==null||result.has("rejectedStatus")){p.sendMessage(result!=null&&result.has("message")?result.get("message").getAsString():message==null?"Spice service unavailable. No unconfirmed inventory sale is made; committed sales remain payable.":message);return;}done.accept(result);});}); } void open(Player p){open(p,null);} - void open(Player p,String selected){call(p,action("read"),data->{if(!arrakis(p))return;var panel=new Panel(p);var policy=data.getAsJsonObject("policy");boolean smuggler=data.get("smugglerEnabled").getAsBoolean();int count;try{count=bridge.spiceCount(p,"guild");}catch(Exception e){p.sendMessage("Spice inventory unavailable.");return;} - put(panel,4,Material.ORANGE_DYE,"Melange Spice",count+" raw-spice units (1 block = 9)\nP&O pays your personal bank account",null,null); - put(panel,11,Material.PLAYER_HEAD,"Spacing Guild · Navigator",money(policy.get("guild").getAsLong())+" per spice before Trust levy\nGuild Navigator\nReview your sale",()->review(p,"guild"),"spice_guild"); - put(panel,15,smuggler?Material.PLAYER_HEAD:Material.BARRIER,"Smuggler · Esmar Tuek",money(policy.get("smuggler").getAsLong())+" per spice before Trust levy\n"+(smuggler?"Contraband spice only · Smuggler quest verified":"Locked · start a Smuggler quest"),smuggler?()->review(p,"smuggler"):null,"spice_smuggler"); + void open(Player p,String selected){ + if(!arrakis(p)){p.sendMessage("Spice buyers are available on Arrakis only.");return;} + var panel=new Panel(p);int guildCount; + try{guildCount=bridge.spiceCount(p,"guild");}catch(Exception e){p.sendMessage("Spice inventory unavailable.");return;} + put(panel,4,Material.ORANGE_DYE,"Melange Spice",guildCount+" raw-spice units (1 block = 9)\nP&O pays your personal bank account",null,null); + put(panel,11,Material.PLAYER_HEAD,"Spacing Guild · Navigator","Sell eligible guild spice at the current authorised rate\nReview your sale",()->review(p,"guild"),"spice_guild"); + put(panel,15,Material.PLAYER_HEAD,"Smuggler · Esmar Tuek","Contraband spice only\nAn active Smuggler quest is required and checked at sale",()->review(p,"smuggler"),"spice_smuggler"); put(panel,26,Material.GOLD_INGOT,"Collect pending payments","Retry committed P&O spice payments",()->recover(p),null); put(panel,22,Material.ARROW,"Back","Imperial Codex",()->back.accept(p),null);show(p,panel); - });} - private void review(Player p,String channel){call(p,action("read"),data->{if(!arrakis(p))return;if(channel.equals("smuggler")&&!data.get("smugglerEnabled").getAsBoolean()){p.sendMessage("Start a Smuggler quest first.");return;}int count;try{count=bridge.spiceCount(p,channel);}catch(Exception e){p.sendMessage("Spice inventory unavailable.");return;}if(count==0){p.sendMessage("You have no matching spice for this buyer.");return;}long gross=count*data.getAsJsonObject("policy").get(channel).getAsLong(),tax=Math.round(gross*data.getAsJsonObject("policy").get("levyBasisPoints").getAsInt()/10000.0);var panel=new Panel(p); - put(panel,13,Material.ORANGE_DYE,"Sell "+count+" spice",(channel.equals("guild")?"Spacing Guild":"Esmar Tuek")+"\nGross: "+money(gross)+"\nTrust levy: "+money(tax)+"\nP&O pays you: "+money(gross-tax),null,null); - put(panel,11,Material.ARROW,"Back","Choose buyer",()->open(p),null);put(panel,15,Material.EMERALD,"Confirm sale","Sell all "+count+" spice · "+money(gross-tax),()->sell(p,channel,count),null);show(p,panel); - });} - void sellAll(Player p,String channel){if(!Set.of("guild","smuggler").contains(channel)){p.sendMessage("/sellall [guild|smuggler]");return;}try{sell(p,channel,bridge.spiceCount(p,channel));}catch(Exception e){p.sendMessage("Spice inventory unavailable.");}} + } + private void review(Player p,String channel){ + if(!arrakis(p))return;int count;try{count=bridge.spiceCount(p,channel);}catch(Exception e){p.sendMessage("Spice inventory unavailable.");return;} + if(count==0){p.sendMessage("You have no matching spice for this buyer.");return;}var panel=new Panel(p); + put(panel,13,Material.ORANGE_DYE,"Sell "+count+" spice",(channel.equals("guild")?"Spacing Guild":"Esmar Tuek")+"\nCurrent authorised price and Trust levy applied at confirmation.\nP&O pays your personal bank account.",null,null); + put(panel,11,Material.ARROW,"Back","Choose buyer",()->open(p),null);put(panel,15,Material.EMERALD,"Confirm sale","Sell all "+count+" eligible spice",()->sellAll(p,channel,count),null);show(p,panel); + } + void sellAll(Player p,String channel){sellAll(p,channel,null);} + private void sellAll(Player p,String channel,Integer expectedCount){ + if(!Set.of("guild","smuggler").contains(channel)){p.sendMessage("/sellall [guild|smuggler]");return;}if(!arrakis(p)){p.sendMessage("Spice buyers are available on Arrakis only.");return;} + int count;try{count=bridge.spiceCount(p,channel);}catch(Exception e){p.sendMessage("Spice inventory unavailable.");return;} + if(expectedCount!=null&&count!=expectedCount){p.sendMessage("Your spice inventory changed. Review the sale again.");review(p,channel);return;} + sell(p,channel,count); + } private void sell(Player p,String channel,int count){if(count<1){p.sendMessage("You have no matching spice for this buyer.");return;}p.closeInventory();var q=action("prepare");UUID request=UUID.randomUUID();q.addProperty("requestId",request.toString());q.addProperty("channel",channel);q.addProperty("amount",count);q.addProperty("confirmed",true); call(p,q,permit->{if(!arrakis(p)){p.sendMessage("Return to Arrakis to sell spice.");return;}try{bridge.sellSpice(p,permit);}catch(Exception e){p.sendMessage(e.getMessage()==null?"Spice sale not confirmed.":e.getMessage());return;}settle(p,request,true);}); }