// main
 public static String getXYZSafeZone(Location l, String world) {
   String r = "";
   // ResultSet rs =
   // tekkitrestrict.db.query("SELECT * FROM `tr_safezones` WHERE `world` = '"+world+"'");
   /*
    * if(tekkitrestrict.config.getBoolean("UseSafeZones")){ //determine if
    * the player is in the 3D cube. for(int i=0;i<zones.size();i++){
    * safeZone rs = zones.get(i); if(world.equals(rs.world)){ int x1 =
    * rs.x1; int y1 = rs.y1; int z1 = rs.z1; int x2 = rs.x2; int y2 =
    * rs.y2; int z2 = rs.z2; double x = l.getX(); double y= l.getY();
    * double z = l.getZ(); boolean it = x >= x1 && x < x2 + 1 && y >= y1 &&
    * y < y2 + 1 && z >= z1 && z < z2 + 1; if(it){ return rs.name; } } } }
    */
   if (tekkitrestrict.config.getBoolean("UseSafeZones")) {
     for (int i = 0; i < zones.size(); i++) {
       TRSafeZone a = zones.get(i);
       if (a.mode == 0) {
         // do nothing... (for now)
       } else if (a.mode == 1) { // WorldGuard!
         // determine whether in WG region or not...
         // com.sk89q.worldguard.protection.regions.ProtectedRegion
         Plugin plugin =
             tekkitrestrict.getInstance().getServer().getPluginManager().getPlugin("WorldGuard");
         try {
           // WorldGuard may not be loaded
           if (plugin != null
               && (plugin instanceof com.sk89q.worldguard.bukkit.WorldGuardPlugin)) {
             com.sk89q.worldguard.bukkit.WorldGuardPlugin WGB =
                 (com.sk89q.worldguard.bukkit.WorldGuardPlugin) plugin;
             com.sk89q.worldguard.protection.regions.ProtectedRegion PR =
                 WGB.getRegionManager(tekkitrestrict.getInstance().getServer().getWorld(a.world))
                     .getRegion(a.name);
             if (PR.contains(l.getBlockX(), l.getBlockY(), l.getBlockZ())) {
               return a.name;
             }
           }
         } catch (Exception E) {
           // E.printStackTrace();
         }
       }
     }
     return r;
   } else {
     return "";
   }
 }
  @Override
  public ApplicableRegionSet getApplicableRegions(Vector pt) {
    TreeSet<ProtectedRegion> appRegions = new TreeSet<ProtectedRegion>();

    for (ProtectedRegion region : regions.values()) {
      if (region.contains(pt)) {
        appRegions.add(region);

        ProtectedRegion parent = region.getParent();

        while (parent != null) {
          if (!appRegions.contains(parent)) {
            appRegions.add(parent);
          }

          parent = parent.getParent();
        }
      }
    }

    return new ApplicableRegionSet(appRegions, regions.get("__global__"));
  }
  @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;
  }