Solved Throwing back an entity

Discussion in 'Plugin Development' started by Robin Bi, Aug 7, 2014.

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

    Robin Bi

    Hi there!


    For my current project i need to throw back entities when they're rightclicked. They shall fly way behind, in the direction the damager looks at.

    For testing, i wrote the following code:
    Code:java
    1.  
    2. import org.bukkit.entity.Player;
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7. import org.bukkit.util.Vector;
    8.  
    9. public class Main extends JavaPlugin implements Listener {
    10.  
    11. public void onEnable() {
    12. getServer().getPluginManager().registerEvents(this, this);
    13. }
    14.  
    15. @EventHandler
    16. public void onDamageEntity(EntityDamageByEntityEvent e) {
    17. if (e.getDamager() instanceof Player) {
    18. Vector direction = e.getDamager().getLocation().getDirection();
    19. e.getEntity().setVelocity(new Vector(direction.getX() * 20, direction.getY() * 5, direction.getZ() * 20));
    20. }
    21. }
    22.  
    23. }


    When i now right click an entity, it is thrown back, but it does not gain any height, regardless if i multiply direction.getY() by 5 or by 50. Does not make any sense to me, i hope you can tell me why it is like this. :)


    ~Robin Bi
     
  2. Offline

    Deleted user

    Code:java
    1. Entity.setVelocity(Player.getLocation().getDirection().multiply(2D));
     
  3. Offline

    Robin Bi

    Joiner Sorry, but this seems to not multiply the Y as well. The entities don't raise.
     
  4. Offline

    Deleted user

    Robin Bi It depends on where the player is in relation to the entity.
     
  5. Offline

    Robin Bi

    Joiner Yeah, didn't get this, sorry. But then i have to change the line of code anyways because the entitie should fly high in the air, also if the damager does look at the ground...
     
  6. Offline

    Deleted user

    Robin Bi
    Code:java
    1. Vector direction = Player.getLocation().getDirection().multiply(2D);
    2. Entity.setVelocity(direction.setY(1));
     
  7. Offline

    Robin Bi

    Joiner Now it's kinda embarrassing but i see no difference between
    Code:java
    1. Vector direction = e.getDamager().getLocation().getDirection().multiply(2D);
    2. e.getEntity().setVelocity(direction.setY(1));

    and
    Code:java
    1. Vector direction = e.getDamager().getLocation().getDirection().multiply(10D);
    2. e.getEntity().setVelocity(direction.setY(50));
     
  8. Offline

    Deleted user

    Why?
     
  9. Offline

    Robin Bi

    Joiner Because you're giving me one solution after the other and I just don't get it ^^
     
  10. Offline

    Deleted user

  11. Offline

    Robin Bi

    Joiner Well you seem to have experience with velocities! Don't you have any idea left? :oops:
     
  12. Offline

    Deleted user

    No.
    Code:java
    1. Entity.setVelocity(Entity.getVelocity().setY(5));
     
  13. Offline

    Robin Bi

    Joiner Does not work either, but thanks for your time.
     
  14. Offline

    Deleted user

  15. Offline

    Robin Bi

    #Bump?
     
  16. Offline

    Deleted user

    Robin Bi This works. If not, make a video.
    Code:java
    1. Entity.setVelocity(Player.getLocation().getDirection().multiply(3));
     
  17. Offline

    Robin Bi

    Joiner Yes it works, but as i mentioned before in this thread, it does not throw the entity as high in the air as i want. And multiplying the Y for its own did not work either, as we bothes detected.
     
  18. Offline

    Deleted user

    Robin Bi
     
  19. Offline

    fireblast709

    Robin Bi get the player's direction Vector, multiply by a number (depending on how far you want it to fly), and manually set the y to a value (> 1 should make them fly noticeably high)
     
  20. Offline

    Robin Bi

    For everyone who has the same problem:

    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageByEntityEvent event) {
    3. event.setCancelled(true);
    4. Vector direction = event.getDamager().getLocation().getDirection();
    5. event.getEntity().setVelocity(new Vector(direction.getX() * 2.5D, 1.5D, direction.getZ() * 2.5D));
    6. }

    works. The problem was that the Minecraft-integrated knockback when damaging an entity was "more important" than my own Velocity. When cancelling the event first, the Entity is not damaged (what can be done via another line of code though) but thrown away.

    However, thanks to everyone who helped me.
    fireblast709 , Joiner



    ~Robin Bi
     
Thread Status:
Not open for further replies.

Share This Page