示例#1
0
  /**
   * Compute the direction to take
   *
   * @param IPaddress the destination address
   * @param sign the BC sign
   * @param RoutingTableWritable the routing table contained in the chest
   * @return the direction to destination, or to ring 0. If ring 0 does not exist, random direction
   */
  protected BlockFace SelectRoute(
      AddressRouted IPaddress, Address sign, RoutingTableWritable RoutingTable) {

    DirectionRegistry face;
    // same region : lookup destination track
    if (IPaddress != null
        && IPaddress.getRegion().getAmount() == sign.getRegion().getAmount()
        && IPaddress.getTTL() != 0) {
      int destination = this.destination.getTrack().getAmount();
      DirectionRegistry out = RoutingTable.getDirection(destination);
      if (out != null) {
        // trigger event
        SignPreRouteEvent event =
            new SignPreRouteEvent(this, this.getRoutingTable().getDirectlyConnected(out));
        Bukkit.getServer().getPluginManager().callEvent(event);
        return RoutingTable.getDirection(event.getTargetTrack()).getBlockFace();
      }
    }

    // If not in same region, or if TTL is 0, or the ring does not exist then we lookup track 0
    if ((face = RoutingTable.getDirection(0)) != null) return face.getBlockFace();

    // If everything has failed, then we randomize output direction
    return AbstractWanderer.getRandomBlockFace(RoutingTable, getCardinal().getOppositeFace());
  }
示例#2
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;
    }
  }
示例#3
0
 /* (non-Javadoc)
  * @see com.github.catageek.ByteCart.Signs.BCSign#getDestinationIP()
  */
 @Override
 public final String getDestinationIP() {
   return destination.toString();
 }