diff --git a/src/main/kotlin/net/axay/kspigot/commands/BrigardierWrapper.kt b/src/main/kotlin/net/axay/kspigot/commands/BrigardierWrapper.kt index 0068f89e..0a264310 100644 --- a/src/main/kotlin/net/axay/kspigot/commands/BrigardierWrapper.kt +++ b/src/main/kotlin/net/axay/kspigot/commands/BrigardierWrapper.kt @@ -5,11 +5,15 @@ import com.mojang.brigadier.builder.ArgumentBuilder import com.mojang.brigadier.builder.LiteralArgumentBuilder import com.mojang.brigadier.builder.RequiredArgumentBuilder import com.mojang.brigadier.context.CommandContext +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async import kotlinx.coroutines.future.asCompletableFuture import net.minecraft.commands.CommandListenerWrapper +import net.minecraft.network.chat.ChatMessage +import org.bukkit.World +import org.bukkit.entity.Player /** * Create a new command. @@ -21,7 +25,7 @@ import net.minecraft.commands.CommandListenerWrapper inline fun command( name: String, register: Boolean = true, - builder: LiteralArgumentBuilder.() -> Unit + builder: LiteralArgumentBuilder.() -> Unit, ): LiteralArgumentBuilder = LiteralArgumentBuilder.literal(name).apply(builder).apply { if (register) @@ -32,7 +36,7 @@ inline fun command( * Add custom execution logic for this command. */ inline fun ArgumentBuilder.simpleExecutes( - crossinline executor: (CommandContext) -> Unit + crossinline executor: (CommandContext) -> Unit, ) { executes wrapped@{ executor.invoke(it) @@ -47,7 +51,7 @@ inline fun ArgumentBuilder.simpleExecutes( */ inline fun ArgumentBuilder.literal( name: String, - builder: LiteralArgumentBuilder.() -> Unit + builder: LiteralArgumentBuilder.() -> Unit, ) { then(command(name, false, builder)) } @@ -61,7 +65,7 @@ inline fun ArgumentBuilder.literal( inline fun ArgumentBuilder.argument( name: String, type: ArgumentType, - builder: RequiredArgumentBuilder.() -> Unit + builder: RequiredArgumentBuilder.() -> Unit, ) { then(RequiredArgumentBuilder.argument(name, type).apply(builder)) } @@ -72,7 +76,7 @@ private val argumentCoroutineScope = CoroutineScope(Dispatchers.IO) * Add custom suspending suggestion logic for an argument. */ fun RequiredArgumentBuilder.simpleSuggests( - suggestionBuilder: suspend (CommandContext) -> Iterable? + suggestionBuilder: suspend (CommandContext) -> Iterable?, ) { suggests { context, builder -> argumentCoroutineScope.async { @@ -92,3 +96,16 @@ fun RequiredArgumentBuilder.simpleSuggests( */ inline fun CommandContext.getArgument(name: String): T = getArgument(name, T::class.java) + +private val REQUIRES_PLAYER_EXCEPTION = SimpleCommandExceptionType(ChatMessage("permissions.requires.player")) + +/** + * Returns the player who executed the command, or fails the command + * if it wasn't executed by a player. + */ +val CommandListenerWrapper.player get() = this.bukkitSender as? Player ?: throw REQUIRES_PLAYER_EXCEPTION.create() + +/** + * Returns the Bukkit world instance. + */ +val CommandListenerWrapper.bukkitWorld get() = world.world as World