Solved Check for oxygen leaks in a room

Discussion in 'Spigot Plugin Development' started by TSEngineer, Jun 13, 2018.

Thread Status:
Not open for further replies.
  1. Stealer !1!!!Q11Q!!Q!"wdwegvh
     
    • Like Like x 1
  2. Could you please implement it for me?
     
    #22 TSEngineer, Jun 15, 2018
    Last edited: Jan 14, 2020
  3. This is what i thought as well, but doesn't torches etc affect "light level," or is it just the sun?
     
  4. Optic_Fusion1

    Resource Staff

    both
     
  5. Here is my code:
    Code (Java):

     public static boolean checkForLeaks(Location start) {
      ArrayList<Block> cb = new ArrayList<Block>();
      Block s = start.getBlock();
      if(s.getRelative(BlockFace.UP).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.UP));
      if(s.getRelative(BlockFace.DOWN).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.DOWN));
      if(s.getRelative(BlockFace.EAST).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.EAST));
      if(s.getRelative(BlockFace.NORTH).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.NORTH));
      if(s.getRelative(BlockFace.SOUTH).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.SOUTH));
      if(s.getRelative(BlockFace.WEST).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.WEST));
     
      int cycles = 0;
      while(!cb.isEmpty()) {
       for(Block tb : cb) {
        if(tb.getRelative(BlockFace.UP).getType() == Material.AIR && !cb.contains(tb)) cb.add(tb.getRelative(BlockFace.UP));
        if(tb.getRelative(BlockFace.DOWN).getType() == Material.AIR && !cb.contains(tb)) cb.add(tb.getRelative(BlockFace.DOWN));
        if(tb.getRelative(BlockFace.EAST).getType() == Material.AIR && !cb.contains(tb)) cb.add(tb.getRelative(BlockFace.EAST));
        if(tb.getRelative(BlockFace.NORTH).getType() == Material.AIR && !cb.contains(tb)) cb.add(tb.getRelative(BlockFace.NORTH));
        if(tb.getRelative(BlockFace.SOUTH).getType() == Material.AIR && !cb.contains(tb)) cb.add(tb.getRelative(BlockFace.SOUTH));
        if(tb.getRelative(BlockFace.WEST).getType() == Material.AIR && !cb.contains(tb)) cb.add(tb.getRelative(BlockFace.WEST));
        cb.remove(tb);
       }
       cycles++;
       if(cycles >= 20000) return true;
      }
      return false;
     }
     
    I'm getting ConcurrentModificationException. I know what I'm doing wrong tho, but I don't want to use an Iterator to solve my issue
     
    #26 TSEngineer, Jun 15, 2018
    Last edited: Jan 14, 2020
  6. Use an Iterator to avoid a ConcurrentModificationException.
     
  7. Okay, I will try to use Iterator to solve my issue, but is there any other ways?
     
    #28 TSEngineer, Jun 15, 2018
    Last edited: Jan 14, 2020
  8. What? Why not? Iterators are perfect for this, and simple..

    You could have a separate List to hold the things you want to remove from the real List. But that's just dumb and repetitive work.
     
  9. Code (Java):

     public static boolean checkForLeaks(Location start) {
      ArrayList<Block> cb = new ArrayList<Block>();
      Block s = start.getBlock();
      if(s.getRelative(BlockFace.UP).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.UP));
      if(s.getRelative(BlockFace.DOWN).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.DOWN));
      if(s.getRelative(BlockFace.EAST).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.EAST));
      if(s.getRelative(BlockFace.NORTH).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.NORTH));
      if(s.getRelative(BlockFace.SOUTH).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.SOUTH));
      if(s.getRelative(BlockFace.WEST).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.WEST));
     
      int cycles = 0;
      while(!cb.isEmpty()) {
       Iterator<Block> bi = cb.iterator();
       while(bi.hasNext()) {
        Block tb = bi.next();
        if(tb.getRelative(BlockFace.UP).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.UP))) cb.add(tb.getRelative(BlockFace.UP));
        if(tb.getRelative(BlockFace.DOWN).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.DOWN))) cb.add(tb.getRelative(BlockFace.DOWN));
        if(tb.getRelative(BlockFace.EAST).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.EAST))) cb.add(tb.getRelative(BlockFace.EAST));
        if(tb.getRelative(BlockFace.NORTH).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.NORTH))) cb.add(tb.getRelative(BlockFace.NORTH));
        if(tb.getRelative(BlockFace.SOUTH).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.SOUTH))) cb.add(tb.getRelative(BlockFace.SOUTH));
        if(tb.getRelative(BlockFace.WEST).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.WEST))) cb.add(tb.getRelative(BlockFace.WEST));
        //ConcurrentModificationException in bi.remove()
        bi.remove();
       }
       cycles++;
       Bukkit.broadcastMessage(cycles + " " + cb.isEmpty());
       if(cycles >= 20000) return true;
      }
      return false;
     }
     
    I'm still getting a ConcurrentModificationException
     
    #30 TSEngineer, Jun 16, 2018
    Last edited: Jan 14, 2020
  10. Fixed
    Code (Java):

    public static boolean checkForLeaks(Location start) {
      ArrayList<Block> cb = new ArrayList<Block>();
      Block s = start.getBlock();
      if(s.getRelative(BlockFace.UP).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.UP));
      if(s.getRelative(BlockFace.DOWN).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.DOWN));
      if(s.getRelative(BlockFace.EAST).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.EAST));
      if(s.getRelative(BlockFace.NORTH).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.NORTH));
      if(s.getRelative(BlockFace.SOUTH).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.SOUTH));
      if(s.getRelative(BlockFace.WEST).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.WEST));
     
      int cycles = 0;
      while(!cb.isEmpty()) {
       Iterator<Block> bi = cb.iterator();
       ArrayList<Block> acb = new ArrayList<Block>();
       while(bi.hasNext()) {
        Block tb = bi.next();
        if(tb.getRelative(BlockFace.UP).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.UP))) acb.add(tb.getRelative(BlockFace.UP));
        if(tb.getRelative(BlockFace.DOWN).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.DOWN))) acb.add(tb.getRelative(BlockFace.DOWN));
        if(tb.getRelative(BlockFace.EAST).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.EAST))) acb.add(tb.getRelative(BlockFace.EAST));
        if(tb.getRelative(BlockFace.NORTH).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.NORTH))) acb.add(tb.getRelative(BlockFace.NORTH));
        if(tb.getRelative(BlockFace.SOUTH).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.SOUTH))) acb.add(tb.getRelative(BlockFace.SOUTH));
        if(tb.getRelative(BlockFace.WEST).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.WEST))) acb.add(tb.getRelative(BlockFace.WEST));
        bi.remove();
       }
       for(Block b : acb) cb.add(b);
       cycles++;
       if(cycles >= 20000) return true;
      }
      return false;
     }
     
    That one is freezing the server entirely
     
    #31 TSEngineer, Jun 16, 2018
    Last edited: Jan 14, 2020
  11. Praya

    Benefactor

    my code already removed before. later I'll make again and then share the code
     
  12. when do you ever use a while loop when using bukkit/spigot??
    Code (Java):
    for (Iterator<E> iter = list.iterator(); iter.hasNext(); ) {
        E element = iter.next();
        // 1 - can call methods of element
        // 2 - can use iter.remove() to remove the current element from the list

        // ...
    }
     
  13. So, I don't know how to do that
     
    #34 TSEngineer, Jun 18, 2018
    Last edited: Jan 14, 2020
  14. Code (Java):


    public static boolean checkForLeaks(Location start) {
      ArrayList<Block> cb = new ArrayList<Block>();
      Block s = start.getBlock();
      if(s.getRelative(BlockFace.UP).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.UP));
      if(s.getRelative(BlockFace.DOWN).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.DOWN));
      if(s.getRelative(BlockFace.EAST).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.EAST));
      if(s.getRelative(BlockFace.NORTH).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.NORTH));
      if(s.getRelative(BlockFace.SOUTH).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.SOUTH));
      if(s.getRelative(BlockFace.WEST).getType() == Material.AIR) cb.add(s.getRelative(BlockFace.WEST));
     
      int cycles = 0;
      while(!cb.isEmpty()) {
       Iterator<Block> bi = cb.iterator();
       ArrayList<Block> acb = new ArrayList<Block>();
       ArrayList<Block> rcb = new ArrayList<Block>();
       while(bi.hasNext()) {
        Block tb = bi.next();
        if(tb.getRelative(BlockFace.UP).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.UP))) acb.add(tb.getRelative(BlockFace.UP));
        if(tb.getRelative(BlockFace.DOWN).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.DOWN))) acb.add(tb.getRelative(BlockFace.DOWN));
        if(tb.getRelative(BlockFace.EAST).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.EAST))) acb.add(tb.getRelative(BlockFace.EAST));
        if(tb.getRelative(BlockFace.NORTH).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.NORTH))) acb.add(tb.getRelative(BlockFace.NORTH));
        if(tb.getRelative(BlockFace.SOUTH).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.SOUTH))) acb.add(tb.getRelative(BlockFace.SOUTH));
        if(tb.getRelative(BlockFace.WEST).getType() == Material.AIR && !cb.contains(tb.getRelative(BlockFace.WEST))) acb.add(tb.getRelative(BlockFace.WEST));
        rcb.add(tb);
       }
       Bukkit.broadcastMessage(cycles + "");
       for(Block b : acb) cb.add(b);
       for(Block b : rcb) cb.remove(b);
       cycles++;
       if(cycles >= 100) return true;
      }
      return false;
     }
     
    Still didn't work
     
    #35 TSEngineer, Jun 18, 2018
    Last edited: Jan 14, 2020
  15. Thanks to Praya.
    (SOLVED)
     
    #36 TSEngineer, Jul 3, 2018
    Last edited: Jan 14, 2020
    • Like Like x 1
Thread Status:
Not open for further replies.