Spawning Player on Arrow Hit and No Damage

Discussion in 'Plugin Development' started by Rangaofdeath, Oct 12, 2014.

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

    Rangaofdeath

    Hello!

    I am making a plugin for my own server and was wondering why this code is not working. One of my friends said it should work and he is not sure why it is not. Your guys help would be greatly appreciated.

    Code:java
    1. package me.Rangaofdeath;
    2.  
    3. import org.bukkit.entity.Arrow;
    4. import org.bukkit.entity.Entity;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    9.  
    10. public class RangaListener implements Listener {
    11. Hunters plugin;
    12. public RangaListener(Hunters instance) { plugin = instance; }
    13.  
    14. @EventHandler
    15. public void death(EntityDamageByEntityEvent event) {
    16. Entity e = event.getEntity();
    17. Entity damager = event.getDamager();
    18.  
    19. if (e instanceof Arrow) {
    20. Player player = (Player) e;
    21.  
    22. player.teleport(player.getWorld().getSpawnLocation());
    23. event.setDamage(0);
    24. }
    25. }
    26. }
    27.  


    Let me know if you see the problem!
     
  2. Offline

    Monkey_Swag

    You're checking if e is an instance of Arrow, then casting Player to e.....
     
  3. Offline

    97WaterPolo

    Rangaofdeath
    Code:
    if (e instanceof Arrow) {
    Player player = (Player) e;
    This is where you error lies. You are checking to see if the entity is an ARROW, but then you are casting the ARROW to a player, which should throw some Cast Exceptions. What you are looking for is

    Code:java
    1. if (e instanceof Arrow ) {
    2. if (((Arrow)e).getShooter() instanceof Player){
    3. Player player = (Player) ((Arrow)e).getShooter();
    4. }
    5. }
     
  4. Offline

    Rangaofdeath

    I followed used what you gave me but still does not work.

    Here is the updated code
    Code:java
    1. package me.Rangaofdeath;
    2.  
    3. import org.bukkit.entity.Arrow;
    4. import org.bukkit.entity.Entity;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    9.  
    10. public class RangaListener implements Listener {
    11. Hunters plugin;
    12. public RangaListener(Hunters instance) { plugin = instance; }
    13.  
    14. @EventHandler
    15. public void death(EntityDamageByEntityEvent event) {
    16. Entity e = event.getEntity();
    17. Entity damager = event.getDamager();
    18.  
    19. if (e instanceof Arrow) {
    20. if (((Arrow)e).getShooter() instanceof Player) {
    21. Player player = (Player) ((Arrow) e).getShooter();
    22. player.teleport(player.getWorld().getSpawnLocation());
    23. event.setDamage(0);
    24. }
    25. }
    26. }
    27. }


    The point of this is that if a player is shot, they get spawned and no damage is done to them.

    Thanks for your help guys!
     
  5. Offline

    mine-care

    Have you registered events? Have you debuted?
    And instead of setting damage to 0 you could cancel the event ;)
     
  6. Offline

    Rangaofdeath

    I did register the event. I also changed the damage to cancel the event. Still not working.
     
  7. Offline

    Rangaofdeath

    Can anyone help? I need to be able to shoot a player and it will teleport the player that got shot to a certain point.
     
  8. Offline

    fireblast709

    Rangaofdeath the arrow will 'die' regardless of what you do. After teleporting the player, spawn a new arrow with the same direction, position and velocity.
     
  9. Offline

    Rangaofdeath

    fireblast709 Idk what the heck you are talking about... all i want is someone to help me fix what I already have... Here is some revised code. Still not working.
    Code:java
    1. @EventHandler
    2. public void death(EntityDamageByEntityEvent event) {
    3. Entity damager = event.getDamager();
    4. if (damager instanceof Arrow) {
    5. if (((Arrow) damager).getShooter() instanceof Player) {
    6. Player player = (Player) event.getEntity();
    7. player.teleport(player.getWorld().getSpawnLocation());
    8. event.setCancelled(true);
    9. }
    10. }
    11. }
     
  10. Offline

    teej107

    Rangaofdeath He is talking about fixing the problem. I don't know what was so hard to understand about it. If you have a question about a comment, ask!
     
  11. Offline

    sethrem

    Lel your problem is that you are teleporting the player on death which is technically impossible because of the respawn event. So you would have to teleport the player inside of the respawn event. Dahhhhhhh..... But then again you would have to create another method so you could get the location of where the arrow hit so you can use that location in your respawn method. Dahhhh.
     
  12. Offline

    teej107

    sethrem Have you looked at the event he is using? Dahhhhh
     
  13. Offline

    Rangaofdeath

    fireblast709 teej107 Sorry guys, I'm relatively new to coding. Could someone show me how to do that? I would be very grateful.
     
  14. Offline

    fireblast709

    Rangaofdeath spawn a new Arrow (hint: check the docs of World for the method) and set its velocity to that of the old arrow.
     
  15. Offline

    sethrem

    Awe fuck my mad I just looked so his method name lol some people gotta learn how to name shit.
     
Thread Status:
Not open for further replies.

Share This Page