broken-arrow

Untitled

Oct 17th, 2021
741
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. import org.broken.cheststorage.ChestStorage;
  4. import org.broken.cheststorage.util.effects.HeavyLoad;
  5. import org.broken.cheststorage.util.effects.SpawnCustomEffects;
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.scheduler.BukkitRunnable;
  8.  
  9. import java.util.*;
  10. import java.util.concurrent.ConcurrentLinkedDeque;
  11.  
  12. public class HeavyTasks extends BukkitRunnable {
  13.  
  14.     private static int taskIDNumber = -1;
  15.  
  16.     private int amount = 0;
  17.     private static final int MAX_MS_PER_TICK = 5;
  18.  
  19.     private static final Queue<HeavyLoad> workloadDeque = new ConcurrentLinkedDeque<>();
  20.  
  21.     private static final Map<Object, Map<Long, LinkedList<HeavyLoad>>> map = new HashMap<>();
  22.  
  23.     private static final LinkedList<HeavyLoad> test = new LinkedList<>();
  24.     private HeavyLoad firstReschudleElement;
  25.  
  26.     public HeavyTasks() {
  27.         //workloadDeque = Queues.newArrayDeque();
  28.         taskIDNumber = runTaskTimer(ChestStorage.getInstance(), 0L, 4L).getTaskId();
  29.     }
  30.  
  31.     public void start() {
  32.         if (Bukkit.getScheduler().isCurrentlyRunning(taskIDNumber) || Bukkit.getScheduler().isQueued(taskIDNumber))
  33.             Bukkit.getScheduler().cancelTask(taskIDNumber);
  34.         new HeavyTasks();
  35.     }
  36.  
  37.  
  38.     public void addLoad(HeavyLoad task) {
  39.         workloadDeque.add(task);
  40.         //test.add(task);
  41.         //System.out.println("testings of " + workloadDeque);
  42.     }
  43.  
  44.     @SuppressWarnings("BooleanMethodIsAlwaysInverted")
  45.     public boolean isContainsMaxAmountInQueue(Object object, int maxAmountIncache) {
  46.         return map.containsKey(object) && map.get(object).size() >= (maxAmountIncache == 0 ? 1 : maxAmountIncache);
  47.     }
  48.  
  49.     public void setMaxAmountEachEntityCanQueue(Object object, long howLongTimeBeforeRemove, HeavyLoad task) {
  50.         Map<Long, LinkedList<HeavyLoad>> addData = new HashMap<>();
  51.         LinkedList<HeavyLoad> linkedList = new LinkedList<>();
  52.         if (map.containsKey(object)) {
  53.  
  54.             for (Map.Entry<Long, LinkedList<HeavyLoad>> data : map.get(object).entrySet())
  55.                 addData.put(data.getKey(), data.getValue());
  56.         }
  57.         linkedList.add(task);
  58.         addData.put(howLongTimeBeforeRemove, linkedList);
  59.         map.put(object, addData);
  60.     }
  61.  
  62.     private boolean cumputeTasks(HeavyLoad task) {
  63.         if (task != null) {
  64.             if (task.reschedule()) {
  65.                 addLoad(task);
  66.                 if (firstReschudleElement == null) {
  67.                     firstReschudleElement = task;
  68.                 } else {
  69.                     return firstReschudleElement != task;
  70.                 }
  71.                 return true;
  72.             }
  73.         }
  74.  
  75.         return true;
  76.     }
  77.  
  78.     private boolean delayTasks(HeavyLoad task, int amount) {
  79.         if (task != null)
  80.             return task.computeWithDelay(amount);
  81.         return false;
  82.     }
  83.  
  84.     private long maxMSPerTick(HeavyLoad task) {
  85.         if (task != null) {
  86.             return task.getMilliPerTick();
  87.         }
  88.         return 4;
  89.     }
  90.  
  91.     @Override
  92.     public void run() {
  93.         long stoptime = System.currentTimeMillis() + MAX_MS_PER_TICK;
  94.         long stoptimeNew = System.currentTimeMillis();
  95.  
  96.         while (!workloadDeque.isEmpty() && System.currentTimeMillis() <= stoptime) {
  97.             HeavyLoad heavyload = workloadDeque.poll();
  98.             if (!(System.currentTimeMillis() <= stoptimeNew + maxMSPerTick(heavyload))) continue;
  99.             if (!delayTasks(heavyload, amount)) continue;
  100.             if (heavyload instanceof SpawnCustomEffects) {
  101.                 heavyload.compute();
  102.                 break;
  103.             }
  104.             heavyload.compute();
  105.             if (cumputeTasks(heavyload)) return;
  106.  
  107.         }
  108.         Set<Long> list = new LinkedHashSet<>();
  109.         if (amount % 20 * 3 == 0) {
  110.             for (Object object : map.keySet()) {
  111.                 if (object != null)
  112.                     for (Map.Entry<Long, LinkedList<HeavyLoad>> entity : map.get(object).entrySet()) {
  113.  
  114.                         if (System.currentTimeMillis() >= entity.getKey()) {
  115.                             list.add(entity.getKey());
  116.                         }
  117.                     }
  118.                 list.forEach(time -> map.get(object).remove(time));
  119.             }
  120.         }
  121.  
  122.         amount++;
  123.     }
  124. }
  125.  
  126.  
RAW Paste Data