Stop snowball falling with NMS?

Discussion in 'Spigot Plugin Development' started by Plo457, Jul 8, 2015.

  1. Hi Id like to make snowballs keep going straight rather than gradually falling down.

    Ive considered a runnable which adds y-velocity but this is very inefficient.

    I have a spigot development environment setup and am happy to edit the server code but i cannot find where (ive checked Entity.java).

    Help would be wounderful!
     
  2. I'm not sure if it's possible this way, but what I'd try is to create a custom snowball entity and remove all it's gravity-related code, then override the original snowball with that. Again, not sure if that'd work, just a suggestion.
     
  3. The easiest way I know how to do this without having to edit server code is to just set the snowball's velocity to an extremely high value. It's not necessary to use a runnable. Instead, listen for a ProjectileLaunchEvent and then if it's a snowball, set the velocity.
     
  4. Couldnt you just disguise a fireball as a snowball or give a snowball the same characteristics as a fireball (with your method of removing the gravity)
     
  5. This is laying around in one of my old plugins, where i wanted an arrow to go straight for a 'sniper' type of class i built. Instead of an arrow though, i used a snowball.

    It does indeed work and still works for 1.8.7
    Code (Text):
    public class ArrowStraightener {

        private Entity e;
        private Vector v;
        public ArrowStraightener(Entity e, Vector first) {
            this.e = e;
            this.v = first;
        }

        private int id;

        public void stop(){
            Bukkit.getScheduler().cancelTask(id);
        }

        public void start(){
            id = Bukkit.getScheduler().scheduleSyncRepeatingTask(Methods.getPlugin(), new Runnable(){

                @Override
                public void run() {
                    if(e.isValid() && !e.isOnGround()){
                        e.setVelocity(v);
                    }else{
                        Bukkit.getScheduler().cancelTask(id);
                    }
                }
           
            }, 0, 2);
        }


    }
     
    Code (Text):
        @Override
        public void onCast() {
            Methods.debug(player, "Oncast: started");
            Snowball ball = player.launchProjectile(Snowball.class);
            ball.setVelocity(player.getLocation().getDirection().multiply(4.1));//4.5 originally
            ball.setShooter(player);
            new ArrowStraightener(ball,ball.getVelocity()).start();
            removeAfterDelay(ball, 20 * 6);
            SniperListener.ids.add(ball.getUniqueId());
            super.onCast();
            Methods.debug(player, "Oncast: Ended");
        }

    Code (Text):
    private void removeAfterDelay(final Entity e, int delay){
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){

                @Override
                public void run() {
                    if(e.isValid()){
                        e.remove();
                    }
                }
             
            }, delay);
        }
    This can work for any entity. Not just projectiles.
     
    • Like Like x 1