How do you get a players location from 12 blocks?

Discussion in 'Plugin Development' started by xXCapzXx, Jul 28, 2015.

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

    xXCapzXx

    So I am making a plugin that if you have a stick, it will heal any player within 12 blocks.

    So this is what I am trying to do.

    You get the player location and then you + 12;

    It maybe incorrect. But can someone help me on this.
     
  2. Offline

    mine-care

    @xXCapzXx Where you add the '12'? x? z? It can work but it will be an absolute pain especially for diagonal locations. For that reason bukkit has a method in the "Location" class called "distance" so if you want to check the distance between two locations it is the best to use.
    But your question has a very simple answer comming from the Entity class. It is the method called "getNearbyEntities" this returns a List of entities in the range you specify (like 12x 12y and 12z) then you need to loop though all of them to check if they are players and perform anything on them! :D
     
  3. Offline

    xXCapzXx

    Thanks, but can you do the line of code for it? I am not sure what you mean but I know in the same time. Can you type the code?
     
  4. Offline

    mine-care

    @xXCapzXx im afraid i cant (specifically i can but i dont want to) In these forums we are not spoonfeeding people because we understand that what is most likely to happend afterwards is a copy - paste action by the one(s) asking that will most of the time result in no knowledge gain therefore we usually let people develop their skills by trying pieces of code themselves and when they reach the dead end we are here to give them a litle walktrough but not the entire solution.

    So i wont do the code. However i will point you twards the right direction to find the solution, its you coding afterall not me.
    Take a look:
    http://d.ld.lc/interfaceorg_1_1bukkit_1_1entity_1_1Entity.html
    (check the "getNearbyEntities" method)
    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
    (check the 'forEach' loop, or how to iterate over Iterable implementations)
    http://www.java2s.com/Tutorial/Java/0060__Operators/TheinstanceofKeyword.htm
    (read this whole thing)
    https://howtoprogramwithjava.com/java-cast/

    All you need to know to do this piece of code is listed above ^
    I didnt include the If statements assuming you are already aware of them.
    good luck, if and when you reach a code dead-end we will be here to help you with your code :D
     
  5. Offline

    xXCapzXx

    I still am not sure.
    I know its kind of spoon feeding, but I really need this done.
     
  6. Offline

    mine-care

    @xXCapzXx And still i cant do the code :- ) What are you unsure about? Have a try :D its easyer than you think and if you het stuck ill be here, send me your code (in this thread) and i will do my best to help.
     
  7. Offline

    xXCapzXx

    I really need help with this please.

    Code:
    
    public void onEnable() {
    
    Bukkit.getServer().getPluginManager().registerEvents(this,  this);
    
    }
    
    
    @EventHandler
    
    public void click(PlayerInteractEvent e) {
    
    Player player = e.getPlayer();
    
    Player target = e.getPlayer();
    
    Location loc = player.getLocation();
    
    if(e.getAction() == Action.RIGHT_CLICK_AIR) {
    
    if(e.getItem().getType() == Material.INK_SACK) {
    
    
    }
    
    
    }
    
    }
    
    
    
    }
    
    When you click an ink sac, it will heal a player your looking at of the distance of 12 blocks.
    I want to get the distance first of Player
    And then get the target from the distance of 12 blocks
    then heal them
    if they are not looking at the target if(target == null)
    player.sendMessage("cant find target);

    That is my plan.
     
  8. Offline

    N00BHUN73R

    @xXCapzXx
    All you have to do as @mine-care has been telling you is to make a for loop that loops through all the entities with getNearbyEntites(x, y, z)
     
    mine-care likes this.
  9. Offline

    xXCapzXx

    Code:
    for(int i = 0; i > 12; i++) {
    
    player.getNearbyEntities(12, 12, 12);
    
    }
    
    Like that?

    Nevermind I did it.

    But is this correct?
    The player you are looking at within 12 block range, gets healed 4 hearts?

    code:
    Code:
    
    public void onEnable() {
    
    Bukkit.getServer().getPluginManager().registerEvents(this,  this);
    
    }
    
    
    @EventHandler
    
    public void click(PlayerInteractEvent e) {
    
    Player player = e.getPlayer();
    
    Player target = e.getPlayer();
    
    Location loc = player.getLocation();
    
    if(e.getAction() == Action.RIGHT_CLICK_AIR) {
    
        if(e.getItem().getType() == Material.INK_SACK) {
    
    for(Entity en : player.getNearbyEntities(12, 12, 12)) {
    
    player.sendMessage(ChatColor.GREEN + "You healed" + (en instanceof Player ? ((Player) en).getName() : en.getType().name()));
    
    if(en instanceof Player) {
    
    ((Player) en).setHealth(8);
    
    }
    
    }
    
    }
    
        }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  10. Offline

    mine-care

    Why is target needed?
     
  11. Offline

    xXCapzXx

    Oh, thats useless, dont mind that.
     
  12. @xXCapzXx Please tell me the code isn't indented like that and the copy & pate screwed up.
     
  13. Offline

    1Rogue

  14. Offline

    xXCapzXx

    Awesome code, I'll use it.
    I am not sure what I sure put in //Map of players in range to the distance away they are (could apply a multiplier based on this)?
     
  15. Offline

    1Rogue

    That would be the Map below it, the comment just explained what it was.
     
  16. Offline

    ChipDev

    Sorry, but it won't be as easy as adding.
    You could use nearbyEntites, but I would use distanceSquared(12*12)
     
Thread Status:
Not open for further replies.

Share This Page