Fix accidental LIFO order on tick list when queueing ticks (when it always should be FIFO)
This commit is contained in:
parent
7c9657c6d2
commit
023081eaaa
@ -1,21 +1,24 @@
|
||||
package ru.dbotthepony.mc.otm.core.util
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap
|
||||
import it.unimi.dsi.fastutil.objects.ObjectAVLTreeSet
|
||||
import org.apache.logging.log4j.LogManager
|
||||
import ru.dbotthepony.mc.otm.core.addSorted
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
class TickList : ITickable {
|
||||
private val conditional = ArrayDeque<IConditionalTickable>()
|
||||
private val conditional = ArrayList<IConditionalTickable>()
|
||||
private val conditionalQueued = ArrayList<IConditionalTickable>()
|
||||
|
||||
private val once = ArrayDeque<ITickable>()
|
||||
private val once = ArrayList<ITickable>()
|
||||
private val onceQueued = ArrayList<ITickable>()
|
||||
|
||||
private val always = ArrayList<ITickable>()
|
||||
private val alwaysQueued = ArrayList<ITickable>()
|
||||
private val toRemoveFromAlways = ArrayList<ITickable>()
|
||||
|
||||
private val timers = ArrayDeque<Timer>()
|
||||
private val timers = ObjectAVLTreeSet<Timer>()
|
||||
private val nextTimerID = AtomicInteger()
|
||||
private val namedTimers = Object2ObjectOpenHashMap<Any, Timer>(0)
|
||||
|
||||
private var shouldTick = false
|
||||
@ -24,21 +27,26 @@ class TickList : ITickable {
|
||||
private set
|
||||
var ticks = 0
|
||||
private set
|
||||
|
||||
private var nextSometime = 0
|
||||
|
||||
inner class Timer(val timerTicks: Int, val runnable: Runnable) : Comparable<Timer> {
|
||||
val ringAt = ticks + timerTicks
|
||||
val timerID = nextTimerID.incrementAndGet()
|
||||
|
||||
var finished = false
|
||||
private set
|
||||
|
||||
init {
|
||||
shouldTick = true
|
||||
timers.addSorted(this)
|
||||
timers.add(this)
|
||||
}
|
||||
|
||||
override fun compareTo(other: Timer): Int {
|
||||
return ringAt.compareTo(other.ringAt)
|
||||
var c = ringAt.compareTo(other.ringAt)
|
||||
if (c != 0) return c
|
||||
c = timerID.compareTo(other.timerID)
|
||||
return c
|
||||
}
|
||||
|
||||
fun execute() {
|
||||
@ -174,22 +182,13 @@ class TickList : ITickable {
|
||||
try {
|
||||
if (conditional.isNotEmpty()) {
|
||||
shouldTick = true
|
||||
val iterator = conditional.iterator()
|
||||
|
||||
for (ticker in iterator) {
|
||||
if (!ticker.tick()) {
|
||||
iterator.remove()
|
||||
}
|
||||
}
|
||||
conditional.removeIf { !it.tick() }
|
||||
}
|
||||
|
||||
if (once.isNotEmpty()) {
|
||||
shouldTick = true
|
||||
|
||||
for (ticker in once) {
|
||||
ticker.tick()
|
||||
}
|
||||
|
||||
for (ticker in once) ticker.tick()
|
||||
once.clear()
|
||||
}
|
||||
|
||||
@ -219,20 +218,14 @@ class TickList : ITickable {
|
||||
if (conditionalQueued.isNotEmpty()) {
|
||||
shouldTick = true
|
||||
|
||||
for (ticker in conditionalQueued) {
|
||||
conditional.addFirst(ticker)
|
||||
}
|
||||
|
||||
for (ticker in conditionalQueued) conditional.add(ticker)
|
||||
conditionalQueued.clear()
|
||||
}
|
||||
|
||||
if (onceQueued.isNotEmpty()) {
|
||||
shouldTick = true
|
||||
|
||||
for (ticker in onceQueued) {
|
||||
once.addFirst(ticker)
|
||||
}
|
||||
|
||||
for (ticker in onceQueued) once.add(ticker)
|
||||
onceQueued.clear()
|
||||
}
|
||||
|
||||
@ -241,7 +234,7 @@ class TickList : ITickable {
|
||||
|
||||
if (head.ringAt <= ticks) {
|
||||
head.execute()
|
||||
timers.removeFirst()
|
||||
timers.remove(head)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user