private static void getNeutralOutOfWarWithAllies(
      final PoliticalActionAttachment paa, final PlayerID player, final IDelegateBridge aBridge) {
    final GameData data = aBridge.getData();
    if (!games.strategy.triplea.Properties.getAlliancesCanChainTogether(data)) {
      return;
    }

    final Collection<PlayerID> players = data.getPlayerList().getPlayers();
    final Collection<PlayerID> p1AlliedWith =
        Match.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(player, data));
    final CompositeChange change = new CompositeChange();
    for (final String relationshipChangeString : paa.getRelationshipChange()) {
      final String[] relationshipChange = relationshipChangeString.split(":");
      final PlayerID p1 = data.getPlayerList().getPlayerID(relationshipChange[0]);
      final PlayerID p2 = data.getPlayerList().getPlayerID(relationshipChange[1]);
      if (!(p1.equals(player) || p2.equals(player))) {
        continue;
      }
      final PlayerID pOther = (p1.equals(player) ? p2 : p1);
      final RelationshipType currentType =
          data.getRelationshipTracker().getRelationshipType(p1, p2);
      final RelationshipType newType =
          data.getRelationshipTypeList().getRelationshipType(relationshipChange[2]);
      if (Matches.RelationshipTypeIsAtWar.match(currentType)
          && Matches.RelationshipTypeIsAtWar.invert().match(newType)) {
        final Collection<PlayerID> pOtherAlliedWith =
            Match.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(pOther, data));
        if (!pOtherAlliedWith.contains(pOther)) {
          pOtherAlliedWith.add(pOther);
        }
        if (!p1AlliedWith.contains(player)) {
          p1AlliedWith.add(player);
        }
        for (final PlayerID p3 : p1AlliedWith) {
          for (final PlayerID p4 : pOtherAlliedWith) {
            final RelationshipType currentOther =
                data.getRelationshipTracker().getRelationshipType(p3, p4);
            if (!currentOther.equals(newType)
                && Matches.RelationshipTypeIsAtWar.match(currentOther)) {
              change.add(ChangeFactory.relationshipChange(p3, p4, currentOther, newType));
              aBridge
                  .getHistoryWriter()
                  .addChildToEvent(
                      p3.getName()
                          + " and "
                          + p4.getName()
                          + " sign a "
                          + newType.getName()
                          + " treaty");
              MoveDelegate.getBattleTracker(data)
                  .addRelationshipChangesThisTurn(p3, p4, currentOther, newType);
            }
          }
        }
      }
    }
    if (!change.isEmpty()) {
      aBridge.addChange(change);
    }
  }
 /**
  * Changes all relationships
  *
  * @param paa the political action to change the relationships for
  */
 private void changeRelationships(final PoliticalActionAttachment paa) {
   getMyselfOutOfAlliance(paa, m_player, m_bridge);
   getNeutralOutOfWarWithAllies(paa, m_player, m_bridge);
   final CompositeChange change = new CompositeChange();
   for (final String relationshipChange : paa.getRelationshipChange()) {
     final String[] s = relationshipChange.split(":");
     final PlayerID player1 = getData().getPlayerList().getPlayerID(s[0]);
     final PlayerID player2 = getData().getPlayerList().getPlayerID(s[1]);
     final RelationshipType oldRelation =
         getData().getRelationshipTracker().getRelationshipType(player1, player2);
     final RelationshipType newRelation =
         getData().getRelationshipTypeList().getRelationshipType(s[2]);
     if (oldRelation.equals(newRelation)) {
       continue;
     }
     change.add(ChangeFactory.relationshipChange(player1, player2, oldRelation, newRelation));
     m_bridge
         .getHistoryWriter()
         .addChildToEvent(
             m_bridge.getPlayerID().getName()
                 + " succeeds on action: "
                 + MyFormatter.attachmentNameToText(paa.getName())
                 + ": Changing Relationship for "
                 + player1.getName()
                 + " and "
                 + player2.getName()
                 + " from "
                 + oldRelation.getName()
                 + " to "
                 + newRelation.getName());
     MoveDelegate.getBattleTracker(getData())
         .addRelationshipChangesThisTurn(player1, player2, oldRelation, newRelation);
     /*
      * creation of new battles is handled at the beginning of the battle delegate, in
      * "setupUnitsInSameTerritoryBattles", not here.
      * if (Matches.RelationshipTypeIsAtWar.match(newRelation))
      * TriggerAttachment.triggerMustFightBattle(player1, player2, m_bridge);
      */
   }
   if (!change.isEmpty()) {
     m_bridge.addChange(change);
   }
   chainAlliancesTogether(m_bridge);
 }
 public static void chainAlliancesTogether(final IDelegateBridge aBridge) {
   final GameData data = aBridge.getData();
   if (!games.strategy.triplea.Properties.getAlliancesCanChainTogether(data)) {
     return;
   }
   final Collection<RelationshipType> allTypes =
       data.getRelationshipTypeList().getAllRelationshipTypes();
   RelationshipType alliedType = null;
   RelationshipType warType = null;
   for (final RelationshipType type : allTypes) {
     if (type.getRelationshipTypeAttachment().getIsDefaultWarPosition()) {
       warType = type;
     } else if (type.getRelationshipTypeAttachment().getAlliancesCanChainTogether()) {
       alliedType = type;
     }
   }
   if (alliedType == null) {
     return;
   }
   // first do alliances. then, do war (since we don't want to declare war on a potential ally).
   final Collection<PlayerID> players = data.getPlayerList().getPlayers();
   for (final PlayerID p1 : players) {
     final HashSet<PlayerID> p1NewAllies = new HashSet<PlayerID>();
     final Collection<PlayerID> p1AlliedWith =
         Match.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(p1, data));
     for (final PlayerID p2 : p1AlliedWith) {
       p1NewAllies.addAll(
           Match.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(p2, data)));
     }
     p1NewAllies.removeAll(p1AlliedWith);
     p1NewAllies.remove(p1);
     for (final PlayerID p3 : p1NewAllies) {
       if (!data.getRelationshipTracker().getRelationshipType(p1, p3).equals(alliedType)) {
         final RelationshipType current =
             data.getRelationshipTracker().getRelationshipType(p1, p3);
         aBridge.addChange(ChangeFactory.relationshipChange(p1, p3, current, alliedType));
         aBridge
             .getHistoryWriter()
             .addChildToEvent(
                 p1.getName()
                     + " and "
                     + p3.getName()
                     + " are joined together in an "
                     + alliedType.getName()
                     + " treaty");
         MoveDelegate.getBattleTracker(data)
             .addRelationshipChangesThisTurn(p1, p3, current, alliedType);
       }
     }
   }
   // now war
   if (warType == null) {
     return;
   }
   for (final PlayerID p1 : players) {
     final HashSet<PlayerID> p1NewWar = new HashSet<PlayerID>();
     final Collection<PlayerID> p1WarWith = Match.getMatches(players, Matches.isAtWar(p1, data));
     final Collection<PlayerID> p1AlliedWith =
         Match.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(p1, data));
     for (final PlayerID p2 : p1AlliedWith) {
       p1NewWar.addAll(Match.getMatches(players, Matches.isAtWar(p2, data)));
     }
     p1NewWar.removeAll(p1WarWith);
     p1NewWar.remove(p1);
     for (final PlayerID p3 : p1NewWar) {
       if (!data.getRelationshipTracker().getRelationshipType(p1, p3).equals(warType)) {
         final RelationshipType current =
             data.getRelationshipTracker().getRelationshipType(p1, p3);
         aBridge.addChange(ChangeFactory.relationshipChange(p1, p3, current, warType));
         aBridge
             .getHistoryWriter()
             .addChildToEvent(
                 p1.getName()
                     + " and "
                     + p3.getName()
                     + " declare "
                     + warType.getName()
                     + " on each other");
         MoveDelegate.getBattleTracker(data)
             .addRelationshipChangesThisTurn(p1, p3, current, warType);
       }
     }
   }
 }