[Solved] Block Location Versus player Location

Discussion in 'Plugin Development' started by moschops, Sep 19, 2011.

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

    moschops

    Hi there guys, im trying to generate a tree based on the location the user is looking at. It works when it uses:

    Code:
    player.getLocation()
    But it spawns the the tree right where the user is standing, i have tired using:

    Code:
    Block block = player.getTargetBlock(null, 50);
    Location location = block.getLocation();
    
    But it returns a false as it fails, as a debug testing i am comparing the locations between the two and it should work :|. I am i missing something noobish here?

    Here is the whole block of source code as a reference.
    Code:
    Player player = (Player) sender;
    
                Block block = player.getTargetBlock(null, 50);
                Location location = block.getLocation();
    
                player.sendMessage( location.toString() );
    
                player.sendMessage( player.getLocation().toString() );
    
                final boolean success = player.getWorld().generateTree(player.getLocation(), TreeType.TREE);
    
                if (success) {
                    player.sendMessage( ChatColor.GREEN + "Tree Grown" );
                } else {
                    player.sendMessage( ChatColor.RED + "An Error Occured" );
                }
    
    Many Thanks
    Edit: sorry didn't close the CODE tag.
     
  2. You are generating a tree at player.getLocation() when it has to be just location?
     
  3. Offline

    moschops

    That is because it fails on location but works on player location which is where the user is standing. The location should be the location of the block the user is looking at, they are both in there i was testing the two of them.
     
  4. As Panedmoneus said, you were still generating the tree at player.getLocation(), also if there is no block in range of .getTargetBlock() it returns null soyou need to test that the return is not null before trying to get its location.

    Code:
    Player player = (Player) sender;
    
    Block block = player.getTargetBlock(null, 50);
    if(block != null)
    {
        Location location = block.getLocation();
        player.sendMessage( location.toString() );
        player.sendMessage( player.getLocation().toString() );
        final boolean success = player.getWorld().generateTree(location, TreeType.TREE);
    
        if (success) {
            player.sendMessage( ChatColor.GREEN + "Tree Grown" );
        } else {
            player.sendMessage( ChatColor.RED + "An Error Occured" );
        }
    }
     
  5. Offline

    moschops

    Thanks for the reply it still seems to be failing and the console is not outputting anything in the form of an error.
     
  6. Offline

    ItsHarry

    add some System.out.println(); stuff to test if it is fired
     
  7. Offline

    moschops

    It was an issues with blockface
     
Thread Status:
Not open for further replies.

Share This Page