Advertisement
broken-arrow

Untitled

Oct 15th, 2021
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1.  
  2. import org.broken.cheststorage.ChestStorage;
  3. import org.broken.cheststorage.util.effects.SpawnCustomEffects;
  4. import org.broken.cheststorage.util.effects.heavyLoad;
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.scheduler.BukkitRunnable;
  7.  
  8. import java.util.HashMap;
  9. import java.util.LinkedList;
  10. import java.util.Map;
  11. import java.util.Queue;
  12. import java.util.concurrent.ConcurrentLinkedDeque;
  13. import java.util.concurrent.TimeUnit;
  14.  
  15. public class HeavyTasks extends BukkitRunnable {
  16.  
  17.     private static int taskIDNumber = -1;
  18.  
  19.     private int amount = 0;
  20.     private static final int MAX_MS_PER_TICK = 5;
  21.  
  22.     public HeavyTasks() {
  23.         //workloadDeque = Queues.newArrayDeque();
  24.         taskIDNumber = runTaskTimer(ChestStorage.getInstance(), 0L, 5L).getTaskId();
  25.     }
  26.  
  27.     public void start() {
  28.         if (Bukkit.getScheduler().isCurrentlyRunning(taskIDNumber) || Bukkit.getScheduler().isQueued(taskIDNumber))
  29.             Bukkit.getScheduler().cancelTask(taskIDNumber);
  30.         new HeavyTasks();
  31.     }
  32.  
  33.     private static final Queue<heavyLoad> workloadDeque = new ConcurrentLinkedDeque<>();
  34.  
  35.     private static final Map<Object, queueCheck> map = new HashMap<>();
  36.  
  37.     private heavyLoad firstReschudleElement;
  38.  
  39.     public void addLoad(heavyLoad task) {
  40.         workloadDeque.add(task);
  41.         //test.add(task);
  42.         //System.out.println("testings of " + workloadDeque);
  43.     }
  44.  
  45.     @SuppressWarnings("BooleanMethodIsAlwaysInverted")
  46.     public boolean isContainsMaxAmountInQueue(Object object) {
  47.         return map.containsKey(object) && map.get(object).getTask().size() <= 5;
  48.     }
  49.  
  50.     public void setMaxAmountEachEntityCanQueue(Object object, long howLongTimeBeforeRemove, heavyLoad task) {
  51.         LinkedList<heavyLoad> addData = new LinkedList<>();
  52.  
  53.         if (map.containsKey(object)) {
  54.             LinkedList<heavyLoad> data = map.get(object).getTask();
  55.             addData.addAll(data);
  56.         }
  57.         addData.add(task);
  58.  
  59.         map.put(object, queueCheck.of(howLongTimeBeforeRemove, addData));
  60.     }
  61.  
  62.     private boolean cumputeTasks(heavyLoad task) {
  63.         if (task != null) {
  64.             if (task.reschedule()) {
  65.                 addLoad(task);
  66.  
  67.                 if (firstReschudleElement == null) {
  68.                     firstReschudleElement = task;
  69.                 } else {
  70.                     return firstReschudleElement != task;
  71.                 }
  72.             }
  73.  
  74.         } else
  75.             this.amount = 0;
  76.         return true;
  77.     }
  78.  
  79.     private boolean delayTasks(heavyLoad task, int amount) {
  80.         if (task != null)
  81.             return task.computeWithDelay(amount);
  82.         return false;
  83.     }
  84.  
  85.     private long maxMSPerTick(heavyLoad task) {
  86.         if (task != null) {
  87.             return task.getMilliPerTick();
  88.         }
  89.         return 4;
  90.     }
  91.  
  92.     @Override
  93.     public void run() {
  94.         long stoptime = System.currentTimeMillis() + MAX_MS_PER_TICK;
  95.         long stoptimeNew = System.currentTimeMillis();
  96.  
  97.         //while ((!workloadDeque.isEmpty() && System.currentTimeMillis() <= stoptime)) {
  98.         heavyLoad tasks = workloadDeque.poll();
  99.         if (cumputeTasks(tasks)) {
  100.             if (delayTasks(tasks, amount)) {
  101.                 if (!(System.currentTimeMillis() <= stoptime)) return;
  102.  
  103.                 if (tasks instanceof SpawnCustomEffects) tasks.compute();
  104.                 if (delayTasks(tasks, amount)) {
  105.                     workloadDeque.forEach(task -> {
  106.                         if (!(System.currentTimeMillis() <= stoptimeNew + maxMSPerTick(task))) return;
  107.                         if (!(task instanceof SpawnCustomEffects))
  108.                             task.compute();
  109.  
  110.  
  111.                     });
  112.  
  113.  
  114.                 }
  115.             }
  116.         }
  117.         if (amount % 20 * 3 == 0) {
  118.             System.out.println("remove from map ");
  119.             for (Map.Entry<Object, queueCheck> entetys : map.entrySet()) {
  120.  
  121.                 if (TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) >= entetys.getValue().getAddedtime())
  122.                     map.remove(entetys.getKey());
  123.             }
  124.         }
  125.         amount++;
  126.  
  127.     }
  128.  
  129.     public static class queueCheck {
  130.  
  131.         private long addedtime;
  132.         private LinkedList<heavyLoad> task;
  133.  
  134.         public queueCheck() {
  135.         }
  136.  
  137.         public queueCheck(long addedtime, LinkedList<heavyLoad> task) {
  138.             this.addedtime = addedtime;
  139.             this.task = task;
  140.         }
  141.  
  142.         public static queueCheck of(long addedtime, LinkedList<heavyLoad> task) {
  143.             queueCheck data = new queueCheck();
  144.             data.addedtime = addedtime;
  145.             data.task = task;
  146.  
  147.             return data;
  148.         }
  149.  
  150.         public long getAddedtime() {
  151.             return addedtime;
  152.         }
  153.  
  154.         public LinkedList<heavyLoad> getTask() {
  155.             return task;
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement