Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.broken.cheststorage.ChestStorage;
- import org.broken.cheststorage.util.effects.HeavyLoad;
- import org.broken.cheststorage.util.effects.SpawnCustomEffects;
- import org.bukkit.Bukkit;
- import org.bukkit.scheduler.BukkitRunnable;
- import java.util.*;
- import java.util.concurrent.ConcurrentLinkedDeque;
- public class HeavyTasks extends BukkitRunnable {
- private static int taskIDNumber = -1;
- private int amount = 0;
- private static final int MAX_MS_PER_TICK = 5;
- private static final Queue<HeavyLoad> workloadDeque = new ConcurrentLinkedDeque<>();
- private static final Map<Object, Map<Long, LinkedList<HeavyLoad>>> map = new HashMap<>();
- private static final LinkedList<HeavyLoad> test = new LinkedList<>();
- private HeavyLoad firstReschudleElement;
- public HeavyTasks() {
- //workloadDeque = Queues.newArrayDeque();
- taskIDNumber = runTaskTimer(ChestStorage.getInstance(), 0L, 4L).getTaskId();
- }
- public void start() {
- if (Bukkit.getScheduler().isCurrentlyRunning(taskIDNumber) || Bukkit.getScheduler().isQueued(taskIDNumber))
- Bukkit.getScheduler().cancelTask(taskIDNumber);
- new HeavyTasks();
- }
- public void addLoad(HeavyLoad task) {
- workloadDeque.add(task);
- //test.add(task);
- //System.out.println("testings of " + workloadDeque);
- }
- @SuppressWarnings("BooleanMethodIsAlwaysInverted")
- public boolean isContainsMaxAmountInQueue(Object object, int maxAmountIncache) {
- return map.containsKey(object) && map.get(object).size() >= (maxAmountIncache == 0 ? 1 : maxAmountIncache);
- }
- public void setMaxAmountEachEntityCanQueue(Object object, long howLongTimeBeforeRemove, HeavyLoad task) {
- Map<Long, LinkedList<HeavyLoad>> addData = new HashMap<>();
- LinkedList<HeavyLoad> linkedList = new LinkedList<>();
- if (map.containsKey(object)) {
- for (Map.Entry<Long, LinkedList<HeavyLoad>> data : map.get(object).entrySet())
- addData.put(data.getKey(), data.getValue());
- }
- linkedList.add(task);
- addData.put(howLongTimeBeforeRemove, linkedList);
- map.put(object, addData);
- }
- private boolean cumputeTasks(HeavyLoad task) {
- if (task != null) {
- if (task.reschedule()) {
- addLoad(task);
- if (firstReschudleElement == null) {
- firstReschudleElement = task;
- } else {
- return firstReschudleElement != task;
- }
- return true;
- }
- }
- return true;
- }
- private boolean delayTasks(HeavyLoad task, int amount) {
- if (task != null)
- return task.computeWithDelay(amount);
- return false;
- }
- private long maxMSPerTick(HeavyLoad task) {
- if (task != null) {
- return task.getMilliPerTick();
- }
- return 4;
- }
- @Override
- public void run() {
- long stoptime = System.currentTimeMillis() + MAX_MS_PER_TICK;
- long stoptimeNew = System.currentTimeMillis();
- while (!workloadDeque.isEmpty() && System.currentTimeMillis() <= stoptime) {
- HeavyLoad heavyload = workloadDeque.poll();
- if (!(System.currentTimeMillis() <= stoptimeNew + maxMSPerTick(heavyload))) continue;
- if (!delayTasks(heavyload, amount)) continue;
- if (heavyload instanceof SpawnCustomEffects) {
- heavyload.compute();
- break;
- }
- heavyload.compute();
- if (cumputeTasks(heavyload)) return;
- }
- Set<Long> list = new LinkedHashSet<>();
- if (amount % 20 * 3 == 0) {
- for (Object object : map.keySet()) {
- if (object != null)
- for (Map.Entry<Long, LinkedList<HeavyLoad>> entity : map.get(object).entrySet()) {
- if (System.currentTimeMillis() >= entity.getKey()) {
- list.add(entity.getKey());
- }
- }
- list.forEach(time -> map.get(object).remove(time));
- }
- }
- amount++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement