Check if blocks is placed as a portal?

Discussion in 'Plugin Development' started by adde, Aug 16, 2013.

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

    adde

    Hey!
    I am not a big fan blocks and how they work. I've never really worked with blocks that much..

    So now my question is, how would I detect/check if one kind of blocks is placed as a portal?

    If someone could give me an example on how to check if they are placed like a square, I can figure it out how to do it so it checks if it is placed as a portal :p

    Thanks! :)
     
  2. Offline

    Technes

    You need to listen for the BlockPlaceEvent, and check to see what is in the players hand when this event fires.
    http://jd.bukkit.org/rb/apidocs/org/bukkit/event/block/BlockPlaceEvent.html

    There is an ID and a material for the portal block, so check to see if that is in the players hand.

    EDIT: If you're not looking for how to determine if someone is placing them in a square, don't read past this :)

    From here, I believe you're asking how to determine if a player is placing more than one portal block in a shape?
    If so, I personally can only think that on BlockPlaceEvent, if the player is holding a portal block, get the location of the placed portal block and check to see if there is another portal block below, above, or next to the one placed, then do something from there. I believe there may be a much better way to do this, but searching around can probably find how.
     
  3. Offline

    adde


    This is not what I am looking for, I know how to check what's in the player's hand and if he placed it.. My question is if a player placed "this" block in a square, up against the sky.

    Here's a screenshot. Check if the player placed blocks like this:
    [​IMG]
     
  4. Offline

    Technes

    Ohh, I see.
    I'm not too sure how to accomplish something like this, unfortunately.
    :(
     
  5. Offline

    lycano

    First, you should implement a command that can transform this shape into a portal. I would not listen on BlockPlaceEvent as every placement would trigger your event and produce tons of overhead as you mostly dont get a positive for the first 7 block placements.

    Use getLastTwoTargetBlocks() to get the block he is looking at. Then implement your detection logic using the Player's facing direction as reference and use getRelative() to check for this structure starting at the last block he is looking at.
     
    adde likes this.
  6. Offline

    adde

    Hm.. okey.. Will see what I can do :)

    Any example everything all together??
     
  7. Offline

    lycano

    adde well you should do that on your own. This is a great opportunity to learn almost all about Blocks as this task is very difficult to solve.

    You have a static shape that shall be detected. Save yourself hours of work if you implement constants like "The player has to look at the corner of that shape" at first. If you know better ways later you can always implement that then.

    If you do that dynamicly you would have to implement an algorhytm that can determine its point of origin which is kinda hard to do. You need the point of origin, the direction the player is looking at and a detection routine that knows after checking n blocks "im at position x,y" in your 2D shape-map.

    To shortcut this you can just fetch all blocks per direction and check if they match at certain points using getRelative.

    E.g. 3x3 block, look at upper right corner.

    7 8 9
    4 5 6
    1 2 3

    targetBlock is position 3.

    targetBlock.getRelative(BlockFace.WEST, 1) you would get block 2
    targetBlock.getRelative(BlockFace.WEST, 2) you would get block 1
    targetBlock.getRelative(BlockFace.SOUTH, 1) you would get block 6
    // make block 6 your new targetBlock and repeat the last two steps and so on ...

    Check that against your Block List that contains a fixed list of block types.
    Code:java
    1.  
    2. List<Material> matchList = new ArrayList<Material>();
    3. for (int i = 0; i < 10; i++) {
    4. matchList.add(Material.DIRT);
    5. }
    6. matchList.set(5, Material.AIR);
    7.  


    If it does not match either they looked at the right bottom corner or its not a valid shape.

    And again, you can always implement a pathfinder that can do that later =)
     
    adde likes this.
  8. Offline

    adde

    lycano, thanks! This will help me alot! :')
     
Thread Status:
Not open for further replies.

Share This Page