Exemple #1
0
  /* (non-Javadoc)
   * @see com.github.catageek.ByteCart.Signs.Triggable#trigger()
   */
  @Override
  public void trigger() throws ClassNotFoundException, IOException {

    CollisionAvoiderBuilder builder = new RouterCollisionAvoiderBuilder(this, center.getLocation());

    try {

      BlockFace direction, to;
      Router router =
          ByteCart.myPlugin.getCollisionAvoiderManager().<Router>getCollisionAvoider(builder);
      boolean isTrain = AbstractTriggeredSign.isTrain(destination);

      // Here begins the triggered action

      // is this an wanderer who needs special routing ? no then routing normally
      if (selectWanderer()) {

        // if this is a cart in a train
        if (this.wasTrain(this.getLocation())) {

          // leave a message to next cart that it is a train
          ByteCart.myPlugin.getIsTrainManager().getMap().reset(getLocation());
          // tell to router not to change position
          ByteCart.myPlugin
              .getCollisionAvoiderManager()
              .<Router>getCollisionAvoider(builder)
              .Book(isTrain);
          return;
        }

        if (destination != null) {
          // Time-to-live management

          // loading TTl of cart
          int ttl = destination.getTTL();

          // if ttl did not reach end of life ( = 0)
          if (ttl != 0) {

            destination.updateTTL(ttl - 1);
          }

          // if ttl was 1 (now 0), we try to return the cart to source station

          if (ttl == 1 && tryReturnCart())
            destination = AddressFactory.getAddress(this.getInventory());

          if (ByteCart.debug) ByteCart.log.info("ByteCart : TTL is " + destination.getTTL());

          // if this is the first car of a train
          // we keep it during 2 s
          if (isTrain) {
            this.setWasTrain(this.getLocation(), true);
          }

          destination.finalizeAddress();
        }

        direction = this.SelectRoute(destination, Sign, RoutingTable);

        // trigger event
        BlockFace bdest = router.WishToGo(From, direction, isTrain);
        int ring = this.getRoutingTable().getDirectlyConnected(new DirectionRegistry(bdest));
        SignPostRouteEvent event = new SignPostRouteEvent(this, ring);
        Bukkit.getServer().getPluginManager().callEvent(event);

        return;
      }

      // it's a wanderer, so let it choosing direction
      Wanderer wanderer = getWanderer();

      // routing normally
      to = router.WishToGo(From, wanderer.giveRouterDirection(), isTrain);

      if (WandererContentFactory.isWanderer(getInventory(), "Updater")) {
        int nextring = this.getRoutingTable().getDirectlyConnected(new DirectionRegistry(to));
        UpdaterPassRouterEvent event = new UpdaterPassRouterEvent(wanderer, to, nextring);
        Bukkit.getServer().getPluginManager().callEvent(event);
      }

      // here we perform routes update
      wanderer.doAction(to);

    } catch (ClassCastException e) {
      if (ByteCart.debug) ByteCart.log.info("ByteCart : " + e.toString());
      e.printStackTrace();

      // Not the good blocks to build the signs
      return;
    } catch (NullPointerException e) {
      if (ByteCart.debug) ByteCart.log.info("ByteCart : " + e.toString());

      e.printStackTrace();

      // there was no inventory in the cart
      return;
    }
  }