Need help with player location Pwease :(

Discussion in 'Plugin Development' started by mrgreen33gamer, Mar 29, 2014.

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

    mrgreen33gamer

    Hello, please i really need help with this, i want to spawn several potions but i want them to spawn at different locations from the players location, for instance: If i want to spawn 4 potions, i want 1 to spawn in front of me, 1 to spawn behind me, 1 to spawn to my left, and 1 to spawn to my right

    So here is my basic code, if anyone can fix it for me or ask me to add something or fix something, please tell me

    Code:java
    1.  
    2. @EventHandler
    3. public void testPotion(PlayerInteractEvent e) {
    4.  
    5. Player p = (Player) e.getPlayer();
    6.  
    7. if (e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    8. if (e.getItem().getType() == Material.BLAZE_POWDER) {
    9.  
    10.  
    11.  
    12. Potion potion = new Potion(PotionType.POISON, 2);
    13. potion.setSplash(true);
    14.  
    15. ItemStack itemStack = new ItemStack(Material.POTION);
    16. potion.apply(itemStack);
    17. ThrownPotion thrownPotion = p.launchProjectile(ThrownPotion.class);
    18. p.sendMessage(ChatColor.GRAY + "Potions released!");
    19.  
    20. thrownPotion.setItem(itemStack);
    21.  
    22. }
    23. }
    24. }
    25.  


    Any ideas or help? : (
     
  2. Offline

    xDGaming

    you would get the players location then add/subtract from the x axis and z axis
     
    cubesteak likes this.
  3. Offline

    itzrobotix

    Why are you casting e.getPlayer() to (Player) when it already returns a player, remove (Player) not required.
     
  4. Offline

    xDGaming

    I didn't notice that when trying to help xD
     
  5. Offline

    itzrobotix

    I saw the cast and just went ok, noticed he was getting a player and went .-.
     
  6. Offline

    CraftBang

    Cant you just do like X - 2 and + 2 and Z -2 and + 2

    So

    Location loc1 = new Location(p.getWorld(), p.getLocation().getX() + 2, p.getLocation().getY(), p.getLocation().getZ());

    And loc2 would be X - 2, loc 3 Z-2 loc 4 Z+2

    I'm not sure if p.getWorld() and p.getLocation().getX() is right! but I hope you understand what I mean.
     
  7. Offline

    SomewhatEvil

    Yea, Craftbang should right, just get the players location then add to the x or y axis, if your not sure how to do this ill show u the code.
     
  8. Offline

    mrgreen33gamer

    I solved it, i had to spend a little bit of spare time working on it. Thanks for your help everyone!
     
Thread Status:
Not open for further replies.

Share This Page