Grappling Hook doesn't work as it should

Discussion in 'Plugin Development' started by Gabibbo, Jul 13, 2021.

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

    Gabibbo

    I am trying to create a plugin that everytime the player interacts shoots a web and sends him to the targeted block, i did Main and my listener but it's not working most of the times and if it manages to work it just makes the player jump around:
    Main file:
    Code:
    package me.gvells.spiderman;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    import me.gvells.spiderman.listeners.SpidListen;
    
    public class Main extends JavaPlugin{
        public boolean on;
      
        public String spidey;
        public void onEnable() {
            new SpidListen(this);
          
        }
    
    }
    
    Listener:
    Code:
    package me.gvells.spiderman.listeners;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitScheduler;
    import org.bukkit.util.Vector;
    
    import me.gvells.spiderman.Main;
    
    
    public class SpidListen implements Listener{
        private Main plugin;
        private int task1;
        public SpidListen(Main plugin) {
            this.plugin = plugin;
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
      
        @EventHandler
        public void SwingEvent(PlayerInteractEvent e) {
          
            if(e.getAction() == Action.LEFT_CLICK_AIR) {
                Player p = e.getPlayer();
                Block b = p.getTargetBlock(null, 120);
                if(b != null && !b.getType().isAir()) {
                    World w = p.getWorld();
                    Location l = p.getLocation();
                    LivingEntity bat = (LivingEntity) w.spawnEntity(l, EntityType.BAT);
                    bat.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY,100000,10000));
                    bat.addPotionEffect(new PotionEffect(PotionEffectType.SLOW,100000,100000));
                    Vector v = l.getDirection();
                    Arrow arr = w.spawnArrow(l.add(v.clone().normalize().multiply(2)),v , 10f, 0);
                    bat.setLeashHolder(arr);
                    BukkitScheduler sched = p.getServer().getScheduler();
                    task1= sched.scheduleSyncRepeatingTask(plugin, new Runnable(){
    
                        @Override
                        public void run() {
                            if(arr.isInBlock()) {
                                p.teleport(p.getLocation().add(0,0.5,0));
                                p.setVelocity(v.clone().multiply(5));
                                p.setFallDistance(-100f);
                                bat.remove();
                                arr.remove();
                                sched.cancelTask(task1);
                            }
                            if(arr.getTicksLived() >= 80){
                        bat.remove();
                        arr.remove();
                        sched.cancelTask(task1);
                    }
                          
                        }
                      
                    }, 0L, 10L);
                }
            }
          }
        
        }
    Plugin.yml:
    Code:
    name: SpiderMan
    version: 1.0
    api-version: 1.17
    auhtor: Happy Di Piero
    main: me.gvells.spiderman.Main
    description: Swing like Spidey
    I forgot that in the console is also shown:
    playername moved wrongly!
     
    Last edited: Jul 13, 2021
  2. Offline

    Tim_M

    Hello! It seems you are not registering you eventlistener correctly here's how you do it:
    Code:
            Bukkit.getPluginManager().registerEvents(EventListenerClass(), this);
    
    EDIT: oops I'm dumb
     
  3. Offline

    davidclue

    @Gabibbo
    Switch your task method with this, it's untested and if it doesn't work I'll try something else tomorrow.
    Code:
    Location loc = b.getLocation();
    task1 = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
        @Override
        public void run() {
            if(arr.getTicksLived() >= 80) {
                bat.remove();
                arr.remove();
                sched.cancelTask(task1);
                return;
            }
            if(!arr.isInBlock()) return;
            p.setVelocity(new Vector(p.getLocation().getX() + loc.getX(), p.getLocation().getY() + loc.getY(), p.getLocation().getZ() + loc.getZ()).normalize().multiply(0.5));
            p.setFallDistance(-100f);
            bat.remove();
            arr.remove();
            sched.cancelTask(task1);
        }
    }, 0L, 10L);
     
    Last edited: Jul 27, 2021
Thread Status:
Not open for further replies.

Share This Page