Skip to content

Commit 686d39f

Browse files
committed
feat: restructure ScriptExecutor and integrate ExecutionCounter; add RedstoneListener for redstone-triggered scripts
1 parent 38af378 commit 686d39f

17 files changed

Lines changed: 995 additions & 446 deletions

File tree

‎src/main/kotlin/com/github/albardoo02/scriptBlockNext/ScriptBlockNext.kt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ class ScriptBlockNext : JavaPlugin() {
5050
it.tabCompleter = scriptCommand
5151
}
5252
ScriptManager.init(this)
53+
com.github.albardoo02.scriptBlockNext.manager.ExecutionCounter.init(this)
5354
server.pluginManager.registerEvents(InteractListener(), this)
5455
server.pluginManager.registerEvents(ScriptListener(), this)
56+
server.pluginManager.registerEvents(com.github.albardoo02.scriptBlockNext.listener.RedstoneListener(), this)
5557
logger.info("ScriptBlockNext has been enabled.")
5658
}
5759

5860
override fun onDisable() {
61+
com.github.albardoo02.scriptBlockNext.manager.ExecutionCounter.save()
5962
logger.info("ScriptBlockNext has been disabled.")
6063
}
6164

‎src/main/kotlin/com/github/albardoo02/scriptBlockNext/command/ScriptCommand.kt‎

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.albardoo02.scriptBlockNext.command
22

3+
import com.github.albardoo02.scriptBlockNext.executor.ScriptExecutor
4+
import com.github.albardoo02.scriptBlockNext.executor.ScriptParser
35
import com.github.albardoo02.scriptBlockNext.hook.MythicMobsManager
46
import com.github.albardoo02.scriptBlockNext.hook.LuckPermsManager
57
import 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) {

‎src/main/kotlin/com/github/albardoo02/scriptBlockNext/data/ScriptData.kt‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@ package com.github.albardoo02.scriptBlockNext.data
22

33
import java.util.UUID
44

5+
/** レッドストーン起動の設定。selector は @p / @a / @r(+ [r=距離])、filter は op / perm / limit */
6+
data class RedstoneSetting(
7+
val selector: String = "@p",
8+
val filterOp: Boolean? = null,
9+
val filterPerm: String? = null,
10+
val filterLimit: Int? = null
11+
)
12+
513
data class ScriptData (
614
val commands: List<String>,
715
val type: String = "interact",
816
val creator: UUID? = null,
9-
val permission: String? = null
17+
val permission: String? = null,
18+
val nametag: String? = null,
19+
val redstone: RedstoneSetting? = null
1020
)
1121

1222
data class BlockLocation(
@@ -18,5 +28,7 @@ data class BlockLocation(
1828

1929
data class ScriptEntry(
2030
val commands: List<String>,
21-
val creator: String? = null
22-
)
31+
val creator: String? = null,
32+
val nametag: String? = null,
33+
val redstone: RedstoneSetting? = null
34+
)

0 commit comments

Comments
 (0)