Solved Launching players with velocity glitching out

Discussion in 'Plugin Development' started by TheWolfBadger, Jul 24, 2017.

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

    TheWolfBadger

    Code:
        public void cannonFunction(final Player p) {
            //Cannon Function
            if(inCannon.contains(p.getUniqueId())) {
                //Yup they in cannon
                Location mainCannonLoc = libroCore.stringToLoc(libroCore.getConfig().getString("Settings.CannonTeleportLocation"));
                p.teleport(mainCannonLoc);
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', libroCore.getConfig().getString("Settings.TeleportedToCannonMessage")));
                int effectTasks = Bukkit.getScheduler().scheduleSyncRepeatingTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        p.playEffect(p.getLocation(), Effect.valueOf(libroCore.getConfig().getString("Settings.CannonEffect")), 1);
                        p.playEffect(p.getLocation().clone().add(0, 1, 0), Effect.valueOf(libroCore.getConfig().getString("Settings.CannonEffect")), 1);
                    }
                }, 0, 20L);
                int launchTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        Integer toss = libroCore.getConfig().getInt("Settings.LaunchMultiplier");
                        Bukkit.getScheduler().cancelTask(effectTasks);
                        Vector v = new Vector(p.getLocation().getDirection().getX(), p.getLocation().getDirection().getY()+toss, p.getLocation().getDirection().getZ());
                        p.setVelocity(v);
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', libroCore.getConfig().getString("Settings.LaunchMessage")));
                    }
                }, 20L*libroCore.getConfig().getLong("Settings.LaunchTime"));
                /**/
                int teleportTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        List<String> teleportLocations = libroCore.getConfig().getStringList("Settings.CannonTeleports");
                        String randomCannonTeleportLoc;
                        int randomNum = rand.nextInt(teleportLocations.size()) - 1;
                        if (teleportLocations.size() > 1) {
                            randomCannonTeleportLoc = teleportLocations.get(randomNum);
                            if (randomNum == -1) {
                                randomCannonTeleportLoc = teleportLocations.get(0);
                            }
                        } else {
                            randomCannonTeleportLoc = teleportLocations.get(0);
                        }
                        p.teleport(libroCore.stringToLoc(randomCannonTeleportLoc));
                        p.playEffect(p.getLocation(), Effect.valueOf(libroCore.getConfig().getString("Settings.OnTeleportEffect")), 1);
                        p.playEffect(p.getLocation().clone().add(0, 1, 0), Effect.valueOf(libroCore.getConfig().getString("Settings.OnTeleportEffect")), 1);
                        if(libroCore.getConfig().getBoolean("Settings.TeleportLightning")) {
                            p.getWorld().strikeLightning(p.getLocation());
                        }
                        p.setHealth(p.getMaxHealth());
                        p.setFireTicks(0);
                        inCannon.remove(p.getUniqueId());
                    }
                }, 20L*libroCore.getConfig().getLong("Settings.TeleportTime"));
                        /**/
            }
        }
    The toss variable is only set to "2" in the config as an Integer.
    This is what happens when the code is ran...
    [​IMG]
     
  2. Offline

    Zombie_Striker

    @TheWolfBadger
    1. If you need to cancel tasks, do not use schedulers. Instead, use BukkitRunnables, as they have the cancel() method.
    2. It looks as though you are continually being teleported. Try commenting out the teleport methods to see if they are the problem.
     
  3. Offline

    TheWolfBadger

    The teleport methods were not the problem. I had previously commented them out and it made no difference...
     
  4. Offline

    ChristianWP3579

    Why not just launch the player by setting their velocity relative to their direction?

    Code:
    p.setVelocity(p.getLocation().getDirection().multiply(multiplier-value));
     
  5. Offline

    TheWolfBadger

    I had tried this too before, still got the glitchy effect which seriously makes no sense...
     
  6. Offline

    ChristianWP3579

    Did you try it without schedulers?

    I used this method for increased knockback in one of my plugins, and it worked fine without a scheduler, but with it, it bugged out.

    Meant to edit my post, not reply

    Just try it without schedulers or runnables in the method. They could be delaying the execution of the velocity change, thus, giving you a different result.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 26, 2017
  7. Offline

    TheWolfBadger

    Well I need the scheduler so the task is delayed though?
     
  8. Offline

    ChristianWP3579

    If you want a delayed response, try making the scheduler before the execution, then make the actual velocity execution without a scheduler. Also, if you want the execution to be relative to the players direction before the delay, make a clone of the direction before the scheduler is made, then base the velocity execution on the cloned direction.
     
  9. Offline

    Zombie_Striker

    @TheWolfBadger
    The setVelocty method is not what is causing the problem. Can you try setting the velocity somewhere else (such as in a temporary command) so that no other method is called besides the it?
     
    ChristianWP3579 likes this.
  10. Offline

    TheWolfBadger

    @Zombie_Striker @ChristianWP3579 Didn't change anything... Here is the code I used:
    Code:
      public void cannonFunction(final Player p) {
            //Cannon Function
            if(inCannon.contains(p.getUniqueId())) {
                //Yup they in cannon
                Location mainCannonLoc = libroCore.stringToLoc(libroCore.getConfig().getString("Settings.CannonTeleportLocation"));
                p.teleport(mainCannonLoc);
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', libroCore.getConfig().getString("Settings.TeleportedToCannonMessage")));
                int effectTasks = Bukkit.getScheduler().scheduleSyncRepeatingTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        p.playEffect(p.getLocation(), Effect.valueOf(libroCore.getConfig().getString("Settings.CannonEffect")), 1);
                        p.playEffect(p.getLocation().clone().add(0, 1, 0), Effect.valueOf(libroCore.getConfig().getString("Settings.CannonEffect")), 1);
                    }
                }, 0, 20L);
                int launchTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        Bukkit.getScheduler().cancelTask(effectTasks);
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', libroCore.getConfig().getString("Settings.LaunchMessage")));
                        Integer toss = libroCore.getConfig().getInt("Settings.LaunchMultiplier");
                        Vector v = new Vector(p.getLocation().getDirection().getX(), p.getLocation().getDirection().getY()+toss, p.getLocation().getDirection().getZ());
                        p.setVelocity(v);
                    }
                }, 20L*libroCore.getConfig().getLong("Settings.LaunchTime"));
                /** /
                int teleportTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        List<String> teleportLocations = libroCore.getConfig().getStringList("Settings.CannonTeleports");
                        String randomCannonTeleportLoc;
                        int randomNum = rand.nextInt(teleportLocations.size()) - 1;
                        if (teleportLocations.size() > 1) {
                            randomCannonTeleportLoc = teleportLocations.get(randomNum);
                            if (randomNum == -1) {
                                randomCannonTeleportLoc = teleportLocations.get(0);
                            }
                        } else {
                            randomCannonTeleportLoc = teleportLocations.get(0);
                        }
                        p.teleport(libroCore.stringToLoc(randomCannonTeleportLoc));
                        p.playEffect(p.getLocation(), Effect.valueOf(libroCore.getConfig().getString("Settings.OnTeleportEffect")), 1);
                        p.playEffect(p.getLocation().clone().add(0, 1, 0), Effect.valueOf(libroCore.getConfig().getString("Settings.OnTeleportEffect")), 1);
                        if(libroCore.getConfig().getBoolean("Settings.TeleportLightning")) {
                            p.getWorld().strikeLightning(p.getLocation());
                        }
                        p.setHealth(p.getMaxHealth());
                        p.setFireTicks(0);
                        inCannon.remove(p.getUniqueId());
                    }
                }, 20L*libroCore.getConfig().getLong("Settings.TeleportTime"));
                        /**/
            }
        }
     
  11. Offline

    ChristianWP3579

    This code works, I tested it --->

    // 3 second delay

    Code:
    @EventHandler
        protected void EventPlayerLaunch(PlayerToggleSneakEvent e) {
            if (e.isSneaking()) {
                Vector directionClone = e.getPlayer().getLocation().getDirection().clone();
                          
                int launchTask = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
                    public void run() {
                        e.getPlayer().setVelocity(directionClone.multiply(9));
                    }
                }, 20L*3L);
            }
        }
    This code works as well, tested it --->

    // No delay

    Code:
    @EventHandler
        protected void EventPlayerLaunch(PlayerToggleSneakEvent e) {
            if (e.isSneaking()) {
                e.getPlayer().setVelocity(e.getPlayer().getLocation().getDirection().multiply(9));
            }
        }
     
  12. Offline

    TheWolfBadger

    Wait, maybe you have to clone it when you have it in a delayed task?
     
  13. Offline

    ChristianWP3579

    First, you are using -->

    Code:
    Vector v = new Vector(p.getLocation().getDirection().getX(), p.getLocation().getDirection().getY()+toss, p.getLocation().getDirection().getZ());
    If you are tryng to launch the player forward, then that math won't do, as you are only adding to Y. Plus, there may be some other internal functions that are at work. So it's best just to change that to --->

    Code:
    Vector v = p.getLocation().getDirection().clone();
    Second, you are going to want to clone the direction before the launchTask scheduler. You may want to clone the direction on the line just after the p.teleport(mainCannonLoc); code line. Like this --->

    Code:
    p.teleport(mainCannonLoc);
    Vector v = p.getLocation().getDirection().clone();
    
    So your new code should look like this --->

    Code:
    public void cannonFunction(final Player p) {
            //Cannon Function
            if(inCannon.contains(p.getUniqueId())) {
                //Yup they in cannon
                Location mainCannonLoc = libroCore.stringToLoc(libroCore.getConfig().getString("Settings.CannonTeleportLocation"));
                p.teleport(mainCannonLoc);
                Vector v = p.getLocation().getDirection().clone();
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', libroCore.getConfig().getString("Settings.TeleportedToCannonMessage")));
                int effectTasks = Bukkit.getScheduler().scheduleSyncRepeatingTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        p.playEffect(p.getLocation(), Effect.valueOf(libroCore.getConfig().getString("Settings.CannonEffect")), 1);
                        p.playEffect(p.getLocation().clone().add(0, 1, 0), Effect.valueOf(libroCore.getConfig().getString("Settings.CannonEffect")), 1);
                    }
                }, 0, 20L);
                int launchTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        Bukkit.getScheduler().cancelTask(effectTasks);
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', libroCore.getConfig().getString("Settings.LaunchMessage")));
                        Integer toss = libroCore.getConfig().getInt("Settings.LaunchMultiplier");
                        p.setVelocity(v.multiply(toss));
                    }
                }, 20L*libroCore.getConfig().getLong("Settings.LaunchTime"));
                /** /
                int teleportTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        List<String> teleportLocations = libroCore.getConfig().getStringList("Settings.CannonTeleports");
                        String randomCannonTeleportLoc;
                        int randomNum = rand.nextInt(teleportLocations.size()) - 1;
                        if (teleportLocations.size() > 1) {
                            randomCannonTeleportLoc = teleportLocations.get(randomNum);
                            if (randomNum == -1) {
                                randomCannonTeleportLoc = teleportLocations.get(0);
                            }
                        } else {
                            randomCannonTeleportLoc = teleportLocations.get(0);
                        }
                        p.teleport(libroCore.stringToLoc(randomCannonTeleportLoc));
                        p.playEffect(p.getLocation(), Effect.valueOf(libroCore.getConfig().getString("Settings.OnTeleportEffect")), 1);
                        p.playEffect(p.getLocation().clone().add(0, 1, 0), Effect.valueOf(libroCore.getConfig().getString("Settings.OnTeleportEffect")), 1);
                        if(libroCore.getConfig().getBoolean("Settings.TeleportLightning")) {
                            p.getWorld().strikeLightning(p.getLocation());
                        }
                        p.setHealth(p.getMaxHealth());
                        p.setFireTicks(0);
                        inCannon.remove(p.getUniqueId());
                    }
                }, 20L*libroCore.getConfig().getLong("Settings.TeleportTime"));
                        /**/
            }
        }
     
    Last edited: Jul 24, 2017
  14. Offline

    TheWolfBadger

    Code:
     public void cannonFunction(final Player p) {
            //Cannon Function
            if(inCannon.contains(p.getUniqueId())) {
                //Yup they in cannon
                Location mainCannonLoc = libroCore.stringToLoc(libroCore.getConfig().getString("Settings.CannonTeleportLocation"));
                p.teleport(mainCannonLoc);
                Integer toss = libroCore.getConfig().getInt("Settings.LaunchMultiplier");
                Vector v = p.getLocation().getDirection().clone();
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', libroCore.getConfig().getString("Settings.TeleportedToCannonMessage")));
                int effectTasks = Bukkit.getScheduler().scheduleSyncRepeatingTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        p.playEffect(p.getLocation(), Effect.valueOf(libroCore.getConfig().getString("Settings.CannonEffect")), 1);
                        p.playEffect(p.getLocation().clone().add(0, 1, 0), Effect.valueOf(libroCore.getConfig().getString("Settings.CannonEffect")), 1);
                    }
                }, 0, 20L);
                int launchTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        Bukkit.getScheduler().cancelTask(effectTasks);
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', libroCore.getConfig().getString("Settings.LaunchMessage")));
                        p.setVelocity(v.multiply(toss));
                    }
                }, 20L*libroCore.getConfig().getLong("Settings.LaunchTime"));
                /**/
                int teleportTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        List<String> teleportLocations = libroCore.getConfig().getStringList("Settings.CannonTeleports");
                        String randomCannonTeleportLoc;
                        int randomNum = rand.nextInt(teleportLocations.size()) - 1;
                        if (teleportLocations.size() > 1) {
                            randomCannonTeleportLoc = teleportLocations.get(randomNum);
                            if (randomNum == -1) {
                                randomCannonTeleportLoc = teleportLocations.get(0);
                            }
                        } else {
                            randomCannonTeleportLoc = teleportLocations.get(0);
                        }
                        p.teleport(libroCore.stringToLoc(randomCannonTeleportLoc));
                        p.playEffect(p.getLocation(), Effect.valueOf(libroCore.getConfig().getString("Settings.OnTeleportEffect")), 1);
                        p.playEffect(p.getLocation().clone().add(0, 1, 0), Effect.valueOf(libroCore.getConfig().getString("Settings.OnTeleportEffect")), 1);
                        if(libroCore.getConfig().getBoolean("Settings.TeleportLightning")) {
                            p.getWorld().strikeLightning(p.getLocation());
                        }
                        p.setHealth(p.getMaxHealth());
                        p.setFireTicks(0);
                        inCannon.remove(p.getUniqueId());
                    }
                }, 20L*libroCore.getConfig().getLong("Settings.TeleportTime"));
                        /**/
            }
        }
    It's still getting the odd glitch :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 26, 2017
  15. Offline

    ChristianWP3579

    Is that working for you?

    EDIT - Just saw ur comment edit. Well, give me some time,I am going to try and replicate your entire method.
     
  16. Offline

    TheWolfBadger

    Appreciate all your help and support :)
     
  17. Offline

    ChristianWP3579

    Np :)

    Ok, so I sort of replicated it. I replaced your plugin-dependant variables with constants for brevity. I also removed what I was almost certain wasn't effecting it. Here is the working result --->

    Code:
        public void cannonFunction(Player p) {
        //Yup they in cannon
        Location mainCannonLoc = p.getLocation().add(p.getLocation().getDirection().multiply(9)).clone();
        p.teleport(mainCannonLoc);
        Integer toss = 9;
        Vector v = p.getLocation().getDirection().clone();
         
        int effectTasks = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
        @Override
        public void run() {
           p.playSound(p.getLocation(), Sound.ENTITY_TNT_PRIMED, 10, 1);
        }
        }, 20L);
        int launchTask = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
        @Override
        public void run() {
           p.playSound(p.getLocation(), Sound.ENTITY_ENDERDRAGON_FIREBALL_EXPLODE, 20, 1);
        Bukkit.getScheduler().cancelTask(effectTasks);
        p.sendMessage(ChatColor.translateAlternateColorCodes('&', "Launched"));
        p.setVelocity(v.multiply(toss));
        }
        }, 20L*3L);
        }
    
    This code works great on my plugin testing server. It teleports them forward then launches them with the correct velocity in the correct direction. Perhaps there are some flaws in those plugin-dependant variables you are using. I recommend using this replicate and modifying it the way you want it. By the way, instead of using code like this --->

    Code:
    Location mainCannonLoc = libroCore.stringToLoc(libroCore.getConfig().getString("Settings.CannonTeleportLocation"));
    Why not just make a global constant for the plugin in the main class and make a protected get method within the main class and just call that method to from other classes that you passed the main class to in order to get that constant? Like this --->

    Main Java Class --->

    Code:
    private cannonLoc;
    
    public void onEnable() {
            cannonLoc = <desired-location>;
    }
    
    protected Location getCannonLocation() {
            return cannonLoc;
    }
    Second Java Class --->

    Code:
    private <name-of-plugin-main-class> plugin;
    
    protected <name-of-class>(<name-of-plugin-main-class> Plugin) {
            plugin = Plugin;
    }
    
    protected Location GetTheCannonLocation() {
            return plugin.getCannonLocation();
    }
    That is the way I handle global constants. It seems a lot less complicated than what you were doing.
     
    Last edited: Jul 24, 2017
  18. Offline

    TheWolfBadger

    Code:
    public void cannonFunction(final Player p) {
            //Cannon Function
            if(inCannon.contains(p.getUniqueId())) {
                //Yup they in cannon
                Location mainCannonLoc = libroCore.stringToLoc(libroCore.getConfig().getString("Settings.CannonTeleportLocation"));
                p.teleport(mainCannonLoc);
                Integer toss = libroCore.getConfig().getInt("Settings.LaunchMultiplier");
                Vector v = p.getLocation().getDirection().clone();
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', libroCore.getConfig().getString("Settings.TeleportedToCannonMessage")));
                /** /
                int effectTasks = Bukkit.getScheduler().scheduleSyncRepeatingTask(libroCore, new BukkitRunnable() {
                    int seconds = 0;
                    @Override
                    public void run() {
                        if (seconds >= libroCore.getConfig().getInt("Settings.LaunchTime")) {
                            this.cancel();
                        }
                        seconds++;
                        p.playEffect(p.getLocation(), Effect.valueOf(libroCore.getConfig().getString("Settings.CannonEffect")), 1);
                        p.playEffect(p.getLocation().clone().add(0, 1, 0), Effect.valueOf(libroCore.getConfig().getString("Settings.CannonEffect")), 1);
                    }
                }, 0, 20L);
                    /**/
                int launchTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', libroCore.getConfig().getString("Settings.LaunchMessage")));
                        p.setVelocity(v.multiply(toss));
                    }
                }, 20L*libroCore.getConfig().getLong("Settings.LaunchTime"));
                /**/
                int teleportTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        List<String> teleportLocations = libroCore.getConfig().getStringList("Settings.CannonTeleports");
                        String randomCannonTeleportLoc;
                        int randomNum = rand.nextInt(teleportLocations.size()) - 1;
                        if (teleportLocations.size() > 1) {
                            randomCannonTeleportLoc = teleportLocations.get(randomNum);
                            if (randomNum == -1) {
                                randomCannonTeleportLoc = teleportLocations.get(0);
                            }
                        } else {
                            randomCannonTeleportLoc = teleportLocations.get(0);
                        }
                        p.teleport(libroCore.stringToLoc(randomCannonTeleportLoc));
                        p.playEffect(p.getLocation(), Effect.valueOf(libroCore.getConfig().getString("Settings.OnTeleportEffect")), 1);
                        p.playEffect(p.getLocation().clone().add(0, 1, 0), Effect.valueOf(libroCore.getConfig().getString("Settings.OnTeleportEffect")), 1);
                        if(libroCore.getConfig().getBoolean("Settings.TeleportLightning")) {
                            p.getWorld().strikeLightning(p.getLocation());
                        }
                        p.setHealth(p.getMaxHealth());
                        p.setFireTicks(0);
                        inCannon.remove(p.getUniqueId());
                    }
                }, 20L*libroCore.getConfig().getLong("Settings.TeleportTime"));
                        /**/
            }
        }
    Still nope. Also if it were a problem with my variables I'd be getting an error, but I'm not.
     
  19. Offline

    ChristianWP3579

    Try testing this event as is --->

    Code:
    @EventHandler
    protected void EventPlayerLaunch(PlayerToggleSneakEvent e) {
            if (!e.isSneaking()) {
                   Player p = e.getPlayer();
        
                   Location mainCannonLoc = p.getLocation().add(p.getLocation().getDirection().multiply(9)).clone();
                   p.teleport(mainCannonLoc);
                   Integer toss = 9;
                   Vector v = p.getLocation().getDirection().clone();
                 
                    int effectTasks = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
                            @Override
                             public void run() {
                                     p.playSound(p.getLocation(), Sound.ENTITY_TNT_PRIMED, 10, 1);
                             }
                     }, 20L);
                  
                     int launchTask = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
                              @Override
                              public void run() {
                                      p.playSound(p.getLocation(), Sound.ENTITY_ENDERDRAGON_FIREBALL_EXPLODE, 20, 1);
                                      p.sendMessage(ChatColor.translateAlternateColorCodes('&', "Launched"));
                                      p.setVelocity(v.multiply(toss));
                              }
                    }, 20L*3L);
            }
    }
    I tested it and it works fine.

    Make sure you don't have some anti-cheat plugin on the server that may be conflicting with your plugin and blocking such an ability.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 26, 2017
  20. Offline

    TheWolfBadger

    About to test this out, will let you know the results:

    Code:
     /* Test */
        @EventHandler
        protected void EventPlayerLaunch(PlayerToggleSneakEvent e) {
            if (!e.isSneaking()) {
                Player p = e.getPlayer();
    
                Location mainCannonLoc = p.getLocation().add(p.getLocation().getDirection().multiply(9)).clone();
                p.teleport(mainCannonLoc);
                Integer toss = 9;
                Vector v = p.getLocation().getDirection().clone();
    
                int effectTasks = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        p.playSound(p.getLocation(), Sound.EXPLODE, 10, 1);
                    }
                }, 20L);
    
                int launchTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        p.playSound(p.getLocation(), Sound.EXPLODE, 20, 1);
                        Bukkit.getScheduler().cancelTask(effectTasks);
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a&lLaunched"));
                        p.setVelocity(v.multiply(toss));
                    }
                }, 20L*3L);
            }
        }
        /**/
    PS: I had to take out the sounds and change them to EXPLODE cause the ones you had used did not exact within the craftbukkit jar I was using (1.8.7)

    [​IMG]
    It works fine, but do I have to spawn them 4 blocks up before I launch them every time? We still need to get this working in my function if possible.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 26, 2017
  21. Offline

    Zombie_Striker

    @TheWolfBadger
    You are trying to cancel a delayed task. Delayed tasks only run once, so you don't need to cancel it. Even if you did, do not use Schedulers if you need to be able to cancel the tasks. Instead, use BukkitRunnables, as they have a built in cancel() method.

    Can you post the plugins that are currently installed on your server? It is looking like something else is interfering with this.
     
    ChristianWP3579 likes this.
  22. Offline

    TheWolfBadger

    I'm not in the original code:
    Code:
    public void cannonFunction(final Player p) {
            //Cannon Function
            if(inCannon.contains(p.getUniqueId())) {
                //Yup they in cannon
                Location mainCannonLoc = libroCore.stringToLoc(libroCore.getConfig().getString("Settings.CannonTeleportLocation"));
                p.teleport(mainCannonLoc);
                Integer toss = libroCore.getConfig().getInt("Settings.LaunchMultiplier");
                Vector v = p.getLocation().getDirection().clone();
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', libroCore.getConfig().getString("Settings.TeleportedToCannonMessage")));
                /**/
                int effectTasks = Bukkit.getScheduler().scheduleSyncRepeatingTask(libroCore, new BukkitRunnable() {
                    int seconds = 0;
                    @Override
                    public void run() {
                        if (seconds >= libroCore.getConfig().getInt("Settings.LaunchTime")) {
                            this.cancel();
                        }
                        seconds++;
                        p.playEffect(p.getLocation(), Effect.valueOf(libroCore.getConfig().getString("Settings.CannonEffect")), 1);
                        p.playEffect(p.getLocation().clone().add(0, 1, 0), Effect.valueOf(libroCore.getConfig().getString("Settings.CannonEffect")), 1);
                    }
                }, 0, 20L);
                    /**/
                int launchTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', libroCore.getConfig().getString("Settings.LaunchMessage")));
                        p.setVelocity(v.multiply(toss));
                    }
                }, 20L*libroCore.getConfig().getLong("Settings.LaunchTime"));
                /**/
                int teleportTask = Bukkit.getScheduler().scheduleSyncDelayedTask(libroCore, new BukkitRunnable() {
                    @Override
                    public void run() {
                        List<String> teleportLocations = libroCore.getConfig().getStringList("Settings.CannonTeleports");
                        String randomCannonTeleportLoc;
                        int randomNum = rand.nextInt(teleportLocations.size()) - 1;
                        if (teleportLocations.size() > 1) {
                            randomCannonTeleportLoc = teleportLocations.get(randomNum);
                            if (randomNum == -1) {
                                randomCannonTeleportLoc = teleportLocations.get(0);
                            }
                        } else {
                            randomCannonTeleportLoc = teleportLocations.get(0);
                        }
                        p.teleport(libroCore.stringToLoc(randomCannonTeleportLoc));
                        p.playEffect(p.getLocation(), Effect.valueOf(libroCore.getConfig().getString("Settings.OnTeleportEffect")), 1);
                        p.playEffect(p.getLocation().clone().add(0, 1, 0), Effect.valueOf(libroCore.getConfig().getString("Settings.OnTeleportEffect")), 1);
                        if(libroCore.getConfig().getBoolean("Settings.TeleportLightning")) {
                            p.getWorld().strikeLightning(p.getLocation());
                        }
                        p.setHealth(p.getMaxHealth());
                        p.setFireTicks(0);
                        inCannon.remove(p.getUniqueId());
                    }
                }, 20L*libroCore.getConfig().getLong("Settings.TeleportTime"));
                        /**/
            }
        }
    Nonetheless, even trying to cancel the delayed task doesn't throw any errors and it still works fine. The sneak event one works fine, but my function has the glitching oddly enough...
     
  23. Offline

    ChristianWP3579

    No, you can raplace the teleport location with the location of your cannon or desired launch location, or you can simply remove the teleport command. If you want to teleport them in midair and keep them from falling before the launch, you can set their gravity to true right after you teleport them, then set it back to false right before or after there launch within the same scheduler of the setVelocity command.

    Here is what it would look like if you wanted to teleport them to a cannon location in midair and stop them from falling before they launch --->

    Code:
    @EventHandler
    protected void EventPlayerLaunch(PlayerToggleSneakEvent e) {
       if (!e.isSneaking()) {
         Vector directionClone = e.getPlayer().getLocation().getDirection().clone();
         Player p = e.getPlayer();
    
         Location mainCannonLoc = <desired-cannon-location>;
         p.teleport(mainCannonLoc);
         p.setGravity(false);
         Integer toss = 9;
         Vector v = p.getLocation().getDirection().clone();
    
         int effectTasks = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
           @Override
           public void run() {
             p.playSound(p.getLocation(), Sound.ENTITY_TNT_PRIMED, 10, 1);
           }
         }, 20L);
    
         int launchTask = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
           @Override
           public void run() {
             p.playSound(p.getLocation(), Sound.ENTITY_ENDERDRAGON_FIREBALL_EXPLODE, 20, 1);
             p.setGravity(true);
             p.sendMessage(ChatColor.translateAlternateColorCodes('&', "Launched"));
             p.setVelocity(v.multiply(toss));
           }
         }, 20L*3L);
       }
    }
    and here is what it would look like with no teleport at all --->

    Code:
    @EventHandler
    protected void EventPlayerLaunch(PlayerToggleSneakEvent e) {
       if (!e.isSneaking()) {
         Vector directionClone = e.getPlayer().getLocation().getDirection().clone();
         Player p = e.getPlayer();
    
         Integer toss = 9;
         Vector v = p.getLocation().getDirection().clone();
    
         int effectTasks = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
           @Override
           public void run() {
             p.playSound(p.getLocation(), Sound.ENTITY_TNT_PRIMED, 10, 1);
           }
         }, 20L);
    
         int launchTask = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
           @Override
           public void run() {
             p.playSound(p.getLocation(), Sound.ENTITY_ENDERDRAGON_FIREBALL_EXPLODE, 20, 1);
             p.sendMessage(ChatColor.translateAlternateColorCodes('&', "Launched"));
             p.setVelocity(v.multiply(toss));
           }
         }, 20L*3L);
       }
    }
    Of course since you are using 1.8.7, you will have to use the proper codelines for playing the sounds in your version.

    @TheWolfBadger Zombie_Striker is right by the way. There is no use in canceling a task that already executed and finished. It's unnecessary code, so I removed that line.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 26, 2017
  24. Offline

    Zombie_Striker

    @ChristianWP3579 @TheWolfBadger
    It's more than just bad; it's potentially damaging. If you cancel the task, a task from another plugin may take that id and you may be canceling that task, which may break that plugin.
     
    ChristianWP3579 likes this.
  25. Offline

    TheWolfBadger

    I fixed all of it! Apparently I had a move event and I was cancelling their movement while in the cannon, but I hadn't removed them from that HashSet when they were getting their velocity set and that was the problem. Thanks for all your help though!
     
    ChristianWP3579 likes this.
  26. Offline

    ChristianWP3579

    No problem.

    Interesting. I was not aware of that internal functionality. I have more programming experience in C, and I like that in C, I get to write all of the functionality manually. I am still getting used to this Java API and all of it's pre-compiled libraries and I am still trying to learn what is going on behind all of it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Jul 27, 2017
Thread Status:
Not open for further replies.

Share This Page