This commit is contained in:
TomTom 2024-01-29 19:55:47 +01:00
parent 9962cd4683
commit 69d072e7e5
4 changed files with 84 additions and 131 deletions

View File

@ -8,7 +8,7 @@ import org.bukkit.block.Block
import org.bukkit.block.BlockFace import org.bukkit.block.BlockFace
object MinionUtils { object MinionUtils {
private val FACES = val FACES =
arrayOf(BlockFace.DOWN, BlockFace.UP, BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST) arrayOf(BlockFace.DOWN, BlockFace.UP, BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST)
@JvmStatic @JvmStatic
@ -36,58 +36,7 @@ object MinionUtils {
return blocks return blocks
} }
@JvmStatic fun Location.relative(face: BlockFace): Location {
fun isStoneGenerator(location: Location): Boolean { return this.clone().add(face.modX.toDouble(), face.modY.toDouble(), face.modZ.toDouble())
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<Block> {
val queue: Queue<Block> = LinkedList()
val visited = mutableSetOf<Block>()
val tree = mutableSetOf<Block>()
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
} }
} }

View File

@ -2,11 +2,20 @@ package com.artillexstudios.axminions.minions
import com.artillexstudios.axminions.api.minions.Minion import com.artillexstudios.axminions.api.minions.Minion
import com.artillexstudios.axminions.api.utils.ChunkPos 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.Collections
import java.util.LinkedList
import java.util.Queue
import java.util.concurrent.locks.ReentrantReadWriteLock import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read import kotlin.concurrent.read
import kotlin.concurrent.write import kotlin.concurrent.write
import org.bukkit.Chunk import org.bukkit.Chunk
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.block.Block
object Minions { object Minions {
val lock = ReentrantReadWriteLock() val lock = ReentrantReadWriteLock()
@ -129,4 +138,60 @@ object Minions {
private infix fun round(double: Double): Int { private infix fun round(double: Double): Int {
return (double + 0.5).toInt() return (double + 0.5).toInt()
} }
@JvmStatic
fun getTree(startBlock: Location): Set<Block> {
val queue: Queue<Location> = LinkedList()
val visited = mutableSetOf<Location>()
val tree = mutableSetOf<Block>()
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
}
} }

View File

@ -4,10 +4,10 @@ import com.artillexstudios.axminions.AxMinionsPlugin
import com.artillexstudios.axminions.api.minions.Minion import com.artillexstudios.axminions.api.minions.Minion
import com.artillexstudios.axminions.api.minions.miniontype.MinionType import com.artillexstudios.axminions.api.minions.miniontype.MinionType
import com.artillexstudios.axminions.api.utils.LocationUtils 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.utils.fastFor
import com.artillexstudios.axminions.api.warnings.Warnings import com.artillexstudios.axminions.api.warnings.Warnings
import com.artillexstudios.axminions.minions.MinionTicker import com.artillexstudios.axminions.minions.MinionTicker
import com.artillexstudios.axminions.minions.Minions
import kotlin.math.roundToInt import kotlin.math.roundToInt
import org.bukkit.Material import org.bukkit.Material
import org.bukkit.block.BlockFace import org.bukkit.block.BlockFace
@ -53,7 +53,7 @@ class LumberMinionType : MinionType("lumber", AxMinionsPlugin.INSTANCE.getResour
val loot = ArrayList<ItemStack>() val loot = ArrayList<ItemStack>()
LocationUtils.getAllBlocksInRadius(minion.getLocation(), minion.getRange(), false).fastFor { location -> 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 val down = it.getRelative(BlockFace.DOWN).type
loot.addAll(it.getDrops(minion.getTool())) loot.addAll(it.getDrops(minion.getTool()))

View File

@ -1,28 +1,23 @@
package com.artillexstudios.axminions.minions.miniontype 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.AxMinionsPlugin
import com.artillexstudios.axminions.api.minions.Minion import com.artillexstudios.axminions.api.minions.Minion
import com.artillexstudios.axminions.api.minions.miniontype.MinionType import com.artillexstudios.axminions.api.minions.miniontype.MinionType
import com.artillexstudios.axminions.api.utils.LocationUtils 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.utils.fastFor
import com.artillexstudios.axminions.api.warnings.Warnings 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.MinionTicker
import com.artillexstudios.axminions.minions.Minions
import java.util.Locale import java.util.Locale
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import kotlin.math.roundToInt import kotlin.math.roundToInt
import me.kryniowesegryderiusz.kgenerators.Main 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.Material
import org.bukkit.block.BlockFace import org.bukkit.block.BlockFace
import org.bukkit.enchantments.Enchantment import org.bukkit.enchantments.Enchantment
class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource("minions/miner.yml")!!) { 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) private val faces = arrayOf(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST)
override fun shouldRun(minion: Minion): Boolean { override fun shouldRun(minion: Minion): Boolean {
@ -64,7 +59,7 @@ class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource
when (getConfig().getString("mode").lowercase(Locale.ENGLISH)) { when (getConfig().getString("mode").lowercase(Locale.ENGLISH)) {
"sphere" -> { "sphere" -> {
LocationUtils.getAllBlocksInRadius(minion.getLocation(), minion.getRange(), false).fastFor { location -> LocationUtils.getAllBlocksInRadius(minion.getLocation(), minion.getRange(), false).fastFor { location ->
val isStoneGenerator = MinionUtils.isStoneGenerator(location) val isStoneGenerator = Minions.isStoneGenerator(location)
if (isStoneGenerator) { if (isStoneGenerator) {
val drops = location.block.getDrops(minion.getTool()) 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" -> { "line" -> {
faces.fastFor { faces.fastFor {
LocationUtils.getAllBlocksFacing(minion.getLocation(), minion.getRange(), it).fastFor { location -> 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 gen = Main.getPlacedGenerators().getLoaded(location)
val possible = gen?.isBlockPossibleToMine(location) ?: false val possible = gen?.isBlockPossibleToMine(location) ?: false
if (possible) { if (possible) {
gen?.scheduleGeneratorRegeneration() type.scheduleGeneratorRegeneration()
return@fastFor return@fastFor
} }
} }
val isStoneGenerator = MinionUtils.isStoneGenerator(location) val isStoneGenerator = Minions.isStoneGenerator(location)
if (isStoneGenerator) { if (isStoneGenerator) {
val drops = location.block.getDrops(minion.getTool()) val drops = location.block.getDrops(minion.getTool())
@ -167,17 +104,19 @@ class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource
"face" -> { "face" -> {
LocationUtils.getAllBlocksFacing(minion.getLocation(), minion.getRange(), minion.getDirection().facing) LocationUtils.getAllBlocksFacing(minion.getLocation(), minion.getRange(), minion.getDirection().facing)
.fastFor { location -> .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 gen = Main.getPlacedGenerators().getLoaded(location)
val possible = gen?.isBlockPossibleToMine(location) ?: false val possible = gen?.isBlockPossibleToMine(location) ?: false
if (possible) { if (possible) {
gen?.scheduleGeneratorRegeneration() type.scheduleGeneratorRegeneration()
return@fastFor return@fastFor
} }
} }
val isStoneGenerator = MinionUtils.isStoneGenerator(location) val isStoneGenerator = Minions.isStoneGenerator(location)
if (isStoneGenerator) { if (isStoneGenerator) {
val drops = location.block.getDrops(minion.getTool()) val drops = location.block.getDrops(minion.getTool())