Solved Getting block behind player via Vector.

Discussion in 'Plugin Development' started by aln0193, Jan 29, 2016.

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

    aln0193

    Hello, I am currently writing a plugin that has been in development for quite some time. I need help figuring out how to get the block behind a player with a Vector, ignoring pitch but using yaw. (or a better method if you have an idea. =] ) Here's an image that kinda hits the mark of what I'm talking about. [​IMG]
    So basically in all what I'm trying to do is do something like player.getTargetBlock(... but turn that around + ignore pitch.

    Current code I have inside of an event that gets called whenever the player moves (I'm trying to make camo armor btw, lol)
    Code:
    Player p = e.getPlayer();
    Location playerLocation = p.getLocation();
                playerLocation.setPitch(0);
                Vector start = playerLocation.toVector();
                Vector end = playerLocation.getDirection().multiply(-1).normalize();
                BlockIterator blockIterator = new BlockIterator(p.getWorld(), start, end, 1, 5);
    
    while(blockIterator.hasNext())
                {
                    if(blockIterator.next().getType() != null && blockIterator.next().getType() != Material.AIR)
                    {
                        block = blockIterator.next();
                        Material mat = block.getType();
                        break;
                        //get colors from material here (I have a method for this already)
                        //set camo armor for player (I already have a method for this too)
                    }
                }
    
    
    Once I'm able to get the proper block disregarding pitch, I'm good to go. Thanks in advance to anyone that can help me solve this problem.
     
  2. Offline

    teej107

  3. Offline

    aln0193

    It just generally doesn't get the right block, it always seems to be off or get the one farthest back. It behaves REALLY weirdly.
     
  4. Offline

    MisterErwin

    @aln0193 You are calling BlockIterator#next() multiple times.

    Code:
    if(blockIterator.next().getType() != null && blockIterator.next().getType() != Material.AIR)
    
    Only call it once within the loop (and store the value temporarily)
     
    aln0193 likes this.
  5. Offline

    aln0193

    @MisterErwin I don't know how I didn't catch that. You ended up being 100% correct! Thank you so much!
     
    MisterErwin likes this.
Thread Status:
Not open for further replies.

Share This Page