diff --git a/api/src/main/kotlin/com/artillexstudios/axminions/api/utils/MinionUtils.kt b/api/src/main/kotlin/com/artillexstudios/axminions/api/utils/MinionUtils.kt index a6b9e7d..9c19a82 100644 --- a/api/src/main/kotlin/com/artillexstudios/axminions/api/utils/MinionUtils.kt +++ b/api/src/main/kotlin/com/artillexstudios/axminions/api/utils/MinionUtils.kt @@ -8,7 +8,7 @@ import org.bukkit.block.Block import org.bukkit.block.BlockFace object MinionUtils { - private val FACES = + val FACES = arrayOf(BlockFace.DOWN, BlockFace.UP, BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST) @JvmStatic @@ -36,58 +36,7 @@ object MinionUtils { return blocks } - @JvmStatic - fun isStoneGenerator(location: Location): Boolean { - var lava = false - var water = false - - val locBlock = location.block - - FACES.fastFor { - val relative = locBlock.getRelative(it) - val type = relative.type - if (!lava) { - lava = type == Material.LAVA - } - - if (!water) { - water = type == Material.WATER - } - - if (water && lava) { - return true - } - } - - - return false - } - - @JvmStatic - fun getTree(startBlock: Block): Set { - val queue: Queue = LinkedList() - val visited = mutableSetOf() - val tree = mutableSetOf() - - queue.add(startBlock) - - while (queue.isNotEmpty()) { - val block = queue.poll() - - val type = block.type.toString() - if (type.endsWith("_WOOD") || type.endsWith("_LOG")) { - tree.add(block) - - FACES.fastFor { - val relative = block.getRelative(it) - if (!visited.contains(relative)) { - queue.add(relative) - visited.add(relative) - } - } - } - } - - return tree + fun Location.relative(face: BlockFace): Location { + return this.clone().add(face.modX.toDouble(), face.modY.toDouble(), face.modZ.toDouble()) } } \ No newline at end of file diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/minions/Minions.kt b/common/src/main/kotlin/com/artillexstudios/axminions/minions/Minions.kt index 90518c5..1077042 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/minions/Minions.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/minions/Minions.kt @@ -2,11 +2,20 @@ package com.artillexstudios.axminions.minions import com.artillexstudios.axminions.api.minions.Minion import com.artillexstudios.axminions.api.utils.ChunkPos +import com.artillexstudios.axminions.api.utils.MinionUtils +import com.artillexstudios.axminions.api.utils.MinionUtils.relative +import com.artillexstudios.axminions.api.utils.fastFor +import com.artillexstudios.axminions.cache.Caches import java.util.Collections +import java.util.LinkedList +import java.util.Queue import java.util.concurrent.locks.ReentrantReadWriteLock import kotlin.concurrent.read import kotlin.concurrent.write import org.bukkit.Chunk +import org.bukkit.Location +import org.bukkit.Material +import org.bukkit.block.Block object Minions { val lock = ReentrantReadWriteLock() @@ -129,4 +138,60 @@ object Minions { private infix fun round(double: Double): Int { return (double + 0.5).toInt() } + + @JvmStatic + fun getTree(startBlock: Location): Set { + val queue: Queue = LinkedList() + val visited = mutableSetOf() + val tree = mutableSetOf() + + val cache = Caches.get(startBlock.world!!) ?: return tree + queue.add(startBlock) + + while (queue.isNotEmpty()) { + val block = queue.poll() + + val type = cache.get(block.x.toInt(), block.y.toInt(), block.z.toInt()) as? Material ?: continue + if (type.name.endsWith("_WOOD") || type.name.endsWith("_LOG")) { + tree.add(block.block) + + MinionUtils.FACES.fastFor { + val relative = block.relative(it) + if (!visited.contains(relative)) { + queue.add(relative) + visited.add(relative) + } + } + } + } + + return tree + } + + @JvmStatic + fun isStoneGenerator(location: Location): Boolean { + var lava = false + var water = false + + val cache = Caches.get(location.world!!) ?: return false + + MinionUtils.FACES.fastFor { + val relative = location.relative(it) + val type = cache.get(relative.x.toInt(), relative.y.toInt(), relative.z.toInt()) as? Material ?: return@fastFor + + if (!lava) { + lava = type == Material.LAVA + } + + if (!water) { + water = type == Material.WATER + } + + if (water && lava) { + return true + } + } + + return false + } } \ No newline at end of file diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/LumberMinionType.kt b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/LumberMinionType.kt index efd4512..3401082 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/LumberMinionType.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/LumberMinionType.kt @@ -4,10 +4,10 @@ import com.artillexstudios.axminions.AxMinionsPlugin import com.artillexstudios.axminions.api.minions.Minion import com.artillexstudios.axminions.api.minions.miniontype.MinionType import com.artillexstudios.axminions.api.utils.LocationUtils -import com.artillexstudios.axminions.api.utils.MinionUtils import com.artillexstudios.axminions.api.utils.fastFor import com.artillexstudios.axminions.api.warnings.Warnings import com.artillexstudios.axminions.minions.MinionTicker +import com.artillexstudios.axminions.minions.Minions import kotlin.math.roundToInt import org.bukkit.Material import org.bukkit.block.BlockFace @@ -53,7 +53,7 @@ class LumberMinionType : MinionType("lumber", AxMinionsPlugin.INSTANCE.getResour val loot = ArrayList() LocationUtils.getAllBlocksInRadius(minion.getLocation(), minion.getRange(), false).fastFor { location -> - MinionUtils.getTree(location.block).forEach { + Minions.getTree(location).forEach { val down = it.getRelative(BlockFace.DOWN).type loot.addAll(it.getDrops(minion.getTool())) diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/MinerMinionType.kt b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/MinerMinionType.kt index e8594c5..6c51422 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/MinerMinionType.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/MinerMinionType.kt @@ -1,28 +1,23 @@ package com.artillexstudios.axminions.minions.miniontype -import com.artillexstudios.axapi.scheduler.Scheduler -import com.artillexstudios.axapi.scheduler.impl.FoliaScheduler import com.artillexstudios.axminions.AxMinionsPlugin import com.artillexstudios.axminions.api.minions.Minion import com.artillexstudios.axminions.api.minions.miniontype.MinionType import com.artillexstudios.axminions.api.utils.LocationUtils -import com.artillexstudios.axminions.api.utils.MinionUtils import com.artillexstudios.axminions.api.utils.fastFor import com.artillexstudios.axminions.api.warnings.Warnings -import com.artillexstudios.axminions.integrations.Integrations +import com.artillexstudios.axminions.cache.Caches import com.artillexstudios.axminions.minions.MinionTicker +import com.artillexstudios.axminions.minions.Minions import java.util.Locale -import java.util.concurrent.ExecutorService -import java.util.concurrent.Executors import kotlin.math.roundToInt import me.kryniowesegryderiusz.kgenerators.Main -import me.kryniowesegryderiusz.kgenerators.api.KGeneratorsAPI +import me.kryniowesegryderiusz.kgenerators.generators.locations.objects.GeneratorLocation import org.bukkit.Material import org.bukkit.block.BlockFace import org.bukkit.enchantments.Enchantment class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource("minions/miner.yml")!!) { - private var asyncExecutor: ExecutorService? = null private val faces = arrayOf(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST) override fun shouldRun(minion: Minion): Boolean { @@ -64,7 +59,7 @@ class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource when (getConfig().getString("mode").lowercase(Locale.ENGLISH)) { "sphere" -> { LocationUtils.getAllBlocksInRadius(minion.getLocation(), minion.getRange(), false).fastFor { location -> - val isStoneGenerator = MinionUtils.isStoneGenerator(location) + val isStoneGenerator = Minions.isStoneGenerator(location) if (isStoneGenerator) { val drops = location.block.getDrops(minion.getTool()) @@ -77,80 +72,22 @@ class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource } } - "asphere" -> { - if (Scheduler.get() !is FoliaScheduler) { - if (asyncExecutor == null) { - asyncExecutor = Executors.newFixedThreadPool(3) - } - - asyncExecutor!!.execute { - LocationUtils.getAllBlocksInRadius(minion.getLocation(), minion.getRange(), false) - .fastFor { location -> - if (AxMinionsPlugin.integrations.kGeneratorsIntegration && Main.getPlacedGenerators().isChunkFullyLoaded(location)) { - val gen = Main.getPlacedGenerators().getLoaded(location) - val possible = gen?.isBlockPossibleToMine(location) ?: false - - if (possible) { - gen?.scheduleGeneratorRegeneration() - return@fastFor - } - } - - val isStoneGenerator = MinionUtils.isStoneGenerator(location) - - if (isStoneGenerator) { - Scheduler.get().run { - val drops = location.block.getDrops(minion.getTool()) - drops.forEach { - amount += it.amount - } - minion.addToContainerOrDrop(drops) - location.block.type = Material.AIR - } - } - } - } - } else { - LocationUtils.getAllBlocksInRadius(minion.getLocation(), minion.getRange(), false) - .fastFor { location -> - if (AxMinionsPlugin.integrations.kGeneratorsIntegration && Main.getPlacedGenerators().isChunkFullyLoaded(location)) { - val gen = Main.getPlacedGenerators().getLoaded(location) - val possible = gen?.isBlockPossibleToMine(location) ?: false - - if (possible) { - gen?.scheduleGeneratorRegeneration() - return@fastFor - } - } - - val isStoneGenerator = MinionUtils.isStoneGenerator(location) - - if (isStoneGenerator) { - val drops = location.block.getDrops(minion.getTool()) - drops.forEach { - amount += it.amount - } - minion.addToContainerOrDrop(drops) - location.block.type = Material.AIR - } - } - } - } - "line" -> { faces.fastFor { LocationUtils.getAllBlocksFacing(minion.getLocation(), minion.getRange(), it).fastFor { location -> - if (AxMinionsPlugin.integrations.kGeneratorsIntegration && Main.getPlacedGenerators().isChunkFullyLoaded(location)) { + val cache = Caches.get(location.world!!) ?: return@fastFor + val type = cache.get(location.x.toInt(), location.y.toInt(), location.z.toInt()) + if (AxMinionsPlugin.integrations.kGeneratorsIntegration && type is GeneratorLocation) { val gen = Main.getPlacedGenerators().getLoaded(location) val possible = gen?.isBlockPossibleToMine(location) ?: false if (possible) { - gen?.scheduleGeneratorRegeneration() + type.scheduleGeneratorRegeneration() return@fastFor } } - val isStoneGenerator = MinionUtils.isStoneGenerator(location) + val isStoneGenerator = Minions.isStoneGenerator(location) if (isStoneGenerator) { val drops = location.block.getDrops(minion.getTool()) @@ -167,17 +104,19 @@ class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource "face" -> { LocationUtils.getAllBlocksFacing(minion.getLocation(), minion.getRange(), minion.getDirection().facing) .fastFor { location -> - if (AxMinionsPlugin.integrations.kGeneratorsIntegration && Main.getPlacedGenerators().isChunkFullyLoaded(location)) { + val cache = Caches.get(location.world!!) ?: return@fastFor + val type = cache.get(location.x.toInt(), location.y.toInt(), location.z.toInt()) + if (AxMinionsPlugin.integrations.kGeneratorsIntegration && type is GeneratorLocation) { val gen = Main.getPlacedGenerators().getLoaded(location) val possible = gen?.isBlockPossibleToMine(location) ?: false if (possible) { - gen?.scheduleGeneratorRegeneration() + type.scheduleGeneratorRegeneration() return@fastFor } } - val isStoneGenerator = MinionUtils.isStoneGenerator(location) + val isStoneGenerator = Minions.isStoneGenerator(location) if (isStoneGenerator) { val drops = location.block.getDrops(minion.getTool())