Increase the swimming speed of a player

Discussion in 'Plugin Development' started by JUSTCAMH, May 10, 2015.

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

    JUSTCAMH

    I want to be able to increase the swimming speed of the player. Obviously this would require the use of Vectors and the PlayerMoveEvent, but I have no idea how I would do it. Help is appreciated as ever!
     
  2. Yea, the PlayerMoveEvent would be useful for this, e.g.:
    Code:
    //In your PlayerMoveEvent
    Location from = e.getFrom();
    Location to = e.getTo();
    if(from.getBlock().getType().name().endsWith("WATER")) { //Check if it's water
      Vector changed = to.clone().subtract(from).toVector(); //Get the difference between the locations
      e.setTo(from.clone().add(changed.multiply(2)); //Set the new location, but twice the speed as usual, you can change the 2 to any amount, depending how fast you want it to go
    }
    I haven't tested it, it might look at bit glitchy but you need to test it out :p
     
  3. Offline

    mine-care

  4. Offline

    nbrandwine

    >when you want to change the walkspeed of a player
    >using that code
    heh

    You could just apply a potion effect every so and so seconds.

    Code:
    Player p  = blah;
    
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 18000, 2));
    
     
  5. Offline

    mine-care

    @nbrandwine but it will become obvious and people will see the potion effect particles or the effect next to player inventory... I'll see if I can find a nms way for speed potion as I did with invisibility potion :- )
     
  6. Offline

    nbrandwine

    I think there's a way to cancel the potion effect packet. I know Essentials or Vanish has done it before.
    I'll get back to you with that.
     
  7. Minecraft 1.8 supports hiding particle effects from potions... Also I think speed and walk speed both only increases the walk speed
     
    nbrandwine likes this.
  8. Offline

    mine-care

    @megamichiel indeed but in these forums 1.8 Is not supported so I have to stay within the supported versions.


    @nbrandwine packet sniffing would make you able to keep the potion only client side but requires libraries or makes your plugin version depended In a way.
     
  9. Offline

    nbrandwine

    didn't read. maybe this will help https://gist.github.com/aadnk/8946262
     
  10. Offline

    mine-care

    @nbrandwine this makes your plugin depended on external libraries
     
  11. @mine-care
    Also trying to intercept the packet is hard because you can't just get the corresponding entity to a particle effect
     
  12. Offline

    mine-care

    nbrandwine likes this.
  13. Offline

    JUSTCAMH

    @mine-care @megamichiel @nbrandwine
    Speed isn't applied when in water! Also, even if this did work (I should have mentioned this when I posted the thread) the speed in the water would need to be very slight changes to the speed. Eg. I wouldn't want speed 1, I would want speed 0.5

    @megamichiel
    I fiddled with your code and here's what I got:
    Code:
    Location from = e.getFrom();
            Location to = e.getTo();
            if (from.getBlock().getType().name().endsWith("WATER")) {
                Vector changed = to.clone().subtract(from).toVector();
                e.getPlayer().setVelocity(changed.multiply(1.5));
            }
    It works and this is pretty much what I wanted. Only problem is that I don't want the vector to change the Y movement. I want only movement in the X and Z cords to be amplified. How might I achieve that?
     
  14. @JUSTCAMH
    Before you set the velocity you can set the changed vector's y to the player's current y
     
  15. Offline

    JUSTCAMH

    @megamichiel
    Thanks! Only problem now is that since water is similar to ice in that when you try to stop it will keep going for a bit, and that small bit is increased due to the speed. Not quite what I wanted but hey! It's all just an addon! What can you expect?
    Code for anyone interested:
    Code:
    Location from = e.getFrom();
            Location to = e.getTo();
            if (from.getBlock().getType().name().endsWith("WATER")) {
                Vector changed = to.clone().subtract(from).toVector().multiply(1.5);
                changed.setY(to.clone().subtract(from).toVector().getY());
                e.getPlayer().setVelocity(changed);
            }
     
  16. Offline

    Zombie_Striker

    Why not use this.
    Code:
    public void run(){
    for(Player p : getServer().getonlinePlayers){
    if(p.getLocaion().getBlock.getType() == Material.WATER){// I don't know which WATER in the material list is for a block,
    if(p.getVelocity().getX() <= 2 && y <= 2 && z <= 2)//makes sure players don't move at speed of light
    p.setVelocity(p.getVelocity.multiply(1.5))
    }
    }
    }
     
Thread Status:
Not open for further replies.

Share This Page