From 65cdc8b6e50f1fa849be559624ac6b3b01e45192 Mon Sep 17 00:00:00 2001 From: XxFran10xX <318299142+XxFran10xX@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:08:06 +0100 Subject: [PATCH 1/3] fix(diplomacy): apply both halves of a trade relation to visiting guilds A guild trading in another faction's province only got the owner's record of their trade relation ("their guilds in our territory"), plus the other side's "our guilds" bonus only when the owner's type was mutual. The leader's half of an unequal treaty is held on the mutual side, so the leader's +20% trade power / +7.5% production never applied in the weaker side's land, while the menu promised it. Visiting guilds now get the owner's trade-effects-them plus their own faction's trade-effects-us towards the owner. Trade agreements and embargoes give the same results as before; income previews follow because both lookups go through getTradeRelation. Co-Authored-By: Claude Opus 5.5 --- .../diplomacy/DiplomacyHandler.java | 38 +++++- .../map/provinces/Province.java | 6 +- .../diplomacy/TradeRelationHalvesTest.java | 125 ++++++++++++++++++ 3 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 src/test/java/net/tfminecraft/simplefactions/diplomacy/TradeRelationHalvesTest.java diff --git a/src/main/java/net/tfminecraft/simplefactions/diplomacy/DiplomacyHandler.java b/src/main/java/net/tfminecraft/simplefactions/diplomacy/DiplomacyHandler.java index d456cc98..f6a75e3b 100644 --- a/src/main/java/net/tfminecraft/simplefactions/diplomacy/DiplomacyHandler.java +++ b/src/main/java/net/tfminecraft/simplefactions/diplomacy/DiplomacyHandler.java @@ -176,13 +176,41 @@ public void clearTreatyNotice(String s) { treatyNotices.remove(s); } + /** + * Trade modifiers on the target faction's guilds in this faction's provinces. Each side keeps its own half + * of a trade relation: this faction's record gives what visiting guilds get ("their guilds in our + * territory"), and the target's record towards this faction gives what its own guilds get here ("our + * guilds in their territory"). Reading both records applies one-sided treaties, such as the leader's half + * of an unequal treaty, the way the Diplomacy menu shows them. Both lookups go through + * {@link #getTradeRelation}, so income previews see proposed relations on either side. + */ public List getTradeModifiersFor(String target) { + Faction visitor = FactionManager.getByString(target); + return tradeModifiersFor(target, visitor); + } + + /** {@link #getTradeModifiersFor(String)} for a faction already in hand, without the lookup by id. */ + public List getTradeModifiersFor(Faction visitor) { + if (visitor == null || visitor.getId() == null) { + return new ArrayList<>(); + } + return tradeModifiersFor(visitor.getId(), visitor); + } + + private List tradeModifiersFor(String target, Faction visitor) { List mods = new ArrayList<>(); - RelationType r = getTradeRelation(target); - if(r == null) return mods; - if(r.hasTradeEffectsThem()) { - for(FactionModifier mod : r.getTradeEffectsThem()) { - mods.add(new FactionModifier(FactionManager.getByString(target), mod)); + RelationType ours = getTradeRelation(target); + if (ours != null) { + for (FactionModifier mod : ours.getTradeEffectsThemRaw()) { + mods.add(new FactionModifier(visitor, mod)); + } + } + RelationType theirs = visitor == null || visitor.getDiplomacyHandler() == null || f == null + ? null + : visitor.getDiplomacyHandler().getTradeRelation(f.getId()); + if (theirs != null) { + for (FactionModifier mod : theirs.getTradeEffectsUsRaw()) { + mods.add(new FactionModifier(visitor, mod)); } } return mods; diff --git a/src/main/java/net/tfminecraft/simplefactions/map/provinces/Province.java b/src/main/java/net/tfminecraft/simplefactions/map/provinces/Province.java index 82f47836..4988d802 100644 --- a/src/main/java/net/tfminecraft/simplefactions/map/provinces/Province.java +++ b/src/main/java/net/tfminecraft/simplefactions/map/provinces/Province.java @@ -437,8 +437,10 @@ public List getModifiersForGuild(Guild guild) { mods.addAll(host.getModifiers(guild.getId(), Scope.DOMESTIC_GUILDS, Region.FOREIGN_TERRITORY)); mods.addAll(owner.getModifiers(guild.getId(), Scope.FOREIGN_GUILDS, Region.OUR_TERRITORY)); } - if(owner.getDiplomacyHandler().hasTradeRelation(host.getId())) { - mods.addAll(owner.getDiplomacyHandler().getTradeModifiersFor(host.getId())); + // Both halves of a trade relation count: the owner's record and the visitor's own record. + if (owner.getDiplomacyHandler().hasTradeRelation(host.getId()) + || host.getDiplomacyHandler().hasTradeRelation(owner.getId())) { + mods.addAll(owner.getDiplomacyHandler().getTradeModifiersFor(host)); } } else { mods.addAll(host.getModifiers(guild.getId(), Scope.DOMESTIC_GUILDS, Region.OUR_TERRITORY)); diff --git a/src/test/java/net/tfminecraft/simplefactions/diplomacy/TradeRelationHalvesTest.java b/src/test/java/net/tfminecraft/simplefactions/diplomacy/TradeRelationHalvesTest.java new file mode 100644 index 00000000..8f8d124e --- /dev/null +++ b/src/test/java/net/tfminecraft/simplefactions/diplomacy/TradeRelationHalvesTest.java @@ -0,0 +1,125 @@ +package net.tfminecraft.simplefactions.diplomacy; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import net.tfminecraft.simplefactions.enums.FactionModifiers; +import net.tfminecraft.simplefactions.objects.Faction; +import net.tfminecraft.simplefactions.objects.FactionModifier; +import net.tfminecraft.simplefactions.testsupport.FactionDomainFixture; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * A guild trading in another faction's province gets the owner's half of their trade relation ("their guilds + * in our territory") plus its own faction's half ("our guilds in their territory"), as the Diplomacy menu + * shows them. The relation types mirror Main's diplomacy.yml. + */ +class TradeRelationHalvesTest { + private FactionDomainFixture domain; + private RelationType leaderSide; + private RelationType subjectSide; + private RelationType agreement; + private RelationType embargo; + + @BeforeEach + void setUp() { + domain = new FactionDomainFixture(); + agreement = + domain.relationType( + "trade_agreement", + Map.of( + "trade-agreement", true, + "mutual", true, + "trade-effects-us", List.of("trade_power(15.0)", "production(5.0)"))); + leaderSide = + domain.relationType( + "unequal_treaty_leader", + Map.of( + "trade-agreement", true, + "mutual", true, + "link", "unequal_treaty_subject", + "trade-effects-us", List.of("trade_power(20.0)", "production(7.5)"))); + subjectSide = + domain.relationType( + "unequal_treaty_subject", + Map.of( + "trade-agreement", true, + "lock", true, + "trade-effects-us", List.of("trade_power(5.0)", "production(2.5)"))); + embargo = + domain.relationType( + "embargo", + Map.of( + "trade-agreement", true, + "trade-effects-them", List.of("trade_power(-95.0)", "production(-95.0)"))); + } + + @AfterEach + void tearDown() { + domain.close(); + } + + @Test + void unequalTreatyGivesTheLeaderItsBonusInTheWeakerSidesLand() { + Faction leader = domain.saved("leader", "LeaderPlayer"); + Faction weaker = domain.saved("weaker", "WeakerPlayer"); + leader.getDiplomacyHandler().setTradeRelation(weaker, leaderSide); + weaker.getDiplomacyHandler().setTradeRelation(leader, subjectSide); + + // The leader's guilds trading in the weaker side's provinces. + assertEquals( + Map.of(FactionModifiers.TRADE_POWER, 20.0, FactionModifiers.PRODUCTION, 7.5), + amounts(weaker.getDiplomacyHandler().getTradeModifiersFor(leader))); + // The weaker side's guilds trading in the leader's provinces. + assertEquals( + Map.of(FactionModifiers.TRADE_POWER, 5.0, FactionModifiers.PRODUCTION, 2.5), + amounts(leader.getDiplomacyHandler().getTradeModifiersFor(weaker))); + assertTrue( + weaker.getDiplomacyHandler().getTradeModifiersFor(leader).stream() + .allMatch(modifier -> modifier.getFrom() == leader)); + } + + @Test + void tradeAgreementStillGivesEachSideItsBonusOnce() { + Faction first = domain.saved("first", "FirstPlayer"); + Faction second = domain.saved("second", "SecondPlayer"); + first.getDiplomacyHandler().setTradeRelation(second, agreement); + second.getDiplomacyHandler().setTradeRelation(first, agreement); + + Map expected = + Map.of(FactionModifiers.TRADE_POWER, 15.0, FactionModifiers.PRODUCTION, 5.0); + assertEquals(expected, amounts(first.getDiplomacyHandler().getTradeModifiersFor(second))); + assertEquals(expected, amounts(second.getDiplomacyHandler().getTradeModifiersFor(first))); + } + + @Test + void embargoStillHitsOnlyTheEmbargoedSide() { + Faction embargoer = domain.saved("embargoer", "EmbargoPlayer"); + Faction target = domain.saved("target", "TargetPlayer"); + embargoer.getDiplomacyHandler().setTradeRelation(target, embargo); + + assertEquals( + Map.of(FactionModifiers.TRADE_POWER, -95.0, FactionModifiers.PRODUCTION, -95.0), + amounts(embargoer.getDiplomacyHandler().getTradeModifiersFor(target))); + assertTrue(target.getDiplomacyHandler().getTradeModifiersFor(embargoer).isEmpty()); + } + + @Test + void noRelationOrUnknownFactionGivesNothing() { + Faction first = domain.saved("first", "FirstPlayer"); + Faction second = domain.saved("second", "SecondPlayer"); + + assertTrue(first.getDiplomacyHandler().getTradeModifiersFor(second).isEmpty()); + assertTrue(first.getDiplomacyHandler().getTradeModifiersFor((Faction) null).isEmpty()); + assertTrue(first.getDiplomacyHandler().getTradeModifiersFor("unknown").isEmpty()); + } + + private static Map amounts(List modifiers) { + return modifiers.stream() + .collect(Collectors.toMap(FactionModifier::getType, FactionModifier::getAmount, Double::sum)); + } +} From c5fe44c1e1911daf91c275495ef13f8f894b6647 Mon Sep 17 00:00:00 2001 From: XxFran10xX <318299142+XxFran10xX@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:12:40 +0100 Subject: [PATCH 2/3] fix(diplomacy): only pair the two halves when they belong to one relation An embargo set over the other side's agreement leaves that side's old half in place; adding it softened the embargo. Unpaired records keep the owner-only reading, so only a matching pair (mutual type and its link) reads both halves. Co-Authored-By: Claude Opus 5.5 --- .../diplomacy/DiplomacyHandler.java | 30 +++++++++++++------ .../diplomacy/TradeRelationHalvesTest.java | 29 ++++++++++++++++++ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/tfminecraft/simplefactions/diplomacy/DiplomacyHandler.java b/src/main/java/net/tfminecraft/simplefactions/diplomacy/DiplomacyHandler.java index f6a75e3b..a55c8580 100644 --- a/src/main/java/net/tfminecraft/simplefactions/diplomacy/DiplomacyHandler.java +++ b/src/main/java/net/tfminecraft/simplefactions/diplomacy/DiplomacyHandler.java @@ -200,22 +200,34 @@ public List getTradeModifiersFor(Faction visitor) { private List tradeModifiersFor(String target, Faction visitor) { List mods = new ArrayList<>(); RelationType ours = getTradeRelation(target); - if (ours != null) { - for (FactionModifier mod : ours.getTradeEffectsThemRaw()) { - mods.add(new FactionModifier(visitor, mod)); - } - } RelationType theirs = visitor == null || visitor.getDiplomacyHandler() == null || f == null ? null : visitor.getDiplomacyHandler().getTradeRelation(f.getId()); - if (theirs != null) { - for (FactionModifier mod : theirs.getTradeEffectsUsRaw()) { - mods.add(new FactionModifier(visitor, mod)); - } + List effects = new ArrayList<>(); + if (ours != null && theirs != null && sameRelation(ours, theirs)) { + effects.addAll(ours.getTradeEffectsThemRaw()); + effects.addAll(theirs.getTradeEffectsUsRaw()); + } else if (ours != null) { + // Unpaired records (an embargo over the other side's old agreement, a missing half) keep the + // owner-only reading, so a stale half never softens an embargo. + effects.addAll(ours.getTradeEffectsThem()); + } + for (FactionModifier mod : effects) { + mods.add(new FactionModifier(visitor, mod)); } return mods; } + /** True when the two records are the two halves of one relation, as {@code RelationManager} sets them. */ + private static boolean sameRelation(RelationType ours, RelationType theirs) { + return (ours.isMutual() && linksTo(ours, theirs)) || (theirs.isMutual() && linksTo(theirs, ours)); + } + + private static boolean linksTo(RelationType type, RelationType other) { + String link = type.getLinkString(); + return link != null && other.getId() != null && link.equalsIgnoreCase(other.getId()); + } + public List getModifiers() { List mods = new ArrayList<>(); for(Map.Entry entry : relations.entrySet()) { diff --git a/src/test/java/net/tfminecraft/simplefactions/diplomacy/TradeRelationHalvesTest.java b/src/test/java/net/tfminecraft/simplefactions/diplomacy/TradeRelationHalvesTest.java index 8f8d124e..3c5ada21 100644 --- a/src/test/java/net/tfminecraft/simplefactions/diplomacy/TradeRelationHalvesTest.java +++ b/src/test/java/net/tfminecraft/simplefactions/diplomacy/TradeRelationHalvesTest.java @@ -108,6 +108,35 @@ void embargoStillHitsOnlyTheEmbargoedSide() { assertTrue(target.getDiplomacyHandler().getTradeModifiersFor(embargoer).isEmpty()); } + @Test + void embargoOverTheOtherSidesAgreementKeepsItsFullForceAndOldResults() { + Faction kept = domain.saved("kept", "KeptPlayer"); + Faction embargoer = domain.saved("embargoer", "EmbargoPlayer"); + // Only the embargoing side's record changes; the other side keeps its old agreement half. + kept.getDiplomacyHandler().setTradeRelation(embargoer, agreement); + embargoer.getDiplomacyHandler().setTradeRelation(kept, embargo); + + assertEquals( + Map.of(FactionModifiers.TRADE_POWER, -95.0, FactionModifiers.PRODUCTION, -95.0), + amounts(embargoer.getDiplomacyHandler().getTradeModifiersFor(kept))); + assertEquals( + Map.of(FactionModifiers.TRADE_POWER, 15.0, FactionModifiers.PRODUCTION, 5.0), + amounts(kept.getDiplomacyHandler().getTradeModifiersFor(embargoer))); + } + + @Test + void leaderEmbargoingTheWeakerSideIsNotSoftenedByTheOldTreatyHalf() { + Faction leader = domain.saved("leader", "LeaderPlayer"); + Faction weaker = domain.saved("weaker", "WeakerPlayer"); + leader.getDiplomacyHandler().setTradeRelation(weaker, embargo); + weaker.getDiplomacyHandler().setTradeRelation(leader, subjectSide); + + assertEquals( + Map.of(FactionModifiers.TRADE_POWER, -95.0, FactionModifiers.PRODUCTION, -95.0), + amounts(leader.getDiplomacyHandler().getTradeModifiersFor(weaker))); + assertTrue(weaker.getDiplomacyHandler().getTradeModifiersFor(leader).isEmpty()); + } + @Test void noRelationOrUnknownFactionGivesNothing() { Faction first = domain.saved("first", "FirstPlayer"); From 6a7d7cef479245bbc95dc2335ffe0298b73939d8 Mon Sep 17 00:00:00 2001 From: XxFran10xX <318299142+XxFran10xX@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:21:53 +0100 Subject: [PATCH 3/3] fix(diplomacy): guard a visiting faction without a diplomacy handler Co-Authored-By: Claude Opus 5.5 --- .../net/tfminecraft/simplefactions/map/provinces/Province.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/tfminecraft/simplefactions/map/provinces/Province.java b/src/main/java/net/tfminecraft/simplefactions/map/provinces/Province.java index 4988d802..c60f65f9 100644 --- a/src/main/java/net/tfminecraft/simplefactions/map/provinces/Province.java +++ b/src/main/java/net/tfminecraft/simplefactions/map/provinces/Province.java @@ -439,7 +439,8 @@ public List getModifiersForGuild(Guild guild) { } // Both halves of a trade relation count: the owner's record and the visitor's own record. if (owner.getDiplomacyHandler().hasTradeRelation(host.getId()) - || host.getDiplomacyHandler().hasTradeRelation(owner.getId())) { + || (host.getDiplomacyHandler() != null + && host.getDiplomacyHandler().hasTradeRelation(owner.getId()))) { mods.addAll(owner.getDiplomacyHandler().getTradeModifiersFor(host)); } } else {