Ejemplo n.º 1
0
  @Override
  public boolean importWorldeditSelection() {
    LocalSession worldeditsession =
        getSelection().getPlugin().getWorldEdit().getSession(getPlayer());
    if (worldeditsession == null) {
      return false;
    }

    Region region;
    try {
      region = worldeditsession.getSelection(worldeditsession.getSelectionWorld());
    } catch (IncompleteRegionException e) {
      return false;
    }

    if (!(region instanceof com.sk89q.worldedit.regions.Polygonal2DRegion)) {
      // getPlayer().sendMessage(ChatColor.RED + "Your worldedit selection is invalid type.");
      return false;
    }

    com.sk89q.worldedit.regions.Polygonal2DRegion npolysel =
        (com.sk89q.worldedit.regions.Polygonal2DRegion) region;
    clearPoints();
    for (BlockVector2D vec : npolysel.getPoints()) {
      addPoint(new ZoneVertice(vec.getBlockX(), vec.getBlockZ()));
    }
    setHeight(
        new ZoneVertice(
            npolysel.getMinimumPoint().getBlockY(), npolysel.getMaximumPoint().getBlockY()),
        true);

    return true;
  }
  public ProtectedPolygonalRegion(String id, List<BlockVector2D> points, int minY, int maxY) {
    super(id);
    this.points = points;
    this.minY = minY;
    this.maxY = maxY;

    int minX = points.get(0).getBlockX();
    int minZ = points.get(0).getBlockZ();
    int maxX = points.get(0).getBlockX();
    int maxZ = points.get(0).getBlockZ();

    for (BlockVector2D v : points) {
      int x = v.getBlockX();
      int z = v.getBlockZ();
      if (x < minX) {
        minX = x;
      }
      if (z < minZ) {
        minZ = z;
      }
      if (x > maxX) {
        maxX = x;
      }
      if (z > maxZ) {
        maxZ = z;
      }
    }

    min = new BlockVector(minX, minY, minZ);
    max = new BlockVector(maxX, maxY, maxZ);
  }
  public boolean selectSecondary(Vector pos) {
    if (region.size() > 0) {
      final List<BlockVector2D> points = region.getPoints();

      final BlockVector2D lastPoint = points.get(region.size() - 1);
      if (lastPoint.getBlockX() == pos.getBlockX() && lastPoint.getBlockZ() == pos.getBlockZ()) {
        return false;
      }

      if (maxPoints >= 0 && points.size() > maxPoints) {
        return false;
      }
    }

    region.addPoint(pos);
    region.expandY(pos.getBlockY());

    return true;
  }
 public Polygonal2DRegionSelector(
     LocalWorld world, List<BlockVector2D> points, int minY, int maxY) {
   final BlockVector2D pos2D = points.get(0);
   pos1 = new BlockVector(pos2D.getX(), minY, pos2D.getZ());
   region = new Polygonal2DRegion(world, points, minY, maxY);
 }
 public void learnChanges() {
   BlockVector2D pt = region.getPoints().get(0);
   pos1 = new BlockVector(pt.getBlockX(), region.getMinimumPoint().getBlockY(), pt.getBlockZ());
 }
  /**
   * Compares all edges of two regions to see if any of them intersect.
   *
   * @param region the region to check
   * @return whether any edges of a region intersect
   */
  protected boolean intersectsEdges(ProtectedRegion region) {
    List<BlockVector2D> pts1 = getPoints();
    List<BlockVector2D> pts2 = region.getPoints();
    BlockVector2D lastPt1 = pts1.get(pts1.size() - 1);
    BlockVector2D lastPt2 = pts2.get(pts2.size() - 1);
    for (BlockVector2D aPts1 : pts1) {
      for (BlockVector2D aPts2 : pts2) {

        Line2D line1 =
            new Line2D.Double(
                lastPt1.getBlockX(), lastPt1.getBlockZ(), aPts1.getBlockX(), aPts1.getBlockZ());

        if (line1.intersectsLine(
            lastPt2.getBlockX(), lastPt2.getBlockZ(), aPts2.getBlockX(), aPts2.getBlockZ())) {
          return true;
        }
        lastPt2 = aPts2;
      }
      lastPt1 = aPts1;
    }
    return false;
  }
 /**
  * Check to see if a position is contained within this region.
  *
  * @param position the position to check
  * @return whether {@code position} is in this region
  */
 public boolean contains(BlockVector2D position) {
   checkNotNull(position);
   return contains(new Vector(position.getBlockX(), min.getBlockY(), position.getBlockZ()));
 }
  @Override
  public List<ProtectedRegion> getIntersectingRegions(List<ProtectedRegion> regions)
      throws UnsupportedIntersectionException {
    int numRegions = regions.size();
    int numPoints = points.size();
    List<ProtectedRegion> intersectingRegions = new ArrayList<ProtectedRegion>();
    int i, i2, i3;

    for (i = 0; i < numRegions; i++) {
      ProtectedRegion region = regions.get(i);
      BlockVector rMinPoint = region.getMinimumPoint();
      BlockVector rMaxPoint = region.getMaximumPoint();

      // Check whether the region is outside the min and max vector
      if ((rMinPoint.getBlockX() < min.getBlockX() && rMaxPoint.getBlockX() < min.getBlockX())
          || (rMinPoint.getBlockX() > max.getBlockX() && rMaxPoint.getBlockX() > max.getBlockX())
              && ((rMinPoint.getBlockY() < min.getBlockY()
                      && rMaxPoint.getBlockY() < min.getBlockY())
                  || (rMinPoint.getBlockY() > max.getBlockY()
                      && rMaxPoint.getBlockY() > max.getBlockY()))
              && ((rMinPoint.getBlockZ() < min.getBlockZ()
                      && rMaxPoint.getBlockZ() < min.getBlockZ())
                  || (rMinPoint.getBlockZ() > max.getBlockZ()
                      && rMaxPoint.getBlockZ() > max.getBlockZ()))) {
        intersectingRegions.add(regions.get(i));
        continue;
      }

      // Check whether the regions points are inside the other region
      for (i2 = 0; i < numPoints; i++) {
        Vector pt = new Vector(points.get(i2).getBlockX(), minY, points.get(i2).getBlockZ());
        Vector pt2 = new Vector(points.get(i2).getBlockX(), maxY, points.get(i2).getBlockZ());
        if (region.contains(pt) || region.contains(pt2)) {
          intersectingRegions.add(regions.get(i));
          continue;
        }
      }

      // Check whether the other regions points are inside the current region
      if (region instanceof ProtectedPolygonalRegion) {
        for (i2 = 0; i < ((ProtectedPolygonalRegion) region).getPoints().size(); i++) {
          BlockVector2D pt2Dr = ((ProtectedPolygonalRegion) region).getPoints().get(i2);
          int minYr = ((ProtectedPolygonalRegion) region).minY;
          int maxYr = ((ProtectedPolygonalRegion) region).maxY;
          Vector ptr = new Vector(pt2Dr.getBlockX(), minYr, pt2Dr.getBlockZ());
          Vector ptr2 = new Vector(pt2Dr.getBlockX(), maxYr, pt2Dr.getBlockZ());

          if (this.contains(ptr) || this.contains(ptr2)) {
            intersectingRegions.add(regions.get(i));
            continue;
          }
        }
      } else if (region instanceof ProtectedCuboidRegion) {
        BlockVector ptcMin = region.getMinimumPoint();
        BlockVector ptcMax = region.getMaximumPoint();

        if (this.contains(new Vector(ptcMin.getBlockX(), ptcMin.getBlockY(), ptcMin.getBlockZ()))
            || this.contains(new Vector(ptcMin.getBlockX(), ptcMin.getBlockY(), ptcMax.getBlockZ()))
            || this.contains(new Vector(ptcMin.getBlockX(), ptcMax.getBlockY(), ptcMax.getBlockZ()))
            || this.contains(new Vector(ptcMin.getBlockX(), ptcMax.getBlockY(), ptcMin.getBlockZ()))
            || this.contains(new Vector(ptcMax.getBlockX(), ptcMax.getBlockY(), ptcMax.getBlockZ()))
            || this.contains(new Vector(ptcMax.getBlockX(), ptcMax.getBlockY(), ptcMin.getBlockZ()))
            || this.contains(new Vector(ptcMax.getBlockX(), ptcMin.getBlockY(), ptcMin.getBlockZ()))
            || this.contains(
                new Vector(ptcMax.getBlockX(), ptcMin.getBlockY(), ptcMax.getBlockZ()))) {
          intersectingRegions.add(regions.get(i));
          continue;
        }
      } else {
        throw new UnsupportedOperationException("Not supported yet.");
      }

      // Check whether the current regions edges collide with the regions edges
      boolean regionIsIntersecting = false;
      for (i2 = 0; i2 < numPoints; i2++) {
        boolean checkNextPoint = false;
        BlockVector2D currPoint = points.get(i2);
        BlockVector2D nextPoint;

        if (i2 == (numPoints - 1)) {
          nextPoint = points.get(0);
        } else {
          nextPoint = points.get(i2 + 1);
        }

        int currX = currPoint.getBlockX();
        int currZ = currPoint.getBlockZ();

        while (!checkNextPoint) {
          for (i3 = this.minY; i3 <= this.maxY; i3++) {
            if (region.contains(new Vector(currX, i3, currZ))) {
              intersectingRegions.add(regions.get(i));
              regionIsIntersecting = true;
              break;
            }
          }

          if (currX == nextPoint.getBlockX()
              || currZ == nextPoint.getBlockZ()
              || regionIsIntersecting) {
            checkNextPoint = true;
          }

          if (nextPoint.getBlockX() > currPoint.getBlockX()) {
            currX++;
          } else {
            currX--;
          }
          if (nextPoint.getBlockZ() > currPoint.getBlockZ()) {
            currZ++;
          } else {
            currZ--;
          }
        }

        if (regionIsIntersecting) {
          break;
        }
      }
    }

    return intersectingRegions;
  }