Solved Pushing players around an other player away

Discussion in 'Plugin Development' started by comniemeer, Dec 22, 2014.

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

    comniemeer

    Hi there!
    I want to push away all players around an other player.
    I tried the following, but sometimes a player gets thrown away far more blocks than I want:
    Code:
    for (Entity entity : player.getNearbyEntities(5, 5, 5)) {
        if (entity instanceof Player) {
            Player p = (Player) entity;
           
            Vector playerCenterVector = player.getEyeLocation().toVector();
            Vector playerToThrowVector = p.getEyeLocation().toVector();
           
            double x = playerToThrowVector.getX() - playerCenterVector.getX();
            double z = playerToThrowVector.getZ() - playerCenterVector.getZ();
           
            Vector throwVector = new Vector(x, 1, z).normalize().multiply(2);
           
            p.setVelocity(throwVector);
        }
    }
    I want all players who are located in the radius of 5 blocks away from the player in the middle, to be thrown away in the relative direction.
    I know, my idea is very complicated to understand, so I've made a picture:
    (red = player in center, blue = surrounding players (to be thrown away), green = direction to be thrown away)
    [​IMG]
    As seen in the picture, I want them, no matter where and how far away they stand, to be thrown up (y) 3 Blocks and to be thrown away (xz) 5 Blocks.

    I appreciate your answers very much!
     
  2. Offline

    Funergy

    bump (I actually want to know too, I have never tried this :/)
     
  3. Offline

    comniemeer

    Solved it myself! I use now only "Location loc = player.getEyeLocation()" without ".toVector()". That solved it... >.<
    Here is the code:
    Code:
    for (Entity entity : player.getNearbyEntities(5, 5, 5)) {
        if (entity instanceof Player) {
            Player p = (Player) entity;
           
            Location playerCenterLocation = player.getEyeLocation();
            Location playerToThrowLocation = p.getEyeLocation();
           
            double x = playerToThrowLocation.getX() - playerCenterLocation.getX();
            double y = playerToThrowLocation.getY() - playerCenterLocation.getY();
            double z = playerToThrowLocation.getZ() - playerCenterLocation.getZ();
           
            Vector throwVector = new Vector(x, y, z);
           
            throwVector.normalize();
            throwVector.multiply(1.5D);
            throwVector.setY(1.0D);
           
            p.setVelocity(throwVector);
        }
    }
     
  4. Offline

    API_Tutorials

  5. Offline

    comniemeer

    API_Tutorials likes this.
  6. Offline

    _Filip

    @comniemeer Thanks for posting your solved code here, I respect that.
     
  7. Offline

    comniemeer

    You could do that very easy yourself. And for my needs, I don't need that.
    No problem :)
     
Thread Status:
Not open for further replies.

Share This Page