Help with BlockFormEvent

Discussion in 'Plugin Development' started by Armandozetaxx, Feb 25, 2017.

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

    Armandozetaxx

    I'm working on a minion plugin for myself where i check if cobblestone or stone is form. Then if theres a minion next to that block it starts mining. It works good, but my problem occurs when there are two minions mining. One minion would be alright but the second one would get 2 blocks from mining just one. How can i make the event "Unique" in a way where the event "BlockFormEvent" is not share by the two or multiple minions?
     
  2. Offline

    Zombie_Striker

    @Armandozetaxx
    You most likely have written the event incorrectly. Post your code.
     
  3. Offline

    Armandozetaxx

    @Zombie_Striker

    Code:
        @EventHandler
        public void BlockForm(BlockFormEvent event){
            if(event.getNewState().getType().equals(Material.COBBLESTONE) || event.getNewState().getType().equals(Material.STONE)){
                Location blockLoc = event.getBlock().getLocation();
                List<Entity> near = blockLoc.getWorld().getEntities();
                for(Entity e : near) {
                    if(e.getType().equals(EntityType.ARMOR_STAND))
                    {
                        ArmorStand stand = (ArmorStand) e;
                        if(stand.isCustomNameVisible()) {
                            String standName = stand.getCustomName();
                            String[] splited = standName.split(" ");
                            for (Player p : Bukkit.getOnlinePlayers()) {
                                if(p.getName().equals(splited[2])){
    
                                    int yaw = (int)stand.getLocation().getYaw()+45;
                                    while(yaw<0)
                                        yaw+=360;
                                    while(yaw>360)
                                        yaw-=360;
                                    int direction = yaw/90;
                                    Block b = stand.getLocation().getBlock();
    
                                    switch(direction)
                                    {
                                    case 0: b=b.getRelative(BlockFace.SOUTH); break;
                                    case 1: b=b.getRelative(BlockFace.WEST);  break;
                                    case 2: b=b.getRelative(BlockFace.NORTH); break;
                                    case 3: b=b.getRelative(BlockFace.EAST);
                                    }
    
                                    Block finalBlock = b;
    
                                    if((b.getType().equals(Material.COBBLESTONE) || (b.getType().equals(Material.STONE) || (b.getType().equals(Material.LAVA)))))
                                    {
    
                                        direction = yaw/90;
                                        b = stand.getLocation().getBlock();
                                        switch(direction)
                                        {
                                        case 0: b=b.getRelative(BlockFace.SOUTH); break;
                                        case 1: b=b.getRelative(BlockFace.WEST);  break;
                                        case 2: b=b.getRelative(BlockFace.NORTH); break;
                                        case 3: b=b.getRelative(BlockFace.EAST);
                                        }
    
                                        int idMinion = getMinionID(stand.getLocation());
                                        if(idMinion == 0) return;
    
                                        Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
                                            @Override
                                            public void run() {
    
                                                FileConfiguration config = null;
                                                File files = new File("plugins"+File.separator+"NeonSkyblock"+File.separator+"Minions"+File.separator+idMinion+"-minions.yml");
                                                config = YamlConfiguration.loadConfiguration(files);
                                                int blocksBroken = config.getInt("Minion.Stats.Stone");
                                                config.set("Minion.Stats.Stone", blocksBroken+1);
                                                try {
                                                    config.save(files);
                                                } catch (IOException e) {
                                                    e.printStackTrace();
                                                }
    
                                                finalBlock.setType(Material.AIR);
                                                stand.getWorld().playEffect(blockLoc, Effect.STEP_SOUND, 4, 5);
                                                stand.getWorld().dropItemNaturally(stand.getLocation(), new ItemStack(Material.COBBLESTONE));
                                                stand.setRightArmPose(new EulerAngle(206f, 0f, 0f));
                                            }
                                        }, 40);
    
                                        stand.setRightArmPose(new EulerAngle(200f, 0f, 0f));
                                        stand.setRightArmPose(new EulerAngle(202f, 0f, 0f));
                                        stand.setRightArmPose(new EulerAngle(204f, 0f, 0f));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
     
  4. Offline

    Zombie_Striker

    Your issue is that you are getting all the entity online, not just the ones near the block. You need to make sure the armror stand is close to the stone, or else armor stands on the other side of the world can mine stone.
     
  5. Offline

    Armandozetaxx

    @Zombie_Striker
    I tried this:

    Code:
    List<Entity> near = blockLoc.getWorld().getEntities();
                for(Entity e : near) {
                    if(e.getLocation().distance(blockLoc){ <= 1D) 
    and the first armor stand works perfect, but makes the others don't work at all :(
     
  6. Offline

    Zombie_Striker

    @Armandozetaxx
    That is because you misplaced the open bracket. it should not have even compiled.
     
  7. Offline

    Armandozetaxx

    @Zombie_Striker

    My bad, in my code is correct and still doesn't work. I guess i have to find another event in order to do this.
     
Thread Status:
Not open for further replies.

Share This Page