Guest User

Untitled

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