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
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 {
return ChunkPos(x, z)
data class ChunkPos(var x: Int, var z: Int) {
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
import com.artillexstudios.axminions.AxMinionsPlugin
import com.artillexstudios.axminions.api.integrations.types.StackerIntegration
import org.bukkit.Location
import org.bukkit.entity.Item
@ -22,6 +21,6 @@ class DefaultStackerIntegration : StackerIntegration {
}
override fun register() {
AxMinionsPlugin.integrations.register(this)
}
}

View File

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

View File

@ -115,7 +115,7 @@ class Minion(
}
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)

View File

@ -7,8 +7,8 @@ object MinionTicker {
private var tick = 0L
private inline fun tickAll() {
Minions.get().values.forEach { list ->
list.fastFor {
Minions.get().fastFor { pos ->
pos.minions.fastFor {
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.utils.fastFor
import java.util.Collections
import java.util.concurrent.ConcurrentHashMap
import org.bukkit.Chunk
object Minions {
private val minions = ConcurrentHashMap<ChunkPos, ArrayList<Minion>>()
private val minions = arrayListOf<ChunkPos>()
fun addTicking(chunk: Chunk) {
val pos = ChunkPos(chunk.x, chunk.z)
val chunkX = chunk.x
val chunkZ = chunk.z
minions[pos]?.fastFor {
it.setTicking(true)
} ?: return
println("LOADING CHUNK! X: ${chunk.x} Z: ${chunk.z}")
run breaking@ {
minions.fastFor {
if (it.x == chunkX && it.z == chunkZ) {
it.setTicking(true)
return@breaking
}
}
}
}
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) {
val pos = ChunkPos(chunk.x, chunk.z)
val chunkX = chunk.x
val chunkZ = chunk.z
val minions = this.minions[pos] ?: return
minions.fastFor {
it.setTicking(false)
run breaking@ {
minions.fastFor {
if (it.x == chunkX && it.z == chunkZ) {
it.setTicking(false)
return@breaking
}
}
}
}
fun load(minion: Minion) {
println("LOADING MINION!!")
val chunkPos = ChunkPos(minion.getLocation().chunk.x, minion.getLocation().chunk.z)
val pos = minions[chunkPos] ?: arrayListOf()
val chunkX = round(minion.getLocation().x) shr 4
val chunkZ = round(minion.getLocation().z) shr 4
pos.add(minion)
minions[chunkPos] = pos
var pos: ChunkPos? = null
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) {
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 (pos.isEmpty()) {
minions.remove(chunkPos)
if (next.x == chunkX && next.z == chunkZ) {
if (next.removeMinion(minion)) {
iterator.remove()
}
break
}
}
}
fun getMinions(): List<Minion> {
val list = mutableListOf<Minion>()
minions.forEach { (_, value) ->
list.addAll(value)
minions.fastFor {
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
}