ScruffyRules

Untitled

Sep 26th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public void farm(Block block) {
  2.     this.area++;
  3.     this.lastFarm = System.currentTimeMillis();
  4.     this.first = false;
  5.     state.addBlockToRegen(block);
  6.     getColor().getBlock().apply(block);
  7.     List < LinkedList < Block >> tracks = new ArrayList < LinkedList < Block >> ();
  8.     List < LinkedList < Block >> closedPolygons = new ArrayList < LinkedList < Block >> ();
  9.  
  10.     tracks.add(new LinkedList < Block > (Arrays.asList(block)));
  11.  
  12.     while (tracks.size() != 0) {
  13.         LinkedList < Block > track = tracks.get(0);
  14.         Block currentBlock = track.getLast();
  15.  
  16.         List < BlockFace > matchingFaces = new ArrayList < BlockFace > ();
  17.  
  18.         for (BlockFace face: Arrays.asList(new BlockFace[] {
  19.             BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST
  20.         }))
  21.         if (getColor().getBlock().match(currentBlock.getRelative(face))) matchingFaces.add(face);
  22.  
  23.         int index;
  24.         if ((index = track.indexOf(currentBlock)) > 0) matchingFaces.remove(currentBlock.getFace(track.get(index - 1)));
  25.         else if (matchingFaces.size() < 2) return;
  26.  
  27.         if (matchingFaces.size() == 0) {
  28.             tracks.remove(track);
  29.             continue;
  30.         }
  31.  
  32.         for (index = 0; index < matchingFaces.size(); index++) {
  33.             if (track.contains(currentBlock.getRelative(matchingFaces.get(index)))) {
  34.                 if (currentBlock.getRelative(matchingFaces.get(index)).equals(block)) closedPolygons.add(track);
  35.                 tracks.remove(track); //crash ici
  36.                 continue;
  37.             }
  38.  
  39.             if (index == matchingFaces.size() - 1) {
  40.                 track.add(currentBlock.getRelative(matchingFaces.get(index)));
  41.                 break;
  42.             }
  43.  
  44.             LinkedList < Block > newTrack = new LinkedList < Block > (track);
  45.             newTrack.add(currentBlock.getRelative(matchingFaces.get(index)));
  46.             tracks.add(newTrack);
  47.         }
  48.     }
  49.  
  50.     getPlayer().sendMessage("Polygons: " + closedPolygons.size());
  51.  
  52.     for (LinkedList < Block > polygon: closedPolygons) {
  53.         int xPos[] = new int[polygon.size()], zPos[] = new int[polygon.size()];
  54.  
  55.         for (int i = 0; i < polygon.size(); i++) {
  56.             xPos[i] = polygon.get(i).getX();
  57.             zPos[i] = polygon.get(i).getZ();
  58.         }
  59.  
  60.         Polygon poly = new Polygon(xPos, zPos, polygon.size());
  61.  
  62.         List < Block > contained = new ArrayList < Block > ();
  63.  
  64.         int minX = MathUtil.getMin(xPos), maxX = MathUtil.getMax(xPos) + 1;
  65.         int minZ = MathUtil.getMin(zPos), maxZ = MathUtil.getMax(zPos) + 1;
  66.  
  67.         for (int x = minX; x < maxX; x++)
  68.         for (int z = minZ; z < maxZ; z++)
  69.         if (poly.contains(x, z)) contained.add(block.getWorld().getBlockAt(x, block.getY(), z));
  70.  
  71.         boolean doContinue = false;
  72.  
  73.         for (Block containedBlock: contained) {
  74.             if (!((BlockFarmersConfig) state.getGame().getConfig()).canFarm(containedBlock)) {
  75.                 doContinue = true;
  76.                 break;
  77.             }
  78.         }
  79.  
  80.         getPlayer().sendMessage("  Blocks: " + contained.size());
  81.  
  82.         if (doContinue) continue;
  83.  
  84.         for (Block containedBlock: contained) {
  85.             state.addBlockToRegen(containedBlock);
  86.             getColor().getBlock().apply(containedBlock);
  87.         }
  88.     }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment