Connect Emperor House passage decision to Guild authority

This commit is contained in:
unimatrix 2026-11-23 11:42:38 +00:00
commit 72b77930d2
3 changed files with 156 additions and 25 deletions

View file

@ -110,7 +110,7 @@ public final class DuneGUI extends JavaPlugin implements Listener,CommandExecuto
var data=item.getItemMeta().getPersistentDataContainer();for(var key:data.getKeys())if(data.has(key,PersistentDataType.STRING)&&"menu_opener".equals(data.get(key,PersistentDataType.STRING))){inventory.setItem(slot,null);break;}
}
}
private void openGuild(Player p,String requested){if(!lobby(p)){p.sendMessage("Imperial House passage departs from the Spacing Guild lobby.");return;}fetch(p,null,0,null,data->{String house=selection(data);if(house==null){p.sendMessage("Choose and confirm your House before Guild passage.");select(p,data,null);return;}if(requested!=null&&!GuildRoutes.allowed(house,requested))p.sendMessage("Other House worlds are unavailable through this Guild passage.");travelMenu(p,data);});}
private void openGuild(Player p,String requested){if(!lobby(p)){p.sendMessage("Imperial House passage departs from the Spacing Guild lobby.");return;}fetch(p,null,0,null,data->{String house=selection(data);if(house==null){p.sendMessage("Choose and confirm your House before Guild passage.");select(p,data,null);return;}travelMenu(p,data);});}
private static GuildBookings.Permit permit(JsonObject data){String house=selection(data);if(house==null||!data.has("keyId")||data.get("keyId").isJsonNull())return null;try{return new GuildBookings.Permit(house,UUID.fromString(data.get("keyId").getAsString()),data.get("version").getAsInt());}catch(RuntimeException invalid){return null;}}
private void travelMenu(Player p,JsonObject data){var q=new JsonObject();q.addProperty("action","quote");guildRequest(p,q,quote->{data.add("guildQuote",quote);renderTravelMenu(p,data);});}
private void guildRequest(Player p,JsonObject input,Consumer<JsonObject> done){UUID id=p.getUniqueId(),world=p.getWorld().getUID();if(!boarding.add(id)){p.sendMessage("J.A.R.V.I.S. is processing your passage.");return;}Bukkit.getScheduler().runTaskAsynchronously(this,()->{JsonObject result=null;String failure=null;try{result=bridge.guild(id,input);}catch(Exception e){failure=e.getMessage();}var response=result;String message=failure;if(!isEnabled())return;Bukkit.getScheduler().runTask(this,()->{if(!boarding.remove(id)||!p.isOnline()||!p.getWorld().getUID().equals(world))return;if(response==null){p.sendMessage(message==null?"The central Guild service is unavailable.":message);return;}done.accept(response);});});}
@ -118,9 +118,9 @@ public final class DuneGUI extends JavaPlugin implements Listener,CommandExecuto
String house=selection(data);var home=GuildRoutes.home(house);if(home==null){select(p,data,null);return;}
String quote="Guild fare: "+String.format(java.util.Locale.ROOT,"%.2f",data.getAsJsonObject("guildQuote").get("fareMinor").getAsInt()/100.0)+" DM from your central personal bank account. Collected after arrival.";
var booked=bookings.get(p.getUniqueId());String detail=quote+"\nTravel opens at :00, :20 and :40 for five minutes."+(booked==null?"":"\nYour passage is already booked.");
if(BedrockForms.isBedrock(p)){List<BedrockForms.Choice> choices=new ArrayList<>();for(var route:GuildRoutes.ALL){boolean allowed=route.house().equals(house);choices.add(new BedrockForms.Choice((allowed?"§6":"§8")+route.name()+(allowed?" · Your House":" · Unavailable"),allowed?detail:"Another House world",allowed?()->bookPassage(p,data):()->travelMenu(p,data),"https://www.dunerealms.com/assets/gui/guild-world-"+route.world().replace('_','-')+(allowed?"":"-locked")+"-v1.png"));}choices.add(new BedrockForms.Choice("Cancel booking","No fare has been charged",()->{cancelBooking(p);travelMenu(p,data);}));choices.add(new BedrockForms.Choice("Your Mentat","Return to your House adviser",()->mentat(p,data)));BedrockForms.show(this,p,"Spacing Guild · Imperial passage",detail,choices);return;}
if(BedrockForms.isBedrock(p)){List<BedrockForms.Choice> choices=new ArrayList<>();for(var route:GuildRoutes.ALL){boolean allowed=quotedDestinations(data.getAsJsonObject("guildQuote")).contains(route.world());choices.add(new BedrockForms.Choice((allowed?"§6":"§8")+route.name()+(allowed?(route.house().equals(house)?" · Your House":" · Imperial passage"):" · Unavailable"),allowed?detail:"Another House world",allowed?()->bookPassage(p,data,route.world()):()->travelMenu(p,data),"https://www.dunerealms.com/assets/gui/guild-world-"+route.world().replace('_','-')+(allowed?"":"-locked")+"-v1.png"));}choices.add(new BedrockForms.Choice("Cancel booking","No fare has been charged",()->{cancelBooking(p);travelMenu(p,data);}));choices.add(new BedrockForms.Choice("Your Mentat","Return to your House adviser",()->mentat(p,data)));BedrockForms.show(this,p,"Spacing Guild · Imperial passage",detail,choices);return;}
int own=Arrays.asList(IDS).indexOf(house);Screen screen=screen(p,"Spacing Guild · Imperial passage",0xE330+own);
for(int i=0;i<GuildRoutes.ALL.size();i++){var route=GuildRoutes.ALL.get(i);boolean allowed=route.house().equals(house);var item=icon(p,route.name(),allowed?detail:"Unavailable · another House world","drgui_transparent",allowed?Material.ENDER_PEARL:Material.GRAY_DYE);int centre=CENTRES[i];for(int row=i<3?-1:0;row<=1;row++)for(int col=-1;col<=1;col++){int slot=centre+row*9+col;screen.inventory.setItem(slot,item.clone());if(allowed)screen.actions.put(slot,()->bookPassage(p,data));}}
for(int i=0;i<GuildRoutes.ALL.size();i++){var route=GuildRoutes.ALL.get(i);boolean allowed=quotedDestinations(data.getAsJsonObject("guildQuote")).contains(route.world());var item=icon(p,route.name(),allowed?detail:"Unavailable · another House world","drgui_transparent",allowed?Material.ENDER_PEARL:Material.GRAY_DYE);int centre=CENTRES[i];for(int row=i<3?-1:0;row<=1;row++)for(int col=-1;col<=1;col++){int slot=centre+row*9+col;screen.inventory.setItem(slot,item.clone());if(allowed)screen.actions.put(slot,()->bookPassage(p,data,route.world()));}}
button(screen,p,45,"Your Mentat","Return to your House adviser",Material.ARROW,()->mentat(p,data));screen.actions.put(46,screen.actions.get(45));
button(screen,p,49,"Cancel booking","No fare is taken for a cancelled booking",Material.BARRIER,()->{cancelBooking(p);travelMenu(p,data);});for(int slot:new int[]{48,50})screen.actions.put(slot,screen.actions.get(49));
if(guild!=null){button(screen,p,53,booked==null?"Book passage":"Review booked passage",detail,Material.EMERALD,()->bookPassage(p,data));screen.actions.put(52,screen.actions.get(53));}
@ -128,16 +128,18 @@ public final class DuneGUI extends JavaPlugin implements Listener,CommandExecuto
p.openInventory(screen.inventory);expire(screen);
}
private void cancelBooking(Player p){boolean removed=bookings.cancel(p.getUniqueId());removed=boarding.remove(p.getUniqueId())||removed;p.sendMessage(removed?"Guild passage cancelled. No fare was charged.":"You have no pending Guild booking.");}
private void bookPassage(Player p,JsonObject shown){
private static Set<String> quotedDestinations(JsonObject quote){Set<String> worlds=new HashSet<>();if(quote.has("destinations"))for(var value:quote.getAsJsonArray("destinations"))worlds.add(value.getAsString());else worlds.add(quote.get("destination").getAsString());return Set.copyOf(worlds);}
private void bookPassage(Player p,JsonObject shown){var booked=bookings.get(p.getUniqueId());bookPassage(p,shown,booked==null?GuildRoutes.home(selection(shown)).world():booked.world());}
private void bookPassage(Player p,JsonObject shown,String destination){
if(boarding.contains(p.getUniqueId())){p.sendMessage("Guild boarding is already being confirmed.");return;}
var expected=permit(shown);if(expected==null){p.sendMessage("Confirm your current House key before booking passage.");select(p,shown,selection(shown));return;}
fetch(p,null,0,null,fresh->{var current=permit(fresh);if(current==null||!current.equals(expected)){p.sendMessage("Your House key changed. Review the current passage.");travelMenu(p,fresh);return;}
var q=new JsonObject();q.addProperty("action","quote");guildRequest(p,q,quote->{if(!quote.get("affordable").getAsBoolean()){p.sendMessage("Not enough available Dunemarks in your central personal bank account.");return;}var home=GuildRoutes.home(current.house());int fare=quote.get("fareMinor").getAsInt();if(!home.world().equals(quote.get("destination").getAsString())){p.sendMessage("Your House destination changed. Reopen the menu.");return;}var booked=bookings.book(p.getUniqueId(),current,home.world(),fare,Instant.now().getEpochSecond());p.closeInventory();p.sendMessage(Component.text("Passage booked to "+home.name()+". Fare "+String.format(java.util.Locale.ROOT,"%.2f",booked.fare()/100.0)+" DM is reserved at boarding and collected after arrival.",NamedTextColor.GOLD));});
var q=new JsonObject();q.addProperty("action","quote");guildRequest(p,q,quote->{if(!quote.get("affordable").getAsBoolean()){p.sendMessage("Not enough available Dunemarks in your central personal bank account.");return;}var home=GuildRoutes.ALL.stream().filter(r->r.world().equals(destination)).findFirst().orElseThrow();int fare=quote.get("fareMinor").getAsInt();if(!quotedDestinations(quote).contains(destination)){p.sendMessage("Your House destination changed. Reopen the menu.");return;}var booked=bookings.book(p.getUniqueId(),current,home.world(),fare,Instant.now().getEpochSecond(),quotedDestinations(quote));p.closeInventory();p.sendMessage(Component.text("Passage booked to "+home.name()+". Fare "+String.format(java.util.Locale.ROOT,"%.2f",booked.fare()/100.0)+" DM is reserved at boarding and collected after arrival.",NamedTextColor.GOLD));});
});
}
private void board(GuildBookings.Booking booking){
Player p=Bukkit.getPlayer(booking.player());if(p==null||!p.isOnline()||!lobby(p)||!boarding.add(booking.player()))return;
Bukkit.getScheduler().runTaskAsynchronously(this,()->{JsonObject ticket=null;String failure=null;try{var fresh=bridge.request(booking.player(),null,0,null);var current=permit(fresh);if(current==null||!booking.matches(current,booking.fare(),Instant.now().getEpochSecond())||!bridge.worldOnline(booking.world()))throw new IllegalStateException("Your key, boarding window or destination changed. Please rebook.");var q=new JsonObject();q.addProperty("action","board");q.addProperty("requestId",booking.requestId().toString());q.addProperty("keyId",current.keyId().toString());q.addProperty("version",current.version());q.addProperty("fareMinor",booking.fare());ticket=bridge.guild(booking.player(),q);}catch(Exception e){failure=e.getMessage();}var result=ticket;String message=failure;if(!isEnabled())return;Bukkit.getScheduler().runTask(this,()->{if(!boarding.remove(booking.player())||!p.isOnline()||!lobby(p))return;try{if(result==null){p.sendMessage(message==null?"Passage was not confirmed. Any unused reservation expires automatically.":message);return;}p.closeInventory();bridge.dispatchGuild(p,result);}catch(Exception unavailable){p.sendMessage("The transfer was not sent. Your unused fare reservation will expire automatically.");}});});
Bukkit.getScheduler().runTaskAsynchronously(this,()->{JsonObject ticket=null;String failure=null;try{var fresh=bridge.request(booking.player(),null,0,null);var current=permit(fresh);if(current==null||!booking.matches(current,booking.fare(),Instant.now().getEpochSecond())||!bridge.worldOnline(booking.world()))throw new IllegalStateException("Your key, boarding window or destination changed. Please rebook.");var q=new JsonObject();q.addProperty("action","board");q.addProperty("requestId",booking.requestId().toString());q.addProperty("keyId",current.keyId().toString());q.addProperty("version",current.version());q.addProperty("fareMinor",booking.fare());q.addProperty("destination",booking.world());ticket=bridge.guild(booking.player(),q);}catch(Exception e){failure=e.getMessage();}var result=ticket;String message=failure;if(!isEnabled())return;Bukkit.getScheduler().runTask(this,()->{if(!boarding.remove(booking.player())||!p.isOnline()||!lobby(p))return;try{if(result==null){p.sendMessage(message==null?"Passage was not confirmed. Any unused reservation expires automatically.":message);return;}p.closeInventory();bridge.dispatchGuild(p,result);}catch(Exception unavailable){p.sendMessage("The transfer was not sent. Your unused fare reservation will expire automatically.");}});});
}
private void fetch(Player p,String house,int version,UUID requestId,Consumer<JsonObject> callback){
UUID owner=p.getUniqueId();if(requests.containsKey(owner)){p.sendMessage("The House register is processing your request.");return;}

View file

@ -4,11 +4,14 @@ import java.util.*;
final class GuildBookings {
record Permit(String house,UUID keyId,int version){Permit{if(GuildRoutes.home(house)==null||keyId==null||version<1)throw new IllegalArgumentException("A current House key is required");}}
record Booking(UUID player,Permit permit,String world,int fare,long departureAt,UUID requestId){
boolean matches(Permit current,int currentFare,long now){return permit.equals(current)&&fare==currentFare&&GuildRoutes.allowed(current.house(),world)&&DepartureWindow.open(now)&&now>=departureAt&&Math.floorDiv(now,DepartureWindow.CYCLE)==Math.floorDiv(departureAt,DepartureWindow.CYCLE);}
boolean matches(Permit current,int currentFare,long now){return permit.equals(current)&&fare==currentFare&&GuildRoutes.candidate(current.house(),world)&&DepartureWindow.open(now)&&now>=departureAt&&Math.floorDiv(now,DepartureWindow.CYCLE)==Math.floorDiv(departureAt,DepartureWindow.CYCLE);}
}
private final Map<UUID,Booking> bookings=new HashMap<>();
Booking book(UUID player,Permit permit,String world,int fare,long now){
if(!GuildRoutes.allowed(permit.house(),world)||fare<0)throw new IllegalArgumentException("This world is unavailable for your House");
return book(player,permit,world,fare,now,Set.of(GuildRoutes.home(permit.house()).world()));
}
Booking book(UUID player,Permit permit,String world,int fare,long now,Set<String> quotedDestinations){
if(!quotedDestinations.contains(world)||!GuildRoutes.candidate(permit.house(),world)||fare<0)throw new IllegalArgumentException("This world is unavailable for your House");
Booking old=bookings.get(player);if(old!=null){if(old.permit().equals(permit)&&old.world().equals(world)&&old.fare()==fare)return old;throw new IllegalStateException("Cancel your existing booking before changing it");}
Booking booking=new Booking(player,permit,world,fare,DepartureWindow.nextBoarding(now),UUID.randomUUID());bookings.put(player,booking);return booking;
}

View file

@ -11,4 +11,5 @@ final class GuildRoutes {
);
static Route home(String house){return ALL.stream().filter(r->r.house().equals(house)).findFirst().orElse(null);}
static boolean allowed(String house,String world){Route route=home(house);return route!=null&&route.world().equals(world);}
static boolean candidate(String house,String world){return home(house)!=null&&(allowed(house,world)||"arrakis".equals(world));}
}