How to get player data after use command

Discussion in 'Plugin Development' started by PetchRex, May 22, 2021.

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

    PetchRex

    I writing a plugin that like parachute, when type the command it will TP on sky(y255) with slow falling potion added. But when i land to ground (around 10 sec to reach ground) i need to remove the potion effect asap, i tried while loop (stand != air) but my server freeze.
    i googling alot of code but it only check player stand on block when use command only.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @PetchRex While loops are bad, use a BukkitRunnable instead.
     
    PetchRex likes this.
  3. Offline

    PetchRex

    Thanks, it work well.

    the result
    Code:
     
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player) sender;
            player.addPotionEffect(PotionEffectType.SLOW_FALLING.createEffect(900, 10));
    
            // to check is landing
            new BukkitRunnable() {
                @Override
                public void run() {
                    if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() != Material.AIR) {
                        player.sendMessage(ChatColor.GREEN + "You reach the ground!");
                        if (player.hasPotionEffect(PotionEffectType.SLOW_FALLING)) {
                            player.removePotionEffect(PotionEffectType.SLOW_FALLING);
                        }
                        this.cancel();
                    } else {
                        player.sendMessage(ChatColor.RED + "You are on mid-air!");
                    }
                }
            }.runTaskTimer(Main.getPlugin(Main.class), 0, 20);
        }
        return false;
    }
    
    
     
    Last edited: May 22, 2021
Thread Status:
Not open for further replies.

Share This Page