Delay on command

Discussion in 'Plugin Development' started by EvilKittyCat123, Dec 27, 2013.

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

    EvilKittyCat123

    Hey guys. I made a command /suicide. Which adds poison to the player. But, it only takes him back to 1/2 hearts. So, I want the plugin to kill the player when they are at 1/2 hearts. (With harming potion.)

    How would I do that? I think I have to use a delay, right? But I am not quite sure how to use the delay thing.
    Here's my current code:

    Code:java
    1. if(cmd.getName().equalsIgnoreCase("suicide")){
    2. player.sendMessage(ChatColor.RED + "" + ChatColor.ITALIC + "You shall die!");
    3. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA + "" + ChatColor.BOLD + "["
    4. + ChatColor.YELLOW + "" + ChatColor.BOLD + "Hippie"
    5. + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "Craft"
    6. + ChatColor.YELLOW + "" + ChatColor.BOLD + "] "
    7. + ChatColor.GREEN + "" + ChatColor.ITALIC + (player.getName()) + ""
    8. + ChatColor.RED + "" + ChatColor.ITALIC + " commited suicide!");
    9. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 18000, 1));
    10. player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 18000, 1));
    11. player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 18000, 1));
     
  2. Offline

    1Rogue Retired Staff

    iirc it's based on server difficulty.
     
  3. Offline

    EvilKittyCat123

    1Rogue
    Yea, I know. Mine is on normal. But how would I add the potion effect "harm" to the player, once they have reached 1/2 hearts?
     
  4. Offline

    1Rogue Retired Staff

  5. Offline

    EvilKittyCat123

    1Rogue
    And what would the code for that be?
     
  6. Offline

    1Rogue Retired Staff


    Read the link and learn it. I'm not going to spoonfeed it to you.
     
  7. Offline

    EvilKittyCat123

    1Rogue
    Idk how to use the sceduler.. I have read the Whole thing...
     
  8. Offline

    xTigerRebornx

    1Rogue I agree, the wiki for Scheduling isn't the best.
    EvilKittyCat123 Look around the resources section for a tutorial on it, you'll definatly find something there
     
  9. Offline

    EvilKittyCat123

  10. Offline

    xTigerRebornx

    EvilKittyCat123 If you are any good with learning from videos, check out this, he has a tutorial on scheduling, and his tutorials are extremely good :p
     
  11. Offline

    nuno1212sss

    EvilKittyCat123 Do a repeating task to get the player health and when it get's to 1 or 2 hearts you give the Instant Harm, first create a new class so that you can cancel it, and it should extend BukkitRunnable like this:
    Code:java
    1. public class YourClass extends BukkitRunnable {
    2. private int id;
    3. public setId(int id){
    4. this.id = id;
    5. }
    6. @Override
    7. public void run(){
    8. if(player.getHealth() == 4 || player.getHealth() == 2){
    9. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 1, 2));
    10. Bukkit.getScheduler().cancelTask(id);
    11. return;
    12. }
    13. }
    14. }

    To call the Runnable:
    Code:java
    1. YourClass task = new YourClass();
    2. task.setId(Bukkit.getScheduler().scheduleSyncRepeatingTask(this, task, 0, 20));

    Just to warn the this is the plugin !

    xTigerRebornx Can't argue with that xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  12. Offline

    EvilKittyCat123

    nuno1212sss
    I did it, but I get a red line under player.getHealth, which says:

    "The method getHealth() is ambiguous for the type Player"
    Here is my code:

    Code:java
    1. package me.stripa.HCaddons;
    2. import org.bukkit.Bukkit;
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.potion.PotionEffect;
    5. import org.bukkit.potion.PotionEffectType;
    6. import org.bukkit.scheduler.BukkitRunnable;
    7.  
    8. public class HCsuicide extends BukkitRunnable {
    9. private int id;
    10. private Player sender;
    11. public void setId(int id){
    12. this.id = id;
    13.  
    14. }
    15.  
    16. Player player = (Player) sender;
    17.  
    18. @Override
    19. public void run(){
    20. if(player.getHealth() == 4 || player.getHealth() == 2){
    21. player.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 1, 2));
    22. Bukkit.getScheduler().cancelTask(id);
    23. return;
    24.  
    25. }
    26.  
    27. }
    28.  
    29. }
    30.  
    31.  
     
  13. Offline

    Garris0n

Thread Status:
Not open for further replies.

Share This Page