Trying to loop mob sounds to a player while having a potion effect.

Discussion in 'Plugin Development' started by RBHB16, Feb 1, 2016.

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

    RBHB16

    Hey guys, I'm trying to make a plugin that haunts a player with a command. Anyways my problem is if there isa better method of using a while loop, because it keeps timing out my player. Also, I don't really find the proper way to convert a user input for time. So for instance, player types /haunt notch 5 I tried to convert it to minutes by multiplying it by 60. Not sure if its the right math though... lol. Also, I try to make a if statement so if the player doesn't type enough args, it shows them how to properly type out the command but I doesn't work. Instead if just a server error occurred while trying to type this or something like that.

    Code:
    Code:
            if(sender instanceof Player){
                Player hauntp = (Player) sender;
                int time = Integer.parseInt(args[1]);
                String haunter = sender.getName().toString();
                int length = args.length;
               
                if(label.equalsIgnoreCase("haunt")){
                   
                    if (length == 1){
                        sender.sendMessage(ChatColor.RED + "Incorrect usage! Refer to: /haunt <Player> <Minutes>");
                        return true;
                    }
                       
                        if(length == 2){
                            for (Player hauntvictim : Bukkit.getOnlinePlayers()){
                                if(hauntvictim.getName().equalsIgnoreCase(args[0])){
                                    Location loc = hauntvictim.getLocation();
                                    hauntvictim.sendMessage(ChatColor.DARK_GREEN + haunter + ChatColor.RED + " has cursed you!");
                                    hauntp.sendMessage(ChatColor.AQUA + "You have cursed " + ChatColor.GOLD + hauntvictim);
                                   
                                    hauntvictim.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, time*60, 0));
                                    hauntvictim.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, time*60, 0));
                                    hauntvictim.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, time*60, 0));
                                   
                                    while(hauntvictim.hasPotionEffect(PotionEffectType.CONFUSION)){
                                        hauntvictim.getWorld().playSound(loc, Sound.CAT_HISS, 1, 0);
     
  2. Offline

    Xerox262

    @RBHB16 Don't use while loops, instead use a bukkit runnable and cancel it after a certain amount of ticks, and your math is incorrect because minecraft bases time on ticks, so 1 second = 20 ticks, so instead of time * 60 you would need (time * 20) * 60. To make your bukkit runnable just store the amount of loops you want, then decrease it every time and check when it hits 0 then cancel();

    For example, if you want it to loop every second and cancel after a minute you would do
    Code:
    new BukkitRunnable() {
        int loops = 60;
    
        @Override
         public void run() {
              if (loops-- > 0) {
                   // Play sounds
              } else cancel();
         }
    }
     
    RBHB16 likes this.
  3. Offline

    RBHB16

    Thank you very much, that's exactly what I needed!
     
Thread Status:
Not open for further replies.

Share This Page