Teleport when hit by mob

Discussion in 'Plugin Development' started by DevDesires, Nov 19, 2018.

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

    DevDesires

    Hello I am trying to work on a code that when a person is hit by a mob, they will teleport to a region in a set x,y,z range! This is what I have so far:
    Code:
    import java.util.Random;
    import org.bukkit.Location;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    
    
    public class MobsEvents implements Listener{
    
    
    
    @EventHandler
    
    public void EntityHitEntityEvent(EntityDamageByEntityEvent event){
    
    Entity damager=event.getDamager();
    Random random = new Random();
    Location teleportLocation=null;
    Entity player=event.getEntity();
    
    int x = random.nextInt(2888) + 1;
    int y = random.nextInt(2) + 1;
    int z = random.nextInt(-328) + 1;
    
    teleportLocation=newLocation(player.getWorld(),x,y,z);
    
    
    if(!(damager instanceof Player)){
    
    
    player.teleport(newLocation(player.getWorld(),
    teleportLocation.getX(),
    teleportLocation.getY(),
    teleportLocation.getZ()));
    
    }
    
    }
    
    }


    I would appreciate some help or just feedback on ways I could go along getting this fixed.
     
    Last edited by a moderator: Nov 19, 2018
  2. Offline

    timtower Administrator Administrator Moderator

    Moved to Plugin debelopment.

    @ShewRadical What is wrong then? What part do you need help with?
     
  3. Offline

    DevDesires

    the code is not working. When the mob hits me it just gives me damage instead of teleporting me into the random set coordinates. Also, I do not know how to make a set range to allow the teleportation to work when a mob hits me. I have a maze and I want to get teleported randomly within the first half of it when I am hit by a mob.

    @timtower I think it does not work because of how the Player player is an entity and how the the if statement work together.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 19, 2018
  4. Offline

    timtower Administrator Administrator Moderator

    Use player.teleport(teleportLocation).
    And did you register the event?
     
  5. Offline

    DevDesires

    @timtower the event is registered and i changed it to teleportLocation it is still not working?

    here is my error code: https://justpaste.it/791gf

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 19, 2018
  6. Offline

    timtower Administrator Administrator Moderator

    @ShewRadical You never said anything about an error...
    And nextInt goes positive only, you are inputting a negative number.
     
  7. Offline

    DevDesires

    @timtower my z location is negative and the error I get it inside the console but my code is not giving me an error.
     
  8. Offline

    timtower Administrator Administrator Moderator

    @ShewRadical Because the compiler doesn't know those rules.
    The value inside the random should be the range that you allow, then you can add or substract the offset.
     
Thread Status:
Not open for further replies.

Share This Page