Integrate open Guild travel and bounded bounty reporting

This commit is contained in:
unimatrixgit 2026-12-03 20:39:10 +01:00
commit d5b890553e
9 changed files with 129 additions and 13 deletions

View file

@ -395,7 +395,10 @@ final class JarvisBackend implements Backend, Listener {
if (unavailable!=null && unavailable.isBlank()) unavailable="Passage unavailable";
String name=offer!=null&&!text(offer,"name").isBlank()?text(offer,"name"):route.getValue();
long fare=offer!=null&&offer.has("fareMinor")?offer.get("fareMinor").getAsLong():quote.get("fareMinor").getAsLong();
worlds.add(new World(id, name, unavailable == null ? "Imperial Guild passage" : unavailable,
String tagline = unavailable != null ? unavailable
: offer != null && offer.has("bountyMinor") ? "Unsanctioned arrival: " + Ui.dm(offer.get("bountyMinor").getAsLong()) + " bounty" + (yes(offer,"reduced") ? " (clan/company discount)" : "")
: offer != null && yes(offer,"free") ? "Free passage" : "Imperial Guild passage";
worlds.add(new World(id, name, tagline,
Material.COMPASS, -1, unavailable == null, fare, key, version));
}
return List.copyOf(worlds);

View file

@ -34,7 +34,7 @@ final class TravelMenu extends Menu {
var w = worlds.get(i);
String fare = w.fareMinor() > 0 ? "<gray>Fare: <gold>" + Ui.dm(w.fareMinor()) : w.fareMinor() == 0 ? "<green>Free passage" : "";
var icon = Ui.item(w.up() ? w.icon() : Material.GRAY_DYE, (w.up() ? "<gold><b>" : "<gray>") + w.name(),
"<gray>" + w.tagline(), w.online() < 0 ? "" : "<gray>Players: <white>" + w.online(), w.up() ? fare : "", "",
(w.tagline().startsWith("Unsanctioned") ? "<red>" : "<gray>") + w.tagline(), w.online() < 0 ? "" : "<gray>Players: <white>" + w.online(), w.up() ? fare : "", "",
w.up() ? "<yellow>Click to travel" : "");
if (w.up()) put(STARS[i], icon, t -> { end(); book(w); });
else put(STARS[i], icon);

View file

@ -140,8 +140,8 @@ public final class DuneGUI extends JavaPlugin implements Listener,CommandExecuto
var passage = data.getAsJsonObject("guildQuote");
var eligible = quotedDestinations(passage);
var catalog = GuildRoutes.catalog(passage);
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."+(data.getAsJsonObject("guildQuote").has("subsidyMinor")?" Guild subsidy: "+String.format(java.util.Locale.ROOT,"%.2f",data.getAsJsonObject("guildQuote").get("subsidyMinor").getAsInt()/100.0)+" DM.":"");
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.");
var booked=bookings.get(p.getUniqueId());
String detail="Travel 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){
@ -149,7 +149,7 @@ public final class DuneGUI extends JavaPlugin implements Listener,CommandExecuto
String reason=offer==null?guildUnavailable(passage,eligible,route.world()):offer.available()?null:offer.reason();
String name=offer==null?route.name():offer.name();
choices.add(new BedrockForms.Choice((reason==null?"§6":"§8")+name+(reason==null?"":" · "+reason),
reason==null?detail:reason,reason==null?()->bookPassage(p,data,route.world()):()->{p.sendMessage(reason);renderTravelMenu(p,data);},
reason==null?guildQuoteText(passage,offer)+"\n"+detail:reason,reason==null?()->bookPassage(p,data,route.world()):()->{p.sendMessage(reason);renderTravelMenu(p,data);},
"https://www.dunerealms.com/assets/gui/guild-world-"+route.world().replace('_','-')+(reason==null?"":"-locked")+"-v1.png"));
}
choices.add(new BedrockForms.Choice("Cancel booking","No fare has been charged",()->{cancelBooking(p);travelMenu(p,data);}));
@ -163,7 +163,7 @@ public final class DuneGUI extends JavaPlugin implements Listener,CommandExecuto
String reason=offer==null?guildUnavailable(passage,eligible,route.world()):offer.available()?null:offer.reason();
String name=offer==null?route.name():offer.name();
int centre=CENTRES[i];
var item=icon(p,name,reason==null?detail:reason,"drgui_transparent",reason==null?Material.ENDER_PEARL:Material.GRAY_DYE);
var item=icon(p,name,reason==null?guildQuoteText(passage,offer)+"\n"+detail:reason,"drgui_transparent",reason==null?Material.ENDER_PEARL:Material.GRAY_DYE);
if(reason!=null){
item.setType(Material.GRAY_DYE);
var meta=item.getItemMeta();meta.setItemModel(null);meta.displayName(Component.text(name,NamedTextColor.GRAY));item.setItemMeta(meta);
@ -177,6 +177,13 @@ public final class DuneGUI extends JavaPlugin implements Listener,CommandExecuto
if(MCGUIMenus.show(p,"Spacing Guild · Imperial passage","",screen.inventory,screen.actions,screen.gate))return;
p.openInventory(screen.inventory);expire(screen);
}
private static String guildQuoteText(JsonObject quote,GuildRoutes.Offer offer){
long fare=offer==null?quote.get("fareMinor").getAsLong():offer.fareMinor();
if(fare==0)return "Free passage";
String terms=String.format(Locale.ROOT,"Fare %.2f DM, collected after arrival.",fare/100.0);
if(offer!=null&&offer.bountyMinor()>0)terms+=String.format(Locale.ROOT,"\nArrival bounty %.2f DM",offer.bountyMinor()/100.0)+(offer.reduced()?" · Home Block discount":"");
return terms;
}
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 static String guildUnavailable(JsonObject quote,Set<String> eligible,String world){
if(quote.has("prologueRequired")&&quote.get("prologueRequired").getAsBoolean())return "Complete your House Prologue.";
@ -190,7 +197,17 @@ public final class DuneGUI extends JavaPlugin implements Listener,CommandExecuto
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.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));});
var q=new JsonObject();q.addProperty("action","quote");guildRequest(p,q,quote->{var available=quotedDestinations(quote);var offer=GuildRoutes.catalog(quote).get(destination);
String reason=offer==null?guildUnavailable(quote,available,destination):offer.available()?null:offer.reason();
if(reason!=null||!available.contains(destination)){p.sendMessage(reason==null||reason.isBlank()?"That route is no longer available.":reason);return;}
var home=GuildRoutes.ALL.stream().filter(r->r.world().equals(destination)).findFirst().orElseThrow();
int fare=offer==null?quote.get("fareMinor").getAsInt():Math.toIntExact(offer.fareMinor());
var shownQuote=shown.getAsJsonObject("guildQuote");
var shownOffer=GuildRoutes.catalog(shownQuote).get(destination);
if(!guildQuoteText(shownQuote,shownOffer).equals(guildQuoteText(quote,offer))){
p.sendMessage("The Guild changed this fare or arrival bounty. Review the current passage.");
fresh.add("guildQuote",quote);renderTravelMenu(p,fresh);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){

View file

@ -14,8 +14,9 @@ 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));}
record Offer(String server,String name,boolean available,String reason,long fareMinor){}
/** Open travel: every House may book any trade world. Fare, sanction and bounty are decided centrally by JARVIS. */
static boolean candidate(String house,String world){return home(house)!=null&&ALL.stream().anyMatch(route->route.world().equals(world));}
record Offer(String server,String name,boolean available,String reason,long fareMinor,long bountyMinor,boolean reduced){}
static Map<String,Offer> catalog(JsonObject quote){
Map<String,Offer> result=new LinkedHashMap<>();
@ -27,7 +28,9 @@ final class GuildRoutes {
String server=row.get("server").getAsString();
if(ALL.stream().noneMatch(route->route.world().equals(server)))continue;
String reason=row.has("reason")&&!row.get("reason").isJsonNull()?row.get("reason").getAsString():"Passage unavailable";
result.putIfAbsent(server,new Offer(server,row.get("name").getAsString(),row.get("available").getAsBoolean(),reason,row.get("fareMinor").getAsLong()));
result.putIfAbsent(server,new Offer(server,row.get("name").getAsString(),row.get("available").getAsBoolean(),reason,row.get("fareMinor").getAsLong(),
row.has("bountyMinor")?row.get("bountyMinor").getAsLong():0,
row.has("reduced")&&row.get("reduced").getAsBoolean()));
}
return result;
}

View file

@ -3,10 +3,12 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.UUID;
class GuildBookingsTest {
@Test void centralQuoteCanAddOnlyArrakisAndDoesNotChangeOrdinaryHouseRoutes(){
@Test void openTravelBooksAnyQuotedWorldButNeverAnUnquotedOne(){
var permit=new GuildBookings.Permit("atreides",UUID.randomUUID(),1);var bookings=new GuildBookings();
var booking=bookings.book(UUID.randomUUID(),permit,"arrakis",100,0,java.util.Set.of("caladan","arrakis"));assertTrue(booking.matches(permit,100,1));assertFalse(GuildRoutes.allowed("atreides","arrakis"));
assertThrows(IllegalArgumentException.class,()->bookings.book(UUID.randomUUID(),permit,"kaitain",100,0,java.util.Set.of("kaitain")));
assertTrue(bookings.book(UUID.randomUUID(),permit,"kaitain",100,0,java.util.Set.of("kaitain")).matches(permit,100,1));
assertThrows(IllegalArgumentException.class,()->bookings.book(UUID.randomUUID(),permit,"kaitain",100,0,java.util.Set.of("caladan")));
assertThrows(IllegalArgumentException.class,()->bookings.book(UUID.randomUUID(),permit,"nowhere",100,0,java.util.Set.of("nowhere")));
}
private final GuildBookings.Permit atreides=new GuildBookings.Permit("atreides",UUID.randomUUID(),1);
@Test void fiveCanonicalHouseRoutesNeverGrantAnotherHouseDestination(){

View file

@ -24,4 +24,17 @@ class GuildRouteCatalogTest {
assertFalse(routes.get("sigma_draconis_iv").available());
assertEquals("Current embargo", routes.get("sigma_draconis_iv").reason());
}
@Test void routeFareAndBountyStayBoundToTheirWorld() {
var quote = com.google.gson.JsonParser.parseString("""
{"fareMinor":0,"routes":[
{"server":"arrakis","name":"Arrakis","available":true,"fareMinor":0},
{"server":"kaitain","name":"Kaitain","available":true,"fareMinor":7500,"bountyMinor":37500,"reduced":true}]}
""").getAsJsonObject();
var offers = GuildRoutes.catalog(quote);
assertEquals(0, offers.get("arrakis").fareMinor());
assertEquals(0, offers.get("arrakis").bountyMinor());
assertEquals(7500, offers.get("kaitain").fareMinor());
assertEquals(37500, offers.get("kaitain").bountyMinor());
assertTrue(offers.get("kaitain").reduced());
}
}

View file

@ -5,6 +5,7 @@ import java.util.*;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.*;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
@ -55,9 +56,29 @@ final class GuildTransport implements Listener {
private void connect(Player p,String target)throws IOException{
var bytes=new ByteArrayOutputStream();var out=new DataOutputStream(bytes);out.writeUTF("Connect");out.writeUTF(target);p.sendPluginMessage(plugin,"BungeeCord",bytes.toByteArray());
}
/** A paid arrival places a central bounty: tell the traveller and announce it to everyone on this world, once per arrival receipt. */
private void announceBounty(Player traveller,JsonObject response){
if(!response.has("bounty")||response.get("bounty").isJsonNull())return;
var bounty=response.getAsJsonObject("bounty");long amount=bounty.get("amountMinor").getAsLong();if(amount<1)return;
traveller.sendMessage(TravelBounty.warnTraveller(server,amount));
Bukkit.broadcastMessage("§c§l"+TravelBounty.announce(traveller.getName(),server,amount));
}
/** A player killed by another player may carry a central travel bounty. JARVIS decides and pays; this only reports the kill once. */
@EventHandler(priority=EventPriority.MONITOR,ignoreCancelled=true)
public void death(PlayerDeathEvent e){
Player victim=e.getEntity(),killer=victim.getKiller();
if(killer==null||killer.getUniqueId().equals(victim.getUniqueId())||!Set.of("caladan","giedi_prime","kaitain","sigma_draconis_iv","arrakis").contains(server))return;
var q=new JsonObject();q.addProperty("action","bounty_claim");q.addProperty("uuid",killer.getUniqueId().toString());q.addProperty("victim",victim.getUniqueId().toString());q.addProperty("server",server);
q.addProperty("requestId",TravelBounty.claimReference(victim.getUniqueId(),killer.getUniqueId(),System.currentTimeMillis()/1000).toString());
String victimName=victim.getName(),killerName=killer.getName();
Bukkit.getScheduler().runTaskAsynchronously(plugin,()->{JsonObject result=null;try{result=api.guild(q);}catch(Exception ignored){}var response=result;if(!plugin.isEnabled())return;
Bukkit.getScheduler().runTask(plugin,()->{if(response==null||!response.has("claimed")||!response.get("claimed").getAsBoolean())return;
Bukkit.broadcastMessage("§6"+TravelBounty.claimed(killerName,victimName,response.get("amountMinor").getAsLong()));});});
}
private void arrival(Player p){
if(!Set.of("caladan","giedi_prime","kaitain","sigma_draconis_iv","arrakis").contains(server)||!p.isOnline()||!busy.add(p.getUniqueId()))return;
UUID id=p.getUniqueId();var q=new JsonObject();q.addProperty("action","arrive");q.addProperty("uuid",id.toString());q.addProperty("server",server);
Bukkit.getScheduler().runTaskAsynchronously(plugin,()->{JsonObject result=null;try{result=api.guild(q);}catch(Exception ignored){}var response=result;if(!plugin.isEnabled())return;Bukkit.getScheduler().runTask(plugin,()->{busy.remove(id);if(response==null||!p.isOnline()||!response.has("passage")||response.get("passage").isJsonNull())return;var receipt=response.getAsJsonObject("passage");UUID key=UUID.fromString(receipt.get("requestId").getAsString());if(notified.add(key))p.sendMessage("J.A.R.V.I.S. · "+(receipt.has("smuggler")&&receipt.get("smuggler").getAsBoolean()?"Smuggler":"Guild")+" arrival confirmed. Fare paid from your central bank account.");});});
Bukkit.getScheduler().runTaskAsynchronously(plugin,()->{JsonObject result=null;try{result=api.guild(q);}catch(Exception ignored){}var response=result;if(!plugin.isEnabled())return;Bukkit.getScheduler().runTask(plugin,()->{busy.remove(id);if(response==null||!p.isOnline()||!response.has("passage")||response.get("passage").isJsonNull())return;var receipt=response.getAsJsonObject("passage");UUID key=UUID.fromString(receipt.get("requestId").getAsString());if(notified.add(key)){p.sendMessage("J.A.R.V.I.S. · "+(receipt.has("smuggler")&&receipt.get("smuggler").getAsBoolean()?"Smuggler":"Guild")+" arrival confirmed."+(Long.parseLong(receipt.get("fareMinor").getAsString())>0?" Fare paid from your central bank account.":" Free passage."));announceBounty(p,response);}});});
}
}

View file

@ -0,0 +1,31 @@
package com.dunerealms.jarvis;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;
/** Wording and identifiers for travel bounties. The amounts and payouts are decided by central JARVIS only. */
final class TravelBounty {
private static final Map<String,String> WORLDS=Map.of("caladan","Kaladam","giedi_prime","Giedermark","kaitain","Kaiterhof","sigma_draconis_iv","Sigmadraken IV","arrakis","Arrakduin");
private TravelBounty(){}
static String dm(long minor){return BigDecimal.valueOf(minor,2).toPlainString()+" DM";}
static String world(String server){return WORLDS.getOrDefault(server,"this world");}
/** Only characters a Minecraft name can contain survive, so a crafted central value cannot add colour or markup. */
static String safe(String name){String clean=name==null?"":name.replaceAll("[^A-Za-z0-9_.\\- ]","").strip();return clean.isEmpty()?"A traveller":clean.substring(0,Math.min(32,clean.length()));}
static String announce(String traveller,String server,long amountMinor){
return "UNSANCTIONED TRAVELLER: "+safe(traveller)+" arrived on "+world(server)+" without Imperial sanction. Bounty: "+dm(amountMinor)+" on their head!";
}
static String warnTraveller(String server,long amountMinor){
return "J.A.R.V.I.S. · You are an unsanctioned traveller on "+world(server)+". A bounty of "+dm(amountMinor)+" has been placed on you.";
}
static String claimed(String hunter,String traveller,long amountMinor){
return safe(hunter)+" claimed the "+dm(amountMinor)+" bounty on "+safe(traveller)+"!";
}
/** The same death always yields the same reference, so a repeated report can never pay twice. */
static UUID claimReference(UUID victim,UUID killer,long deathMillis){
return UUID.nameUUIDFromBytes(("travel-bounty-kill:"+victim+":"+killer+":"+deathMillis).getBytes(StandardCharsets.UTF_8));
}
}

View file

@ -0,0 +1,26 @@
package com.dunerealms.jarvis;
import static org.junit.jupiter.api.Assertions.*;
import java.util.UUID;
import org.junit.jupiter.api.Test;
class TravelBountyTest {
@Test void amountsAreShownInDm(){assertEquals("750.00 DM",TravelBounty.dm(75000));assertEquals("375.00 DM",TravelBounty.dm(37500));assertEquals("0.01 DM",TravelBounty.dm(1));}
@Test void theAnnouncementNamesTheTravellerTheWorldAndTheBounty(){
String text=TravelBounty.announce("Muad_Dib","giedi_prime",75000);
assertTrue(text.contains("Muad_Dib")&&text.contains("Giedermark")&&text.contains("750.00 DM")&&text.contains("UNSANCTIONED"));
assertTrue(TravelBounty.warnTraveller("arrakis",37500).contains("375.00 DM"));
}
@Test void centralNamesCannotInjectColourOrMarkup(){
assertEquals("cEvil",TravelBounty.safe("§cEvil§"));assertEquals("A traveller",TravelBounty.safe("§§§"));assertEquals("A traveller",TravelBounty.safe(null));assertEquals(32,TravelBounty.safe("x".repeat(80)).length());
assertFalse(TravelBounty.announce("§4Bad<red>","kaitain",100).contains("§"));
}
@Test void aRepeatedReportOfTheSameDeathHasTheSameReference(){
UUID victim=UUID.randomUUID(),killer=UUID.randomUUID();
assertEquals(TravelBounty.claimReference(victim,killer,1000),TravelBounty.claimReference(victim,killer,1000));
assertNotEquals(TravelBounty.claimReference(victim,killer,1000),TravelBounty.claimReference(victim,killer,2000));
assertNotEquals(TravelBounty.claimReference(victim,killer,1000),TravelBounty.claimReference(killer,victim,1000));
}
@Test void aClaimIsAnnouncedWithTheAmount(){assertEquals("Hunter claimed the 750.00 DM bounty on Prey!",TravelBounty.claimed("Hunter","Prey",75000));}
}