@@ -6,7 +6,9 @@ import me.scoretwo.fastscript.api.format.FormatHeader
66import me.scoretwo.fastscript.api.script.Script
77import me.scoretwo.fastscript.api.script.custom.CustomScript
88import me.scoretwo.fastscript.utils.assist
9+ import me.scoretwo.utils.sender.GlobalPlayer
910import me.scoretwo.utils.sender.GlobalSender
11+ import me.scoretwo.utils.server.globalServer
1012import org.apache.commons.lang.StringUtils
1113import javax.script.Invocable
1214import javax.script.ScriptEngine
@@ -26,11 +28,7 @@ abstract class TypeEngineExpansion: FastScriptExpansion() {
2628 return this
2729 }
2830
29- override fun eval (script : Script , sender : GlobalSender , args : Array <Any ?>, otherBindings : Map <String , Any ?>): Any? {
30- val newEngine = engine.factory.scriptEngine
31- if (! script.texts.keys.contains(sign))
32- return null
33-
31+ fun addEntries (newEngine : ScriptEngine , script : Script ? , sender : GlobalSender , args : Array <Any ?>, otherBindings : Map <String , Any ?>) {
3432 newEngine.put(" meta" , HashMap <String , Any ?>())
3533 newEngine.put(" plugin" , plugin.toOriginalPlugin())
3634 newEngine.put(" script" , script)
@@ -39,23 +37,34 @@ abstract class TypeEngineExpansion: FastScriptExpansion() {
3937 newEngine.put(" scriptManager" , FastScript .instance.scriptManager)
4038 newEngine.put(" expansionManager" , FastScript .instance.expansionManager)
4139
42- if (sender.isPlayer()) {
43- sender.toPlayer().let {
44- newEngine.put(" globalPlayer" , it)
45- newEngine.put(" player" , plugin.toOriginalPlayer(it!! ))
46- }
40+ newEngine.put(" args" , args)
41+ newEngine.put(" utils" , assist)
42+ newEngine.put(" util" , assist)
43+ otherBindings.forEach { newEngine.put(it.key, it.value) }
44+ }
45+
46+ fun addSenders (newEngine : ScriptEngine , script : Script ? , sender : GlobalSender , args : Array <Any ?>, otherBindings : Map <String , Any ?>) {
47+
48+ if (globalServer.isOnlinePlayer(sender.name)) {
49+ newEngine.put(" globalPlayer" , globalServer.getPlayer(sender.name).get())
50+ newEngine.put(" player" , plugin.toOriginalPlayer(globalServer.getPlayer(sender.name).get()))
4751 } else {
4852 newEngine.put(" globalPlayer" , null )
4953 newEngine.put(" player" , null )
5054 }
5155 newEngine.put(" globalSender" , sender)
5256 newEngine.put(" sender" , plugin.toOriginalSender(sender))
53- newEngine.put(" args" , args)
54- newEngine.put(" utils" , assist)
55- newEngine.put(" util" , assist)
56- otherBindings.forEach { newEngine.put(it.key, it.value) }
57+ }
5758
59+ override fun eval (script : Script , sender : GlobalSender , args : Array <Any ?>, otherBindings : Map <String , Any ?>): Any? {
60+ val newEngine = engine.factory.scriptEngine
61+ if (! script.texts.keys.contains(sign))
62+ return null
63+
64+ addSenders(newEngine, script, sender, args, otherBindings)
65+ addEntries(newEngine, script, sender, args, otherBindings)
5866 engineScripts[script] = newEngine
67+
5968 return let {
6069 try {
6170 newEngine.eval(script.texts[sign]).also {
@@ -78,30 +87,8 @@ abstract class TypeEngineExpansion: FastScriptExpansion() {
7887 if (text.isBlank())
7988 return null
8089
81- newEngine.put(" meta" , HashMap <String , Any ?>())
82- newEngine.put(" plugin" , plugin.toOriginalPlugin())
83- newEngine.put(" server" , plugin.toOriginalServer())
84- newEngine.put(" globalServer" , plugin.server)
85- newEngine.put(" globalPlugin" , plugin)
86- newEngine.put(" scriptManager" , FastScript .instance.scriptManager)
87- newEngine.put(" expansionManager" , FastScript .instance.expansionManager)
88-
89- if (sender.isPlayer()) {
90- sender.toPlayer().let {
91- newEngine.put(" globalPlayer" , it)
92- newEngine.put(" player" , plugin.toOriginalPlayer(it!! ))
93- }
94- } else {
95- newEngine.put(" globalPlayer" , null )
96- newEngine.put(" player" , null )
97- }
98- newEngine.put(" globalSender" , sender)
99- newEngine.put(" sender" , plugin.toOriginalSender(sender))
100- newEngine.put(" args" , args)
101- newEngine.put(" utils" , assist)
102- newEngine.put(" util" , assist)
103- otherBindings.forEach { newEngine.put(it.key, it.value) }
104-
90+ addSenders(newEngine, null , sender, args, otherBindings)
91+ addEntries(newEngine, null , sender, args, otherBindings)
10592 engineTexts[text] = newEngine
10693 return let {
10794 try {
@@ -123,6 +110,8 @@ abstract class TypeEngineExpansion: FastScriptExpansion() {
123110 return try {
124111 if (engineScripts[script] !is Invocable )
125112 eval(script, sender, arrayOf(), otherBindings)
113+ else
114+ addSenders(engineScripts[script]!! , script, sender, args, otherBindings)
126115
127116 val invocable = engineScripts[script] as Invocable
128117
@@ -162,6 +151,8 @@ abstract class TypeEngineExpansion: FastScriptExpansion() {
162151 return try {
163152 if (engineTexts[text] !is Invocable )
164153 eval(text, sender, arrayOf(), otherBindings)
154+ else
155+ addSenders(engineTexts[text]!! , null , sender, args, otherBindings)
165156
166157 val invocable = engineTexts[text] as Invocable
167158
0 commit comments