Added /runmolangscript, updated molang version

This commit is contained in:
Paul
2024-12-14 12:22:25 +10:00
parent dab89a9f8c
commit 0c48a318c5
8 changed files with 109 additions and 5 deletions
@@ -56,6 +56,7 @@ object CobblemonCommands {
AbandonMultiTeam.register(dispatcher)
FreezePokemonCommand.register(dispatcher)
ApplyPlayerTextureCommand.register(dispatcher)
RunMolangScriptCommand.register(dispatcher)
// Possibly lock down registration if and only if under dev environment or running in an environment
// with a certain system environment variable set
@@ -82,6 +82,8 @@ object CobblemonPermissions {
val ABANDON_MULTITEAM = this.create("${COMMAND_PREFIX}abandonmultiteam", PermissionLevel.NONE)
val RUN_MOLANG_SCRIPT = this.create("${COMMAND_PREFIX}runmolangscript", PermissionLevel.CHEAT_COMMANDS_AND_COMMAND_BLOCKS)
fun all(): Iterable<Permission> = this.permissions
private fun create(node: String, level: PermissionLevel): Permission {
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2023 Cobblemon Contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package com.cobblemon.mod.common.command
import com.bedrockk.molang.runtime.MoLangRuntime
import com.cobblemon.mod.common.api.molang.MoLangFunctions.asMoLangValue
import com.cobblemon.mod.common.api.molang.MoLangFunctions.setup
import com.cobblemon.mod.common.api.permission.CobblemonPermissions
import com.cobblemon.mod.common.api.scripting.CobblemonScripts
import com.cobblemon.mod.common.entity.npc.NPCEntity
import com.cobblemon.mod.common.util.permission
import com.cobblemon.mod.common.util.player
import com.cobblemon.mod.common.util.resourceLocation
import com.cobblemon.mod.common.util.uuid
import com.cobblemon.mod.common.util.withQueryValue
import com.mojang.brigadier.Command
import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.context.CommandContext
import java.util.UUID
import net.minecraft.commands.CommandSourceStack
import net.minecraft.commands.Commands.argument
import net.minecraft.commands.Commands.literal
import net.minecraft.commands.arguments.EntityArgument
import net.minecraft.commands.arguments.ResourceLocationArgument
import net.minecraft.resources.ResourceLocation
import net.minecraft.server.level.ServerPlayer
object RunMolangScriptCommand {
private const val NAME = "runmolangscript"
private const val SCRIPT = "script"
private const val PLAYER = "player"
private const val NPC = "npc"
private const val POKEMON = "pokemon"
fun register(dispatcher : CommandDispatcher<CommandSourceStack>) {
dispatcher.register(literal(NAME)
.permission(CobblemonPermissions.RUN_MOLANG_SCRIPT)
.then(
argument(SCRIPT, ResourceLocationArgument.id())
.executes { execute(it, it.resourceLocation(SCRIPT), null, null, null) }
.then(argument(PLAYER, EntityArgument.player())
.executes { execute(it, it.resourceLocation(SCRIPT), it.player(PLAYER), null, null) }
.then(argument(NPC, StringArgumentType.string())
.executes { execute(it, it.resourceLocation(SCRIPT), it.player(PLAYER), it.uuid(NPC), null) }
.then(argument(POKEMON, StringArgumentType.string())
.executes { execute(it, it.resourceLocation(SCRIPT), it.player(PLAYER), it.uuid(NPC), it.uuid(POKEMON)) }
)
)
.then(argument(POKEMON, StringArgumentType.string())
.executes { execute(it, it.resourceLocation(SCRIPT), it.player(PLAYER), null, it.uuid(POKEMON)) }
)
)
.then(argument(NPC, StringArgumentType.string())
.executes { execute(it, it.resourceLocation(SCRIPT), null, it.uuid(NPC), null) }
.then(argument(POKEMON, StringArgumentType.string())
.executes { execute(it, it.resourceLocation(SCRIPT), null, it.uuid(NPC), it.uuid(POKEMON)) }
)
)
.then(argument(POKEMON, StringArgumentType.string())
.executes { execute(it, it.resourceLocation(SCRIPT), null, null, it.uuid(POKEMON)) }
)
)
)
}
private fun execute(context: CommandContext<CommandSourceStack>, scriptId: ResourceLocation, player: ServerPlayer?, npcId: UUID?, pokemonId: UUID? = null): Int {
try {
val runtime = MoLangRuntime().setup()
val npc = npcId?.let { context.source.level.getEntity(it) as? NPCEntity }
val pokemon = pokemonId?.let { context.source.level.getEntity(it) as? NPCEntity }
npc?.let { runtime.withQueryValue("npc", npc.struct) }
pokemon?.let { runtime.withQueryValue("pokemon", pokemon.struct) }
player?.let { runtime.withQueryValue("player", player.asMoLangValue()) }
CobblemonScripts.run(scriptId, runtime)
} catch (e: Exception) {
e.printStackTrace()
}
return Command.SINGLE_SUCCESS
}
}
@@ -11,5 +11,9 @@ package com.cobblemon.mod.common.util
import com.mojang.brigadier.context.CommandContext
import net.minecraft.commands.CommandSourceStack
import net.minecraft.commands.arguments.EntityArgument
import net.minecraft.commands.arguments.ResourceLocationArgument
fun CommandContext<CommandSourceStack>.player(argumentName: String = "player") = EntityArgument.getPlayer(this, argumentName)
fun CommandContext<CommandSourceStack>.player(argumentName: String = "player") = EntityArgument.getPlayer(this, argumentName)
fun CommandContext<CommandSourceStack>.string(argumentName: String) = this.getArgument(argumentName, String::class.java)
fun CommandContext<CommandSourceStack>.uuid(argumentName: String) = this.getArgument(argumentName, String::class.java).asUUID
fun CommandContext<CommandSourceStack>.resourceLocation(argumentName: String) = ResourceLocationArgument.getId(this, argumentName)
Binary file not shown.
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>local.com.bedrockk</groupId>
<artifactId>molang</artifactId>
<version>1.1.18</version>
</project>
+3 -3
View File
@@ -3,10 +3,10 @@
<groupId>local.com.bedrockk</groupId>
<artifactId>molang</artifactId>
<versioning>
<release>1.1.16</release>
<release>1.1.18</release>
<versions>
<version>1.1.16</version>
<version>1.1.18</version>
</versions>
<lastUpdated>20231949390002</lastUpdated>
<lastUpdated>20231949390005</lastUpdated>
</versioning>
</metadata>
+1 -1
View File
@@ -13,7 +13,7 @@ loom = "1.7-SNAPSHOT"
architectury-plugin = "3.4-SNAPSHOT"
# Common
molang = "1.1.16"
molang = "1.1.18"
graal = "22.3.0"
icu4j = "71.1"
jei-api = "19.8.2.99"