How do I always check for time change?

Discussion in 'Plugin Development' started by parat26, Aug 18, 2014.

Thread Status:
Not open for further replies.
  1. Hello,

    I am developing a custom plugin for my server where players will get more health and speed boost when nighttime. There is one practical problem, where I cannot trigger that with an event, as there is no TimeChangeEvent. I thought of checking when the players joins with PlayerJoinEvent, but still, the time always changes and I cannot perform more than one check (because it's an event).

    I tried using a repeated task but no luck, because I am giving these buffs to the players every time this task reset, which is not what I want.

    Any ideas on how I can check always for night, or another way of seeing it?
  2. Offline


    parat26 Unfortunately, there is only one way of doing this. You would need a daylight sensor in every chunk. Check to see if the daylight sensor is emitting a red stone signal. There is literally no other way.
  3. Offline


    parat26 In the repeating task, add if(p.getActivePotionEffects().contains())
  4. Thanks Tecno_Wizard and DinosParkour but I've already seen this feature on other servers, that's why I am wondering.

    DinosParkour, I did not get it, can you explain it abit further :p?
  5. Offline


    parat26 If you still have the repeating task, can you post it?
  6. Code:java
    1. @EventHandler
    2. public void Enhance(PlayerJoinEvent e)
    3. {
    4. final Player player = e.getPlayer();
    6. //player.setMaxHealth(30);
    8. Bukkit.getScheduler().scheduleSyncRepeatingTask(GetPlugin(), new Runnable()
    9. {
    10. public void run()
    11. {
    12. // Okay, but what do I check for?
    13. }
    14. }, 100, 1);
    15. }
  7. Offline


    parat26 that would create a new task every time a player joins the game. It would be better to have a repeating task in the onEnable method and check the time there.
    parat26 likes this.
  8. Offline


    parat26 put this in your onEnable()
    1. final ArrayList<PotionEffect> myPots = new ArrayList<PotionEffect>();
    2. myPots.add(new PotionEffect(PotionEffectType.[whatever], [whatever], [whatever]));
    3. myPots.add(new PotionEffect(PotionEffectType.[whatever], [whatever], [whatever]));
    5. Runnable givePots = new Runnable(){
    6. public void run(){
    7. for(Player p : Bukkit.getOnlinePlayers(){
    8. if(p.getWorld().getTime() > 12000 && p.getWorld().getTime() < 23000){
    9. for(PotionEffect pot : myPots){
    10. if(!p.getActivePotionEffects().contains(pot)){p.addPotionEffect(pot);}
    11. }
    12. }
    13. }
    14. }
    15. };
    16. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, givePots, [whatever], [whatever]);
    parat26 likes this.
  9. All right, seems to work. Thanks DinosParkour and viper_monster.

    One thing though, I am worried abit about any side effects. Will this repeating task affect my server performance in any way?
  10. Offline


    parat26 it depends on how often it repeats. If you run it every second there will be a performance drop, but this is way better then having a repeating task for each player.
Thread Status:
Not open for further replies.

Share This Page