Skip to content

Commit

Permalink
✨ Wait for all scripts when loading
Browse files Browse the repository at this point in the history
  • Loading branch information
way-zer committed Mar 7, 2020
1 parent ef153ad commit a54973b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main/kotlin/cf/wayzer/SuperItem/ItemManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import org.bukkit.inventory.ItemStack
import java.io.File
import java.net.URLClassLoader
import java.util.*
import java.util.concurrent.atomic.AtomicInteger
import java.util.logging.Level

object ItemManager {
private val logger = main.logger
private val items = HashMap<String, Item>()
private lateinit var ucl: URLClassLoader
private var asyncLoad = AtomicInteger(0)
lateinit var rootDir: File

/**
Expand All @@ -32,6 +34,10 @@ object ItemManager {
ucl = URLClassLoader.newInstance(arrayOf(rootDir.toURI().toURL()),
ItemManager::class.java.classLoader)
loadDir(rootDir, "")
if (asyncLoad.get()>0){
logger.info("Wait for all scripts loaded")
while (asyncLoad.get()>0) Thread.sleep(100)
}
}

private fun loadDir(dir: File, prefix: String) {
Expand All @@ -57,9 +63,14 @@ object ItemManager {
if (file.name.endsWith("superitem.kts")) {
ScriptSupporter.init(logger)
logger.info("Load Item in async: ${file.name}")
asyncLoad.incrementAndGet()
GlobalScope.launch {
callBack(ScriptSupporter.load(file))
}
try {
callBack(ScriptSupporter.load(file))
}finally {
asyncLoad.decrementAndGet()
}
}.start()
} else if (file.name.endsWith(".class") && !file.name.contains("$")) {
val c = ucl.loadClass(className)
if (c.superclass != Item::class.java) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/cf/wayzer/SuperItem/features/NBT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cf.wayzer.SuperItem.Feature
import cf.wayzer.SuperItem.Main
import de.tr7zw.changeme.nbtapi.NBTCompound
import de.tr7zw.changeme.nbtapi.NBTItem
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion
import org.bukkit.inventory.ItemStack
import java.util.logging.Level

Expand Down Expand Up @@ -71,6 +72,10 @@ class NBT(override vararg val defaultData: AttributeModifier) : Feature<Array<ou
}

object API {
init {
MinecraftVersion.disableUpdateCheck()
MinecraftVersion.disableBStats()
}
fun read(item: ItemStack): NBTCompound? = NBTItem(item).let { if (it.hasNBTData()) it else null }
fun readOrCreate(item: ItemStack): NBTCompound = NBTItem(item)
fun write(item: ItemStack,nbt:NBTCompound){
Expand Down

0 comments on commit a54973b

Please sign in to comment.