Example #1
0
  public static void performMission(ConfigMission mission, String playerName) {
    Player player;
    try {
      player = CivGlobal.getPlayer(playerName);
    } catch (CivException e1) {
      return;
    }

    try {
      Resident resident = CivGlobal.getResident(playerName);
      if (!resident.getTown().getTreasury().hasEnough(mission.cost)) {
        throw new CivException(
            "Your town requires " + mission.cost + " coins to perform this mission.");
      }

      switch (mission.id) {
        case "spy_investigate_town":
          performInvestigateTown(player, mission);
          break;
        case "spy_steal_treasury":
          performStealTreasury(player, mission);
          break;
        case "spy_incite_riots":
          performInciteRiots(player, mission);
          break;
        case "spy_poison_granary":
          performPosionGranary(player, mission);
          break;
        case "spy_pirate":
          performPirate(player, mission);
          break;
        case "spy_sabotage":
          performSabotage(player, mission);
          break;
      }

    } catch (CivException e) {
      CivMessage.sendError(player, e.getMessage());
    }
  }
Example #2
0
  @Override
  public void onInteract(PlayerInteractEvent event) {

    try {

      if (War.isWarTime()) {
        throw new CivException("Cannot use spy missions during war time.");
      }

      ConfigMission mission = CivSettings.missions.get(this.getId());
      if (mission == null) {
        throw new CivException("Unknown mission " + this.getId());
      }

      Resident resident = CivGlobal.getResident(event.getPlayer());
      if (resident == null || !resident.hasTown()) {
        throw new CivException("Only residents of towns can perform spy missions.");
      }

      Date now = new Date();

      if (!event.getPlayer().isOp()) {
        try {
          int spyRegisterTime =
              CivSettings.getInteger(CivSettings.espionageConfig, "espionage.spy_register_time");
          int spyOnlineTime =
              CivSettings.getInteger(CivSettings.espionageConfig, "espionage.spy_online_time");

          long expire = resident.getRegistered() + (spyRegisterTime * 60 * 1000);
          if (now.getTime() <= expire) {
            throw new CivException(
                "You cannot use a spy yet, you must play CivCraft a bit longer before you can use it.");
          }

          expire = resident.getLastOnline() + (spyOnlineTime * 60 * 1000);
          if (now.getTime() <= expire) {
            throw new CivException("You must be online longer before you can use a spy.");
          }
        } catch (InvalidConfiguration e) {
          e.printStackTrace();
        }
      }

      ConfigUnit unit = Unit.getPlayerUnit(event.getPlayer());
      if (unit == null || !unit.id.equals("u_spy")) {
        event.getPlayer().getInventory().remove(event.getItem());
        throw new CivException("Only spies can use mission books.");
      }

      ChunkCoord coord = new ChunkCoord(event.getPlayer().getLocation());
      CultureChunk cc = CivGlobal.getCultureChunk(coord);
      TownChunk tc = CivGlobal.getTownChunk(coord);

      if (cc == null || cc.getCiv() == resident.getCiv()) {
        throw new CivException(
            "You must be in a civilization's culture that's not your own to spy on them.");
      }

      if ((cc != null && cc.getCiv().isAdminCiv())
          || (tc != null && tc.getTown().getCiv().isAdminCiv())) {
        throw new CivException("You cannot spy on an admin civ.");
      }

      if (CivGlobal.isCasualMode()) {
        if (!cc.getCiv().getDiplomacyManager().isHostileWith(resident.getCiv())
            && !cc.getCiv().getDiplomacyManager().atWarWith(resident.getCiv())) {
          throw new CivException(
              "You must be hostile or at war with "
                  + cc.getCiv().getName()
                  + " in order to perform spy missions in casual mode.");
        }
      }

      resident.setInteractiveMode(
          new InteractiveSpyMission(
              mission, event.getPlayer().getName(), event.getPlayer().getLocation(), cc.getTown()));
    } catch (CivException e) {
      CivMessage.sendError(event.getPlayer(), e.getMessage());
    }
  }