Check if there's a block before the player

Discussion in 'Plugin Development' started by CraterHater, Jul 5, 2015.

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

    CraterHater

    how do I check if there's a block immediately ahead of the player in the way he watches and if there is no tp the player one block ahead. How to do that.
     
  2. Offline

    stefvanschie

    Interesting question. Well let's say this has two major things to check for. The direction the player is facing to and is one block ahead of them a block. So first we're getting the 4 directions (which direction that exactly is is labelled). Than we look if the block in fron of them not air is. If it's not we're teleporting them one block ahead. This should work:

    Code:
    if (player.getLocation().getYaw() >= -45 && player.getLocation().getYaw() <= 45) {
                    //towards positive z
                    if (new Location(player.getWorld(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ() + 1).getBlock().getType() != Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ() + 1));
                    }
                }
                else if (player.getLocation().getYaw() >= 45 && player.getLocation().getYaw() <= 135) {
                    //towards negative x
                    if (new Location(player.getWorld(), player.getLocation().getBlockX() - 1, player.getLocation().getBlockY(), player.getLocation().getBlockZ()).getBlock().getType() != Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX() - 1, player.getLocation().getY(), player.getLocation().getZ()));
                    }
                }
                else if (player.getLocation().getYaw() >= 135 && player.getLocation().getYaw() <= -135) {
                    //towards negative z
                    if (new Location(player.getWorld(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ() - 1).getBlock().getType() != Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ() - 1));
                    }
                }
                else if (player.getLocation().getYaw() >= -135 && player.getLocation().getYaw() <= -45) {
                    //towards positive x
                    if (new Location(player.getWorld(), player.getLocation().getBlockX() + 1, player.getLocation().getBlockY(), player.getLocation().getBlockZ()).getBlock().getType() != Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX() + 1, player.getLocation().getY(), player.getLocation().getZ()));
                    }
                }
     
  3. Offline

    pie_flavor

    I would imagine you would get the player's facing angle as a Vector and then see whether the x or z is greater and if so if it is negative. No idea how to get the face angle tho.
     
  4. Offline

    stefvanschie

    @pie_flavor The facing angle is divided in two parts: the pitch (vertical facing) and the yaw (horizontal facing).
    Code:
    player.getLocation().getYaw();
    should do the trick
     
  5. Offline

    CraterHater

    @stefvanschie I did this, but the only thing it says is "0" and it does nothing
    Code:
      @EventHandler
                    public void onPlayerUse2(PlayerInteractEvent event){
                        Player player = event.getPlayer();         
                        if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                            if(player.getItemInHand().getType() == Material.GOLD_SWORD){  
                               int value2 = getConfig().getInt(player.getName() + ".Permission1");
                                if(value2 == 1) {
                               // if (Cooldowns.tryCooldown(player, "x3", 18000)) {
                                   
    
    if (player.getLocation().getYaw() >= -45 && player.getLocation().getYaw() <= 45) {
        player.sendMessage("0");
                    if (new Location(player.getWorld(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ() + 1).getBlock().getType() != Material.AIR) {
                       player.teleport(new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ() + 1));
                       player.sendMessage("1");
                    }
                }
                else if (player.getLocation().getYaw() >= 45 && player.getLocation().getYaw() <= 135) {
                    //towards negative x
                    if (new Location(player.getWorld(), player.getLocation().getBlockX() - 1, player.getLocation().getBlockY(), player.getLocation().getBlockZ()).getBlock().getType() != Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX() - 1, player.getLocation().getY(), player.getLocation().getZ()));
                        player.sendMessage("2");
                    }
                }
                else if (player.getLocation().getYaw() >= 135 && player.getLocation().getYaw() <= -135) {
                    //towards negative z
                    if (new Location(player.getWorld(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ() - 1).getBlock().getType() != Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ() - 1));
                        player.sendMessage("3");
                    }
                }
                else if (player.getLocation().getYaw() >= -135 && player.getLocation().getYaw() <= -45) {
                    //towards positive x
                    if (new Location(player.getWorld(), player.getLocation().getBlockX() + 1, player.getLocation().getBlockY(), player.getLocation().getBlockZ()).getBlock().getType() != Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX() + 1, player.getLocation().getY(), player.getLocation().getZ()));
                        player.sendMessage("4");
                    }
                }
    
    
                                     player.playSound(player.getLocation(), Sound.NOTE_PLING, 5, 5);
    
                                    
    
                                    player.sendMessage("§b§2[Spells]:§rYou casted the charge Spell!");
                                   return;
                                }else{
                                player.sendMessage("§b§2[Spells]:§rYou have " + (Cooldowns.getCooldown(player, "x3") / 1000) + " seconds left!");
                                player.playSound(player.getLocation(), Sound.NOTE_PLING, 3, 3);
                                return;
     
  6. Offline

    stefvanschie

    Oops replace every
    Code:
    player.getWorld()
    with
    Code:
    player.getLocation().getWorld()
     
  7. Offline

    CraterHater

    @stefvanschie it says nothing right now
    Code:
                    @EventHandler
                    public void onPlayerUse2(PlayerInteractEvent event){
                        Player player = event.getPlayer();         
                        if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                            if(player.getItemInHand().getType() == Material.GOLD_SWORD){  
                               int value2 = getConfig().getInt(player.getName() + ".Permission1");
                                if(value2 == 1) {
                               // if (Cooldowns.tryCooldown(player, "x3", 18000)) {
                                   
    
    if (player.getLocation().getYaw() >= -45 && player.getLocation().getYaw() <= 45) {
        player.sendMessage("0");
                    if (new Location(player.getLocation().getWorld(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ() + 1).getBlock().getType() != Material.AIR) {
                       player.teleport(new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ() + 1));
                       player.sendMessage("1");
                    }
                }
                else if (player.getLocation().getYaw() >= 45 && player.getLocation().getYaw() <= 135) {
                    //towards negative x
                    if (new Location(player.getLocation().getWorld(), player.getLocation().getBlockX() - 1, player.getLocation().getBlockY(), player.getLocation().getBlockZ()).getBlock().getType() != Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX() - 1, player.getLocation().getY(), player.getLocation().getZ()));
                        player.sendMessage("2");
                    }
                }
                else if (player.getLocation().getYaw() >= 135 && player.getLocation().getYaw() <= -135) {
                    //towards negative z
                    if (new Location(player.getLocation().getWorld(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ() - 1).getBlock().getType() != Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ() - 1));
                        player.sendMessage("3");
                    }
                }
                else if (player.getLocation().getYaw() >= -135 && player.getLocation().getYaw() <= -45) {
                    //towards positive x
                    if (new Location(player.getLocation().getWorld(), player.getLocation().getBlockX() + 1, player.getLocation().getBlockY(), player.getLocation().getBlockZ()).getBlock().getType() != Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX() + 1, player.getLocation().getY(), player.getLocation().getZ()));
                        player.sendMessage("4");
                    }
                }
    
    
                                     player.playSound(player.getLocation(), Sound.NOTE_PLING, 5, 5);
    
                                    
    
                                    player.sendMessage("§b§2[Spells]:§rYou casted the charge Spell!");
                                   return;
                                }else{
                                player.sendMessage("§b§2[Spells]:§rYou have " + (Cooldowns.getCooldown(player, "x3") / 1000) + " seconds left!");
                                player.playSound(player.getLocation(), Sound.NOTE_PLING, 3, 3);
                                return;
     
  8. Offline

    stefvanschie

    @CraterHater It should say zero if you're looking in bteween -45 and 45 I assume you were not facing that way. Are you getting any errors in console?
     
  9. Offline

    CraterHater

  10. Offline

    stefvanschie

    @CraterHater Sorry for being really stupid. Change every
    Code:
    !=
    into
    Code:
    ==
     
  11. Offline

    CraterHater

    @stefvanschie ok it works but when you tp you turn around to a diffirent angle so if you tp to the left for exemple you will be facing to the original point and not to the point you are facing.

    Code:
                    @EventHandler
                    public void onPlayerUse2(PlayerInteractEvent event){
                        Player player = event.getPlayer();         
                        if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                            if(player.getItemInHand().getType() == Material.GOLD_SWORD){  
                               int value2 = getConfig().getInt(player.getName() + ".Permission1");
                                if(value2 == 1) {
                               // if (Cooldowns.tryCooldown(player, "x3", 18000)) {
                                   
    
    if (player.getLocation().getYaw() >= -45 && player.getLocation().getYaw() <= 45) {
        player.sendMessage("0");
                    if (new Location(player.getLocation().getWorld(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ() + 1).getBlock().getType() == Material.AIR) {
                       player.teleport(new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ() + 1));
                       player.sendMessage("1");
                    }
                }
                else if (player.getLocation().getYaw() >= 45 && player.getLocation().getYaw() <= 135) {
                    //towards negative x
                    if (new Location(player.getLocation().getWorld(), player.getLocation().getBlockX() - 1, player.getLocation().getBlockY(), player.getLocation().getBlockZ()).getBlock().getType() == Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX() - 1, player.getLocation().getY(), player.getLocation().getZ()));
                        player.sendMessage("2");
                    }
                }
                else if (player.getLocation().getYaw() >= 135 && player.getLocation().getYaw() <= -135) {
                    //towards negative z
                    if (new Location(player.getLocation().getWorld(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ() - 1).getBlock().getType() == Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ() - 1));
                        player.sendMessage("3");
                    }
                }
                else if (player.getLocation().getYaw() >= -135 && player.getLocation().getYaw() <= -45) {
                    //towards positive x
                    if (new Location(player.getLocation().getWorld(), player.getLocation().getBlockX() + 1, player.getLocation().getBlockY(), player.getLocation().getBlockZ()).getBlock().getType() == Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX() + 1, player.getLocation().getY(), player.getLocation().getZ()));
                        player.sendMessage("4");
                    }
                }
    
    
                                     player.playSound(player.getLocation(), Sound.NOTE_PLING, 5, 5);
    
                                    
    
                                    player.sendMessage("§b§2[Spells]:§rYou casted the charge Spell!");
                                   return;
                                }else{
                                player.sendMessage("§b§2[Spells]:§rYou have " + (Cooldowns.getCooldown(player, "x3") / 1000) + " seconds left!");
                                player.playSound(player.getLocation(), Sound.NOTE_PLING, 3, 3);
                                return;
                                }
                                }
     
  12. Offline

    stefvanschie

    @CraterHater Add to the player.teleport the yaw and pitch. You probably want to set the yaw and pitch the same as first so:
    Code:
    player.getLocation().getYaw(), player.getLocation().getPitch()
     
  13. Offline

    CraterHater

    @stefvanschie thanks! the only problem left is that it doesn't work fine, a lot of times it works and a lot of times it doesn't... any ideas?

    code:
    Code:
                  @EventHandler
                    public void onPlayerUse2(PlayerInteractEvent event){
                        Player player = event.getPlayer();       
                        if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                            if(player.getItemInHand().getType() == Material.GOLD_SWORD){
                               int value2 = getConfig().getInt(player.getName() + ".Permission1");
                                if(value2 == 1) {
                               // if (Cooldowns.tryCooldown(player, "x3", 18000)) {
                                 
    
    if (player.getLocation().getYaw() >= -45 && player.getLocation().getYaw() <= 45) {
        player.sendMessage("0");
                    if (new Location(player.getLocation().getWorld(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ() + 1).getBlock().getType() == Material.AIR) {
                       player.teleport(new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ() + 1, player.getLocation().getYaw(), player.getLocation().getPitch()));
                       player.sendMessage("1");
                    }
                }
                else if (player.getLocation().getYaw() >= 45 && player.getLocation().getYaw() <= 135) {
                    //towards negative x
                    if (new Location(player.getLocation().getWorld(), player.getLocation().getBlockX() - 1, player.getLocation().getBlockY(), player.getLocation().getBlockZ()).getBlock().getType() == Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX() - 1, player.getLocation().getY(), player.getLocation().getZ(), player.getLocation().getYaw(), player.getLocation().getPitch()));
                        player.sendMessage("2");
                    }
                }
                else if (player.getLocation().getYaw() >= 135 && player.getLocation().getYaw() <= -135) {
                    //towards negative z
                    if (new Location(player.getLocation().getWorld(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ() - 1).getBlock().getType() == Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ() - 1, player.getLocation().getYaw(), player.getLocation().getPitch()));
                        player.sendMessage("3");
                    }
                }
                else if (player.getLocation().getYaw() >= -135 && player.getLocation().getYaw() <= -45) {
                    //towards positive x
                    if (new Location(player.getLocation().getWorld(), player.getLocation().getBlockX() + 1, player.getLocation().getBlockY(), player.getLocation().getBlockZ()).getBlock().getType() == Material.AIR) {
                        player.teleport(new Location(player.getWorld(), player.getLocation().getX() + 1, player.getLocation().getY(), player.getLocation().getZ(), player.getLocation().getYaw(), player.getLocation().getPitch()));
                        player.sendMessage("4");
                    }
                }
    
    
                                     player.playSound(player.getLocation(), Sound.NOTE_PLING, 5, 5);
    
                                  
    
                                    player.sendMessage("§b§2[Spells]:§rYou casted the charge Spell!");
                                   return;
                                }else{
                                player.sendMessage("§b§2[Spells]:§rYou have " + (Cooldowns.getCooldown(player, "x3") / 1000) + " seconds left!");
                                player.playSound(player.getLocation(), Sound.NOTE_PLING, 3, 3);
                                return;
     
    Last edited: Jul 5, 2015
  14. @stefvanschie
    Oh my god, please no.
    @CraterHater
    There's a class called BlockIterator, call a new instance of that with the player as parameter, and you can get blocks in his line of sight and do stuff with that.
     
  15. Offline

    CraterHater

Thread Status:
Not open for further replies.

Share This Page