-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore the previous versions Now ImportClass works Material has been set to defaultImport
- Loading branch information
Showing
9 changed files
with
133 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package cf.wayzer.SuperItem.scripts | ||
|
||
@Target(AnnotationTarget.FILE) | ||
@Repeatable | ||
annotation class ImportClass( | ||
val value:String | ||
val name:String | ||
) |
58 changes: 58 additions & 0 deletions
58
src/main/kotlin/cf/wayzer/SuperItem/scripts/ScriptLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cf.wayzer.SuperItem.scripts | ||
|
||
import cf.wayzer.SuperItem.Item | ||
import cf.wayzer.SuperItem.Main | ||
import java.io.File | ||
import java.util.logging.Level | ||
import java.util.logging.Logger | ||
import kotlin.script.experimental.api.* | ||
import kotlin.script.experimental.host.ScriptingHostConfiguration | ||
import kotlin.script.experimental.host.configurationDependencies | ||
import kotlin.script.experimental.host.toScriptSource | ||
import kotlin.script.experimental.jvm.JvmDependency | ||
import kotlin.script.experimental.jvm.baseClassLoader | ||
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration | ||
import kotlin.script.experimental.jvm.jvm | ||
import kotlin.script.experimental.jvm.util.classpathFromClassloader | ||
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost | ||
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate | ||
|
||
class ScriptLoader { | ||
lateinit var logger: Logger | ||
private val hostConfiguration by lazy { ScriptingHostConfiguration(defaultJvmScriptingHostConfiguration){ | ||
configurationDependencies(JvmDependency(classpathFromClassloader(Main::class.java.classLoader) ?:listOf())) | ||
}} | ||
private val compilationConfiguration by lazy {createJvmCompilationConfigurationFromTemplate<SuperItemScript> (hostConfiguration) } | ||
|
||
fun load(file: File):Item?{ | ||
var item:Item?=null | ||
val result = load0(file) | ||
result.onSuccess { | ||
val res = result.resultOrNull()!!.returnValue | ||
if(res is ResultValue.Value && res.value is Item) { | ||
item = (res.value as Item) | ||
result.reports.filterNot { it.severity== ScriptDiagnostic.Severity.DEBUG }.forEachIndexed { index, rep -> | ||
logger.log(Level.WARNING,"##$index##"+rep.message,rep.exception) | ||
} | ||
return@onSuccess ResultWithDiagnostics.Success(ResultValue.Unit) | ||
} else { | ||
return@onSuccess ResultWithDiagnostics.Failure(ScriptDiagnostic("非物品Kts: ${file.name}")) | ||
} | ||
}.onFailure { | ||
logger.warning("物品Kts加载失败: ") | ||
it.reports.forEachIndexed { index, rep -> | ||
logger.log(Level.WARNING,"##$index##"+rep.message,rep.exception) | ||
} | ||
} | ||
return item | ||
} | ||
|
||
private fun load0(f: File):ResultWithDiagnostics<EvaluationResult>{ | ||
return BasicJvmScriptingHost(hostConfiguration).eval(f.toScriptSource(),compilationConfiguration, ScriptEvaluationConfiguration { | ||
jvm { | ||
baseClassLoader(ScriptSupporter::class.java.classLoader) | ||
} | ||
constructorArgs(f.name.split(".")[0].toUpperCase()) | ||
}) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/cf/wayzer/SuperItem/scripts/ScriptSupporter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cf.wayzer.SuperItem.scripts | ||
|
||
import cf.wayzer.SuperItem.Item | ||
import cf.wayzer.SuperItem.Main | ||
import cf.wayzer.libraryManager.Dependency | ||
import cf.wayzer.libraryManager.LibraryManager | ||
import java.io.File | ||
import java.net.URLClassLoader | ||
import java.nio.file.Paths | ||
import java.util.logging.Logger | ||
|
||
object ScriptSupporter { | ||
lateinit var loader:ScriptLoader | ||
private var inited=false | ||
fun init(logger:Logger){ | ||
if(inited)return | ||
LibraryManager(Paths.get("./libs/")).apply { | ||
addMavenCentral() | ||
require(Dependency("org.jetbrains.kotlin:kotlin-script-runtime:${Main.kotlinVersion}")) | ||
require(Dependency("org.jetbrains.kotlin:kotlin-scripting-common:${Main.kotlinVersion}")) | ||
require(Dependency("org.jetbrains.intellij.deps:trove4j:1.0.20181211")) | ||
require(Dependency("org.jetbrains.kotlin:kotlin-daemon-client:${Main.kotlinVersion}")) | ||
require(Dependency("org.jetbrains.kotlin:kotlin-scripting-jvm:${Main.kotlinVersion}")) | ||
require(Dependency("org.jetbrains.kotlin:kotlin-scripting-jvm-host:${Main.kotlinVersion}")) | ||
require(Dependency("org.jetbrains.kotlin:kotlin-scripting-compiler:${Main.kotlinVersion}")) | ||
require(Dependency("org.jetbrains.kotlin:kotlin-scripting-compiler-impl:${Main.kotlinVersion}")) | ||
require(Dependency("org.jetbrains.kotlin:kotlin-compiler:${Main.kotlinVersion}")) | ||
require(Dependency("org.jetbrains.kotlin:kotlin-script-util:${Main.kotlinVersion}")) | ||
}.loadToClassLoader(Main::class.java.classLoader as URLClassLoader) | ||
loader = ScriptLoader() | ||
loader.logger=logger | ||
inited =true | ||
} | ||
|
||
fun load(file: File): Item?{ | ||
if(!inited)throw IllegalStateException("Must init first!!") | ||
return loader.load(file) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
@file:JvmName("io.lumine.xikage.mythicmobs.api.bukkit.events.MythicDropLoadEvent") | ||
@file:ImportClass("io.lumine.xikage.mythicmobs.api.bukkit.events.MythicDropLoadEvent") | ||
|
||
import cf.wayzer.SuperItem.ItemManager | ||
import io.lumine.xikage.mythicmobs.adapters.bukkit.BukkitItemStack | ||
import io.lumine.xikage.mythicmobs.api.bukkit.events.MythicDropLoadEvent | ||
import io.lumine.xikage.mythicmobs.drops.droppables.* | ||
import org.bukkit.Material | ||
import io.lumine.xikage.mythicmobs.drops.droppables.ItemDrop | ||
|
||
require(ItemInfo(Material.NAME_TAG,"&cMythicmobs_Support", listOf("&c This Item doesn't have other effect"))) | ||
|
||
listen<MythicDropLoadEvent>{ | ||
val name = (it.dropName as String).toUpperCase() | ||
if(!name.startsWith("SI_"))return@listen | ||
val item = ItemManager.getItem(name.substring(3)) | ||
if(item==null)it.register(NothingDrop(it.config.line,it.config)) | ||
else it.register(ItemDrop(it.config.line,it.config,BukkitItemStack(item.get<ItemInfo>().newItemStack()))) | ||
//if(item==null)it.register(NothingDrop(it.config.line,it.config)) | ||
if(item!=null)it.register(ItemDrop(it.config.line,it.config,BukkitItemStack(item.get<ItemInfo>().newItemStack()))) | ||
} | ||
|