Block Detect Code

Discussion in 'Spigot Plugin Development' started by TpittPlays, May 14, 2016.

  1. Hello SpigotMC users! Today I have been trying to figure out how to code a plugin that detects if a block at certain coordinates is broken and if it is it teleports you to a certain area (I know how to have the teleport code in it just need the detect for the coordinates of the block broken) Does anyone have any ideas how to do this?
     
  2. Choco

    Moderator

    1. Listen for a BlockBreakEvent
    2. Get the broken blocks location (BlockBreakEvent#getBlock(), Block#getLocation())
    3. Check if the x, y, and z are equal to what you want (Location#getBlockX(), Location#getBlockY(), Location.getBlockZ())
    4. Teleport the player to where you want
    5. Profit ;)
     
    • Agree Agree x 1
  3. What would a code for this look like I know the teleport like
    p.teleport(new Location(Bukkit.getWorld("world"), -1, 8, 0));

    But for the detect what would that look like?
     
  4. Choco

    Moderator

    https://www.spigotmc.org/wiki/bbcode.1415/archive
    Feel free to take a gander at this Wiki page for listening to events
     
  5. 1) Don't bump <24hours bumped threads.
    2) You got your answers already.
     
    • Agree Agree x 1
  6. Well to see if a particular block is broken just use
    Code (Java):
    event.getClickedBlock().getType().equals(Material.STONE)
    to test if it is the correct block.
     
  7. how do I search for the block the player stepped on for example
     
  8. Well to check a block the player stepped on you need to listen for player move event and get the block that is y-1 under the players current location. Playermoveevent is called lots of times though, to reduce your performance impact you should first check if the player has entered a new block before executing your code.
     
  9. - Why spoonfeeding ?
    - You should use instance comparison (==) instead of Object#equals for constants, primitives and enums
     
    • Agree Agree x 1