Solved get the blocks inside a cuboid?

Discussion in 'Plugin Development' started by EnchantedMiners, Jun 9, 2015.

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

    EnchantedMiners

    So how would i do that i have the cuboid but have no idea how to get the blocks inside it then check if it contains a block then replace them i have done something like this

    Code:
            public static void checkServerStatus(){
                YamlConfiguration config = YamlConfiguration
                        .loadConfiguration(new File(
                                "plugins/ServerTeleporters/teleporters.yml"));
    
                for (String teleporters : config.getConfigurationSection("Teleporters")
                        .getKeys(false)) {
                    teleporters = "Teleporters." + teleporters;
    
                    final String server_name = config.getString(teleporters
                            + ".server_name");
    
                    String world = config.getString(teleporters + ".World");
    
                    String minPoint = config.getString("" + teleporters + ".minPoint");
                    String maxPoint = config.getString(teleporters + ".maxPoint");
    
                    String[] minPointSplitted = minPoint.split(",");
                    String[] maxPointSplitted = maxPoint.split(",");
    
                    double minX = Double.valueOf(minPointSplitted[0]);
                    double minY = Double.valueOf(minPointSplitted[1]);
                    double minZ = Double.valueOf(minPointSplitted[2]);
                    double maxX = Double.valueOf(maxPointSplitted[0]);
                    double maxY = Double.valueOf(maxPointSplitted[1]);
                    double maxZ = Double.valueOf(maxPointSplitted[2]);
    
                    Location minPointLoc = new Location(Bukkit.getWorld(world), minX,
                            minY, minZ);
                    Location maxPointLoc = new Location(Bukkit.getWorld(world), maxX,
                            maxY, maxZ);
    
                    CuboidSelection cs = new CuboidSelection(Bukkit.getWorld(world),
                            minPointLoc, maxPointLoc);
                  
                    getServerIP(server_name);
                    if(status(ip, port) == false){
                        for (int x = min(minPointSplitted[0], maxPointSplitted[0]); x <= max(minPointSplitted[0], maxPointSplitted[0]); x++) {
                            for (int y = min(minPointSplitted[1], maxPointSplitted[1]); y <= max(minPointSplitted[1], maxPointSplitted[1]); y++) {
                                for (int z = min(minPointSplitted[2], maxPointSplitted[2]); z <= max(minPointSplitted[2], maxPointSplitted[2]); z++) {
                                    Block b0 = cs.getWorld().getBlockAt(x, y, z);
                                    if(b0.getType().equals(Material.EMERALD_BLOCK)){
                                        b0.setType(Material.REDSTONE_BLOCK);
                                    }
                                }
                            }
                        }
                    } else {
                        for (int x = min(minPointSplitted[0], maxPointSplitted[0]); x <= max(minPointSplitted[0], maxPointSplitted[0]); x++) {
                            for (int y = min(minPointSplitted[1], maxPointSplitted[1]); y <= max(minPointSplitted[1], maxPointSplitted[1]); y++) {
                                for (int z = min(minPointSplitted[2], maxPointSplitted[2]); z <= max(minPointSplitted[2], maxPointSplitted[2]); z++) {
                                    Block b0 = cs.getWorld().getBlockAt(x, y, z);
                                    if(b0.getType().equals(Material.REDSTONE_BLOCK)){
                                        b0.setType(Material.EMERALD_BLOCK);
                                    }
                                }
                            }
                        }
                    }
                }
            }
    EDIT:
    The Method i used at the end which suppost to loop tru all the blocks in the Cuboid i found it online in a thread and didn't know how to use it the min() and max() are not defined i need help with those or if there is an easier way or same way to check can someone post it please or explain the method

    anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  2. Offline

    Adamki11s

    @EnchantedMiners
    You want to use a for loop, with x, y and z to iterate over all the blocks in your container.

    Take a base location, min x, y and z; and then your width variable;

    Code:
    for(int x = 0; x < width; x++){
      for(int y = 0; y < height; y++){
        for(int z = 0; z < length; z++){
          Block b = location.getRelative(x, y, z);
        }
      }
    }
     
  3. Offline

    EnchantedMiners

    Code:
                    CuboidSelection cs = new CuboidSelection(Bukkit.getWorld(world),
                            minPointLoc, maxPointLoc);
                   
                    getServerIP(server_name);
                    if(status(ip, port) == false){
                       
                       
                        for(int x = 0; x < cs.getWidth(); x++){
                              for(int y = 0; y < cs.getHeight(); y++){
                                for(int z = 0; z < cs.getHeight(); z++){
                                  Block b = location.getRelative(x, y, z);
                                }
                              }
                            }
                    }
    ok thanks!, i have this so far what do i put on location ?

    @Adamki11s
    Code:
                        for(int x = 0; x < cs.getWidth(); x++){
                            for(int y = 0; y < cs.getHeight(); y++){
                                for(int z = 0; z < cs.getLength(); z++){
                                    Block b = cs.getWorld().getBlockAt(x,y,z);
                                    ParticleEffect.FIREWORKS_SPARK.display(0, 0, 0, 0, 2, b.getLocation(), 100);
                                    System.out.println(b.getLocation().toString());
                                }
                            }
                        }
    uhmm i think i did somethign wrong cs = CuboidSelection which will return a low value of 6 or 2 depends how big the cuboid is so it will play at the location that returns but won't play at the cuboid location

    Got it working!!!!!! final code:

    Code:
        public static void playPortalEffect(){
            final YamlConfiguration config = YamlConfiguration
                    .loadConfiguration(new File(
                            "plugins/ServerTeleporters/teleporters.yml"));
           
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(instance, new Runnable(){
                @Override
                public void run() {
                    for (String teleporters : config.getConfigurationSection("Teleporters").getKeys(false)) {
                        teleporters = "Teleporters." + teleporters;
                       
                        String world = config.getString(teleporters + ".World");
    
                        String minPoint = config.getString("" + teleporters + ".minPoint");
                        String maxPoint = config.getString(teleporters + ".maxPoint");
    
                        String[] minPointSplitted = minPoint.split(",");
                        String[] maxPointSplitted = maxPoint.split(",");
    
                        double minX = Double.valueOf(minPointSplitted[0]);
                        double minY = Double.valueOf(minPointSplitted[1]);
                        double minZ = Double.valueOf(minPointSplitted[2]);
                        double maxX = Double.valueOf(maxPointSplitted[0]);
                        double maxY = Double.valueOf(maxPointSplitted[1]);
                        double maxZ = Double.valueOf(maxPointSplitted[2]);
    
                        Location minPointLoc = new Location(Bukkit.getWorld(world), minX,
                                minY, minZ);
                        Location maxPointLoc = new Location(Bukkit.getWorld(world), maxX,
                                maxY, maxZ);
    
                        CuboidSelection cs = new CuboidSelection(Bukkit.getWorld(world),
                                minPointLoc, maxPointLoc);
                        for(int x = (int)minX; x < (int)maxX; x++){
                            for(int y = (int)minY; y < (int)maxY; y++){
                                for(int z = (int)minZ; z <= (int)maxZ; z++){
                                    Block b = cs.getWorld().getBlockAt(x,y,z);
                                    ParticleEffect.FIREWORKS_SPARK.display(0, 0, 0, (float)0.3, 5, b.getLocation(), 100);
                                }
                            }
                        }
                    }
                }
            }, 0L, 10L);
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  4. Offline

    Adamki11s

    You're getting the black at the x, y, z in the for loop coordinates. Those are OFFSET coordinates from your starting point, not real coordinates.

    So make sure where you are getting your block location you add the initial x, y and z values.
     
    EnchantedMiners likes this.
  5. Offline

    EnchantedMiners

    @Adamki11s ya thanks i got it working now its PERFECT thanks, i did some more research and there is a post exactly like this one idk how i didn't saw it before
     
Thread Status:
Not open for further replies.

Share This Page