broken-arrow

Untitled

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