Solved Repeating Event

Discussion in 'Plugin Development' started by TheAJ471, Apr 16, 2014.

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

    TheAJ471

    So I am making this become a ghost on death plugin and I got so when a player dies they get added to a list and when they respawn they get the message that they become a ghost and they get flying and stuff. But how do I make a repeating even so when the player's potion effect of invisibility goes away it will change them back to survival and remove flying.

    I do know how to do this in the onEnable but i cant do stuff with players there.

    Code:java
    1. public void onEnable() {
    2. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    3. public void run() {
    4. Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "Just to show you.");
    5. }
    6. }, 60, 100); // 20 ticks in 1 second
    7. }
    8. }


    So basically I need to add this (look below) to a repeating task.

    Code:java
    1. if (plugin.dead.contains(p) && !p.hasPotionEffect(PotionEffectType.INVISIBILITY)){
    2. p.setAllowFlight(false);
    3. p.setFlying(false);
    4. p.setCanPickupItems(true);
    5. p.setGameMode(GameMode.SURVIVAL);
    6. p.sendMessage(ChatColor.GREEN + "You are no longer a ghost and can now play again!");
    7. }
     
  2. TheAJ471 In your onEnable you could schedule a tasks that runs your code for each player in 'dead'
     
  3. Offline

    TheAJ471

    AdamQpzm But I cant use players in my onEnable. Like I cant do
    Code:java
    1. Player p = onEnable.getPlayer()

    or something
     
  4. TheAJ471 But you can get the players from dead?
     
  5. Offline

    TheAJ471

    AdamQpzm Dead is a list of players. When they die they get added to the list. Soif I wanted to use it in my onEnable I would need
    Code:
    if(dead.contains(p)){
    // Do stuff
    }
    But I cant define the player by using Player p = onEnable.getPlayer(); because that is not actually a thing (the onEnable.getPlayer()
     
  6. TheAJ471 You can loop through lists
     
  7. Offline

    TheAJ471

  8. Offline

    TheAJ471

    AdamQpzm Thanks for tis, but I just realized that I could probably just call on a method

    AdamQpzm Never mind. What I just said before wont work because I still need the player for this to work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  9. TheAJ471 Then read the tutorial I linked. :)
     
  10. Offline

    coasterman10

    You can iterate over each player in the list, and then run your code on them, except you wouldn't be checking if the list contains them. It's also possible just to loop over every online player but there's no need to when you already have a list.

    Code:java
    1. for (Player p : plugin.dead) {
    2. // ...
    3. }
     
  11. Offline

    TheAJ471

    AdamQpzm likes this.
  12. Offline

    TheMcScavenger

    It's probably a dumb question, but when you give the player the potion effect, you know the duration of it... Can't you just add a delayed task?

    Code:
    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
                public void run(){
                    if(!p.hasPotionEffect(PotionEffectType.INVISIBILITY){
                        p.setAllowFlight(false);
                        p.setFlying(false);
                        p.setCanPickupItems(true);
                        p.setGameMode(GameMode.SURVIVAL);
                        p.sendMessage(ChatColor.GREEN + "You are no longer a ghost and can now play again!");
                    }
                }
            }, 200);
     
  13. Offline

    TheAJ471

    TheMcScavenger Well I guess it depends, but where would you put this? If it is n the onEnable that wouldnt work thatI know of.unless you have what coasterman10 said
     
  14. Offline

    TheMcScavenger

    No, that definitely wouldn't work, because that could get dead players that have been dead for a minute too. I'm assuming you have a PlayerDeathEvent somewhere... You put the code right after where you add the potion effect to them ;)

    Off-Topic: Man, you've made a lot of plugins since I last saw you! :p
     
  15. Offline

    TheAJ471

    TheMcScavenger Well thanks for the help. I am uploading the plugin for this to bukkit now. Also yes I have made a lot of plugins :D
     
Thread Status:
Not open for further replies.

Share This Page