11package com.github.albardoo02.scriptBlockNext.command
22
3+ import com.github.albardoo02.scriptBlockNext.executor.ScriptExecutor
4+ import com.github.albardoo02.scriptBlockNext.executor.ScriptParser
35import com.github.albardoo02.scriptBlockNext.hook.MythicMobsManager
46import com.github.albardoo02.scriptBlockNext.hook.LuckPermsManager
57import com.github.albardoo02.scriptBlockNext.manager.ScriptManager
@@ -35,9 +37,48 @@ class ScriptCommand: CommandExecutor, TabCompleter {
3537 }
3638 }
3739
40+ private val scriptTypes = listOf (" interact" , " break" , " walk" , " hit" )
41+
42+ /* * /sbn <type> run [player] <world> <x> <y> <z> */
43+ private fun runScript (sender : CommandSender , args : Array <out String >): Boolean {
44+ val type = args[0 ].lowercase()
45+ if (! sender.hasPermission(" scriptblocknext.command.$type " )) {
46+ sender.sendMsg(" error_no_permission" )
47+ return true
48+ }
49+ val rest = args.drop(2 )
50+ val hasPlayerArg = rest.size >= 5
51+ if (rest.size < 4 ) {
52+ sender.sendMsg(" cmd_usage_run" , " type" to type)
53+ return true
54+ }
55+ val target: Player ? = if (hasPlayerArg) Bukkit .getPlayerExact(rest[0 ]) else sender as ? Player
56+ if (target == null ) {
57+ sender.sendMsg(if (hasPlayerArg) " error_player_not_found" else " error_only_player" , " player" to rest.getOrElse(0 ) { " " })
58+ return true
59+ }
60+ val coords = if (hasPlayerArg) rest.drop(1 ) else rest
61+ val world = Bukkit .getWorld(coords[0 ])
62+ val x = coords[1 ].toIntOrNull()
63+ val y = coords[2 ].toIntOrNull()
64+ val z = coords[3 ].toIntOrNull()
65+ if (world == null || x == null || y == null || z == null ) {
66+ sender.sendMsg(" cmd_usage_run" , " type" to type)
67+ return true
68+ }
69+ val loc = org.bukkit.Location (world, x.toDouble(), y.toDouble(), z.toDouble())
70+ val data = ScriptManager .getScript(loc, type)
71+ if (data == null ) {
72+ sender.sendMsg(" error_no_script_info" , " type" to type)
73+ return true
74+ }
75+ ScriptExecutor .run (target, data, loc)
76+ return true
77+ }
78+
3879 override fun onCommand (sender : CommandSender , command : Command , label : String , args : Array <out String >): Boolean {
3980 if (args.isEmpty()) {
40- if (sender is Player ) sender.sendMsg(" cmd_usage_main" )
81+ sender.sendMsg(" cmd_usage_main" )
4182 return true
4283 }
4384
@@ -53,6 +94,29 @@ class ScriptCommand: CommandExecutor, TabCompleter {
5394 return true
5495 }
5596
97+ if (firstArg == " backup" ) {
98+ if (! sender.hasPermission(" scriptblocknext.command.backup" )) {
99+ sender.sendMsg(" error_no_permission" )
100+ return true
101+ }
102+ val dest = ScriptManager .backup()
103+ if (dest != null ) sender.sendMsg(" success_backup" , " path" to dest.path) else sender.sendMsg(" error_backup_failed" )
104+ return true
105+ }
106+
107+ if (firstArg == " datamigr" ) {
108+ if (! sender.hasPermission(" scriptblocknext.command.datamigr" )) {
109+ sender.sendMsg(" error_no_permission" )
110+ return true
111+ }
112+ ScriptManager .migrateFromScriptBlockPlus(sender)
113+ return true
114+ }
115+
116+ if (args.size >= 2 && args[1 ].lowercase() == " run" && args[0 ].lowercase() in scriptTypes) {
117+ return runScript(sender, args)
118+ }
119+
56120 if (sender !is Player ) {
57121 sender.sendMsg(" error_only_player" )
58122 return true
@@ -223,10 +287,11 @@ class ScriptCommand: CommandExecutor, TabCompleter {
223287 sender.sendMsg(" cmd_usage_args" , " type" to type, " action" to action)
224288 return true
225289 }
226- val cmdString = args.drop(2 ).joinToString(" " )
227- val regex = Regex (" \\ [(.*?)\\ ]" )
228- val matches = regex.findAll(cmdString)
229- val commands = matches.map { it.groupValues[1 ] }.toList().ifEmpty { listOf (cmdString) }
290+ val commands = ScriptParser .split(args.drop(2 ).joinToString(" " ))
291+ if (commands.isEmpty()) {
292+ sender.sendMsg(" cmd_usage_args" , " type" to type, " action" to action)
293+ return true
294+ }
230295
231296 if (action == " create" ) {
232297 ScriptManager .creationMode[sender.uniqueId] = type to commands
@@ -285,6 +350,7 @@ class ScriptCommand: CommandExecutor, TabCompleter {
285350 if (sender.hasPermission(" scriptblocknext.command.tool" )) firstArgs.add(" tool" )
286351 if (sender.hasPermission(" scriptblocknext.command.selector" )) firstArgs.add(" selector" )
287352 if (sender.hasPermission(" scriptblocknext.command.datamigr" )) firstArgs.add(" datamigr" )
353+ if (sender.hasPermission(" scriptblocknext.command.backup" )) firstArgs.add(" backup" )
288354 return firstArgs.filter { it.startsWith(args[0 ], ignoreCase = true ) }
289355 }
290356
@@ -304,7 +370,7 @@ class ScriptCommand: CommandExecutor, TabCompleter {
304370
305371 if (args.size == 2 && first in listOf (" interact" , " break" , " walk" , " hit" )) {
306372 if (! sender.hasPermission(" scriptblocknext.command.$first " )) return emptyList()
307- return listOf (" create" , " add" , " remove" , " view" , " info" , " copy" , " show" ).filter { it.startsWith(args[1 ], ignoreCase = true ) }
373+ return listOf (" create" , " add" , " remove" , " view" , " info" , " copy" , " show" , " run " ).filter { it.startsWith(args[1 ], ignoreCase = true ) }
308374 }
309375
310376 if (args.size >= 3 && first in listOf (" interact" , " break" , " walk" , " hit" )) {
@@ -345,7 +411,8 @@ class ScriptCommand: CommandExecutor, TabCompleter {
345411 " [@server " , " [@console " , " [@bypass " , " [@sound:" , " [@title:" , " [@actionBar:" ,
346412 " [@velocity:" , " [@checkpoint]" , " [@return]" , " [@noFall:" , " [@potion:" ,
347413 " [@if " , " [@hand:" , " [@bypassPerm:" , " [@perm:" , " [@amount:" , " [@globalAmount:" ,
348- " [@cancelEvent:true]" , " [@cancelEvent:false]"
414+ " [@cancelEvent:true]" , " [@cancelEvent:false]" , " [@say " , " [@execute:" , " [@invalid]" ,
415+ " [@playerAmount:" , " [@permAdd:" , " [@permRemove:"
349416 )
350417
351418 if (VaultManager .isHooked) {
0 commit comments