Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. public class MultiTickTask<T> {
  2. private Consumer<T> consumer;
  3.  
  4. private boolean running = false;
  5.  
  6. public MultiTickTask(Consumer<T> consumer) {
  7. this.consumer = consumer;
  8. }
  9.  
  10. public void runTask(JavaPlugin plugin, Collection<T> collection, int operationsPerTick, int tickPeriod) {
  11. if(running)
  12. throw new IllegalArgumentException("This task is already running!");
  13.  
  14. running = true;
  15.  
  16. final List<T> copy = new ArrayList<>(collection);
  17.  
  18. new BukkitRunnable() {
  19. int index = 0;
  20.  
  21. public void run() {
  22. if(!running) {
  23. cancel();
  24. return;
  25. }
  26.  
  27. for(int i = 0; i < operationsPerTick; i++) {
  28. consumer.accept(copy.get(index));
  29.  
  30. index++;
  31. if(index >= copy.size()) {
  32. running = false;
  33. cancel();
  34. break;
  35. }
  36. }
  37. }
  38. }.runTaskTimer(plugin, 0, tickPeriod);
  39. }
  40.  
  41. public void cancel() {
  42. running = false;
  43. }
  44.  
  45. public boolean isRunning() {
  46. return running;
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement