public static MinecartMember getAt(Block railblock, boolean checkmoving) {
   if (railblock == null) return null;
   return getAt(
       WorldUtil.getNative(railblock.getWorld()),
       BlockUtil.getCoordinates(railblock),
       checkmoving);
 }
Exemple #2
0
  private double calcHorizontalLength() {
    double length = 0.0;
    // Count the amount of horizontal tracks
    final BlockFace[] toCheck;
    if (this.instruction == BlockFace.SELF) {
      toCheck = FaceUtil.getFaces(this.railDirection);
    } else {
      toCheck = new BlockFace[] {this.instruction};
    }
    for (BlockFace face : toCheck) {
      int tlength = 0;
      // Get the type of rail required
      BlockFace checkface = face;
      if (checkface == BlockFace.NORTH) checkface = BlockFace.SOUTH;
      if (checkface == BlockFace.EAST) checkface = BlockFace.WEST;

      Block b = this.railsBlock;
      for (int i = 0; i < 20; i++) {
        // Next until invalid
        b = b.getRelative(face);
        Rails rr = BlockUtil.getRails(b);
        if (rr == null || rr.getDirection() != checkface) {
          break;
        }
        tlength++;
      }
      // Update the length
      if (tlength > length) {
        length = tlength;
      }
    }
    return length;
  }
Exemple #3
0
 public boolean find(Block rail, int maxstepcount) {
   Block next;
   for (; maxstepcount > 0; --maxstepcount) {
     next = this.next();
     if (next == null) return false;
     if (BlockUtil.equals(rail, next)) return true;
   }
   return false;
 }
 /**
  * Ignores signs of current-tick redstone changes caused by the lever
  *
  * @param lever to ignore
  */
 public void ignoreOutputLever(Block lever) {
   // Ignore signs that are attached to the block the lever is attached to
   Block att = BlockUtil.getAttachedBlock(lever);
   for (BlockFace face : FaceUtil.ATTACHEDFACES) {
     Block signblock = att.getRelative(face);
     if (MaterialUtil.ISSIGN.get(signblock)
         && BlockUtil.getAttachedFace(signblock) == face.getOppositeFace()) {
       if (ignoredSigns.isEmpty()) {
         // clear this the next tick
         CommonUtil.nextTick(
             new Runnable() {
               public void run() {
                 ignoredSigns.clear();
               }
             });
       }
       ignoredSigns.add(signblock);
     }
   }
 }
Exemple #5
0
  private double calcDiagonalLength() {
    double length = 0.0;
    // Count the amount of zig-zagging curved tracks
    final BlockFace[] toCheck;
    if (this.instruction == BlockFace.SELF) {
      toCheck =
          new BlockFace[] {
            FaceUtil.rotate(this.railDirection, -2), FaceUtil.rotate(this.railDirection, 2)
          };
    } else {
      toCheck = new BlockFace[] {this.instruction};
    }
    for (BlockFace direction : toCheck) {
      double tlength = 0.0;
      // Find out the starting offset
      final BlockFace[] dirs = FaceUtil.getFaces(direction);
      BlockFace[] railDirs = FaceUtil.getFaces(this.railDirection);
      BlockFace railDirection = this.railDirection;

      Block b = this.railsBlock;
      for (int i = 0; i < 20; i++) {
        // Invert the direction
        railDirection = railDirection.getOppositeFace();
        railDirs[0] = railDirs[0].getOppositeFace();
        railDirs[1] = railDirs[1].getOppositeFace();
        // Obtain the new offset
        final BlockFace offset;
        if (LogicUtil.contains(railDirs[0], dirs)) {
          offset = railDirs[0];
        } else {
          offset = railDirs[1];
        }
        // Check if the new block is the expected curve direction
        b = b.getRelative(offset);
        Rails rr = BlockUtil.getRails(b);
        if (rr == null || rr.getDirection() != railDirection) {
          break;
        }
        tlength += MathUtil.HALFROOTOFTWO;
      }

      // Update the length
      if (tlength > length) {
        length = tlength;
      }
    }
    return length;
  }
 @EventHandler(priority = EventPriority.HIGHEST)
 public void onBlockRedstoneChange(BlockRedstoneEvent event) {
   if (TrainCarts.isWorldDisabled(event)) {
     return;
   }
   Material type = event.getBlock().getType();
   if (BlockUtil.isType(type, Material.LEVER)) {
     Block up = event.getBlock().getRelative(BlockFace.UP);
     Block down = event.getBlock().getRelative(BlockFace.DOWN);
     if (MaterialUtil.ISSIGN.get(up)) {
       updateRedstonePower(up, event.getNewCurrent() > 0);
     }
     if (MaterialUtil.ISSIGN.get(down)) {
       updateRedstonePower(down, event.getNewCurrent() > 0);
     }
     ignoreOutputLever(event.getBlock());
   } else if (MaterialUtil.ISSIGN.get(type)) {
     updateRedstonePower(event.getBlock(), event.getNewCurrent() > 0);
   }
 }
  @SuppressWarnings("rawtypes")
  public static MinecartMember getAt(World world, ChunkCoordinates coord, boolean checkmoving) {
    net.minecraft.server.Chunk chunk = WorldUtil.getChunk(world, coord.x >> 4, coord.z >> 4);
    if (chunk != null) {
      MinecartMember mm;
      MinecartMember result = null;
      for (List list : chunk.entitySlices) {
        for (Object e : list) {
          if (e instanceof MinecartMember) {
            mm = (MinecartMember) e;
            if (mm.getBlockX() != coord.x) continue;
            if (mm.getBlockY() != coord.y) continue;
            if (mm.getBlockZ() != coord.z) continue;
            result = mm;
            if (result.isHeadingTo(coord)) return result;
          }
        }
      }
      if (result == null && checkmoving) {
        Block b = world.getWorld().getBlockAt(coord.x, coord.y, coord.z);
        int id = b.getTypeId();

        // get the two connected rails to check
        if (BlockUtil.isRails(id)) {
          BlockFace[] possible = FaceUtil.getFaces(BlockUtil.getRails(b).getDirection());
          MinecartMember mm1 = getAt(Util.getRailsBlock(b.getRelative(possible[0])), false);
          MinecartMember mm2 = getAt(Util.getRailsBlock(b.getRelative(possible[1])), false);
          if (mm1 != null && mm2 != null && mm1.group == mm2.group) {
            Location loc = b.getLocation();
            return mm1.distance(loc) < mm2.distance(loc) ? mm1 : mm2;
          } else if (isHeadingTo(mm1, coord)) {
            return mm1;
          } else if (isHeadingTo(mm2, coord)) {
            return mm2;
          } else {
            return null;
          }
        } else if (Util.isPressurePlate(id)) {
          // check all directions
          MinecartMember mm1 = getAt(Util.getRailsBlock(b.getRelative(BlockFace.NORTH)), false);
          MinecartMember mm2 = getAt(Util.getRailsBlock(b.getRelative(BlockFace.SOUTH)), false);
          MinecartMember mm3 = getAt(Util.getRailsBlock(b.getRelative(BlockFace.EAST)), false);
          MinecartMember mm4 = getAt(Util.getRailsBlock(b.getRelative(BlockFace.WEST)), false);
          if (mm1 != null && mm2 != null && mm1.group == mm2.group) {
            Location loc = b.getLocation();
            return mm1.distance(loc) < mm2.distance(loc) ? mm1 : mm2;
          } else if (mm3 != null && mm4 != null && mm3.group == mm4.group) {
            Location loc = b.getLocation();
            return mm3.distance(loc) < mm4.distance(loc) ? mm3 : mm4;
          } else if (isHeadingTo(mm1, coord)) {
            return mm1;
          } else if (isHeadingTo(mm2, coord)) {
            return mm2;
          } else if (isHeadingTo(mm3, coord)) {
            return mm3;
          } else if (isHeadingTo(mm4, coord)) {
            return mm4;
          } else {
            return null;
          }
        }
      }
      return result;
    }
    return null;
  }