Guest User

UpdateEvent

a guest
Oct 20th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. // Clase UpdateEvent.
  2.  
  3. package me.gonzalociocca.minelevel.core.updater;
  4.  
  5.  
  6. import me.gonzalociocca.minelevel.core.updater.UpdateType;
  7. import org.bukkit.event.Event;
  8. import org.bukkit.event.HandlerList;
  9.  
  10. public class UpdateEvent extends Event
  11. {
  12. private static final HandlerList handlers = new HandlerList();
  13. private final UpdateType _type;
  14.  
  15. public UpdateEvent(UpdateType example)
  16. {
  17. _type = example;
  18. }
  19.  
  20. public UpdateType getType()
  21. {
  22. return _type;
  23. }
  24.  
  25. @Override
  26. public HandlerList getHandlers()
  27. {
  28. return handlers;
  29. }
  30.  
  31. public static HandlerList getHandlerList()
  32. {
  33. return handlers;
  34. }
  35. }
  36.  
  37. // Clase Updater
  38. package me.gonzalociocca.minelevel.core.updater;
  39.  
  40. import me.gonzalociocca.minelevel.core.updater.UpdateEvent;
  41. import org.bukkit.plugin.java.JavaPlugin;
  42.  
  43. public class Updater
  44. implements Runnable
  45. {
  46. private JavaPlugin _plugin;
  47.  
  48. public Updater(JavaPlugin plugin)
  49. {
  50. _plugin = plugin;
  51. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(_plugin, this, 0L, 20L);
  52. }
  53.  
  54. @Override
  55. public void run()
  56. {
  57. for (UpdateType updateType : UpdateType.values())
  58. {
  59. if (updateType.Elapsed())
  60. {
  61. _plugin.getServer().getPluginManager().callEvent(new UpdateEvent(updateType));
  62. }
  63. }
  64. }
  65. }
  66.  
  67. //Clase UpdateType
  68. package me.gonzalociocca.minelevel.core.updater;
  69.  
  70. import me.gonzalociocca.minelevel.core.updater.UtilTime;
  71.  
  72.  
  73.  
  74. public enum UpdateType
  75. {
  76. MIN1y2(90000L),
  77. MIN1(60000L),
  78. SLOW2(8000L),
  79. SEC3(3000L),
  80. SEC(1000L),
  81. SLOW(750L),
  82. FAST(150L);
  83.  
  84. private final long _time;
  85. private long _last;
  86. private long _timeSpent;
  87. private long _timeCount;
  88.  
  89. private UpdateType(long time) { _time = time;
  90. _last = System.currentTimeMillis();
  91. }
  92.  
  93. public boolean Elapsed()
  94. {
  95. if (UtilTime.elapsed(_last, _time))
  96. {
  97. _last = System.currentTimeMillis();
  98. return true;
  99. }
  100.  
  101. return false;
  102. }
  103.  
  104. public void StartTime()
  105. {
  106. _timeCount = System.currentTimeMillis();
  107. }
  108.  
  109. public void StopTime()
  110. {
  111. _timeSpent += System.currentTimeMillis() - _timeCount;
  112. }
  113.  
  114. public void PrintAndResetTime()
  115. {
  116. System.out.println(name() + " in a second: " + _timeSpent);
  117. _timeSpent = 0L;
  118. }
  119. }
  120.  
  121. //Evento de prueba
  122. boolean gameEnabled = false;
  123. @EventHandler
  124. public void onEvento(UpdateEvent event){
  125. if(event.getType().equals(UpdateType.SEC)){
  126. if(!gameEnabled){
  127. return;}
  128. for(Player p : Bukkit.getOnlinePlayers()){
  129. if(!this.isInside(p.getLocation()){
  130. this.getEvent().remove(p);
  131. }
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment