Try fixing minion placement - refactor Minions & ChunkPos

This commit is contained in:
TomTom 2023-10-14 22:23:46 +02:00
parent f34b153da2
commit d37cda336f
6 changed files with 90 additions and 37 deletions

View File

@ -1,8 +1,28 @@
package com.artillexstudios.axminions.api.minions.utils package com.artillexstudios.axminions.api.minions.utils
data class ChunkPos(var x: Int, var z: Int) { import com.artillexstudios.axminions.api.minions.Minion
import com.artillexstudios.axminions.api.utils.fastFor
fun clone(): ChunkPos { data class ChunkPos(var x: Int, var z: Int) {
return ChunkPos(x, z) val minions = arrayListOf<Minion>()
fun addMinion(minion: Minion) {
minions.add(minion)
}
fun removeMinion(minion: Minion): Boolean {
minions.remove(minion)
return minions.isEmpty()
}
fun setTicking(ticking: Boolean) {
minions.fastFor {
it.setTicking(ticking)
}
}
override fun toString(): String {
return "ChunkPos{x=$x,z=$z,minions=$minions}"
} }
} }

View File

@ -1,6 +1,5 @@
package com.artillexstudios.axminions.integrations.stacker package com.artillexstudios.axminions.integrations.stacker
import com.artillexstudios.axminions.AxMinionsPlugin
import com.artillexstudios.axminions.api.integrations.types.StackerIntegration import com.artillexstudios.axminions.api.integrations.types.StackerIntegration
import org.bukkit.Location import org.bukkit.Location
import org.bukkit.entity.Item import org.bukkit.entity.Item
@ -22,6 +21,6 @@ class DefaultStackerIntegration : StackerIntegration {
} }
override fun register() { override fun register() {
AxMinionsPlugin.integrations.register(this)
} }
} }

View File

@ -58,7 +58,6 @@ class MinionPlaceListener : Listener {
minion.setLevel(level) minion.setLevel(level)
minion.setActions(stats) minion.setActions(stats)
minion.setTicking(true) minion.setTicking(true)
Minions.addTicking(location.chunk)
event.player.sendMessage("Placed minion $minion. Ticking? ${minion.isTicking()} Is chunk ticking? ${Minions.isTicking(location.chunk)}") event.player.sendMessage("Placed minion $minion. Ticking? ${minion.isTicking()} Is chunk ticking? ${Minions.isTicking(location.chunk)}")
AxMinionsPlugin.dataHandler.saveMinion(minion) AxMinionsPlugin.dataHandler.saveMinion(minion)
} }

View File

@ -115,7 +115,7 @@ class Minion(
} }
if (Config.DEBUG() && debugHologram != null) { if (Config.DEBUG() && debugHologram != null) {
debugHologram?.setLine(0, StringUtils.format("Ticking: $ticking Chunk ticking: ${Minions.isTicking(location.chunk)} CHUNK X: ${location.chunk.x} CHUNK Z: ${location.chunk.z}")) debugHologram?.setLine(0, StringUtils.format("Ticking: $ticking"))
} }
type.tick(this) type.tick(this)

View File

@ -7,8 +7,8 @@ object MinionTicker {
private var tick = 0L private var tick = 0L
private inline fun tickAll() { private inline fun tickAll() {
Minions.get().values.forEach { list -> Minions.get().fastFor { pos ->
list.fastFor { pos.minions.fastFor {
it.tick() it.tick()
} }
} }

View File

@ -4,67 +4,102 @@ import com.artillexstudios.axminions.api.minions.Minion
import com.artillexstudios.axminions.api.minions.utils.ChunkPos import com.artillexstudios.axminions.api.minions.utils.ChunkPos
import com.artillexstudios.axminions.api.utils.fastFor import com.artillexstudios.axminions.api.utils.fastFor
import java.util.Collections import java.util.Collections
import java.util.concurrent.ConcurrentHashMap
import org.bukkit.Chunk import org.bukkit.Chunk
object Minions { object Minions {
private val minions = ConcurrentHashMap<ChunkPos, ArrayList<Minion>>() private val minions = arrayListOf<ChunkPos>()
fun addTicking(chunk: Chunk) { fun addTicking(chunk: Chunk) {
val pos = ChunkPos(chunk.x, chunk.z) val chunkX = chunk.x
val chunkZ = chunk.z
minions[pos]?.fastFor { run breaking@ {
it.setTicking(true) minions.fastFor {
} ?: return if (it.x == chunkX && it.z == chunkZ) {
println("LOADING CHUNK! X: ${chunk.x} Z: ${chunk.z}") it.setTicking(true)
return@breaking
}
}
}
} }
fun isTicking(chunk: Chunk): Boolean { fun isTicking(chunk: Chunk): Boolean {
val pos = ChunkPos(chunk.x, chunk.z) val chunkX = chunk.x
val chunkZ = chunk.z
return minions.contains(pos) minions.fastFor {
if (it.x == chunkX && it.z == chunkZ) {
return true
}
}
return false
} }
fun removeTicking(chunk: Chunk) { fun removeTicking(chunk: Chunk) {
val pos = ChunkPos(chunk.x, chunk.z) val chunkX = chunk.x
val chunkZ = chunk.z
val minions = this.minions[pos] ?: return run breaking@ {
minions.fastFor {
minions.fastFor { if (it.x == chunkX && it.z == chunkZ) {
it.setTicking(false) it.setTicking(false)
return@breaking
}
}
} }
} }
fun load(minion: Minion) { fun load(minion: Minion) {
println("LOADING MINION!!") val chunkX = round(minion.getLocation().x) shr 4
val chunkPos = ChunkPos(minion.getLocation().chunk.x, minion.getLocation().chunk.z) val chunkZ = round(minion.getLocation().z) shr 4
val pos = minions[chunkPos] ?: arrayListOf()
pos.add(minion) var pos: ChunkPos? = null
minions[chunkPos] = pos run breaking@ {
minions.fastFor {
if (it.x == chunkX && it.z == chunkZ) {
pos = it
return@breaking
}
}
}
if (pos === null) {
pos = ChunkPos(chunkX, chunkZ)
println("Adding new pos! $pos")
minions.add(pos!!)
}
pos!!.addMinion(minion)
} }
fun remove(minion: Minion) { fun remove(minion: Minion) {
val chunkPos = ChunkPos(minion.getLocation().chunk.x, minion.getLocation().chunk.z) val chunkX = round(minion.getLocation().x) shr 4
val chunkZ = round(minion.getLocation().z) shr 4
val pos = minions[chunkPos] ?: return val iterator = minions.iterator()
while (iterator.hasNext()) {
val next = iterator.next()
pos.remove(minion) if (next.x == chunkX && next.z == chunkZ) {
if (pos.isEmpty()) { if (next.removeMinion(minion)) {
minions.remove(chunkPos) iterator.remove()
}
break
}
} }
} }
fun getMinions(): List<Minion> { fun getMinions(): List<Minion> {
val list = mutableListOf<Minion>() val list = mutableListOf<Minion>()
minions.forEach { (_, value) -> minions.fastFor {
list.addAll(value) list.addAll(it.minions)
} }
return Collections.unmodifiableList(list)
return Collections.unmodifiableList(list)
} }
internal fun get(): ConcurrentHashMap<ChunkPos, ArrayList<Minion>> { internal fun get(): ArrayList<ChunkPos> {
return minions return minions
} }