Check if a player reaches a radius around a block

Discussion in 'Plugin Development' started by mickedplay, Mar 22, 2014.

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

    mickedplay

    How do I check if a player reaches a radius around a specific block?
    So I mean:

    Thanks!

    Any ideas?​

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Offline

    willeb96

    You can check the distance between two locations.

    Code:java
    1. // I'm not sure where you get these variables from
    2. Block block;
    3. Player player;
    4.  
    5. // This is how you check the radius
    6. if (block.getLocation().distance(player.getLocation()) < 5) {
    7. // The block is within 5 blocks of the player
    8. }
     
    Konkz likes this.
  3. Offline

    mickedplay

    willeb96 Thanks.

    Anoby an idea where I get These variables from?
     
  4. Offline

    MRPS

    Cloaking_Ocean likes this.
  5. Offline

    mickedplay

  6. Offline

    drtshock

    Don't ask for people to write code for you. Try it yourself and then people will be happy to help you.
     
  7. Offline

    Cloaking_Ocean

    He means you need to use like a listener or something to get the blocks and players from maybe on PlayerMoveEvent...
     
  8. Offline

    Europia79

    mickedplay

    First, think of some kind of event that you want to listen to...

    Next, once you have some kind of idea in mind, then goto

    http://jd.bukkit.org/dev/apidocs/

    After you go there... Control+F and search thru all the events and choose the one you want to handle.

    Then make a listener for that event... and put the code that you want to perform inside.

    Make your main class that extends JavaPlugin and register your listener:
    Main.java (open)

    Code:java
    1. public class Main extends JavaPlugin {
    2.  
    3. @Override
    4. public void onEnable() {
    5.  
    6. getServer().getPluginManager().registerEvents(new ExampleListener(), this);
    7.  
    8. }
    9.  
    10.  
    11. }



    Make your listener class that implements Listener. Notice a couple of things here:
    1. each event has @EventHandler
    2. method names can be whatever you want (be descriptive)
    3. It's NOT the method NAME that matters... it's the method parameters that identify what kind of event the method is listening for and handling.
    4. The name of the argument is normally "event" or "evt" or just "e"
    But i've seen some people do "ioe" for InventoryOpenEvent.
    I like to always do "e" so I know that I'm using a parameter VS a class field.
    ExampleListener.java (open)

    Code:java
    1. public class ExampleListener implements Listener {
    2.  
    3. Main plugin;
    4.  
    5. // constructor
    6. public ExampleListener() {
    7. plugin = (Main) Bukkit.getServer().getPluginManger().getPlugin("YourPluginName");
    8. }
    9. @EventHandler (priority = EventPriority.NORMAL)
    10. public void onInventoryClose(InventoryCloseEvent e) {
    11.  
    12. }
    13. @EventHandler (priority = EventPriority.NORMAL)
    14. public void onInventoryOpen(InventoryOpenEvent e) {
    15.  
    16. }
    17. @EventHandler (priority = EventPriority.NORMAL)
    18. public void onBlockPlace(BlockPlaceEvent e) {
    19. }
    20. @EventHandler (priority = EventPriority.NORMAL)
    21. public void onBlockBreak(BlockBreakEvent e) {
    22. }
    23. }



    So to answer your question about "where you get these variables from"... You can use the argument "e" from an event to gain a lot of information about the Minecraft world: players, blocks, etc.

    You could use one Event to just record information into a HashMap... Then you could use another Event to get the information from the HashMap, like last location... Then use "e" + HashMap to calculate your distance:

    Code:java
    1. if (e.getPlayer().getLocation().distance(LAST_LOC) > 10) {
    2. e.getPlayer().sendMessage("You traveled far!");
    3. e.getPlayer().sendMessage("Distance: "
    4. + e.getPlayer().getLocation().distance(LAST_LOC);
    5. }
     
Thread Status:
Not open for further replies.

Share This Page