Getting the block the player is looking at within a range of 5

Discussion in 'Plugin Development' started by kaskoepoes112, Nov 7, 2014.

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

    kaskoepoes112

    I'm recreating an old plugin I made in the past, created it about a year ago but got a new PC so I lost all the files :(

    I want to get the block the player is looking at within the a range of 5, which means if the player is looking at a log, and the log is closer than 5 blocks to the player, it should get that block. But if the log is farther away than 5 blocks it should get the air block at 5 blocks range instead.

    I thought you did that with:

    player.getTargetBlock(transparant blocks , range);

    So I have:

    player.getTargetBlock(null, 5);

    the thing is, even if the log is farther away than 5 blocks, it will still return the log. Instead it should return the air block at 5 blocks distance.
    I know this is possible, I've done it before but I just can't remember how to do it.

    Hope I'm clear.
    Thanks
     
  2. Offline

    Skionz

    kaskoepoes112 iterate through the players line of sight and check if the block is a log.
     
  3. Offline

    lycano

    kaskoepoes112 in theory this is very easy as Skionz said but for that you would probably listen for an event that fires when you change your viewpoint ... this is a very very spammy event so you would iterate constantly producing high amount of CPU load.

    From what i get is that you want some sort of radar. Like you get nearby a block then bam fire your custom event that will do stuff.

    Please be more specific so i can give you more details. But if you want to just "scan" for specific blocks then this will be tricky.
     
  4. Offline

    kaskoepoes112



    So here is what I want:

    The player should be able to look at a block and then right click with a blaze rod, which triggers the event.
    The block the player is looking at should be returned if it's within the range.
    Let's say the range is 20, the player looks at a log which is 20 blocks away, there is only air between the player and the block.

    Now I want to select all air blocks between the player and the selected block, then "play" (idk if this is how you say this in English) a particle at all blocks between them in a line.

    I understand that this may not be clear but I actually uploaded a video of my older plugin


    the thing I want is at the 8 second mark (with the orange fireworks) when you click it should get the targeted block and all blocks between them so I can play a particle at those blocks. In this case the particles are the orange fireworks.

    However right now, I'm unable to get the air blocks between the player and the targetedblock because the range function on the player.getTargetBlock(null , 5 <- this one) doesn't work.
     
  5. Offline

    Skionz

    kaskoepoes112 likes this.
  6. Offline

    lycano

    Cool idea well in this case you use an event that is controlable and can also be limited to that item.

    Anyways getTargetBlock should work. What version do you use?

    Also as already mentioned you could use lineofsight and only check the last item since you can limit the distance. Probably this is what getTargetBlock does.

    If maxdistance does not work just getline of sight and check the n-th item for Material with getType ().equals(Material.NAME) if it does not match return.

    If it does fire a custom event since this easier to control. For the delay use a hashmap playername timestamp. And check if the value (which is a timestamp) is bigger or equal then value + seconds. If so remove from map. Put when used repeat. No need for scheduled events :)
     
    kaskoepoes112 likes this.
  7. Offline

    kaskoepoes112


    Thanks guys, I got it working by using the .getLineOfSight() method :)!


    Currently I got it working by using a delayed event, I'll try using your way when I have time.
    Thanks !
     
  8. Offline

    lycano

Thread Status:
Not open for further replies.

Share This Page