Pushing entities away

Discussion in 'Plugin Development' started by Woobie, Oct 15, 2012.

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

    Woobie

    I'm trying to push entites away from the player, if the player right clicks with a stick.. but doesnt seem to work.
    Code:
        @EventHandler
            public void onPush(PlayerInteractEvent e){
            Player player = e.getPlayer();
            if (e.getAction().equals(Action.RIGHT_CLICK_AIR)
                    || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                if (e.getPlayer().getItemInHand().getType() == Material.STICK) {
           
            Integer dist = 5;
            List<Entity> nearby = player.getNearbyEntities(dist, dist, dist);
            for (@SuppressWarnings("unused") Entity entity : nearby) {
                Location loc = player.getLocation();
                Location mloc = ((Entity) e).getLocation();
                Double distance = mloc.distance(loc);
                if (distance <= dist) {
                    ((Entity) e).setVelocity((new Vector(mloc.getX() - loc.getX(), mloc.getY() - loc.getY(), mloc.getZ() - loc.getZ())).multiply(dist - distance).add(((Entity) e).getVelocity()));
                }
                
    Had some red lines on the code, so i just did what Eclipse suggested.
     
  2. Offline

    TwistedMexi

    Here's some working code from andf54 posted elsewhere, it may help:

    Code:
    /**
    * Pushes away an entity from the player
    *
    * @param entity entity
    * @param speed speed
    */
    public void pushAwayEntity(Entity entity, double speed) {
    // Ignore if the player isn't online:
    if(!isOnline()){
    return;
    }
    // Get velocity unit vector:
    Vector unitVector = entity.getLocation().toVector().subtract(player.getLocation().toVector()).normalize();
    // Set speed and push entity:
    entity.setVelocity(unitVector.multiply(speed));
    }
     
    Ultimate_n00b likes this.
  3. Offline

    Woobie

    Didnt really help me at all.. how can i use that code to push entities back with a stick?
     
  4. Offline

    TwistedMexi

    I thought the part that wasn't working was the actual entity movement. The code above accomplishes that so you should be able to reference it and figure out what's wrong.

    As far as doing it with a stick, you would just check to make sure they have a stick in their hand before executing the code, but it looks like you're already doing that fine.

    Please clarify on what part of this isn't exactly working, thanks.
     
  5. Offline

    Woobie

    Well, i didnt even get to the testing part, becouse i dont know how to fix all the red lines.
     
  6. Offline

    CorrieKay

    What do the red lines say?
     
  7. Offline

    TwistedMexi

    The line "so i just did what Eclipse suggested." made is seem as if you had worked those parts out. If that's not the case we'll definitely need to know what errors you're receiving, as Corrie noted.
     
  8. Offline

    Woobie

    Oh.. i thought you were talking about
    The code i created just simply does nothing, no errors in console.
     
  9. Offline

    CorrieKay

    do you... call the method anywhere..?
     
  10. Offline

    Woobie

    Nope.. some helpful code would be appreciated.
     
  11. Offline

    TwistedMexi

    I intended that for reference, not for use. I need to know what is exactly going on in your code before I can suggest the next steps.

    if your code has no errors, but is doing nothing, then add a System.out.println("This code ran");
    to the inside of your if statements to make sure that part is even running.
     
  12. Offline

    CorrieKay

    uh.. you do know how to call methods, right?
     
  13. Offline

    Woobie

    Maybe.... :3
     
  14. Offline

    CorrieKay

    call the method then silly. if you call it when a player interacts with another entity, (in the player interact event) it should work.
     
    TwistedMexi likes this.
  15. Offline

    Woobie

    If you edit the code i posted, i will love you forever :3
     
  16. Offline

    TwistedMexi

    Though you should have really looked it up yourself, here's how you would implement the method I provided. Your part of the code should look like this:

    Code:
    @EventHandler
    public void onPush(PlayerInteractEntityEvent e){
    Player player = e.getPlayer();
    if (e.getPlayer().getItemInHand().getType() == Material.STICK) {
      Entity ent = e.getRightClicked();
      pushAwayEntity(ent, speedyouwanthere);
      }
    }
    public void pushAwayEntity(Entity entity, double speed) {
    // Get velocity unit vector:
    Vector unitVector = entity.getLocation().toVector().subtract(player.getLocation().toVector()).normalize();
    // Set speed and push entity:
    entity.setVelocity(unitVector.multiply(speed));
    }
     
                
    replace speedyouwanthere with your speed, of course.
     
  17. Offline

    CorrieKay

    Not to be condescending or anything, but if you don't know how to utilize methods, you should watch this. This is basic java stuff. Based on the code you originally posted in the OP, if you dont know how to do this, im curious as to how you even know some of the things required to even write that code, such as knowledge of enums, and class casting..
     
    TwistedMexi likes this.
  18. Offline

    gcflames5

    Yeah I agree with Corrie, plugins aren't all that simple, I suggest thenewboston's beginner java series on YouTube, because calling methods is learned on day one of java. Sorry I'm harsh but its the truth :/
     
  19. Offline

    Woobie

    If you people would tell me what "method" actually is. I dont know the coding terms, also I dont have that good english.
    Pretty sure I know how to use "methods", but I just dont know what part of the code it is.
     
  20. Offline

    gcflames5

    A method would be:
    Code:
    public void methodName() {
        //code here
    }
    To call the method:
    Code:
    methodName();
     
  21. Offline

    TwistedMexi

    I understand there's a slight barrier as far as language goes, but the main thing here is some things you need to go learn yourself (including what "Methods" refer to): http://lmgtfy.com/?q=Java Methods
     
  22. Offline

    Woobie

    A small problem,
    Code:
    (player.getLocation().toVector()).normalize();
    player has red line under it
    Code:
    player cannot be resolved
    Also, there is no event for the method, so i cant do
    Code:
    Player player = e.getPlayer();
    How can I fix this? Sorry I'm being a noob :3
     
  23. Offline

    TwistedMexi

    A method can carry variables from the place it is called from.

    You get your player in your OnPush Event, you can pass it to the method by adding a variable to it.

    Example:

    You would call the below method like this:

    ThisMethod(player, somestring, somelocation);

    ThisMethod(Player p, String s, Location l)
    {
    if (l.getBlock.getType().equals(Material.CHEST)
    {
    p.sendMessage(s);
    }
    }
     
  24. Offline

    Woobie

    Not sure if I'm retarted,, but still not working
    Code:
        @EventHandler
        public void onPush(PlayerInteractEntityEvent e){
        if (e.getPlayer().getItemInHand().getType() == Material.STICK) {
          Entity ent = e.getRightClicked();
          pushAwayEntity(ent, 3, player);
          }
        }
        public void pushAwayEntity(Entity entity, double speed, Player p) {
        // Get velocity unit vector:
        Vector unitVector = entity.getLocation().toVector().subtract(p.getLocation().toVector()).normalize();
        // Set speed and push entity:
        entity.setVelocity(unitVector.multiply(speed));
     
        }
        
    Tried that, with this
    Code:
    p.setVelocity(unitVector.multiply(speed));
    instead of
    Code:
    entity.setVelocity(unitVector.multiply(speed));
    And then i tried this
    Code:
        @EventHandler
        public void onPush(PlayerInteractEntityEvent e){
        if (e.getPlayer().getItemInHand().getType() == Material.STICK) {
          Entity ent = e.getRightClicked();
          pushAwayEntity(ent, 3);
          }
        }
        public void pushAwayEntity(Entity entity, double speed) {
        // Get velocity unit vector:
        Vector unitVector = entity.getLocation().toVector().subtract(entity.getLocation().toVector()).normalize();
        // Set speed and push entity:
        entity.setVelocity(unitVector.multiply(speed));
     
        }

    EDIT: It works, but it pushes the entity down, not forward/up
    EDIT2: it works on mobs only.
     
  25. Offline

    TwistedMexi


    ok HOLD UP for a minute, quit changing code. Go back to what you pasted the first time (the one I left in the quote), and add this line
    Player player = e.getPlayer();

    back to the onPush event. Looks like you probably moved it to the method and deleted it without putting it back.
     
  26. Offline

    Woobie

    Okay.. sorry :D
    My code atm, it works on mobs only, but it pushes them down.
    Code:
        @EventHandler
        public void onPush(PlayerInteractEntityEvent e){
        Player player = e.getPlayer();
        if (e.getPlayer().getItemInHand().getType() == Material.STICK) {
          Entity ent = e.getRightClicked();
          pushAwayEntity(ent, player, 3);
          }
        }
        public void pushAwayEntity(Entity entity, Player p, double speed) {
        // Get velocity unit vector:
        Vector unitVector = entity.getLocation().toVector().subtract(p.getLocation().toVector()).normalize();
        // Set speed and push entity:
        p.setVelocity(unitVector.multiply(speed));
       
        }
     
  27. Offline

    TwistedMexi

    Just a side-note, if player will not be used outside of the if item = stick statement, it's more efficient to call it from inside, so that the event has to do 0 work if they don't have a stick.

    As for the mobs, what do you mean? What all did you intend for this to affect? item drops as well?

    For the direction, try toying with the unitVector and speed a bit, but if you get really oddball results, return back to how it is above and try again. (try .add instead of .subtract for starts, but not sure.)
     
  28. Offline

    Woobie

    This works on mobs only, I was trying to push players like 10 blocks away.. kind of like a shockwave.
     
  29. Offline

    TwistedMexi

    For that you're probably going to need to take over
    EntityDamageEvent

    and use .DamageCause = ENTITY_ATTACK to cancel the damage when the attacker has a stick and then push the away with the same code in your method.
     
  30. Offline

    Woobie

    Code:
    Entity ent = e.getRightClicked();
    Right click doesnt cause damage..
     
Thread Status:
Not open for further replies.

Share This Page