Optimise the plugin a bit

This commit is contained in:
TomTom 2023-10-21 22:40:46 +02:00
parent c781bf0587
commit d5a48830ba
16 changed files with 59 additions and 33 deletions

View File

@ -7,6 +7,7 @@ import com.artillexstudios.axapi.utils.ItemBuilder
import com.artillexstudios.axminions.api.AxMinionsAPI
import com.artillexstudios.axminions.api.minions.Minion
import com.artillexstudios.axminions.api.utils.Keys
import com.artillexstudios.axminions.api.utils.fastFor
import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataType
import java.io.File
@ -75,12 +76,12 @@ abstract class MinionType(private val name: String, private val defaults: InputS
private fun <T> get(key: String, level: Int, defaultValue: T?, clazz: Class<T>): T? {
var n = defaultValue
config.getSection("upgrades").getRoutesAsStrings(false).forEach {
config.getSection("upgrades").getRoutesAsStrings(false).fastFor {
if (it.toInt() > level) {
return n
}
if (config.backingDocument.getAsOptional("upgrades.$it.$key", clazz).isEmpty) return@forEach
if (config.backingDocument.getAsOptional("upgrades.$it.$key", clazz).isEmpty) return@fastFor
n = config.get("upgrades.$it.$key")
}

View File

@ -2,6 +2,7 @@ package com.artillexstudios.axminions.api.minions.miniontype
import com.artillexstudios.axminions.api.AxMinionsAPI
import com.artillexstudios.axminions.api.exception.MinionTypeAlreadyRegisteredException
import com.artillexstudios.axminions.api.utils.fastFor
import java.util.Collections
import org.bukkit.World
@ -21,8 +22,8 @@ object MinionTypes {
@JvmStatic
fun loadForWorld(world: World) {
TYPES.forEach {
AxMinionsAPI.INSTANCE.getDataHandler().loadMinionsForWorld(it.value, world)
TYPES.fastFor { _, v ->
AxMinionsAPI.INSTANCE.getDataHandler().loadMinionsForWorld(v, world)
}
}

View File

@ -1,5 +1,8 @@
package com.artillexstudios.axminions.api.utils
import java.util.Collections
import java.util.Queue
inline fun <T> Array<T>.fastFor(action: (T) -> Unit) {
val indices = indices
for (i in indices) {
@ -13,4 +16,20 @@ inline fun <T> List<T>.fastFor(action: (T) -> Unit) {
for (i in indices) {
action(get(i))
}
}
inline fun <T> Collection<T>.fastFor(action: (T) -> Unit) {
if (isEmpty()) return
val indices = indices
for (i in indices) {
action(elementAt(i))
}
}
inline fun <K, V> Map<K, V>.fastFor(action: (K, V) -> Unit) {
if (isEmpty()) return
entries.fastFor {
action(it.key, it.value)
}
}

View File

@ -78,15 +78,17 @@ object MinionUtils {
FACES.fastFor {
val relative = block.getRelative(it)
if (visited.add(relative)) {
if (visited.contains(relative)) {
queue.add(relative)
visited.add(relative)
}
}
} else if (type.endsWith("_LEAVES")) {
FACES.fastFor {
val relative = block.getRelative(it)
if (visited.add(relative)) {
if (visited.contains(relative)) {
queue.add(relative)
visited.add(relative)
}
}
}

View File

@ -101,8 +101,8 @@ class AxMinionsPlugin : AxPlugin() {
// Retroactively load minions for the already loaded worlds
Bukkit.getWorlds().fastFor { world ->
MinionTypes.getMinionTypes().forEach { map ->
dataHandler.loadMinionsForWorld(map.value, world)
MinionTypes.getMinionTypes().fastFor { _, v ->
dataHandler.loadMinionsForWorld(v, world)
}
}

View File

@ -7,6 +7,7 @@ import com.artillexstudios.axminions.api.config.Messages
import com.artillexstudios.axminions.api.data.DataHandler
import com.artillexstudios.axminions.api.integrations.Integrations
import com.artillexstudios.axminions.api.minions.Minion
import com.artillexstudios.axminions.api.utils.fastFor
import com.artillexstudios.axminions.minions.Minions
import org.bukkit.entity.Player
import java.io.File
@ -40,9 +41,9 @@ class AxMinionsAPIImpl(private val plugin: AxMinionsPlugin) : AxMinionsAPI {
override fun getMinionLimit(player: Player): Int {
var limit = Config.DEFAULT_MINION_LIMIT()
player.effectivePermissions.forEach {
player.effectivePermissions.fastFor {
val permission = it.permission
if (!permission.startsWith("axminions.limit.")) return@forEach
if (!permission.startsWith("axminions.limit.")) return@fastFor
if (permission.contains("*")) {
return Int.MAX_VALUE
}

View File

@ -47,8 +47,8 @@ class AxMinionsCommand {
AxMinionsPlugin.config.reload()
AxMinionsPlugin.messages.reload()
MinionTypes.getMinionTypes().forEach {
it.value.getConfig().reload()
MinionTypes.getMinionTypes().fastFor { _, v ->
v.getConfig().reload()
}
AxMinionsAPI.INSTANCE.getMinions().fastFor {

View File

@ -18,7 +18,7 @@ class DefaultStackerIntegration : StackerIntegration {
}
override fun dropItemAt(itemStack: ItemStack, amount: Int, location: Location) {
Scheduler.get().runAt(location) {
Scheduler.get().executeAt(location) {
location.world!!.dropItem(location, itemStack)
}
}

View File

@ -73,7 +73,7 @@ class Minion(
Minions.load(this)
if (linkedChest != null) {
Scheduler.get().runAt(linkedChest) {
Scheduler.get().executeAt(linkedChest) {
linkedInventory = (linkedChest?.block?.state as? Container)?.inventory
}
}
@ -130,7 +130,7 @@ class Minion(
val remaining = event.player.inventory.addItem(tool, asItem)
remove()
remaining.forEach { (_, i) ->
remaining.fastFor { _, i ->
AxMinionsPlugin.integrations.getStackerIntegration().dropItemAt(i, i.amount, location)
}
}
@ -145,7 +145,7 @@ class Minion(
debugHologram?.setLine(0, StringUtils.format("Ticking: $ticking"))
}
Scheduler.get().runAt(location) {
Scheduler.get().executeAt(location) {
type.tick(this)
}
animate()
@ -162,8 +162,8 @@ class Minion(
}
private fun updateInventory(inventory: Inventory) {
AxMinionsAPI.INSTANCE.getConfig().getConfig().getSection("gui.items").getRoutesAsStrings(false).forEach {
if (it.equals("filler")) return@forEach
AxMinionsAPI.INSTANCE.getConfig().getConfig().getSection("gui.items").getRoutesAsStrings(false).fastFor {
if (it.equals("filler")) return@fastFor
val item: ItemStack
if (it.equals("upgrade", true) || it.equals("statistics", true)) {
val level = Placeholder.unparsed("level", level.toString())
@ -370,7 +370,7 @@ class Minion(
override fun setLinkedChest(location: Location?) {
this.linkedChest = location?.clone()
if (linkedChest != null) {
Scheduler.get().runAt(linkedChest) {
Scheduler.get().executeAt(linkedChest) {
linkedInventory = (linkedChest?.block?.state as? Container)?.inventory
updateInventories()
@ -422,7 +422,7 @@ class Minion(
val remaining = linkedInventory?.addItem(itemStack)
remaining?.forEach { (_, u) ->
remaining?.fastFor { _, u ->
AxMinionsPlugin.integrations.getStackerIntegration().dropItemAt(u, u.amount, location)
}
}

View File

@ -4,10 +4,11 @@ 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.ConcurrentLinkedQueue
import org.bukkit.Chunk
object Minions {
private val minions = arrayListOf<ChunkPos>()
private val minions = ConcurrentLinkedQueue<ChunkPos>()
fun addTicking(chunk: Chunk) {
val chunkX = chunk.x
@ -98,7 +99,7 @@ object Minions {
return Collections.unmodifiableList(list)
}
internal fun get(): ArrayList<ChunkPos> {
internal fun get(): ConcurrentLinkedQueue<ChunkPos> {
return minions
}

View File

@ -3,6 +3,7 @@ package com.artillexstudios.axminions.minions.miniontype
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.fastFor
import com.artillexstudios.axminions.api.warnings.Warnings
import com.artillexstudios.axminions.minions.MinionTicker
import kotlin.math.roundToInt
@ -58,7 +59,7 @@ class CollectorMinionType : MinionType("collector", AxMinionsPlugin.INSTANCE.get
minion.getRange()
)
entities?.filterIsInstance<Item>()?.forEach { item ->
entities?.filterIsInstance<Item>()?.fastFor { item ->
if (minion.getLinkedInventory()?.firstEmpty() == -1) {
Warnings.CONTAINER_FULL.display(minion)
return

View File

@ -55,7 +55,7 @@ class FarmerMinionType : MinionType("farmer", AxMinionsPlugin.INSTANCE.getResour
Material.CACTUS, Material.SUGAR_CANE, Material.BAMBOO -> {
MinionUtils.getPlant(block).fastFor {
val blockDrops = it.getDrops(minion.getTool())
blockDrops.forEach { itemStack ->
blockDrops.fastFor { itemStack ->
size += itemStack.amount
}
drops.addAll(blockDrops)
@ -65,7 +65,7 @@ class FarmerMinionType : MinionType("farmer", AxMinionsPlugin.INSTANCE.getResour
Material.MELON, Material.PUMPKIN -> {
val blockDrops = block.getDrops(minion.getTool())
blockDrops.forEach { itemStack ->
blockDrops.fastFor { itemStack ->
size += itemStack.amount
}
drops.addAll(blockDrops)
@ -76,7 +76,7 @@ class FarmerMinionType : MinionType("farmer", AxMinionsPlugin.INSTANCE.getResour
val ageable = block.blockData as Ageable
if (ageable.age != ageable.maximumAge) return@fastFor
val blockDrops = block.getDrops(minion.getTool())
blockDrops.forEach { itemStack ->
blockDrops.fastFor { itemStack ->
size += itemStack.amount
}
drops.addAll(blockDrops)
@ -88,7 +88,7 @@ class FarmerMinionType : MinionType("farmer", AxMinionsPlugin.INSTANCE.getResour
val ageable = block.blockData as Ageable
if (ageable.age != ageable.maximumAge) return@fastFor
val blockDrops = block.getDrops(minion.getTool())
blockDrops.forEach { itemStack ->
blockDrops.fastFor { itemStack ->
size += itemStack.amount
}
drops.addAll(blockDrops)

View File

@ -49,7 +49,7 @@ class LumberMinionType : MinionType("lumber", AxMinionsPlugin.INSTANCE.getResour
val loot = ArrayList<ItemStack>()
LocationUtils.getAllBlocksInRadius(minion.getLocation(), minion.getRange(), false).fastFor { location ->
MinionUtils.getTree(location.block).forEach {
MinionUtils.getTree(location.block).fastFor {
val down = it.getRelative(BlockFace.DOWN).type
loot.addAll(it.getDrops(minion.getTool()))

View File

@ -60,7 +60,7 @@ class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource
if (isStoneGenerator) {
val drops = location.block.getDrops(minion.getTool())
drops.forEach {
drops.fastFor {
amount += it.amount
}
minion.addToContainerOrDrop(drops)
@ -83,7 +83,7 @@ class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource
if (isStoneGenerator) {
Scheduler.get().run {
val drops = location.block.getDrops(minion.getTool())
drops.forEach {
drops.fastFor {
amount += it.amount
}
minion.addToContainerOrDrop(drops)
@ -99,7 +99,7 @@ class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource
if (isStoneGenerator) {
val drops = location.block.getDrops(minion.getTool())
drops.forEach {
drops.fastFor {
amount += it.amount
}
minion.addToContainerOrDrop(drops)
@ -116,7 +116,7 @@ class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource
if (isStoneGenerator) {
val drops = location.block.getDrops(minion.getTool())
drops.forEach {
drops.fastFor {
amount += it.amount
}
minion.addToContainerOrDrop(drops)
@ -133,7 +133,7 @@ class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource
if (isStoneGenerator) {
val drops = location.block.getDrops(minion.getTool())
drops.forEach {
drops.fastFor {
amount += it.amount
}
minion.addToContainerOrDrop(drops)

Binary file not shown.

Binary file not shown.