2424
2525import net .dv8tion .jda .api .entities .Message ;
2626import net .dv8tion .jda .api .entities .channel .middleman .MessageChannel ;
27- import org .luaj .vm2 .Globals ;
28- import org .luaj .vm2 .LoadState ;
29- import org .luaj .vm2 .LuaTable ;
30- import org .luaj .vm2 .LuaValue ;
31- import org .luaj .vm2 .compiler .LuaC ;
32- import org .luaj .vm2 .lib .PackageLib ;
33- import org .luaj .vm2 .lib .jse .CoerceJavaToLua ;
34- import org .luaj .vm2 .lib .jse .JseBaseLib ;
35- import org .luaj .vm2 .lib .jse .JseMathLib ;
36- import org .mangorage .mangobotplugin .entrypoint .MangoBot ;
37- import org .mangorage .mangobotplugin .commands .trick .lua .LuaJDA ;
38- import org .mangorage .mangobotplugin .commands .trick .lua .objects .LuaStringArray ;
3927
40- import java .util .concurrent .Executors ;
41- import java .util .concurrent .Future ;
42- import java .util .concurrent .ScheduledExecutorService ;
43- import java .util .concurrent .ScheduledFuture ;
44- import java .util .concurrent .TimeUnit ;
45- import java .util .concurrent .atomic .AtomicReference ;
46- import java .util .function .Consumer ;
28+ import org .mangorage .mangobotplugin .entrypoint .MangoBot ;
4729
4830public class TrickScriptable {
4931 private final MangoBot plugin ;
@@ -53,79 +35,83 @@ public TrickScriptable(MangoBot plugin) {
5335 }
5436
5537
56- public Globals sandBoxedGlobals () {
57- Globals server_globals = new Globals ();
58-
59- server_globals .load (new JseBaseLib ());
60- server_globals .load (new PackageLib ());
61- server_globals .load (new JseMathLib ());
62-
63-
64- LoadState .install (server_globals );
65- LuaC .install (server_globals );
66-
67- var array = new String []{"os" , "io" , "luajava" , "debug" , "load" , "loadfile" };
68-
69- Consumer <String > NILLIFY = a -> {
70- server_globals .set (a , LuaValue .NIL );
71- };
72-
73- for (String library : array )
74- NILLIFY .accept (library );
75-
76- return server_globals ;
77- }
38+ // public Globals sandBoxedGlobals() {
39+ // Globals server_globals = new Globals();
40+ //
41+ // server_globals.load(new JseBaseLib());
42+ // server_globals.load(new PackageLib());
43+ // server_globals.load(new JseMathLib());
44+ //
45+ //
46+ // LoadState.install(server_globals);
47+ // LuaC.install(server_globals);
48+ //
49+ // var array = new String[]{"os", "io", "luajava", "debug", "load", "loadfile"};
50+ //
51+ // Consumer<String> NILLIFY = a -> {
52+ // server_globals.set(a, LuaValue.NIL);
53+ // };
54+ //
55+ // for (String library : array)
56+ // NILLIFY.accept(library);
57+ //
58+ // return server_globals;
59+ // }
7860
7961 public void execute (Trick trick , String script , Message message , MessageChannel messageChannel , String [] args ) {
80- // Create a ScheduledExecutorService
81- ScheduledExecutorService executor = Executors .newScheduledThreadPool (2 );
82- AtomicReference <ScheduledFuture <?>> TASK = new AtomicReference <>();
83-
84- // Define the task you want to execute
85-
86- Globals globals = sandBoxedGlobals ();
87- LuaValue code = globals .load (script );
88-
89- Runnable task = () -> {
90- try {
91- code .call ();
92-
93- LuaValue method = globals .get ("execute" );
94- LuaTable arr = new LuaTable ();
95- for (int i = 0 ; i < args .length ; i ++)
96- arr .set (i + 1 , LuaValue .valueOf (args [i ]));
97-
98- if (!method .isnil ()) {
99- method .call (
100- CoerceJavaToLua .coerce (new LuaJDA (plugin , trick , message , messageChannel )),
101- CoerceJavaToLua .coerce (new LuaStringArray (args ))
102- );
103- }
104- if (TASK .get () != null ) {
105- var a = TASK .get ();
106- if (a .isDone () || a .isCancelled ()) return ;
107- a .cancel (true );
108- }
109- } catch (Exception e ) {
110- message .reply (e .getMessage ()).mentionRepliedUser (false ).queue ();
111- }
112- };
113-
114- // Submit the task to the executor
115- Future <?> future = executor .submit (() -> {
116- long time = System .currentTimeMillis ();
117- task .run ();
118- System .out .println ("Script Took: %s ms" .formatted (System .currentTimeMillis () - time ));
119- });
120-
121- // Schedule a task to cancel the original task if it runs longer than the timeout
122- TASK .set (executor .schedule (() -> {
123- if (future .isDone () || future .isCancelled ()) return ;
124- message .reply ("Trick took to long to run. Stopping Trick..." ).mentionRepliedUser (false ).queue ();
125- future .cancel (true );
126- }, 1000 , TimeUnit .MILLISECONDS ));
127-
128- // Shutdown the executor
129- executor .shutdown ();
62+ // // Create a ScheduledExecutorService
63+ // ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
64+ // AtomicReference<ScheduledFuture<?>> TASK = new AtomicReference<>();
65+ //
66+ // // Define the task you want to execute
67+ //
68+ // Globals globals = sandBoxedGlobals();
69+ // LuaValue code = globals.load(script);
70+ //
71+ // Runnable task = () -> {
72+ // try {
73+ // code.call();
74+ //
75+ // LuaValue method = globals.get("execute");
76+ // LuaTable arr = new LuaTable();
77+ // for (int i = 0; i < args.length; i++)
78+ // arr.set(i + 1, LuaValue.valueOf(args[i]));
79+ //
80+ // if (!method.isnil()) {
81+ // method.call(
82+ // CoerceJavaToLua.coerce(new LuaJDA(plugin, trick, message, messageChannel)),
83+ // CoerceJavaToLua.coerce(new LuaStringArray(args))
84+ // );
85+ // }
86+ // if (TASK.get() != null) {
87+ // var a = TASK.get();
88+ // if (a.isDone() || a.isCancelled()) return;
89+ // a.cancel(true);
90+ // }
91+ // } catch (Exception e) {
92+ // message.reply(e.getMessage()).mentionRepliedUser(false).queue();
93+ // }
94+ // };
95+ //
96+ // // Submit the task to the executor
97+ // Future<?> future = executor.submit(() -> {
98+ // long time = System.currentTimeMillis();
99+ // task.run();
100+ // System.out.println("Script Took: %s ms".formatted(System.currentTimeMillis() - time));
101+ // });
102+ //
103+ // // Schedule a task to cancel the original task if it runs longer than the timeout
104+ // TASK.set(executor.schedule(() -> {
105+ // if (future.isDone() || future.isCancelled()) return;
106+ // message.reply("Trick took to long to run. Stopping Trick...").mentionRepliedUser(false).queue();
107+ // future.cancel(true);
108+ // }, 1000, TimeUnit.MILLISECONDS));
109+ //
110+ // // Shutdown the executor
111+ // executor.shutdown();
112+
113+ if (message != null ) {
114+ message .reply ("Scripts Disabled" ).queue ();
115+ }
130116 }
131117}
0 commit comments