Solved Disable gravity for FishHook's?

Discussion in 'Plugin Development' started by critikull, Aug 5, 2018.

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

    critikull

    Hi there,

    I'd like to disable FishHook's from falling due to gravity when they hit a block but I cannot seem to get it working using `setGravity(false)`. Is there a way to achieve this?

    Code:
      @EventHandler
       public void onProjectileHit(ProjectileHitEvent e) {
         if (e.getEntity() instanceof FishHook) {
           FishHook fishHook = (FishHook) e.getEntity();
           if (fishHook.getShooter() instanceof Player) {
             Player player = (Player) fishHook.getShooter();
               if (e.getHitBlock() != null) {
                 fishHook.setGravity(false);
                 // Also tried using a recurring bukkit task to set velocity
                 // And also tried using a recurring bukkit task to teleport the FishHook to e.getHitBlock().getLocation()
               }
           }
         }
       }
    
     
  2. Offline

    Tango_

    @critikull
    You could possibly spawn an invisible armour stand and set the hook as the armour stands passenger like so

    Code:
        @EventHandler
        public void hookStuck(ProjectileHitEvent e) {
            if (e.getEntity() instanceof FishHook) {
                FishHook fishHook = (FishHook) e.getEntity();
                if (fishHook.getShooter() instanceof Player) {
                    Player player = (Player) fishHook.getShooter();
                    if (!e.getHitBlock().getLocation().getBlock().isEmpty()) {             
                        Location hitblock = e.getHitBlock().getLocation().add(0.5, 0, 0.5);
                        ArmorStand armorStand = player.getWorld().spawn(hitblock, ArmorStand.class);
                        armorStand.addPassenger(fishHook);
                        armorStand.setGravity(false);
                        armorStand.setVisible(false);
                        armorStand.setSmall(true);
                        armorStand.setArms(false);
                        armorStand.setMarker(true);
                        armorStand.setBasePlate(false);
                        fishHook.setGravity(false);
                        fishHook.setBounce(true);
                        fishHook.setMetadata("hook", new FixedMetadataValue (this, ""));
                    }
                }
            }
        }
    
        @EventHandler
        public void hookRemove(PlayerFishEvent e){
            Player p = e.getPlayer();
            FishHook hook = e.getHook();
            if(p instanceof Player){
                if(e.getState() == State.IN_GROUND && hook.hasMetadata("hook")) {
                    hook.removeMetadata("hook", this);
                    hook.getVehicle().remove();
                }
            }
        }
    This code isn't polished, so the hooks position may look strange, but I am sure you can fix that easily.
     
    Last edited: Aug 6, 2018
  3. Offline

    critikull

    With a few edits that worked flawlessly. Much appreciated @Tango_!
     
  4. Offline

    Tango_

    Glad I could help :)
     
Thread Status:
Not open for further replies.

Share This Page