problem looping through blocks and setting type to water.

Discussion in 'Plugin Development' started by the_merciless, Nov 10, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    the_merciless

    I have 2 locations saved, and loop through all blocks between them, and set the material to water. Works perfectly the 1st time but each time after that the water instantly drains away. Not sure why its draining. I tried using Material.WATER then Material.STATIONARY_WATER and then tried setting the blocks to air, adding a 1 second delay, then setting them to water.

    Code as of now:

    Code:
    private void refillWater() {
            final World world = Bukkit.getWorld(config.getString("Bases." + basename + ".waterpos1.world"));
            int x1 = config.getInt("Bases." + basename + ".waterpos1.x");
            int x2 = config.getInt("Bases." + basename + ".waterpos2.x");
            int z1 = config.getInt("Bases." + basename + ".waterpos1.z");
            int z2 = config.getInt("Bases." + basename + ".waterpos2.z");
            final int y = config.getInt("Bases." + basename + ".waterpos1.y");
            final int xmin = Math.min(x1, x2) - 1;
            final int xmax = Math.max(x1, x2);
            final int zmin = Math.min(z1, z2) - 1;
            final int zmax = Math.max(z1, z2);
           
            for (int x = xmin; x < xmax; x++){
                for (int z = zmin; z < zmax; z++){
                    Material mat = world.getBlockAt(x, y, z).getType();
                    if (!(mat.equals(Material.WATER) || mat.equals(Material.STATIONARY_WATER))){
                        world.getBlockAt(x, y, z).setType(Material.AIR);
                    }
                }
            }
           
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
                @Override
                public void run() {
                    for (int x = xmin; x < xmax; x++){
                        for (int z = zmin; z < zmax; z++){
                            Material mat = world.getBlockAt(x, y, z).getType();
                            if (!(mat.equals(Material.WATER) || mat.equals(Material.STATIONARY_WATER))){
                                world.getBlockAt(x, y, z).setType(Material.STATIONARY_WATER);
                            }
                        }
                    }
                    Bukkit.broadcastMessage("Water refilled");               
                }       
            },20L);
           
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  2. Offline

    GusGold

    the_merciless
    By drain way, what do you mean? Like it disappears, or does gravity take effect?
     
  3. Offline

    the_merciless

  4. Offline

    the_merciless

Thread Status:
Not open for further replies.

Share This Page