Solved Checking for all wool blocks in a selection

Discussion in 'Plugin Development' started by jmmalcolm3, Jun 26, 2015.

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

    jmmalcolm3

    Hi, I'm having trouble trying to find all wool blocks within a selection which I saved to a config.
    Code:
        public void onGameEnd(String i){
            int red = 0;
            int blue = 0;
            int xma = main.getConfig().getInt(i + "max" + ".x");
            int xmi = main.getConfig().getInt(i + "min" + ".x");
            int yma = main.getConfig().getInt(i + "max" + ".y");
            int ymi = main.getConfig().getInt(i + "min" + ".y");
            int zma = main.getConfig().getInt(i + "max" + ".z");
            int zmi = main.getConfig().getInt(i + "min" + ".z");
            String lobbyw = main.getConfig().getString(i + "main" + ".world");
            int blocks = 0;
            for (int x = xmi ; x < xma ; x++) {
               for (int y = ymi ; y < yma; y ++) {
                   for (int z = zmi ; z < zma ; z++) {
                      String world1 = main.getConfig().getString(i + ".world");
                      World world = main.getServer().getWorld(world1);
                      Block block = world.getBlockAt(x,y,z);
                      if(block.getType() == Material.WOOL){
                          blocks ++;
                          main.getConfig().set(i + ".blocks", blocks);
                        Wool wool = (Wool) block;
                        if(wool.getColor() == DyeColor.RED){
                        red ++;
                        }
                        else if(wool.getColor() == DyeColor.BLUE){
                        blue ++;
                        }
                   }
                   
                      else{
                          return;
                      }
               }
               }
            }
            int redper = red/100;
            final int blueper = blue/100;
            for(final Player p : Bukkit.getOnlinePlayers()){
                p.sendMessage(ChatColor.GOLD + "And the winners are...");
                Bukkit.broadcastMessage("There are " + blocks + " blocks of wool in this arena.");
                if(redper > blueper){
                main.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable(){
    
                    @Override
                    public void run() {
                        p.sendMessage(ChatColor.RED + "The red team! They won with " + redper);
                    }
             
               }, 40);
                }
                else{
                    main.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable(){
    
                            @Override
                            public void run() {
                                p.sendMessage(ChatColor.BLUE + "The blue team! They won with " + blueper);
                            }
                     
                       }, 40);
                }
                    }
    }
    
    Here is my config.yml
    Code:
    '4':
      min:
        x: -43.0
        y: 67.0
        z: 269.0
      max:
        x: -41.0
        y: 67.0
        z: 271.0
      name: Test
    
    
    4 is an arena name, and I'm using a command, where "i" in a command is the arena #. Whenever I call the event, it always say that the blue team has won with 0. i = 4 in this case. This is the command that initiates it:
    Code:
    else if(args[0].equalsIgnoreCase("test")){
                    if(main.isInt(args[1])){
                        int num = Integer.parseInt(args[1]);
                        onGameEnd(args[1]);
                    }
                }
     
  2. @jmmalcolm3
    When getting the config path, you did i + "max" + ".x", 2 things on that.
    1. "max" + ".x" can just be "max.x"
    2. Your issue is that the path will be on e.g. 4 "4max.x", you need to put a space before each max or min
     
  3. Offline

    jmmalcolm3

    @megamichiel
    Holy crap, I didn't even notice that, after all, I did write this at 2 AM. Thanks.

    Edit: Still doesn't seem to work, updated onGameEnd:
    Code:
        public void onGameEnd(String i){
            int red = 0;
            int blue = 0;
           int xmi = main.getConfig().getInt(i + ".min" + ".x");
           int ymi = main.getConfig().getInt(i + ".min" + ".y");
           int zmi = main.getConfig().getInt(i + ".min" + ".z");
           int xma = main.getConfig().getInt(i + ".max" + ".x");
           int yma = main.getConfig().getInt(i + ".max" + ".y");
           int zma = main.getConfig().getInt(i + ".max" + ".z");
           Player player = Bukkit.getPlayer("TheTimMan");
           player.sendMessage(ChatColor.RED + "xmi = " + xmi);
            int blocks = 0;
            for (int x = xmi ; x < xma ; x++) {
                for (int y = ymi ; y < yma; y ++) {
                    for (int z = zmi ; z < zma ; z++) {
                        String world1 = main.getConfig().getString(i + ".world");
                        World world = main.getServer().getWorld(world1);
                        Block block = world.getBlockAt(x,y,z);
                        if(block.getType() == Material.WOOL){
                            blocks ++;
                            main.getConfig().set(i + ".blocks", blocks);
                        Wool wool = (Wool) block;
                        if(wool.getColor() == DyeColor.RED){
                        red ++;
                        }
                        else if(wool.getColor() == DyeColor.BLUE){
                        blue ++;
                        }
                    }
                    
                        else{
                            return;
                        }
                }
                }
            }
            int redper = red/100;
            final int blueper = blue/100;
             for(final Player p : Bukkit.getOnlinePlayers()){
                 p.sendMessage(ChatColor.GOLD + "And the winners are...");
                 Bukkit.broadcastMessage("There are " + blocks + " blocks of wool in this arena.");
                 if(redper > blueper){
                 main.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable(){
    
                    @Override
                    public void run() {
                         p.sendMessage(ChatColor.RED + "The red team!");
                    }
            
                }, 40);
                 }
                 else{
                     main.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable(){
    
                            @Override
                            public void run() {
                                 p.sendMessage(ChatColor.BLUE + "The blue team! They won with " + blueper + "%");
                            }
                    
                        }, 40);
                 }
                     }
        }
    I saved each coord as [#].min.x and so on, and I put a message in there to make sure that it was getting the correct coords, and it is, yet it still says the same thing.
     
    Last edited: Jun 26, 2015
  4. I'm guessing this
    Code:
    Wool wool = (Wool) block;
    will throw an error, because Wool is not an instance of block. You need to replace block with block.getState.getData(), since Wool is an instance of MaterialData.
    Another thing, redper and blueper won't give accurate percentages. You need to make an int where you count red + blue, and then divide red / the total and blue / the total.
     
  5. Offline

    jmmalcolm3

    Thanks, and I know how to calculate percentages lol, it was to debug :p. It kept saying devide by 0 error, so I changed it for the sake of getting more info about why it wouldn't work.
     
Thread Status:
Not open for further replies.

Share This Page